Commit 239812a9 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make it possible to load scripts from kalgebramobile

parent 865a57e3
...@@ -14,9 +14,7 @@ KAlgebraPage ...@@ -14,9 +14,7 @@ KAlgebraPage
// variables: app.variables // variables: app.variables
mode: ConsoleModel.Evaluate mode: ConsoleModel.Evaluate
onErrorMessage: { onErrorMessage: {
var toadd = i18n("Error: %1", error) itemModel.insert(0, { result: error })
itemModel.insert(0, { result: toadd })
input.selectAll() input.selectAll()
view.currentIndex = 0 view.currentIndex = 0
} }
...@@ -36,7 +34,7 @@ KAlgebraPage ...@@ -36,7 +34,7 @@ KAlgebraPage
} }
function proceedLoadScript() { function proceedLoadScript() {
consoleModel.loadScript(fileDialog.fileUrl)
} }
contextualActions: [ contextualActions: [
...@@ -44,6 +42,7 @@ KAlgebraPage ...@@ -44,6 +42,7 @@ KAlgebraPage
text: i18n("Load Script...") text: i18n("Load Script...")
onTriggered: { onTriggered: {
fileDialog.title = text fileDialog.title = text
fileDialog.proceed = page.proceedLoadScript
var v = fileDialog.open() var v = fileDialog.open()
console.log("opened...", v) console.log("opened...", v)
} }
......
...@@ -63,20 +63,19 @@ Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, s_css, ( ...@@ -63,20 +63,19 @@ Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, s_css, (
"\tp { font-size: " +QByteArray::number(QFontMetrics(QApplication::font()).height())+ "px; }\n" "\tp { font-size: " +QByteArray::number(QFontMetrics(QApplication::font()).height())+ "px; }\n"
"</style>\n")) "</style>\n"))
static QString temporaryPath() static QUrl temporaryPath()
{ {
QTemporaryFile temp(QStringLiteral("consolelog")); QTemporaryFile temp(QStringLiteral("consolelog"));
temp.open(); temp.open();
temp.close(); temp.close();
temp.setAutoRemove(false); 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(); const QUrl path = temporaryPath();
KIO::CopyJob* job=KIO::copyAs(remoteUrl, path);
KIO::CopyJob* job=KIO::copyAs(remoteUrl, QUrl::fromLocalFile(path));
job->exec(); job->exec();
...@@ -153,15 +152,15 @@ Analitza::Analyzer* ConsoleHtml::analitza() ...@@ -153,15 +152,15 @@ Analitza::Analyzer* ConsoleHtml::analitza()
bool ConsoleHtml::loadScript(const QUrl& path) 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 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); bool correct = m_model->saveScript(savePath);
if(!path.isLocalFile()) { if(!path.isLocalFile()) {
KIO::CopyJob* job=KIO::move(QUrl(savePath), path); KIO::CopyJob* job=KIO::move(savePath, path);
correct=job->exec(); correct=job->exec();
} }
return correct; return correct;
...@@ -171,8 +170,8 @@ bool ConsoleHtml::saveLog(const QUrl& path) const ...@@ -171,8 +170,8 @@ bool ConsoleHtml::saveLog(const QUrl& path) const
{ {
Q_ASSERT(!path.isEmpty()); Q_ASSERT(!path.isEmpty());
//FIXME: We have to choose between txt and html //FIXME: We have to choose between txt and html
QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath(); const QUrl savePath=path.isLocalFile() ? path : temporaryPath();
QFile file(savePath); QFile file(savePath.toLocalFile());
bool correct = file.open(QIODevice::WriteOnly | QIODevice::Text); bool correct = file.open(QIODevice::WriteOnly | QIODevice::Text);
if(correct) { if(correct) {
...@@ -185,7 +184,7 @@ bool ConsoleHtml::saveLog(const QUrl& path) const ...@@ -185,7 +184,7 @@ bool ConsoleHtml::saveLog(const QUrl& path) const
} }
if(!path.isLocalFile()) { if(!path.isLocalFile()) {
KIO::CopyJob* job=KIO::move(QUrl::fromLocalFile(savePath), path); KIO::CopyJob* job=KIO::move(savePath, path);
correct=job->exec(); correct=job->exec();
} }
return correct; return correct;
......
...@@ -63,13 +63,13 @@ bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& in ...@@ -63,13 +63,13 @@ bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& in
return a.isCorrect(); 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 //FIXME: We have expression-only script support
bool correct=false; bool correct=false;
QFile file(path); QFile file(path.toLocalFile());
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file); QTextStream stream(&file);
...@@ -79,20 +79,20 @@ bool ConsoleModel::loadScript(const QString& path) ...@@ -79,20 +79,20 @@ bool ConsoleModel::loadScript(const QString& path)
} }
if(!correct) { if(!correct) {
Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path, a.errors().join(QStringLiteral("<br/>")))); Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path.toDisplayString(), a.errors().join(QStringLiteral("<br/>"))));
Q_EMIT updateView(QString()); Q_EMIT updateView(QString());
} }
else else
Q_EMIT updateView(i18n("Imported: %1", path)); Q_EMIT updateView(i18n("Imported: %1", path.toDisplayString()));
return correct; return correct;
} }
bool ConsoleModel::saveScript(const QString& savePath) bool ConsoleModel::saveScript(const QUrl& savePath)
{ {
Q_ASSERT(!savePath.isEmpty()); Q_ASSERT(!savePath.isEmpty());
QFile file(savePath); QFile file(savePath.toLocalFile());
bool correct=file.open(QIODevice::WriteOnly | QIODevice::Text); bool correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
if(correct) { if(correct) {
......
...@@ -41,8 +41,8 @@ public: ...@@ -41,8 +41,8 @@ public:
Q_SCRIPTABLE bool addOperation(const QString& input); Q_SCRIPTABLE bool addOperation(const QString& input);
bool addOperation(const Analitza::Expression& e, const QString& input); bool addOperation(const Analitza::Expression& e, const QString& input);
Q_SCRIPTABLE bool loadScript(const QString &path); Q_SCRIPTABLE bool loadScript(const QUrl &path);
Q_SCRIPTABLE bool saveScript(const QString &path); Q_SCRIPTABLE bool saveScript(const QUrl &path);
Q_SCRIPTABLE void clear() { m_script.clear(); } Q_SCRIPTABLE void clear() { m_script.clear(); }
ConsoleMode mode() const { return m_mode; } ConsoleMode mode() const { return m_mode; }
......
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