Commit 32ed4b5a authored by Alexander Semke's avatar Alexander Semke
Browse files

Moved the functions parseOutput() and parseError() to the base clase...

Moved the functions parseOutput() and parseError() to the base clase Expression and removed multiple obsolete casts in the derived classes.
Furthermore, replaced the remaining occurancies of the deprecated QProcess:pid().
parent 9a4e9ba3
Pipeline #123950 canceled with stage
......@@ -19,8 +19,8 @@ class RExpression : public Cantor::Expression
void evaluate() override;
void interrupt() override;
void parseOutput(const QString&);
void parseError(const QString&);
void parseOutput(const QString&) override;
void parseError(const QString&) override;
void showFilesAsResult(const QStringList&);
void addInformation(const QString&) override;
......
......@@ -86,13 +86,13 @@ void RSession::interrupt()
if(m_process && m_process->state() != QProcess::NotRunning)
{
#ifndef Q_OS_WIN
const int pid=m_process->pid();
const int pid = m_process->processId();
kill(pid, SIGINT);
#else
; //TODO: interrupt the process on windows
#endif
}
foreach (Cantor::Expression* expression, expressionQueue())
for (auto* expression : expressionQueue())
expression->setStatus(Cantor::Expression::Interrupted);
expressionQueue().clear();
......@@ -105,7 +105,7 @@ void RSession::interrupt()
Cantor::Expression* RSession::evaluateExpression(const QString& cmd, Cantor::Expression::FinishingBehavior behave, bool internal)
{
qDebug()<<"evaluating: "<<cmd;
RExpression* expr=new RExpression(this, internal);
auto* expr = new RExpression(this, internal);
expr->setFinishingBehavior(behave);
expr->setCommand(cmd);
......@@ -116,7 +116,7 @@ Cantor::Expression* RSession::evaluateExpression(const QString& cmd, Cantor::Exp
Cantor::CompletionObject* RSession::completionFor(const QString& command, int index)
{
RCompletionObject *cmp=new RCompletionObject(command, index, this);
auto* cmp = new RCompletionObject(command, index, this);
return cmp;
}
......@@ -141,11 +141,11 @@ void RSession::expressionFinished(int returnCode, const QString& text, const QSt
{
if (!expressionQueue().isEmpty())
{
RExpression* expr = static_cast<RExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
if (expr->status() == Cantor::Expression::Interrupted)
return;
expr->showFilesAsResult(files);
static_cast<RExpression*>(expr)->showFilesAsResult(files);
if(returnCode==RExpression::SuccessCode)
expr->parseOutput(text);
......@@ -162,7 +162,7 @@ void RSession::runFirstExpression()
if (expressionQueue().isEmpty())
return;
RExpression* expr = static_cast<RExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
qDebug()<<"running expression: "<<expr->command();
expr->setStatus(Cantor::Expression::Computing);
......@@ -172,9 +172,9 @@ void RSession::runFirstExpression()
void RSession::sendInputToServer(const QString& input)
{
QString s=input;
QString s = input;
if(!input.endsWith(QLatin1Char('\n')))
s+=QLatin1Char('\n');
s += QLatin1Char('\n');
m_rServer->answerRequest(s);
}
......
......@@ -34,6 +34,9 @@ public:
QString internalCommand() override;
void parseOutput(const QString&) override {};
void parseError(const QString&) override {};
/**
* Call this function from session when JuliaServer ends evaluation of
* this expression.
......
......@@ -194,7 +194,7 @@ void MaximaExpression::forceDone()
latex mode - "<cantor-result><cantor-text>\n(%o1) 10\n</cantor-text><cantor-latex>\\mbox{\\tt\\red(\\mathrm{\\%o1}) \\black}10</cantor-latex></cantor-result>\n<cantor-prompt>(%i2) </cantor-prompt>\n"
text mode - "<cantor-result><cantor-text>\n(%o1) 10\n</cantor-text></cantor-result>\n<cantor-prompt>(%i2) </cantor-prompt>\n"
*/
bool MaximaExpression::parseOutput(QString& out)
void MaximaExpression::parseOutput(const QString& out)
{
const int promptStart = out.indexOf(QLatin1String("<cantor-prompt>"));
const int promptEnd = out.indexOf(QLatin1String("</cantor-prompt>"));
......@@ -210,7 +210,7 @@ bool MaximaExpression::parseOutput(QString& out)
qDebug()<<"asking for additional input for " << textContent;
emit needsAdditionalInformation(textContent);
return true;
return;
}
qDebug()<<"new input label: " << prompt;
......@@ -304,8 +304,6 @@ bool MaximaExpression::parseOutput(QString& out)
setStatus(Cantor::Expression::Error);
}
}
return true;
}
void MaximaExpression::parseResult(const QString& resultContent)
......
......@@ -30,8 +30,8 @@ public:
void forceDone();
//reads from @param out until a prompt indicates that a new expression has started
bool parseOutput(QString&);
void parseError(const QString&);
void parseOutput(const QString&) override;
void parseError(const QString&) override;
void addInformation(const QString&) override;
......
......@@ -133,7 +133,7 @@ void MaximaSession::readStdErr()
if(expressionQueue().size()>0)
{
auto* expr = static_cast<MaximaExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
expr->parseError(out);
}
}
......@@ -155,7 +155,7 @@ void MaximaSession::readStdOut()
return;
}
auto* expr = static_cast<MaximaExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
if (!expr)
return; //should never happen
......@@ -176,7 +176,7 @@ void MaximaSession::reportProcessError(QProcess::ProcessError e)
void MaximaSession::currentExpressionChangedStatus(Cantor::Expression::Status status)
{
Cantor::Expression* expression = expressionQueue().first();
auto* expression = expressionQueue().first();
qDebug() << "expression status changed: command = " << expression->command() << ", status = " << status;
switch (status)
......@@ -203,15 +203,15 @@ void MaximaSession::runFirstExpression()
if(!expressionQueue().isEmpty())
{
auto* expr = static_cast<MaximaExpression*>(expressionQueue().first());
QString command=expr->internalCommand();
auto* expr = expressionQueue().first();
QString command = expr->internalCommand();
connect(expr, SIGNAL(statusChanged(Cantor::Expression::Status)), this, SLOT(currentExpressionChangedStatus(Cantor::Expression::Status)));
expr->setStatus(Cantor::Expression::Computing);
if(command.isEmpty())
{
qDebug()<<"empty command";
expr->forceDone();
static_cast<MaximaExpression*>(expr)->forceDone();
}
else
{
......@@ -229,14 +229,15 @@ void MaximaSession::interrupt()
if(m_process && m_process->state() != QProcess::NotRunning)
{
#ifndef Q_OS_WIN
const int pid=m_process->pid();
const int pid=m_process->processId();
kill(pid, SIGINT);
#else
; //TODO: interrupt the process on windows
#endif
}
foreach (Cantor::Expression* expression, expressionQueue())
for (auto* expression : expressionQueue())
expression->setStatus(Cantor::Expression::Interrupted);
expressionQueue().clear();
qDebug()<<"done interrupting";
......@@ -253,7 +254,7 @@ void MaximaSession::sendInputToProcess(const QString& input)
void MaximaSession::restartMaxima()
{
qDebug()<<"restarting maxima cooldown: "<<m_justRestarted;
qDebug()<<"restarting maxima cooldown: " << m_justRestarted;
if(!m_justRestarted)
{
......@@ -285,7 +286,7 @@ void MaximaSession::setTypesettingEnabled(bool enable)
{
//we use the lisp command to set the variable, as those commands
//don't mess with the labels and history
const QString& val=QLatin1String((enable==true ? "t":"nil"));
const QString& val = QLatin1String((enable==true ? "t":"nil"));
evaluateExpression(QString::fromLatin1(":lisp(setf $display2d %1)").arg(val), Cantor::Expression::DeleteOnFinish, true);
Cantor::Session::setTypesettingEnabled(enable);
......
......@@ -26,8 +26,8 @@ public:
void evaluate() override;
QString internalCommand() override;
void parseOutput(const QString&);
void parseError(const QString&);
void parseOutput(const QString&) override;
void parseError(const QString&) override;
void imageChanged();
public:
......
......@@ -267,7 +267,7 @@ void OctaveSession::readError()
QString error = QString::fromLocal8Bit(m_process->readAllStandardError());
if (!expressionQueue().isEmpty() && !error.isEmpty())
{
auto* const exp = static_cast<OctaveExpression*>(expressionQueue().first());
auto* const exp = expressionQueue().first();
if (m_syntaxError)
{
m_syntaxError = false;
......@@ -301,7 +301,7 @@ void OctaveSession::readOutput()
if (!expressionQueue().isEmpty())
{
readError();
static_cast<OctaveExpression*>(expressionQueue().first())->parseOutput(m_output);
expressionQueue().first()->parseOutput(m_output);
}
}
else
......
......@@ -75,12 +75,13 @@ QString PythonExpression::internalCommand()
return commandProcessing;
}
void PythonExpression::parseOutput(QString output)
void PythonExpression::parseOutput(const QString& output)
{
qDebug() << "expression output: " << output;
if(command().simplified().startsWith(QLatin1String("help(")))
{
setResult(new Cantor::HelpResult(output.remove(output.lastIndexOf(QLatin1String("None")), 4)));
QString resultStr = output;
setResult(new Cantor::HelpResult(resultStr.remove(output.lastIndexOf(QLatin1String("None")), 4)));
}
else if (!output.isEmpty())
{
......
......@@ -19,9 +19,9 @@ class PythonExpression : public Cantor::Expression
void interrupt() override;
QString internalCommand() override;
void parseOutput(QString);
void parseOutput(const QString&) override;
void parseWarning(const QString&);
void parseError(const QString&);
void parseError(const QString&) override;
};
#endif /* _PYTHONEXPRESSION_H */
......@@ -155,7 +155,7 @@ void PythonSession::interrupt()
if(m_process && m_process->state() != QProcess::NotRunning)
{
#ifndef Q_OS_WIN
const int pid=m_process->pid();
const int pid = m_process->processId();
kill(pid, SIGINT);
#else
; //TODO: interrupt the process on windows
......@@ -255,7 +255,7 @@ void PythonSession::readOutput()
const QString& output = message.section(unitSep, 0, 0);
const QString& error = message.section(unitSep, 1, 1);
bool isError = message.section(unitSep, 2, 2).toInt();
auto* expr = static_cast<PythonExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
if (isError)
{
if(error.isEmpty()){
......@@ -266,7 +266,7 @@ void PythonSession::readOutput()
}
else
{
expr->parseWarning(error);
static_cast<PythonExpression*>(expr)->parseWarning(error);
expr->parseOutput(output);
}
finishFirstExpression(true);
......
......@@ -41,7 +41,7 @@
#include <QStack>
QalculateExpression::QalculateExpression( QalculateSession* session, bool internal)
: Cantor::Expression(session, internal), m_tempFile(nullptr)
: Cantor::Expression(session, internal)
{
}
......@@ -85,39 +85,39 @@ void QalculateExpression::evaluate()
// we are here because the commands entered by user are regular commands. We would have returned by now otherwise
QalculateSession* currentSession = dynamic_cast<QalculateSession*>(session());
currentSession->runExpression();
}
void QalculateExpression::parseOutput(QString& output)
void QalculateExpression::parseOutput(const QString& output)
{
output.remove(QLatin1String(">"));
output = output.trimmed();
QString resultStr = output;
resultStr.remove(QLatin1String(">"));
resultStr = resultStr.trimmed();
qDebug() << "output from qalc for command: " << command() << " " << output << endl;
setResult(new Cantor::TextResult(output));
qDebug() << "output from qalc for command: " << command() << " " << resultStr << endl;
setResult(new Cantor::TextResult(resultStr));
// update the variable model
updateVariables();
setStatus(Cantor::Expression::Done);
}
void QalculateExpression::updateVariables()
{
QalculateSession* currentSession = dynamic_cast<QalculateSession*>(session());
QMap<QString,QString> &variables = currentSession->variables;
QMap<QString, QString>::const_iterator it = variables.constBegin();
auto* currentSession = dynamic_cast<QalculateSession*>(session());
auto& variables = currentSession->variables;
auto it = variables.constBegin();
while (it != variables.constEnd()) {
currentSession->variableModel()->addVariable(it.key(), it.value());
++it;
}
}
void QalculateExpression::parseError(QString& error)
void QalculateExpression::parseError(const QString& error)
{
error.remove(QLatin1String(">"));
error = error.trimmed();
QString errorStr = error;
errorStr.remove(QLatin1String(">"));
errorStr = errorStr.trimmed();
qDebug() << "Error from qalc for command: " << command() << " " << error << endl;
setErrorMessage(error);
setErrorMessage(errorStr);
setStatus(Cantor::Expression::Error);
}
......@@ -142,8 +142,8 @@ void QalculateExpression::evaluatePlotCommand()
const QString msgFormat(QLatin1String("<font color=\"%1\">%2: %3</font><br>\n"));
if (!CALCULATOR->canPlot()) {
showMessage(i18n("Qalculate reports it cannot print. Is gnuplot installed?"), MESSAGE_ERROR);
return;
showMessage(i18n("Qalculate reports it cannot print. Is gnuplot installed?"), MESSAGE_ERROR);
return;
}
// Split argString into the arguments
......@@ -668,7 +668,6 @@ void QalculateExpression::evaluatePlotCommand()
#endif
setStatus(Cantor::Expression::Done);
}
}
void QalculateExpression::showMessage(QString msg, MessageType mtype)
......@@ -822,8 +821,6 @@ std::string QalculateExpression::unlocalizeExpression(QString expr)
);
}
QSharedPointer<PrintOptions> QalculateExpression::printOptions()
{
QSharedPointer<PrintOptions> po(new PrintOptions);
......
......@@ -21,8 +21,17 @@ class QalculateExpression : public Cantor::Expression
{
Q_OBJECT
public:
explicit QalculateExpression( QalculateSession* session, bool internal = false);
~QalculateExpression() override;
void evaluate() override;
void interrupt() override;
void parseOutput(const QString&) override;
void parseError(const QString&) override;
private:
QTemporaryFile *m_tempFile;
QTemporaryFile* m_tempFile{nullptr};
QString m_message;
enum MsgType { MSG_NONE=0, MSG_INFO=1, MSG_WARN=2, MSG_ERR=4 };
......@@ -38,15 +47,6 @@ private:
EvaluationOptions evaluationOptions();
ParseOptions parseOptions();
std::string unlocalizeExpression(QString expr);
public:
explicit QalculateExpression( QalculateSession* session, bool internal = false);
~QalculateExpression() override;
void evaluate() override;
void interrupt() override;
void parseOutput(QString& output);
void parseError(QString& error);
};
#endif
......@@ -17,8 +17,8 @@ class SageExpression : public Cantor::Expression
void evaluate() override;
void interrupt() override;
void parseOutput(const QString&);
void parseError(const QString&);
void parseOutput(const QString&) override;
void parseError(const QString&) override;
void addFileResult(const QString&);
......
......@@ -45,7 +45,6 @@ static QByteArray legacyInitCmd=
static QByteArray endOfInitMarker="print('____END_OF_INIT____')\n ";
SageSession::VersionInfo::VersionInfo(int major, int minor)
{
m_major=major;
......@@ -299,7 +298,7 @@ void SageSession::readStdOut()
{
if (!expressionQueue().isEmpty())
{
SageExpression* expr = static_cast<SageExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
expr->parseOutput(m_outputCache);
}
m_outputCache.clear();
......@@ -309,11 +308,11 @@ void SageSession::readStdOut()
void SageSession::readStdErr()
{
qDebug()<<"reading stdErr";
QString out=QLatin1String(m_process->readAllStandardError());
QString out = QLatin1String(m_process->readAllStandardError());
qDebug()<<"err: "<<out;
if (!expressionQueue().isEmpty())
{
SageExpression* expr = static_cast<SageExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
expr->parseError(out);
}
}
......@@ -372,12 +371,12 @@ void SageSession::runFirstExpression()
{
if(!expressionQueue().isEmpty())
{
SageExpression* expr = static_cast<SageExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
if (m_isInitialized)
{
connect(expr, SIGNAL(statusChanged(Cantor::Expression::Status)), this, SLOT(currentExpressionChangedStatus(Cantor::Expression::Status)));
QString command=expr->command();
QString command = expr->command();
if(command.endsWith(QLatin1Char('?')) && !command.endsWith(QLatin1String("??")))
command=QLatin1String("help(")+command.left(command.size()-1)+QLatin1Char(')');
if(command.startsWith(QLatin1Char('?')))
......@@ -404,14 +403,15 @@ void SageSession::interrupt()
if(m_process && m_process->state() != QProcess::NotRunning)
{
#ifndef Q_OS_WIN
const int pid=m_process->pid();
const int pid=m_process->processId();
kill(pid, SIGINT);
#else
; //TODO: interrupt the process on windows
#endif
}
foreach (Cantor::Expression* expression, expressionQueue())
for (auto* expression : expressionQueue())
expression->setStatus(Cantor::Expression::Interrupted);
expressionQueue().clear();
m_outputCache.clear();
......@@ -432,8 +432,8 @@ void SageSession::fileCreated( const QString& path )
qDebug()<<"got a file "<<path;
if (!expressionQueue().isEmpty())
{
SageExpression* expr = static_cast<SageExpression*>(expressionQueue().first());
if ( expr )
auto* expr = static_cast<SageExpression*>(expressionQueue().first());
if (expr)
expr->addFileResult( path );
}
}
......@@ -443,7 +443,7 @@ void SageSession::setTypesettingEnabled(bool enable)
Cantor::Session::setTypesettingEnabled(enable);
//tell the sage server to enable/disable pretty_print
const QString cmd=QLatin1String("__cantor_enable_typesetting(%1)");
const QString cmd = QLatin1String("__cantor_enable_typesetting(%1)");
evaluateExpression(cmd.arg(enable ? QLatin1String("true"):QLatin1String("false")), Cantor::Expression::DeleteOnFinish);
}
......@@ -470,7 +470,7 @@ SageSession::VersionInfo SageSession::sageVersion()
void SageSession::defineCustomFunctions()
{
//typesetting
QString cmd=QLatin1String("def __cantor_enable_typesetting(enable):\n");
QString cmd = QLatin1String("def __cantor_enable_typesetting(enable):\n");
if(m_sageVersion<VersionInfo(5, 7))
{
//the _ and __IP.outputcache() are needed to keep the
......
......@@ -66,7 +66,7 @@ void ScilabExpression::evaluate()
session()->enqueueExpression(this);
}
void ScilabExpression::parseOutput(QString output)
void ScilabExpression::parseOutput(const QString& output)
{
qDebug() << "output: " << output;
const QStringList lines = output.split(QLatin1String("\n"));
......@@ -87,7 +87,7 @@ void ScilabExpression::parseOutput(QString output)
setStatus(Cantor::Expression::Done);
}
void ScilabExpression::parseError(QString error)
void ScilabExpression::parseError(const QString& error)
{
qDebug() << "error" << error;
......
......@@ -19,8 +19,8 @@ class ScilabExpression : public Cantor::Expression
void evaluate() override;
void interrupt() override;
void parseOutput(QString output);
void parseError(QString error);
void parseOutput(const QString&) override;
void parseError(const QString&) override;
void parsePlotFile(QString filename);
void setPlotPending(bool plot);
......
......@@ -143,7 +143,7 @@ void ScilabSession::interrupt()
if(m_process && m_process->state() != QProcess::NotRunning)
{
#ifndef Q_OS_WIN
const int pid=m_process->pid();
const int pid = m_process->processId();
kill(pid, SIGINT);
#else
; //TODO: interrupt the process on windows
......@@ -190,14 +190,14 @@ void ScilabSession::runFirstExpression()
if(!expressionQueue().isEmpty())
{
ScilabExpression* expr = static_cast<ScilabExpression*>(expressionQueue().first());
auto* expr = expressionQueue().first();
QString command;
command.prepend(QLatin1String("\nprintf('begin-cantor-scilab-command-processing')\n"));
command += expr->command();
command += QLatin1String("\nprintf('terminated-cantor-scilab-command-processing')\n");
connect(expr, &ScilabExpression::statusChanged, this, &ScilabSession::currentExpressionStatusChanged);
connect(expr, &Cantor::Expression::statusChanged, this, &ScilabSession::currentExpressionStatusChanged);
expr->setStatus(Cantor::Expression::Computing);
qDebug() << "Writing command to process" << command;
......@@ -214,23 +214,21 @@ void ScilabSession::readError()
qDebug() << "error: " << error;
if (!expressionQueue().isEmpty())
static_cast<ScilabExpression*>(expressionQueue().first())->parseError(error);
expressionQueue().first()->parseError(error);
}
void ScilabSession::readOutput()
{
qDebug() << "readOutput";
while(m_process->bytesAvailable() > 0){
while(m_process->bytesAvailable() > 0)
m_output.append(QString::fromLocal8Bit(m_process->readLine()));