File indexing completed on 2024-04-21 03:42:04

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 2004 Fredrik Edemar <f_edemar@linux.se>
0005 
0006     This file is part of the KDE Project.
0007     KmPlot is part of the KDE-EDU Project.
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 */
0012 
0013 #include "coordsconfigdialog.h"
0014 
0015 #include <KMessageBox>
0016 
0017 #include <QPushButton>
0018 
0019 #include "settings.h"
0020 #include "xparser.h"
0021 
0022 #include "ui_editcoords.h"
0023 
0024 class EditCoords : public QWidget, public Ui::EditCoords
0025 {
0026 public:
0027     EditCoords(QWidget *parent = nullptr)
0028         : QWidget(parent)
0029     {
0030         setupUi(this);
0031     }
0032 };
0033 
0034 CoordsConfigDialog::CoordsConfigDialog(QWidget *parent)
0035     : KConfigDialog(parent, "coords", Settings::self())
0036 {
0037     configAxesDialog = new EditCoords(nullptr);
0038     configAxesDialog->kcfg_XMin->setTabChain(configAxesDialog->kcfg_XMax->focusProxy());
0039     configAxesDialog->kcfg_XMax->setTabChain(configAxesDialog->kcfg_YMin->focusProxy());
0040     configAxesDialog->kcfg_YMin->setTabChain(configAxesDialog->kcfg_YMax->focusProxy());
0041     configAxesDialog->layout()->setContentsMargins(0, 0, 0, 0);
0042     addPage(configAxesDialog, i18n("Coordinates"), "coords", i18n("Coordinate System"));
0043     setWindowTitle(i18nc("@title:window", "Coordinate System"));
0044     setHelp(QString::fromLatin1("axes-config"));
0045     setFaceType(Plain);
0046     connect(configAxesDialog->kcfg_XMin, &EquationEdit::textEdited, this, &CoordsConfigDialog::updateButtons);
0047     connect(configAxesDialog->kcfg_XMax, &EquationEdit::textEdited, this, &CoordsConfigDialog::updateButtons);
0048     connect(configAxesDialog->kcfg_YMin, &EquationEdit::textEdited, this, &CoordsConfigDialog::updateButtons);
0049     connect(configAxesDialog->kcfg_YMax, &EquationEdit::textEdited, this, &CoordsConfigDialog::updateButtons);
0050 }
0051 
0052 CoordsConfigDialog::~CoordsConfigDialog()
0053 {
0054 }
0055 
0056 bool CoordsConfigDialog::evalX(bool showError)
0057 {
0058     Parser::Error error;
0059 
0060     double const min = XParser::self()->eval(configAxesDialog->kcfg_XMin->text(), &error);
0061     if (error != Parser::ParseSuccess) {
0062         if (showError)
0063             XParser::self()->displayErrorDialog(error);
0064         return false;
0065     }
0066 
0067     double const max = XParser::self()->eval(configAxesDialog->kcfg_XMax->text(), &error);
0068     if (error != Parser::ParseSuccess) {
0069         if (showError)
0070             XParser::self()->displayErrorDialog(error);
0071         return false;
0072     }
0073 
0074     if (min >= max) {
0075         if (showError)
0076             KMessageBox::error(this, i18n("The minimum range value must be lower than the maximum range value"));
0077         return false;
0078     }
0079     return true;
0080 }
0081 
0082 bool CoordsConfigDialog::evalY(bool showError)
0083 {
0084     Parser::Error error;
0085 
0086     double const min = XParser::self()->eval(configAxesDialog->kcfg_YMin->text(), &error);
0087     if (error != Parser::ParseSuccess) {
0088         if (showError)
0089             XParser::self()->displayErrorDialog(error);
0090         return false;
0091     }
0092 
0093     double const max = XParser::self()->eval(configAxesDialog->kcfg_YMax->text(), &error);
0094     if (error != Parser::ParseSuccess) {
0095         if (showError)
0096             XParser::self()->displayErrorDialog(error);
0097         return false;
0098     }
0099 
0100     if (min >= max) {
0101         if (showError)
0102             KMessageBox::error(this, i18n("The minimum range value must be lower than the maximum range value"));
0103         return false;
0104     }
0105     return true;
0106 }
0107 
0108 void CoordsConfigDialog::updateButtons()
0109 {
0110     buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(evalX(false) && evalY(false));
0111 }
0112 
0113 void CoordsConfigDialog::done(int result)
0114 {
0115     // https://www.qtcentre.org/threads/8048-Validate-Data-in-QDialog
0116     if (result == QDialog::Accepted) {
0117         if (!evalX() || !evalY()) {
0118             return;
0119         }
0120     }
0121     KConfigDialog::done(result);
0122 }
0123 
0124 void CoordsConfigDialog::updateXYRange()
0125 {
0126     configAxesDialog->kcfg_XMin->setText(Settings::xMin());
0127     configAxesDialog->kcfg_XMax->setText(Settings::xMax());
0128     configAxesDialog->kcfg_YMin->setText(Settings::yMin());
0129     configAxesDialog->kcfg_YMax->setText(Settings::yMax());
0130 }
0131 
0132 #include "moc_coordsconfigdialog.cpp"