consolemodel.cpp 3.9 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
/*************************************************************************************
 *  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   *
 *************************************************************************************/

#include "consolemodel.h"

#include <QFile>
#include <QUrl>
#include <KLocalizedString>

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
ConsoleModel::ConsoleModel(QObject* parent, bool preferString)
    : QObject(parent)
{
    if(preferString) {
        connect(this, &ConsoleModel::operationSuccessful, this, [this](const Analitza::Expression &exp, const Analitza::Expression &res) {
            operationSuccessfulString(exp.toString(), res.toString());
        });
    }
}

bool ConsoleModel::addOperation(const QString& input)
{
    return addOperation(Analitza::Expression(input), input);
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& input)
{
    Analitza::Expression res;

    a.setExpression(e);
    if(a.isCorrect()) {
        if (m_mode==ConsoleModel::Evaluation) {
            res=a.evaluate();
        } else {
            res=a.calculate();
        }
    }

    if(a.isCorrect()) {
        a.insertVariable(QStringLiteral("ans"), res);
        m_script += e; //Script won't have the errors
56 57

        Q_EMIT operationSuccessful(e, res);
58
    } else {
59 60
        Q_EMIT errorMessage(i18n("<ul class='error'>Error: <b>%1</b><li>%2</li></ul>", input.toHtmlEscaped(), a.errors().join(QStringLiteral("</li>\n<li>"))));
        Q_EMIT updateView({});
61 62 63 64 65
    }

    return a.isCorrect();
}

66
bool ConsoleModel::loadScript(const QUrl& path)
67
{
68
    Q_ASSERT(!path.isEmpty() && path.isLocalFile());
69 70 71

    //FIXME: We have expression-only script support
    bool correct=false;
72
    QFile file(path.toLocalFile());
73 74 75 76 77 78 79 80 81

    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream stream(&file);

        a.importScript(&stream);
        correct=a.isCorrect();
    }

    if(!correct) {
82
        Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path.toDisplayString(), a.errors().join(QStringLiteral("<br/>"))));
83
        Q_EMIT updateView(QString());
84 85
    }
    else
86
        Q_EMIT updateView(i18n("Imported: %1", path.toDisplayString()));
87 88 89 90

    return correct;
}

91
bool ConsoleModel::saveScript(const QUrl& savePath)
92 93 94
{
    Q_ASSERT(!savePath.isEmpty());

95
    QFile file(savePath.toLocalFile());
96 97 98 99 100 101 102 103 104 105
    bool correct=file.open(QIODevice::WriteOnly | QIODevice::Text);

    if(correct) {
        QTextStream out(&file);
        foreach(const Analitza::Expression& exp, m_script)
            out << exp.toString() << endl;
    }

    return correct;
}
106 107 108 109 110 111 112 113 114

void ConsoleModel::setMode(ConsoleMode mode)
{
    if (m_mode != mode) {
        m_mode = mode;
        Q_EMIT modeChanged(mode);
    }
}