Changeset View
Changeset View
Standalone View
Standalone View
src/backends/R/rexpression.cpp
Show All 28 Lines | |||||
29 | 29 | | |||
30 | #include <QDebug> | 30 | #include <QDebug> | ||
31 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
32 | #include <QMimeType> | 32 | #include <QMimeType> | ||
33 | #include <QMimeDatabase> | 33 | #include <QMimeDatabase> | ||
34 | #include <QFile> | 34 | #include <QFile> | ||
35 | #include <QStringList> | 35 | #include <QStringList> | ||
36 | #include <QTextDocument> | 36 | #include <QTextDocument> | ||
37 | #include <QRegExp> | ||||
37 | 38 | | |||
38 | RExpression::RExpression( Cantor::Session* session ) : Cantor::Expression(session) | 39 | RExpression::RExpression( Cantor::Session* session ) : Cantor::Expression(session) | ||
39 | { | 40 | { | ||
40 | qDebug(); | 41 | qDebug(); | ||
41 | 42 | | |||
42 | } | 43 | } | ||
43 | 44 | | |||
44 | RExpression::~RExpression() | 45 | RExpression::~RExpression() | ||
45 | { | 46 | { | ||
46 | 47 | | |||
47 | } | 48 | } | ||
48 | 49 | | |||
49 | 50 | | |||
50 | void RExpression::evaluate() | 51 | void RExpression::evaluate() | ||
51 | { | 52 | { | ||
52 | qDebug()<<"evaluating "<<command(); | 53 | qDebug()<<"evaluating "<<command(); | ||
53 | setStatus(Cantor::Expression::Computing); | 54 | setStatus(Cantor::Expression::Computing); | ||
54 | if(command().startsWith(QLatin1Char('?'))) | 55 | if(command().trimmed().startsWith(QLatin1Char('?')) || command().trimmed().startsWith(QLatin1String("help"))) | ||
55 | m_isHelpRequest=true; | 56 | m_isHelpRequest=true; | ||
56 | else | 57 | else | ||
57 | m_isHelpRequest=false; | 58 | m_isHelpRequest=false; | ||
58 | 59 | | |||
59 | static_cast<RSession*>(session())->queueExpression(this); | 60 | | ||
61 | RSession* session = dynamic_cast<RSession*>(this->session()); | ||||
62 | | ||||
63 | /* check if the command is a quit command. following are quit commands supported by R | ||||
64 | * quit(save = "default", status = 0, runLast = TRUE) | ||||
65 | * q(save = "default", status = 0, runLast = TRUE) | ||||
66 | */ | ||||
67 | QRegExp regexp; | ||||
68 | regexp.setCaseSensitivity(Qt::CaseSensitive); | ||||
69 | regexp.setPattern(QLatin1String("\\s*q\\s*\\([\\w\\W]*\\)\\s*$|\\s*quit\\s*\\([\\w\\W]*\\)\\s*$")); | ||||
70 | if(regexp.exactMatch(command().trimmed())) { | ||||
71 | // since it's a quit command, logout from the session | ||||
72 | setStatus(Cantor::Expression::Done); | ||||
73 | session->logout(); | ||||
74 | return; | ||||
60 | } | 75 | } | ||
61 | 76 | | |||
62 | void RExpression::interrupt() | 77 | // done evaluation, finally running the command | ||
63 | { | 78 | session->runExpression(this); | ||
64 | qDebug()<<"interrupting command"; | 79 | | ||
65 | if(status()==Cantor::Expression::Computing) | 80 | | ||
66 | session()->interrupt(); | | |||
67 | setStatus(Cantor::Expression::Interrupted); | | |||
68 | } | 81 | } | ||
69 | 82 | | |||
70 | void RExpression::finished(int returnCode, const QString& text) | 83 | void RExpression::parseOutput(QString output) | ||
71 | { | 84 | { | ||
72 | if(returnCode==RExpression::SuccessCode) | 85 | // remove garabage from output | ||
73 | { | 86 | | ||
74 | setResult(new Cantor::TextResult(Qt::convertFromPlainText(text))); | 87 | output.replace(command(), QLatin1String("")); | ||
88 | output.replace(QLatin1String(">"), QLatin1String("")); | ||||
89 | output = output.trimmed(); | ||||
90 | | ||||
91 | qDebug() << " final output of the command " << command() << output << endl; | ||||
92 | | ||||
93 | if(m_isHelpRequest) | ||||
94 | setResult(new Cantor::HelpResult(output)); | ||||
95 | else | ||||
96 | setResult(new Cantor::TextResult(output)); | ||||
97 | | ||||
75 | setStatus(Cantor::Expression::Done); | 98 | setStatus(Cantor::Expression::Done); | ||
76 | }else if (returnCode==RExpression::ErrorCode) | 99 | } | ||
100 | | ||||
101 | void RExpression::parseError(QString error) | ||||
77 | { | 102 | { | ||
78 | setResult(new Cantor::TextResult(Qt::convertFromPlainText(text))); | 103 | error.replace(command(), QLatin1String("")); | ||
104 | error.replace(QLatin1String(">"), QLatin1String("")); | ||||
105 | error = error.trimmed(); | ||||
106 | | ||||
107 | setResult(new Cantor::TextResult(error)); | ||||
108 | | ||||
79 | setStatus(Cantor::Expression::Error); | 109 | setStatus(Cantor::Expression::Error); | ||
80 | setErrorMessage(Qt::convertFromPlainText(text)); | 110 | setErrorMessage(error); | ||
81 | } | | |||
82 | } | 111 | } | ||
83 | 112 | | |||
84 | void RExpression::evaluationStarted() | 113 | void RExpression::interrupt() | ||
85 | { | 114 | { | ||
86 | setStatus(Cantor::Expression::Computing); | 115 | qDebug()<<"interrupting command"; | ||
116 | if(status()==Cantor::Expression::Computing) | ||||
117 | session()->interrupt(); | ||||
118 | setStatus(Cantor::Expression::Interrupted); | ||||
87 | } | 119 | } | ||
88 | 120 | | |||
89 | void RExpression::addInformation(const QString& information) | 121 | void RExpression::addInformation(const QString& information) | ||
90 | { | 122 | { | ||
91 | static_cast<RSession*>(session())->sendInputToServer(information); | 123 | /* not using this anymore since it was making use of rserver | ||
124 | * | ||||
125 | */ | ||||
126 | // static_cast<RSession*>(session())->sendInputToServer(information); | ||||
92 | } | 127 | } | ||
93 | 128 | | |||
94 | void RExpression::showFilesAsResult(const QStringList& files) | 129 | void RExpression::showFilesAsResult(const QStringList& files) | ||
95 | { | 130 | { | ||
96 | qDebug()<<"showing files: "<<files; | 131 | qDebug()<<"showing files: "<<files; | ||
97 | foreach(const QString& file, files) | 132 | foreach(const QString& file, files) | ||
98 | { | 133 | { | ||
99 | QMimeType type; | 134 | QMimeType type; | ||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |