Commit 865a57e3 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Use the ConsoleModel from kalgebramobile

parent f209635f
......@@ -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
......
......@@ -22,6 +22,8 @@
#include <analitza/variables.h>
#include <analitza/expression.h>
#include "../src/consolemodel.h"
#include <qqml.h>
#include "pluginsmodel.h"
......@@ -37,7 +39,9 @@ KAlgebraMobile::KAlgebraMobile(QObject* parent)
s_self=this;
qmlRegisterType<PluginsModel>("org.kde.kalgebra.mobile", 1, 0, "PluginsModel");
qmlRegisterType<ConsoleModel>("org.kde.kalgebra.mobile", 1, 0, "ConsoleModel");
qmlRegisterType<QAbstractItemModel>();
qmlRegisterUncreatableType<Analitza::Expression>("org.kde.kalgebra.mobile", 1, 0, "Expression", "because");
}
PlotsModel* KAlgebraMobile::functionsModel()
......
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 {
......
......@@ -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("<a title='%1' href='kalgebra:/query?id=copy&func=%2'><span class='exp'>%3</span></a><br />=<a title='kalgebra:%1' href='/query?id=copy&func=%4'><span class='result'>%5</span>")
.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 = "<div class='options'>"+i18n("Options: %1", options)+"</div>";
}
updateViewWithOptions(newEntry, options);
}
void ConsoleHtml::updateView(const QString& newEntry)
{
updateViewWithOptions(newEntry, {});
}
void ConsoleHtml::updateViewWithOptions(const QString& newEntry, const QString &options)
{
QByteArray code;
code += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
code += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n<head>\n\t<title> :) </title>\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"
......@@ -105,10 +105,11 @@ class ConsoleHtml : public QWebEngineView
void addMessage(const QString& message);
private:
QStringList m_htmlLog;
void updateView(const QString& newEntry, const Analitza::Expression &exp);
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;
QList<InlineOptions*> m_options;
QScopedPointer<ConsoleModel> m_model;
};
......
......@@ -22,6 +22,21 @@
#include <QUrl>
#include <KLocalizedString>
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("<a title='%1' href='kalgebra:/query?id=copy&func=%2'><span class='exp'>%3</span></a><br />=<a title='kalgebra:%1' href='/query?id=copy&func=%4'><span class='result'>%5</span>")
.arg(i18n("Paste to Input")).arg(e.toString()).arg(e.toHtml()).arg(res.toString()).arg(result);
Q_EMIT operationSuccessful(e, res);
} else {
errorMessage(i18n("<ul class='error'>Error: <b>%1</b><li>%2</li></ul>", input.toHtmlEscaped(), a.errors().join(QStringLiteral("</li>\n<li>"))));
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({});
}
updateView(newEntry);
return a.isCorrect();
}
......@@ -69,10 +80,10 @@ bool ConsoleModel::loadScript(const QString& path)
if(!correct) {
Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path, a.errors().join(QStringLiteral("<br/>"))));
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);
}
}
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment