Commit f9e340b8 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Move the ConsoleModel into a separate file

parent 8468cc12
set(kalgebra_SRCS
kalgebra.cpp
dictionary.cpp
#console.cpp
consolemodel.cpp
consolehtml.cpp
varedit.cpp
main.cpp
......
......@@ -17,13 +17,13 @@
*************************************************************************************/
#include "consolehtml.h"
#include <QFile>
#include <QClipboard>
#include <QApplication>
#include <QTemporaryFile>
#include <qevent.h>
#include <QUrlQuery>
#include <QTemporaryFile>
#include <QDir>
#include <qurlquery.h>
#include <KStandardAction>
#include <QMenu>
......@@ -63,113 +63,25 @@ Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, s_css, (
"\tp { font-size: " +QByteArray::number(QFontMetrics(QApplication::font()).height())+ "px; }\n"
"</style>\n"))
class ConsoleModel : public QObject
static QString temporaryPath()
{
Q_OBJECT
public:
bool loadScript(const QUrl &path) {
Q_ASSERT(!path.isEmpty());
//FIXME: We have expression-only script support
bool correct=false;
QFile file(path.isLocalFile() ? path.toLocalFile() : retrieve(path));
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
a.importScript(&stream);
correct=a.isCorrect();
}
if(!correct) {
Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path.toString(), a.errors().join(QStringLiteral("<br/>"))));
updateView(QString());
}
else
updateView(i18n("Imported: %1", path.toString()));
return correct;
}
bool addOperation(const Analitza::Expression& e, const QString& input) {
Analitza::Expression res;
a.setExpression(e);
if(a.isCorrect()) {
if (m_mode==ConsoleHtml::Evaluation) {
res=a.evaluate();
} else {
res=a.calculate();
}
}
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);
} else {
errorMessage(i18n("<ul class='error'>Error: <b>%1</b><li>%2</li></ul>", input.toHtmlEscaped(), a.errors().join(QStringLiteral("</li>\n<li>"))));
}
updateView(newEntry);
return a.isCorrect();
}
bool saveScript(const QUrl &path) {
bool correct=false;
Q_ASSERT(!path.isEmpty());
QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
QFile file(savePath);
correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
if(correct) {
QTextStream out(&file);
foreach(const Analitza::Expression& exp, m_script)
out << exp.toString() << endl;
}
if(!path.isLocalFile()) {
KIO::CopyJob* job=KIO::move(QUrl(savePath), path);
correct=job->exec();
}
return correct;
}
Q_SIGNALS:
void errorMessage(const QString &error);
void updateView(const QString &newEntry, const Analitza::Expression &result = {});
public:
static QString temporaryPath()
{
QTemporaryFile temp(QStringLiteral("consolelog"));
temp.open();
temp.close();
temp.setAutoRemove(false);
return temp.fileName();
}
QTemporaryFile temp(QStringLiteral("consolelog"));
temp.open();
temp.close();
temp.setAutoRemove(false);
return temp.fileName();
}
static QString retrieve(const QUrl& remoteUrl)
{
QString path = temporaryPath();
static QString retrieve(const QUrl& remoteUrl)
{
QString path = temporaryPath();
KIO::CopyJob* job=KIO::copyAs(remoteUrl, QUrl::fromLocalFile(path));
KIO::CopyJob* job=KIO::copyAs(remoteUrl, QUrl::fromLocalFile(path));
job->exec();
job->exec();
return path;
}
Analitza::Analyzer a;
ConsoleHtml::ConsoleMode m_mode = ConsoleHtml::Evaluation;
QList<Analitza::Expression> m_script;
};
return path;
}
class ConsolePage : public QWebEnginePage
{
......@@ -223,12 +135,12 @@ bool ConsoleHtml::addOperation(const Analitza::Expression& e, const QString& inp
return m_model->addOperation(e, input);
}
ConsoleHtml::ConsoleMode ConsoleHtml::mode() const
ConsoleModel::ConsoleMode ConsoleHtml::mode() const
{
return m_model->m_mode;
}
void ConsoleHtml::setMode(ConsoleMode newMode)
void ConsoleHtml::setMode(ConsoleModel::ConsoleMode newMode)
{
m_model->m_mode = newMode;
}
......@@ -240,19 +152,25 @@ Analitza::Analyzer* ConsoleHtml::analitza()
bool ConsoleHtml::loadScript(const QUrl& path)
{
return m_model->loadScript(path);
return m_model->loadScript(path.isLocalFile() ? path.toLocalFile() : retrieve(path));
}
bool ConsoleHtml::saveScript(const QUrl & path) const
{
return m_model->saveScript(path);
const QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
bool correct = m_model->saveScript(savePath);
if(!path.isLocalFile()) {
KIO::CopyJob* job=KIO::move(QUrl(savePath), path);
correct=job->exec();
}
return correct;
}
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() : ConsoleModel::temporaryPath();
QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
QFile file(savePath);
bool correct = file.open(QIODevice::WriteOnly | QIODevice::Text);
......
......@@ -22,6 +22,7 @@
#include <QWidget>
#include <QWebEngineView>
#include "consolemodel.h"
#include <analitza/analyzer.h>
class ConsoleModel;
......@@ -47,12 +48,6 @@ class ConsoleHtml : public QWebEngineView
{
Q_OBJECT
public:
/** 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. */
Calculation /**< Calculates everything, if it finds a not defined variable shows an error. */
};
/** Constructor. Creates a console widget. */
ConsoleHtml(QWidget *parent = 0);
......@@ -63,10 +58,10 @@ class ConsoleHtml : public QWebEngineView
Analitza::Analyzer* analitza();
/** Sets a @p newMode console mode. */
void setMode(ConsoleMode newMode);
void setMode(ConsoleModel::ConsoleMode newMode);
/** Retrieves the console mode. */
ConsoleMode mode() const;
ConsoleModel::ConsoleMode mode() const;
void addOptionsObserver(InlineOptions* opt) { m_options += opt; }
......
/*************************************************************************************
* 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>
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();
}
}
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);
} else {
errorMessage(i18n("<ul class='error'>Error: <b>%1</b><li>%2</li></ul>", input.toHtmlEscaped(), a.errors().join(QStringLiteral("</li>\n<li>"))));
}
updateView(newEntry);
return a.isCorrect();
}
bool ConsoleModel::loadScript(const QString& path)
{
Q_ASSERT(!path.isEmpty());
//FIXME: We have expression-only script support
bool correct=false;
QFile file(path);
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
a.importScript(&stream);
correct=a.isCorrect();
}
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());
}
else
updateView(i18n("Imported: %1", path));
return correct;
}
bool ConsoleModel::saveScript(const QString& savePath)
{
Q_ASSERT(!savePath.isEmpty());
QFile file(savePath);
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;
}
/*************************************************************************************
* 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 *
*************************************************************************************/
#ifndef CONSOLEMODEL_H
#define CONSOLEMODEL_H
#include <QObject>
#include <analitza/expression.h>
#include <analitza/analyzer.h>
class ConsoleModel : public QObject
{
Q_OBJECT
public:
/** 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. */
Calculation /**< Calculates everything, if it finds a not defined variable shows an error. */
};
bool addOperation(const Analitza::Expression& e, const QString& input);
bool loadScript(const QString &path);
bool saveScript(const QString &path);
Q_SIGNALS:
void errorMessage(const QString &error);
void updateView(const QString &newEntry, const Analitza::Expression &result = {});
public:
Analitza::Analyzer a;
ConsoleMode m_mode = Evaluation;
QVector<Analitza::Expression> m_script;
};
#endif
......@@ -667,12 +667,12 @@ void KAlgebra::updateInformation()
void KAlgebra::consoleCalculate()
{
c_results->setMode(ConsoleHtml::Calculation);
c_results->setMode(ConsoleModel::Calculation);
}
void KAlgebra::consoleEvaluate()
{
c_results->setMode(ConsoleHtml::Evaluation);
c_results->setMode(ConsoleModel::Evaluation);
}
void KAlgebra::valueChanged()
......
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