File indexing completed on 2024-04-14 03:40:45

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 2004 Fredrik Edemar <f_edemar@linux.se>
0005     SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org>
0006 
0007     This file is part of the KDE Project.
0008     KmPlot is part of the KDE-EDU Project.
0009 
0010     SPDX-License-Identifier: GPL-2.0-or-later
0011 
0012 */
0013 
0014 #ifndef KCONSTANTEDITOR_H
0015 #define KCONSTANTEDITOR_H
0016 
0017 #include <QDialog>
0018 #include <QValidator>
0019 
0020 class Constant;
0021 class ConstantValidator;
0022 class ConstantsEditorWidget;
0023 class QTreeWidgetItem;
0024 
0025 /**
0026  * Handles all the constants.
0027  * @author Fredrik Edemar, David Saxton
0028  */
0029 class KConstantEditor : public QDialog
0030 {
0031     Q_OBJECT
0032 public:
0033     explicit KConstantEditor(QWidget *parent = nullptr);
0034     ~KConstantEditor();
0035 
0036 public slots:
0037     /// actions for the buttons
0038     void cmdNew_clicked();
0039     void cmdDelete_clicked();
0040 
0041     /// called when the user changes the text in the Constant name edit box
0042     void constantNameEdited(const QString &newName);
0043 
0044     /// actions for the visible constant list
0045     void selectedConstantChanged(QTreeWidgetItem *current);
0046 
0047     /// saves the value in the edit boxes of the constant currently being edited
0048     void saveCurrentConstant();
0049 
0050     /// updates whether or not the "value is invalid" label is shown, (and returns the validity of the current value)
0051     bool checkValueValid();
0052 
0053     /**
0054      * Updates the list of constants.
0055      */
0056     void updateConstantsList();
0057 
0058 protected slots:
0059     /**
0060      * Called when an item is clicked on. This might mean that the check state
0061      * has changed, so will save the constants list.
0062      */
0063     void itemClicked();
0064 
0065     void dialogFinished();
0066 
0067 protected:
0068     /**
0069      * Initializes the values, checkstates, tooltips, etc.
0070      */
0071     void init(QTreeWidgetItem *item, const QString &name, const Constant &constant);
0072 
0073 private:
0074     QString m_previousConstantName;
0075     ConstantValidator *m_constantValidator;
0076     ConstantsEditorWidget *m_widget;
0077 };
0078 
0079 /**
0080 Validates the constant; ensuring that Roman letters are alphabetical and only
0081 proper constant letters are used.
0082 @author David Saxton
0083 */
0084 class ConstantValidator : public QValidator
0085 {
0086 public:
0087     explicit ConstantValidator(KConstantEditor *parent);
0088 
0089     State validate(QString &input, int &pos) const Q_DECL_OVERRIDE;
0090 
0091     bool isValid(const QString &name) const;
0092 
0093     /**
0094      * There cannot be more than one constant with the same name. So
0095      * this validator checks that the input does not conflict with any
0096      * existing names - except, of course, the name of the constant being
0097      * edited.
0098      */
0099     void setWorkingName(const QString &name);
0100 
0101 protected:
0102     /// @see setWorkingName
0103     QString m_workingName;
0104 };
0105 
0106 #endif