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

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org>
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 #ifndef EQUATIONEDIT_H
0014 #define EQUATIONEDIT_H
0015 
0016 #include "function.h"
0017 
0018 #include <QMap>
0019 #include <QWidget>
0020 
0021 class Equation;
0022 class EquationEdit;
0023 class EquationEditWidget;
0024 class EquationEditor;
0025 class EquationEditorWidget;
0026 class EquationHighlighter;
0027 class QPushButton;
0028 
0029 typedef QMap<QChar, QChar> CharMap;
0030 
0031 /**
0032  * A line edit for equations that also does syntax highlighting, error checking,
0033  * etc, and provides a button for invoking an advanced equation editor.
0034  * \author David Saxton
0035  */
0036 class EquationEdit : public QWidget
0037 {
0038     Q_OBJECT
0039     Q_PROPERTY(QString text READ text WRITE setText NOTIFY textEdited USER true)
0040 
0041 public:
0042     enum InputType {
0043         Function,
0044         Expression,
0045     };
0046 
0047     explicit EquationEdit(QWidget *parent);
0048 
0049     /**
0050      * Set the current error message and position of the error. This is
0051      * used to inform the user if his equation is Ok or not.
0052      */
0053     void setError(const QString &message, int position);
0054     /**
0055      * Check the validity of the current text. Called from
0056      * EquationHighlighter when it needs to highlight text.
0057      */
0058     void checkTextValidity();
0059     /**
0060      * Sets whether the equation edit should be storing a function (i.e. of
0061      * the form f(x)=[expression]), or just an expression (i.e. without the
0062      * f(x)= part). By default, this assumes that an expression is being
0063      * stored.
0064      */
0065     void setInputType(InputType type);
0066     /**
0067      * Prepends \p prefix to the start of the text when validating it.
0068      */
0069     void setValidatePrefix(const QString &prefix);
0070     /**
0071      * Hide/show the edit button.
0072      */
0073     void showEditButton(bool show);
0074     /**
0075      * Changes the equation type.
0076      */
0077     void setEquationType(Equation::Type type);
0078     /**
0079      * \return a pointer that the equation that this equation edit uses for
0080      * validation, etc.
0081      */
0082     Equation *equation() const
0083     {
0084         return m_equation;
0085     }
0086     /**
0087      * For inserting the currently selected text into a function. For
0088      * example, if "2+x" is selected, \p before is "sin(" and \p after is
0089      * ")", then the text will become "sin(2+x)".
0090      */
0091     void wrapSelected(const QString &before, const QString &after);
0092 
0093     QString text() const;
0094     void clear();
0095     void selectAll();
0096     void insertText(const QString &text);
0097 
0098     /**
0099      * Attempts to evaluate the text and return it.
0100      * \a ok Will be set to whether the text could be evaluated as a number.
0101      */
0102     double value(bool *ok = nullptr);
0103 
0104     /**
0105      * @internal workaround for QTBUG-10907
0106      */
0107     void setTabChain(QWidget *next);
0108 
0109 signals:
0110     void editingFinished();
0111     void textEdited(const QString &text);
0112     void textChanged(const QString &text);
0113     void returnPressed();
0114     void upPressed();
0115     void downPressed();
0116 
0117 public Q_SLOTS:
0118     void setText(const QString &text);
0119     /**
0120      * Launches a dialog for editing the equation.
0121      */
0122     void invokeEquationEditor();
0123 
0124     void reHighlight();
0125 
0126 protected Q_SLOTS:
0127     void slotTextChanged();
0128 
0129 protected:
0130     EquationHighlighter *m_highlighter;
0131     Equation *m_equation;
0132     InputType m_inputType;
0133     bool m_settingText : 1;
0134     bool m_cleaningText : 1;
0135     bool m_forcingRehighlight : 1;
0136     QString m_validatePrefix;
0137     EquationEditWidget *m_equationEditWidget;
0138     QPushButton *m_editButton;
0139 
0140     static CharMap m_replaceMap;
0141 
0142     friend class EquationEditor;
0143     friend class EquationEditorWidget;
0144     friend class EquationEditWidget;
0145     friend class EquationHighlighter;
0146 };
0147 
0148 #endif