File indexing completed on 2024-05-12 15:28:17

0001 /***************************************************************************
0002     File             : ExpressionTextEdit.h
0003     Project          : LabPlot
0004     --------------------------------------------------------------------
0005     Copyright        : (C) 2014-2017 Alexander Semke (alexander.semke@web.de)
0006     Description      : widget for defining mathematical expressions
0007     modified version of https://doc.qt.io/qt-5/qtwidgets-tools-customcompleter-example.html
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 /****************************************************************************
0030  **
0031  ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
0032  ** Contact: http://www.qt-project.org/legal
0033  **
0034  ** This file is part of the examples of the Qt Toolkit.
0035  **
0036  ** $QT_BEGIN_LICENSE:BSD$
0037  ** You may use this file under the terms of the BSD license as follows:
0038  **
0039  ** "Redistribution and use in source and binary forms, with or without
0040  ** modification, are permitted provided that the following conditions are
0041  ** met:
0042  **   * Redistributions of source code must retain the above copyright
0043  **     notice, this list of conditions and the following disclaimer.
0044  **   * Redistributions in binary form must reproduce the above copyright
0045  **     notice, this list of conditions and the following disclaimer in
0046  **     the documentation and/or other materials provided with the
0047  **     distribution.
0048  **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
0049  **     of its contributors may be used to endorse or promote products derived
0050  **     from this software without specific prior written permission.
0051  **
0052  **
0053  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0054  ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0055  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0056  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0057  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0058  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0059  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0060  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0061  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0062  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0063  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
0064  **
0065  ** $QT_END_LICENSE$
0066  **
0067  ****************************************************************************/
0068 
0069 #ifndef EXPRESSIONTEXTEDIT_H
0070 #define EXPRESSIONTEXTEDIT_H
0071 
0072 #include "backend/worksheet/plots/cartesian/XYEquationCurve.h"
0073 #include <KTextWidgets/KTextEdit>
0074 
0075 class QCompleter;
0076 class EquationHighlighter;
0077 
0078 class ExpressionTextEdit : public KTextEdit {
0079     Q_OBJECT
0080 
0081 public:
0082     explicit ExpressionTextEdit(QWidget *parent = nullptr);
0083     EquationHighlighter* highlighter();
0084     void setExpressionType(XYEquationCurve::EquationType);
0085     void setVariables(const QStringList&);
0086     bool isValid() const;
0087 
0088 protected:
0089     void keyPressEvent(QKeyEvent*) override;
0090     void focusInEvent(QFocusEvent*) override;
0091     void focusOutEvent(QFocusEvent*) override;
0092     void mouseMoveEvent(QMouseEvent*) override;
0093 
0094 private slots:
0095     void insertCompletion(const QString&);
0096     void validateExpression(bool force = false);
0097 
0098 private:
0099     EquationHighlighter* m_highlighter;
0100     XYEquationCurve::EquationType m_expressionType{XYEquationCurve::EquationType::Neutral};
0101     QStringList m_variables;
0102     QCompleter* m_completer;
0103     bool m_isValid{false};
0104     QString m_currentExpression;
0105 
0106 signals:
0107     void expressionChanged();
0108 };
0109 
0110 #endif