From 865a57e3155a18db9eb4321037c27bdf95d0ba93 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 14 Jun 2017 04:56:31 +0200 Subject: [PATCH] Use the ConsoleModel from kalgebramobile --- mobile/CMakeLists.txt | 1 + mobile/kalgebramobile.cpp | 4 ++++ mobile/plugins/Console.qml | 41 +++++++++++++++++++++----------------- src/consolehtml.cpp | 24 ++++++++++++++++------ src/consolehtml.h | 7 ++++--- src/consolemodel.cpp | 40 +++++++++++++++++++++++++++---------- src/consolemodel.h | 22 +++++++++++++------- 7 files changed, 95 insertions(+), 44 deletions(-) diff --git a/mobile/CMakeLists.txt b/mobile/CMakeLists.txt index fc8affb..c528310 100644 --- a/mobile/CMakeLists.txt +++ b/mobile/CMakeLists.txt @@ -6,6 +6,7 @@ endif() qt5_add_resources(KALGEBRAMOBILE_SRCS resources.qrc) add_executable(kalgebramobile pluginsmodel.cpp + ../src/consolemodel.cpp kalgebramobile.cpp main.cpp ${KALGEBRAMOBILE_SRCS}) target_link_libraries(kalgebramobile Qt5::Qml Qt5::Quick Qt5::Gui diff --git a/mobile/kalgebramobile.cpp b/mobile/kalgebramobile.cpp index 5522d47..153bff2 100644 --- a/mobile/kalgebramobile.cpp +++ b/mobile/kalgebramobile.cpp @@ -22,6 +22,8 @@ #include #include +#include "../src/consolemodel.h" + #include #include "pluginsmodel.h" @@ -37,7 +39,9 @@ KAlgebraMobile::KAlgebraMobile(QObject* parent) s_self=this; qmlRegisterType("org.kde.kalgebra.mobile", 1, 0, "PluginsModel"); + qmlRegisterType("org.kde.kalgebra.mobile", 1, 0, "ConsoleModel"); qmlRegisterType(); + qmlRegisterUncreatableType("org.kde.kalgebra.mobile", 1, 0, "Expression", "because"); } PlotsModel* KAlgebraMobile::functionsModel() diff --git a/mobile/plugins/Console.qml b/mobile/plugins/Console.qml index a71ec4d..ea76e72 100644 --- a/mobile/plugins/Console.qml +++ b/mobile/plugins/Console.qml @@ -1,6 +1,7 @@ import QtQuick 2.2 import QtQuick.Dialogs 1.0 import org.kde.analitza 1.0 +import org.kde.kalgebra.mobile 1.0 import widgets 1.0 KAlgebraPage @@ -8,10 +9,22 @@ KAlgebraPage id: page ListModel { id: itemModel } - Analitza { - id: a - variables: app.variables - calculate: false + ConsoleModel { + id: consoleModel +// variables: app.variables + mode: ConsoleModel.Evaluate + onErrorMessage: { + var toadd = i18n("Error: %1", error) + + itemModel.insert(0, { result: toadd }) + input.selectAll() + view.currentIndex = 0 + } + onOperationSuccessfulString: { + itemModel.insert(0, { result: expression + "=" + result }) + input.selectAll() + view.currentIndex = 0 + } } FileDialog { @@ -22,7 +35,9 @@ KAlgebraPage property var proceed } - function proceedLoadScript() + function proceedLoadScript() { + + } contextualActions: [ Action { @@ -42,8 +57,8 @@ KAlgebraPage }, // -- Action { - text: a.calculate ? i18n("Evaluate...") : i18n("Calculate...") - onTriggered: a.calculate = !a.calculate + text: consoleModel.mode == ConsoleModel.Calculate ? i18n("Evaluate...") : i18n("Calculate...") + onTriggered: consoleModel.mode = !consoleModel.mode }, // -- Action { @@ -58,17 +73,7 @@ KAlgebraPage focus: true Keys.onReturnPressed: { - var res = a.execute(text) - - var toadd = "" - if(!a.isCorrect) - toadd = "Error: " + (res ? res : a.errors) - else - toadd = text + " = " + res.expression - - itemModel.insert(0, { result: toadd, resultsInput: text }) - input.selectAll() - view.currentIndex = 0 + consoleModel.addOperation(text) } anchors { diff --git a/src/consolehtml.cpp b/src/consolehtml.cpp index bb176e3..857f1a7 100644 --- a/src/consolehtml.cpp +++ b/src/consolehtml.cpp @@ -98,9 +98,10 @@ public: ConsoleHtml::ConsoleHtml(QWidget *parent) : QWebEngineView(parent) - , m_model(new ConsoleModel) + , m_model(new ConsoleModel(this, false)) { connect(m_model.data(), &ConsoleModel::updateView, this, &ConsoleHtml::updateView); + connect(m_model.data(), &ConsoleModel::operationSuccessful, this, &ConsoleHtml::includeOperation); connect(m_model.data(), &ConsoleModel::errorMessage, this, &ConsoleHtml::addMessage); setPage(new ConsolePage(this)); } @@ -190,10 +191,14 @@ bool ConsoleHtml::saveLog(const QUrl& path) const return correct; } -void ConsoleHtml::updateView(const QString& newEntry, const Analitza::Expression &res) +void ConsoleHtml::includeOperation(const Analitza::Expression& e, const Analitza::Expression& res) { - QString options; + QString options, newEntry; if (res.isCorrect()) { + const auto result = res.toHtml(); + newEntry = QStringLiteral("%3
=%5") + .arg(i18n("Paste to Input"), e.toString(), e.toHtml(), res.toString(), result); + Analitza::Analyzer lambdifier(m_model->variables()); lambdifier.setExpression(res); Analitza::Expression lambdaexp = lambdifier.dependenciesToLambda(); @@ -214,7 +219,16 @@ void ConsoleHtml::updateView(const QString& newEntry, const Analitza::Expression if(!options.isEmpty()) options = "
"+i18n("Options: %1", options)+"
"; } + updateViewWithOptions(newEntry, options); +} +void ConsoleHtml::updateView(const QString& newEntry) +{ + updateViewWithOptions(newEntry, {}); +} + +void ConsoleHtml::updateViewWithOptions(const QString& newEntry, const QString &options) +{ QByteArray code; code += "\n"; code += "\n\n\t :) \n"; @@ -265,7 +279,7 @@ void ConsoleHtml::clear() { m_model->clear(); m_htmlLog.clear(); - updateView(QString(), {}); + updateView(QString()); } void ConsoleHtml::modifyVariable(const QString& name, const Analitza::Expression& exp) @@ -282,5 +296,3 @@ void ConsoleHtml::paste() { emit paste(selectedText()); } - -#include "consolehtml.moc" diff --git a/src/consolehtml.h b/src/consolehtml.h index c2306ad..44a22f2 100644 --- a/src/consolehtml.h +++ b/src/consolehtml.h @@ -105,10 +105,11 @@ class ConsoleHtml : public QWebEngineView void addMessage(const QString& message); private: + void includeOperation(const Analitza::Expression &expression, const Analitza::Expression &result); + void updateView(const QString& newEntry); + void updateViewWithOptions(const QString& newEntry, const QString &options); + QStringList m_htmlLog; - - void updateView(const QString& newEntry, const Analitza::Expression &exp); - QList m_options; QScopedPointer m_model; }; diff --git a/src/consolemodel.cpp b/src/consolemodel.cpp index d9eea25..1799c04 100644 --- a/src/consolemodel.cpp +++ b/src/consolemodel.cpp @@ -22,6 +22,21 @@ #include #include +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); +} + bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& input) { Analitza::Expression res; @@ -35,20 +50,16 @@ bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& in } } - QString result, newEntry; if(a.isCorrect()) { - result = res.toHtml(); - a.insertVariable(QStringLiteral("ans"), res); m_script += e; //Script won't have the errors - newEntry = QString("
%3
=%5") - .arg(i18n("Paste to Input")).arg(e.toString()).arg(e.toHtml()).arg(res.toString()).arg(result); + + Q_EMIT operationSuccessful(e, res); } else { - errorMessage(i18n("
    Error: %1
  • %2
", input.toHtmlEscaped(), a.errors().join(QStringLiteral("\n
  • ")))); + Q_EMIT errorMessage(i18n("
      Error: %1
    • %2
    ", input.toHtmlEscaped(), a.errors().join(QStringLiteral("
  • \n
  • ")))); + Q_EMIT updateView({}); } - updateView(newEntry); - return a.isCorrect(); } @@ -69,10 +80,10 @@ bool ConsoleModel::loadScript(const QString& path) if(!correct) { Q_EMIT errorMessage(i18n("
      Error: Could not load %1.
      %2
    ", path, a.errors().join(QStringLiteral("
    ")))); - updateView(QString()); + Q_EMIT updateView(QString()); } else - updateView(i18n("Imported: %1", path)); + Q_EMIT updateView(i18n("Imported: %1", path)); return correct; } @@ -92,3 +103,12 @@ bool ConsoleModel::saveScript(const QString& savePath) return correct; } + +void ConsoleModel::setMode(ConsoleMode mode) +{ + if (m_mode != mode) { + m_mode = mode; + Q_EMIT modeChanged(mode); + } +} + diff --git a/src/consolemodel.h b/src/consolemodel.h index 1968439..a142515 100644 --- a/src/consolemodel.h +++ b/src/consolemodel.h @@ -26,8 +26,11 @@ class ConsoleModel : public QObject { Q_OBJECT - Q_PROPERTY(ConsoleMode mode READ mode WRITE setMode) + Q_PROPERTY(ConsoleMode mode READ mode WRITE setMode NOTIFY modeChanged) + Q_PROPERTY(Analitza::Variables* variables READ variables) public: + ConsoleModel(QObject* parent = nullptr, bool preferString = true); + /** 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. */ @@ -35,20 +38,25 @@ public: }; Q_ENUM(ConsoleMode) + Q_SCRIPTABLE bool addOperation(const QString& input); bool addOperation(const Analitza::Expression& e, const QString& input); - bool loadScript(const QString &path); - bool saveScript(const QString &path); + Q_SCRIPTABLE bool loadScript(const QString &path); + Q_SCRIPTABLE bool saveScript(const QString &path); + Q_SCRIPTABLE void clear() { m_script.clear(); } - Analitza::Variables* variables() const { return a.variables(); } ConsoleMode mode() const { return m_mode; } - void setMode(ConsoleMode mode) { m_mode = mode; } - void clear() { m_script.clear(); } + void setMode(ConsoleMode mode); + + Analitza::Variables* variables() const { return a.variables(); } Analitza::Analyzer* analyzer() { return &a; } Q_SIGNALS: void errorMessage(const QString &error); - void updateView(const QString &newEntry, const Analitza::Expression &result = {}); + 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); private: Analitza::Analyzer a; -- GitLab