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

0001 /*************************************************************************************
0002  *  Copyright (C) 2007-2017 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 CONSOLEMODEL_H
0020 #define CONSOLEMODEL_H
0021 
0022 #include <QObject>
0023 #include <analitza/analyzer.h>
0024 #include <analitza/expression.h>
0025 #include <analitza/variables.h>
0026 
0027 class ConsoleModel : public QObject
0028 {
0029     Q_OBJECT
0030     Q_PROPERTY(ConsoleMode mode READ mode WRITE setMode NOTIFY modeChanged)
0031     Q_PROPERTY(QSharedPointer<Analitza::Variables> variables READ variables WRITE setVariables NOTIFY variablesChanged)
0032 public:
0033     ConsoleModel(QObject *parent = nullptr);
0034 
0035     /** This enumeration controles the way the console will calculate and show his results. */
0036     enum ConsoleMode {
0037         Evaluation, /**< Simplifies the expression, tries to simplify when sees a variable not defined. */
0038         Calculation /**< Calculates everything, if it finds a not defined variable shows an error. */
0039     };
0040     Q_ENUM(ConsoleMode)
0041 
0042     Q_SCRIPTABLE bool addOperation(const QString &input);
0043     bool addOperation(const Analitza::Expression &e, const QString &input);
0044 
0045     Q_SCRIPTABLE bool loadScript(const QUrl &path);
0046     Q_SCRIPTABLE bool saveScript(const QUrl &path);
0047     Q_SCRIPTABLE void clear();
0048     Q_SCRIPTABLE bool saveLog(const QUrl &path) const;
0049 
0050     Q_SCRIPTABLE static QString readContent(const QUrl &url);
0051 
0052     QByteArray css() const;
0053 
0054     ConsoleMode mode() const
0055     {
0056         return m_mode;
0057     }
0058     void setMode(ConsoleMode mode);
0059 
0060     QSharedPointer<Analitza::Variables> variables() const
0061     {
0062         return a.variables();
0063     }
0064     void setVariables(const QSharedPointer<Analitza::Variables> &vars);
0065     Analitza::Analyzer *analyzer()
0066     {
0067         return &a;
0068     }
0069 
0070     QList<QByteArray> htmlLog() const
0071     {
0072         return m_htmlLog;
0073     }
0074 
0075 Q_SIGNALS:
0076     void message(const QString &msg, const Analitza::Expression &operation, const Analitza::Expression &result);
0077     void updateView();
0078     void modeChanged(ConsoleModel::ConsoleMode mode);
0079     void operationSuccessful(const Analitza::Expression &expression, const Analitza::Expression &result);
0080     void variablesChanged();
0081 
0082 private:
0083     void addMessage(const QString &msg, const Analitza::Expression &operation, const Analitza::Expression &result);
0084 
0085     QList<QByteArray> m_htmlLog;
0086     Analitza::Analyzer a;
0087     ConsoleMode m_mode = Evaluation;
0088     QList<Analitza::Expression> m_script;
0089 };
0090 
0091 #endif