File indexing completed on 2022-10-04 13:44:19

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
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 ALGEBRAHIGHLIGHTER_H
0020 #define ALGEBRAHIGHLIGHTER_H
0021 
0022 #include <QSyntaxHighlighter>
0023 #include "analitzaguiexport.h"
0024 
0025 /**
0026  *    The AlgebraHighlighter class is used to highlight the ExpressionEdit text.
0027  *    @author <aleixpol@kde.org>  
0028  */
0029 
0030 namespace Analitza { class Analyzer; }
0031 
0032 class ANALITZAGUI_EXPORT AlgebraHighlighter : public QSyntaxHighlighter
0033 {
0034     public:
0035         /** Defines the format status that could be used. */
0036         typedef enum {
0037             Expression,    /**< String expression format. */
0038             MathML,        /**< MathML format. */
0039             Autodetect    /**< Try to guess which format is being used. */
0040         } Mode;
0041         
0042         /** Constructor. Creates an AlgebraHighlighter from a QTextDocument @p doc. */
0043         explicit AlgebraHighlighter(QTextDocument *doc, const Analitza::Analyzer* na=nullptr);
0044         //int highlightParagraph(const QString &text, int endStateOfLastPara);
0045         
0046         /** Returns the currently highlight mode. */
0047         Mode mode() { return m_mode; }
0048         
0049         /** Sets the highlight mode. */
0050         void setMode(const Mode& newMode){ m_mode=newMode; rehighlight(); }
0051         
0052         /** 
0053          *    Returns whether something wrong has been found. It is an uncomplete way
0054          *    to know if it is correct because doesn't do any recursive check, but could be useful.
0055          * @returns whether it is a lexically correct expression.
0056          */
0057         bool isCorrect() const { return m_correct; }
0058         
0059         /** Sets the cursor position. */
0060         void setPos(uint p) { m_pos=p; }
0061         
0062         /** Sets the corresponding Analitza class. */
0063         void setAnalitza(const Analitza::Analyzer* na) { a = na; }
0064         
0065         ///@returns the name of the function that's being edited, if any
0066         QString editingName() const;
0067         
0068         ///@returns the number of the parameter that's being edited
0069         int editingParameter() const;
0070         bool editingBounds() const;
0071     private:
0072         void highlightBlock(const QString &text) override;
0073         
0074         enum MMLtokEnum { //For mathml highlighting
0075             gt,    
0076             lt,
0077             tag,
0078             value
0079         };
0080         
0081         bool m_correct;
0082         Mode m_mode;
0083         uint m_pos;
0084         int m_editingParameter;
0085         QString m_editingName;
0086         bool m_editingBounds;
0087         QString m_aName;
0088         
0089         QTextCharFormat bold;
0090         const Analitza::Analyzer* a;
0091 };
0092 
0093 #endif