From 239812a93246b07715428ed760cff0ce72b48c8d Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 14 Jun 2017 05:08:59 +0200 Subject: [PATCH] Make it possible to load scripts from kalgebramobile --- mobile/plugins/Console.qml | 7 +++---- src/consolehtml.cpp | 23 +++++++++++------------ src/consolemodel.cpp | 14 +++++++------- src/consolemodel.h | 4 ++-- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/mobile/plugins/Console.qml b/mobile/plugins/Console.qml index ea76e72..a12e301 100644 --- a/mobile/plugins/Console.qml +++ b/mobile/plugins/Console.qml @@ -14,9 +14,7 @@ KAlgebraPage // variables: app.variables mode: ConsoleModel.Evaluate onErrorMessage: { - var toadd = i18n("Error: %1", error) - - itemModel.insert(0, { result: toadd }) + itemModel.insert(0, { result: error }) input.selectAll() view.currentIndex = 0 } @@ -36,7 +34,7 @@ KAlgebraPage } function proceedLoadScript() { - + consoleModel.loadScript(fileDialog.fileUrl) } contextualActions: [ @@ -44,6 +42,7 @@ KAlgebraPage text: i18n("Load Script...") onTriggered: { fileDialog.title = text + fileDialog.proceed = page.proceedLoadScript var v = fileDialog.open() console.log("opened...", v) } diff --git a/src/consolehtml.cpp b/src/consolehtml.cpp index 857f1a7..644e5ee 100644 --- a/src/consolehtml.cpp +++ b/src/consolehtml.cpp @@ -63,20 +63,19 @@ Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, s_css, ( "\tp { font-size: " +QByteArray::number(QFontMetrics(QApplication::font()).height())+ "px; }\n" "\n")) -static QString temporaryPath() +static QUrl temporaryPath() { QTemporaryFile temp(QStringLiteral("consolelog")); temp.open(); temp.close(); temp.setAutoRemove(false); - return temp.fileName(); + return QUrl::fromLocalFile(temp.fileName()); } -static QString retrieve(const QUrl& remoteUrl) +static QUrl retrieve(const QUrl& remoteUrl) { - QString path = temporaryPath(); - - KIO::CopyJob* job=KIO::copyAs(remoteUrl, QUrl::fromLocalFile(path)); + const QUrl path = temporaryPath(); + KIO::CopyJob* job=KIO::copyAs(remoteUrl, path); job->exec(); @@ -153,15 +152,15 @@ Analitza::Analyzer* ConsoleHtml::analitza() bool ConsoleHtml::loadScript(const QUrl& path) { - return m_model->loadScript(path.isLocalFile() ? path.toLocalFile() : retrieve(path)); + return m_model->loadScript(path.isLocalFile() ? path : retrieve(path)); } bool ConsoleHtml::saveScript(const QUrl & path) const { - const QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath(); + const QUrl savePath=path.isLocalFile() ? path : temporaryPath(); bool correct = m_model->saveScript(savePath); if(!path.isLocalFile()) { - KIO::CopyJob* job=KIO::move(QUrl(savePath), path); + KIO::CopyJob* job=KIO::move(savePath, path); correct=job->exec(); } return correct; @@ -171,8 +170,8 @@ bool ConsoleHtml::saveLog(const QUrl& path) const { Q_ASSERT(!path.isEmpty()); //FIXME: We have to choose between txt and html - QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath(); - QFile file(savePath); + const QUrl savePath=path.isLocalFile() ? path : temporaryPath(); + QFile file(savePath.toLocalFile()); bool correct = file.open(QIODevice::WriteOnly | QIODevice::Text); if(correct) { @@ -185,7 +184,7 @@ bool ConsoleHtml::saveLog(const QUrl& path) const } if(!path.isLocalFile()) { - KIO::CopyJob* job=KIO::move(QUrl::fromLocalFile(savePath), path); + KIO::CopyJob* job=KIO::move(savePath, path); correct=job->exec(); } return correct; diff --git a/src/consolemodel.cpp b/src/consolemodel.cpp index 1799c04..e260af1 100644 --- a/src/consolemodel.cpp +++ b/src/consolemodel.cpp @@ -63,13 +63,13 @@ bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& in return a.isCorrect(); } -bool ConsoleModel::loadScript(const QString& path) +bool ConsoleModel::loadScript(const QUrl& path) { - Q_ASSERT(!path.isEmpty()); + Q_ASSERT(!path.isEmpty() && path.isLocalFile()); //FIXME: We have expression-only script support bool correct=false; - QFile file(path); + QFile file(path.toLocalFile()); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream stream(&file); @@ -79,20 +79,20 @@ bool ConsoleModel::loadScript(const QString& path) } if(!correct) { - Q_EMIT errorMessage(i18n("", path, a.errors().join(QStringLiteral("
")))); + Q_EMIT errorMessage(i18n("", path.toDisplayString(), a.errors().join(QStringLiteral("
")))); Q_EMIT updateView(QString()); } else - Q_EMIT updateView(i18n("Imported: %1", path)); + Q_EMIT updateView(i18n("Imported: %1", path.toDisplayString())); return correct; } -bool ConsoleModel::saveScript(const QString& savePath) +bool ConsoleModel::saveScript(const QUrl& savePath) { Q_ASSERT(!savePath.isEmpty()); - QFile file(savePath); + QFile file(savePath.toLocalFile()); bool correct=file.open(QIODevice::WriteOnly | QIODevice::Text); if(correct) { diff --git a/src/consolemodel.h b/src/consolemodel.h index a142515..468550b 100644 --- a/src/consolemodel.h +++ b/src/consolemodel.h @@ -41,8 +41,8 @@ public: Q_SCRIPTABLE bool addOperation(const QString& input); bool addOperation(const Analitza::Expression& e, const QString& input); - Q_SCRIPTABLE bool loadScript(const QString &path); - Q_SCRIPTABLE bool saveScript(const QString &path); + Q_SCRIPTABLE bool loadScript(const QUrl &path); + Q_SCRIPTABLE bool saveScript(const QUrl &path); Q_SCRIPTABLE void clear() { m_script.clear(); } ConsoleMode mode() const { return m_mode; } -- GitLab