aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Lakhtanov <ivan.lakhtanov@gmail.com>2016-08-22 01:11:36 (GMT)
committerIvan Lakhtanov <ivan.lakhtanov@gmail.com>2016-08-24 18:44:30 (GMT)
commit8da94fe3427c7dbc2157e3385012eb69b6392e82 (patch)
tree3fdc256819607a22a1ff1011d74e19b880bd14ae
parentfacd869bb88e933f50d84e6915fb1df16a7b77c3 (diff)
Implemented inline plots
* Appending savefig to the ends of the commands mentioning plotting functions * Reading resulting files, that are saved to tmp dir * Saving intermediate graph in user chosen format (svg default) * Option to disable plot integration Differential Revision: https://phabricator.kde.org/D2459
-rw-r--r--src/backends/julia/juliabackend.kcfg12
-rw-r--r--src/backends/julia/juliaexpression.cpp49
-rw-r--r--src/backends/julia/juliaexpression.h5
-rw-r--r--src/backends/julia/juliakeywords.cpp6
-rw-r--r--src/backends/julia/juliakeywords.h5
-rw-r--r--src/backends/julia/juliasession.cpp14
-rw-r--r--src/backends/julia/juliasession.h2
-rw-r--r--src/backends/julia/keywords.xml19
-rw-r--r--src/backends/julia/settings.ui63
9 files changed, 172 insertions, 3 deletions
diff --git a/src/backends/julia/juliabackend.kcfg b/src/backends/julia/juliabackend.kcfg
index e7b6ee1..a10e27e 100644
--- a/src/backends/julia/juliabackend.kcfg
+++ b/src/backends/julia/juliabackend.kcfg
@@ -12,5 +12,17 @@
QUrl::fromLocalFile(QStandardPaths::findExecutable(QLatin1String("julia")))
</default>
</entry>
+ <entry name="integratePlots" type="Bool">
+ <label>Integrate Plots into the Worksheet</label>
+ <default>true</default>
+ </entry>
+ <entry name="inlinePlotFormat" type="Enum">
+ <choices>
+ <choice name="svg"/>
+ <choice name="eps"/>
+ <choice name="png"/>
+ </choices>
+ <default>svg</default>
+ </entry>
</group>
</kcfg>
diff --git a/src/backends/julia/juliaexpression.cpp b/src/backends/julia/juliaexpression.cpp
index 9ced140..6746a6d 100644
--- a/src/backends/julia/juliaexpression.cpp
+++ b/src/backends/julia/juliaexpression.cpp
@@ -19,8 +19,14 @@
*/
#include "juliaexpression.h"
+#include <QDir>
+#include <QUuid>
+
+#include "settings.h"
#include "juliasession.h"
+#include "juliakeywords.h"
#include "textresult.h"
+#include "imageresult.h"
JuliaExpression::JuliaExpression(Cantor::Session *session)
: Cantor::Expression(session)
@@ -30,7 +36,28 @@ JuliaExpression::JuliaExpression(Cantor::Session *session)
void JuliaExpression::evaluate()
{
setStatus(Cantor::Expression::Computing);
- dynamic_cast<JuliaSession *>(session())->runExpression(this);
+ auto juliaSession = dynamic_cast<JuliaSession *>(session());
+
+ m_plot_filename.clear();
+ if (juliaSession->integratePlots() and checkPlotShowingCommands()) {
+ QStringList inlinePlotFormats;
+ inlinePlotFormats << QLatin1String("svg");
+ inlinePlotFormats << QLatin1String("eps");
+ inlinePlotFormats << QLatin1String("png");
+
+ auto inlinePlotFormat =
+ inlinePlotFormats[JuliaSettings::inlinePlotFormat()];
+ m_plot_filename = QDir::tempPath() +
+ QString::fromLatin1("/cantor-julia-export-%1.%2")
+ .arg(QUuid::createUuid().toString()).arg(inlinePlotFormat);
+
+ QString saveFigCommand =
+ QString::fromLatin1("\nGR.savefig(\"%1\")\n").arg(m_plot_filename);
+
+ setCommand(command().append(saveFigCommand));
+ }
+
+ juliaSession->runExpression(this);
}
void JuliaExpression::finalize()
@@ -44,7 +71,14 @@ void JuliaExpression::finalize()
setResult(new Cantor::TextResult(juliaSession->getOutput()));
setStatus(Cantor::Expression::Error);
} else {
- setResult(new Cantor::TextResult(juliaSession->getOutput()));
+ if (not m_plot_filename.isEmpty()
+ and QFileInfo(m_plot_filename).exists()) {
+ setResult(
+ new Cantor::ImageResult(QUrl::fromLocalFile(m_plot_filename)));
+ QDir().remove(m_plot_filename);
+ } else {
+ setResult(new Cantor::TextResult(juliaSession->getOutput()));
+ }
setStatus(Cantor::Expression::Done);
}
}
@@ -54,4 +88,15 @@ void JuliaExpression::interrupt()
setStatus(Cantor::Expression::Interrupted);
}
+bool JuliaExpression::checkPlotShowingCommands()
+{
+ for (auto showingCommand :
+ JuliaKeywords::instance()->plotShowingCommands()) {
+ if (command().contains(showingCommand + QLatin1String("("))) {
+ return true;
+ }
+ }
+ return false;
+}
+
#include "juliaexpression.moc"
diff --git a/src/backends/julia/juliaexpression.h b/src/backends/julia/juliaexpression.h
index 5d4fc03..344c585 100644
--- a/src/backends/julia/juliaexpression.h
+++ b/src/backends/julia/juliaexpression.h
@@ -31,4 +31,9 @@ public:
virtual void evaluate() override;
virtual void interrupt() override;
void finalize();
+
+private:
+ QString m_plot_filename;
+
+ bool checkPlotShowingCommands();
};
diff --git a/src/backends/julia/juliakeywords.cpp b/src/backends/julia/juliakeywords.cpp
index 2178705..f0a5846 100644
--- a/src/backends/julia/juliakeywords.cpp
+++ b/src/backends/julia/juliakeywords.cpp
@@ -33,6 +33,7 @@ JuliaKeywords *JuliaKeywords::instance()
inst->loadFromFile();
qSort(inst->m_keywords);
qSort(inst->m_variables);
+ qSort(inst->m_plotShowingCommands);
}
return inst;
@@ -61,7 +62,8 @@ void JuliaKeywords::loadFromFile()
const QStringRef name = xml.name();
if (name == QLatin1String("keywords")
- or name == QLatin1String("variables")) {
+ or name == QLatin1String("variables")
+ or name == QLatin1String("plot_showing_commands")) {
while (xml.readNextStartElement()) {
Q_ASSERT(
xml.isStartElement() and xml.name() == QLatin1String("word")
@@ -73,6 +75,8 @@ void JuliaKeywords::loadFromFile()
m_keywords << text;
} else if (name == QLatin1String("variables")) {
m_variables << text;
+ } else if (name == QLatin1String("plot_showing_commands")) {
+ m_plotShowingCommands << text;
}
}
} else {
diff --git a/src/backends/julia/juliakeywords.h b/src/backends/julia/juliakeywords.h
index 0643e40..3ea16a0 100644
--- a/src/backends/julia/juliakeywords.h
+++ b/src/backends/julia/juliakeywords.h
@@ -27,6 +27,10 @@ public:
static JuliaKeywords *instance();
const QStringList &keywords() const { return m_keywords; }
+ const QStringList &plotShowingCommands() const
+ {
+ return m_plotShowingCommands;
+ }
const QStringList &variables() const { return m_variables; }
const QStringList &removedVariables() const { return m_removedVariables; }
@@ -40,6 +44,7 @@ public:
private:
QStringList m_keywords;
+ QStringList m_plotShowingCommands;
QStringList m_variables;
QStringList m_removedVariables;
QStringList m_functions;
diff --git a/src/backends/julia/juliasession.cpp b/src/backends/julia/juliasession.cpp
index 2a5e521..7958280 100644
--- a/src/backends/julia/juliasession.cpp
+++ b/src/backends/julia/juliasession.cpp
@@ -98,6 +98,13 @@ void JuliaSession::login()
listVariables();
+ // Plots integration
+ if (integratePlots()) {
+ runJuliaCommand(
+ QLatin1String("import GR; ENV[\"GKS_WSTYPE\"] = \"nul\"")
+ );
+ }
+
emit ready();
}
@@ -322,4 +329,11 @@ QAbstractItemModel *JuliaSession::variableModel()
return m_variableModel;
}
+
+bool JuliaSession::integratePlots()
+{
+ return JuliaSettings::integratePlots();
+}
+
+
#include "juliasession.moc"
diff --git a/src/backends/julia/juliasession.h b/src/backends/julia/juliasession.h
index 8387d98..9f5a787 100644
--- a/src/backends/julia/juliasession.h
+++ b/src/backends/julia/juliasession.h
@@ -54,6 +54,8 @@ public:
virtual QSyntaxHighlighter *syntaxHighlighter(QObject *parent);
virtual QAbstractItemModel *variableModel() override;
+ bool integratePlots();
+
Q_SIGNALS:
void updateHighlighter();
diff --git a/src/backends/julia/keywords.xml b/src/backends/julia/keywords.xml
index 45fe08a..f8dff7e 100644
--- a/src/backends/julia/keywords.xml
+++ b/src/backends/julia/keywords.xml
@@ -41,4 +41,23 @@
<word>using</word>
<word>while</word>
</keywords>
+ <plot_showing_commands>
+ <word>contourf</word>
+ <word>contour</word>
+ <word>grid3d</word>
+ <word>grid</word>
+ <word>histogram</word>
+ <word>imshow</word>
+ <word>plot3</word>
+ <word>plot</word>
+ <word>polar</word>
+ <word>polyline3d</word>
+ <word>polyline</word>
+ <word>polymarker3d</word>
+ <word>polymarker</word>
+ <word>scatter3</word>
+ <word>scatter</word>
+ <word>show</word>
+ <word>surface</word>
+ </plot_showing_commands>
</julia>
diff --git a/src/backends/julia/settings.ui b/src/backends/julia/settings.ui
index f978d03..1e12d5a 100644
--- a/src/backends/julia/settings.ui
+++ b/src/backends/julia/settings.ui
@@ -4,6 +4,19 @@
<widget class="QWidget" name="JuliaSettingsBase">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
+ <spacer name="verticalSpacer">
+ <property name="orientation">
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
@@ -17,6 +30,56 @@
</item>
</layout>
</item>
+ <item>
+ <widget class="QCheckBox" name="kcfg_integratePlots">
+ <property name="text">
+ <string>Integrate Plots in Worksheet (start a new session when changed)</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <widget class="QLabel" name="label_2">
+ <property name="text">
+ <string>Inline Plots Intermediate Format:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QComboBox" name="kcfg_inlinePlotFormat">
+ <item>
+ <property name="text">
+ <string notr="true">svg</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string notr="true">eps</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string notr="true">png</string>
+ </property>
+ </item>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <spacer name="verticalSpacer">
+ <property name="orientation">
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
</layout>
</widget>
<customwidgets>