File indexing completed on 2024-03-24 15:13:35

0001 /*************************************************************************************
0002  *                                                                                   *
0003  *  This program is free software; you can redistribute it and/or                    *
0004  *  Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org>                               *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #ifndef EXPRESSIONEDIT_H
0020 #define EXPRESSIONEDIT_H
0021 
0022 #include <QPlainTextEdit>
0023 #include <QCompleter>
0024 #include <QLabel>
0025 #include <QTreeView>
0026 #include <analitzagui/algebrahighlighter.h>
0027 
0028 #include "analitzawidgets_export.h"
0029 
0030 class QKeyEvent;
0031 
0032 class OperatorsModel;
0033 
0034 namespace Analitza
0035 {
0036 class Variables;
0037 class Analyzer;
0038 class Expression;
0039 
0040 /**
0041  * \class ExpressionEdit
0042  * 
0043  * \ingroup AnalitzaGUIModule
0044  *
0045  * \brief A widget for manipulate mathemathical expressions.
0046  *
0047  * The expression edit widget is the one where we will input our expressions.
0048  */
0049 
0050 class ANALITZAWIDGETS_EXPORT ExpressionEdit : public QPlainTextEdit
0051 {
0052     Q_OBJECT
0053     public:
0054         /** Constructor. Creates a new ExpressionEdit.
0055         *    @param parent is the widget parent.
0056         *    @param ini specifies what input format is going to expect the highlighting.
0057         */
0058         explicit ExpressionEdit(QWidget *parent = nullptr, AlgebraHighlighter::Mode ini=AlgebraHighlighter::Autodetect);
0059         
0060         /** Destructor. */
0061         ~ExpressionEdit() override;
0062         
0063         /** Returns the ExpressionEdit input mode. */
0064         AlgebraHighlighter::Mode mode() { return m_highlight->mode(); }
0065         
0066         /** Sets the ExpressionEdit input mode. */
0067         void setMode(AlgebraHighlighter::Mode en);
0068         
0069         /** Returns whether there is MathML on the widget. */
0070         bool isMathML() const;
0071         
0072         /** Sets an Analyzer @p in module associated to the ExpressionEdit. It is used to autocomplete variables. */
0073         void setAnalitza(Analitza::Analyzer* in);
0074         
0075         /** Returns the expression string that we have. */
0076         QString text() const { return this->toPlainText();}
0077         
0078         /** Sets the expression string @p str we have. */
0079         void setText(const QString &str) { setPlainText(str);}
0080         
0081         /** Sets whether it is correct. Changes the background color. */
0082         void setCorrect(bool cor);
0083         
0084         /** Checks whether it has been set to correct. */
0085         bool isCorrect() const;
0086         
0087         /** Sets the string @p ans that will be entered when an operator is pressed. */
0088         void setAns(const QString &ans) { m_ans=ans; }
0089         
0090         /** Returns the string that will be entered when the first operator is pressed. */
0091         QString ans() const { return m_ans; }
0092         
0093         /** Sets an expression to the input box. */
0094         void setExpression(const Analitza::Expression& e);
0095         
0096         /** Returns the expression we have in the text. */
0097         Analitza::Expression expression() const;
0098         
0099         /** Sets the @p examples to be shown in the context menu */
0100         void setExamples(const QStringList& ex) { m_examples=ex; }
0101     public Q_SLOTS:
0102         
0103         /** Inserts @p text text where the cursor is and selects it */
0104         void insertText(const QString& text);
0105         
0106     private Q_SLOTS:
0107         void showSimplified();
0108         void cursorMov();
0109         void updateCompleter();
0110         void setActionText(QAction* text);
0111         
0112         /** Shows a little tip widget containing the string @p str. If @p str is empty the widget is hidden. */
0113         void helper(const QString& str);
0114         
0115         /** Tries to complete the currently edited word with the @p word. */
0116         void completed(const QString &word);
0117         
0118         /** Sets the Mode to MathML */
0119         void toMathML() { setMode(AlgebraHighlighter::MathML); }
0120         
0121         /** Sets the Mode to Expression */
0122         void toExpression() { setMode(AlgebraHighlighter::Expression); }
0123         
0124         /** Simplify the current expression. */
0125         void simplify();
0126         
0127         /** Is the execution function, when return is pressed. */
0128         void returnP(); //FIXME: Change my name please
0129         
0130     Q_SIGNALS:
0131         /** Emits that a return has been pressed. */
0132         void returnPressed();
0133         
0134         /** Deprecated. */
0135         void signalHelper(QString);
0136         
0137     protected:
0138         /** Inherited from QTextEdit, just deals with the menu. */
0139         void contextMenuEvent(QContextMenuEvent * e) override;
0140     
0141     private:
0142         bool returnPress();
0143         QString helpShow(const QString& funcname, int param, bool bounds, const Analitza::Variables* v) const;
0144         void helper(const QString&, const QPoint& p);
0145         QString lastWord(int);
0146         void focusInEvent (QFocusEvent * event) override;
0147         void focusOutEvent ( QFocusEvent * event ) override;
0148         
0149         void removenl();
0150         void keyPressEvent(QKeyEvent * e) override;
0151         
0152         QLabel *m_helptip;
0153         AlgebraHighlighter *m_highlight;
0154         
0155         int m_histPos;
0156         QStringList m_history;
0157         
0158         Analitza::Analyzer *a;
0159         bool m_correct;
0160         QString m_ans;
0161         QCompleter *m_completer;
0162         OperatorsModel *m_ops;
0163         
0164         QStringList m_examples;
0165         QTimer* m_hideHelpTip;
0166         int m_lineHeight;
0167 };
0168 
0169 }
0170 
0171 #endif