consolemodel.h 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*************************************************************************************
 *  Copyright (C) 2007-2017 by Aleix Pol <aleixpol@kde.org>                          *
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#ifndef CONSOLEMODEL_H
#define CONSOLEMODEL_H

#include <QObject>
#include <analitza/expression.h>
#include <analitza/analyzer.h>

class ConsoleModel : public QObject
{
    Q_OBJECT
29 30
    Q_PROPERTY(ConsoleMode mode READ mode WRITE setMode NOTIFY modeChanged)
    Q_PROPERTY(Analitza::Variables* variables READ variables)
31
public:
32 33
    ConsoleModel(QObject* parent = nullptr, bool preferString = true);

34 35 36 37 38
    /** This enumeration controles the way the console will calculate and show his results. */
    enum ConsoleMode {
        Evaluation, /**< Simplifies the expression, tries to simplify when sees a variable not defined. */
        Calculation /**< Calculates everything, if it finds a not defined variable shows an error. */
    };
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
39
    Q_ENUM(ConsoleMode)
40

41
    Q_SCRIPTABLE bool addOperation(const QString& input);
42 43
    bool addOperation(const Analitza::Expression& e, const QString& input);

44 45
    Q_SCRIPTABLE bool loadScript(const QUrl &path);
    Q_SCRIPTABLE bool saveScript(const QUrl &path);
46
    Q_SCRIPTABLE void clear() { m_script.clear(); }
47

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
48
    ConsoleMode mode() const { return m_mode; }
49 50 51
    void setMode(ConsoleMode mode);

    Analitza::Variables* variables() const { return a.variables(); }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
52 53
    Analitza::Analyzer* analyzer() { return &a; }

54 55
Q_SIGNALS:
    void errorMessage(const QString &error);
56 57 58 59
    void updateView(const QString &entry);
    void modeChanged(ConsoleMode mode);
    void operationSuccessful(const Analitza::Expression &expression, const Analitza::Expression &result);
    void operationSuccessfulString(const QString &expression, const QString &result);
60

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
61
private:
62 63 64 65 66 67
    Analitza::Analyzer a;
    ConsoleMode m_mode = Evaluation;
    QVector<Analitza::Expression> m_script;
};

#endif