Commit 063ba6ef authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

[Octave] Add possibility to change used file plot format.

parent 818f4889
......@@ -5,6 +5,7 @@
### New features
* Change entries replacement logic and add possibility to use previous logic via Cantor setting option.
* Add possibility to change plot extension (available variants: jpeg, png, svg and eps (if builded with eps support)) in Octave backend.
## 20.04
......
......@@ -12,10 +12,21 @@ set( OctaveBackend_SRCS
add_subdirectory(scripts)
if (WITH_EPS)
set(DEFAULT_PLOT_FORMAT "eps")
set(EPS_PLOT_FORMAT_CHOICE "<choice name=\"eps\"/>")
file(READ with_eps_ui_part.txt EPS_PLOT_FORMAT_UI_ELEMENT)
else (WITH_EPS)
set(DEFAULT_PLOT_FORMAT "png")
endif (WITH_EPS)
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
configure_file(octavebackend.kcfg.in ${CMAKE_CURRENT_BINARY_DIR}/octavebackend.kcfg)
configure_file(settings.ui.in ${CMAKE_CURRENT_BINARY_DIR}/settings.ui)
kconfig_add_kcfg_files(OctaveBackend_SRCS settings.kcfgc)
install(FILES octavebackend.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/octavebackend.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR})
ki18n_wrap_ui(OctaveBackend_SRCS settings.ui)
ki18n_wrap_ui(OctaveBackend_SRCS ${CMAKE_CURRENT_BINARY_DIR}/settings.ui)
add_backend(octavebackend ${OctaveBackend_SRCS})
......@@ -28,7 +39,7 @@ target_link_libraries(cantor_octavebackend
if(BUILD_TESTING)
add_executable( testoctave testoctave.cpp)
add_executable( testoctave testoctave.cpp octaveexpression.cpp settings.cpp)
add_test(NAME testoctave COMMAND testoctave)
ecm_mark_as_test(testoctave)
target_link_libraries( testoctave
......
......@@ -24,5 +24,14 @@
<entry name="autorunScripts" type="StringList">
<label>List of scripts to autorun at the beginning of session</label>
</entry>
<entry name="inlinePlotFormat" type="Enum">
<choices>
@EPS_PLOT_FORMAT_CHOICE@
<choice name="png"/>
<choice name="svg"/>
<choice name="jpeg"/>
</choices>
<default>@DEFAULT_PLOT_FORMAT@</default>
</entry>
</group>
</kcfg>
......@@ -22,6 +22,8 @@
#include "defaultvariablemodel.h"
#include "textresult.h"
#include "epsresult.h"
#include "imageresult.h"
#include <QDebug>
#include <QFile>
......@@ -34,8 +36,7 @@
#include "settings.h"
static const QLatin1String printCommandBegin("cantor_print('");
static const QLatin1String printCommandEnd("');");
static const QString printCommandTemplate = QString::fromLatin1("cantor_print('%1', '%2');");
static const QStringList plotCommands({
QLatin1String("plot"), QLatin1String("semilogx"), QLatin1String("semilogy"),
QLatin1String("loglog"), QLatin1String("polar"), QLatin1String("contour"),
......@@ -55,6 +56,14 @@ static const QStringList plotCommands({
QLatin1String("ezplot3"), QLatin1String("ezmesh"), QLatin1String("ezmeshc"),
QLatin1String("ezsurf"), QLatin1String("ezsurfc"), QLatin1String("cantor_plot2d"),
QLatin1String("cantor_plot3d")});
const QStringList OctaveExpression::plotExtensions({
#ifdef WITH_EPS
QLatin1String("eps"),
#endif
QLatin1String("png"),
QLatin1String("svg"),
QLatin1String("jpeg")
});
OctaveExpression::OctaveExpression(Cantor::Session* session, bool internal): Expression(session, internal)
{
......@@ -92,12 +101,14 @@ void OctaveExpression::evaluate()
if (cmdWords.contains(plotCmd))
{
qDebug() << "Executing a plot command";
/*
#ifdef WITH_EPS
QLatin1String ext(".eps");
#else
QLatin1String ext(".png");
#endif
m_tempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/cantor_octave-XXXXXX")+ext);
*/
m_tempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/cantor_octave-XXXXXX.")+plotExtensions[OctaveSettings::inlinePlotFormat()]);
m_tempFile->open();
qDebug() << "plot temp file" << m_tempFile->fileName();
......@@ -127,7 +138,7 @@ QString OctaveExpression::internalCommand()
{
if (!cmd.endsWith(QLatin1Char(';')) && !cmd.endsWith(QLatin1Char(',')))
cmd += QLatin1Char(',');
cmd += printCommandBegin + m_tempFile->fileName() + printCommandEnd;
cmd += printCommandTemplate.arg(QFileInfo(m_tempFile->fileName()).suffix()).arg(m_tempFile->fileName());
}
// We need remove all comments here, because below we merge all strings to one long string
......@@ -206,7 +217,13 @@ void OctaveExpression::imageChanged()
if(m_tempFile->size() <= 0)
return;
OctavePlotResult* newResult = new OctavePlotResult(QUrl::fromLocalFile(m_tempFile->fileName()));
const QUrl& url = QUrl::fromLocalFile(m_tempFile->fileName());
Cantor::Result* newResult;
if (m_tempFile->fileName().endsWith(QLatin1String(".eps")))
newResult = new Cantor::EpsResult(url);
else
newResult = new Cantor::ImageResult(url);
bool found = false;
for (int i = 0; i < results().size(); i++)
if (results()[i]->type() == newResult->type())
......
......@@ -25,14 +25,6 @@
#include <expression.h>
#include <QStringList>
#ifdef WITH_EPS
#include "epsresult.h"
using OctavePlotResult = Cantor::EpsResult;
#else
#include "imageresult.h"
typedef Cantor::ImageResult OctavePlotResult;
#endif
class QTemporaryFile;
class OctaveExpression : public Cantor::Expression
......@@ -51,6 +43,9 @@ public:
void parseError(const QString&);
void imageChanged();
public:
const static QStringList plotExtensions;
private:
QString m_resultString;
bool m_finished = false;
......
......@@ -80,7 +80,12 @@ void OctaveVariableModel::parseNewVariables(Expression::Status status)
{
static const QLatin1String delimiter("__cantor_delimiter_line__");
// Result always must be, if we done, so don't check it
if (m_expr->results().isEmpty())
{
qWarning() << "Octave code for parsing variables finish with done status, but without results";
break;
}
QString text = static_cast<Cantor::TextResult*>(m_expr->result())->plain();
const QStringList& lines = text.split(delimiter, QString::SkipEmptyParts);
......
if (WITH_EPS)
set(PLOT_FILE_FORMAT epsc)
set(PLOT_FILE_SUFFIX eps)
else (WITH_EPS)
set(PLOT_FILE_FORMAT png)
set(PLOT_FILE_SUFFIX png)
endif (WITH_EPS)
configure_file(cantor_print.m.in ${CMAKE_CURRENT_BINARY_DIR}/cantor_print.m)
install( FILES cantor_eigenvectors.m cantor_plot2d.m cantor_plot3d.m ${CMAKE_CURRENT_BINARY_DIR}/cantor_print.m DESTINATION ${KDE_INSTALL_DATADIR}/cantor/octavebackend )
install( FILES cantor_eigenvectors.m cantor_plot2d.m cantor_plot3d.m cantor_print.m DESTINATION ${KDE_INSTALL_DATADIR}/cantor/octavebackend )
......@@ -17,10 +17,10 @@
Boston, MA 02110-1301, USA.
%}
function cantor_print(filename)
function cantor_print(plot_format, filename)
try
print('-d${PLOT_FILE_FORMAT}',filename,'-S480,336','-tight');
print(strcat('-d', plot_format), filename, '-tight');
catch
print('-d${PLOT_FILE_FORMAT}',filename,'-S480,336');
print(strcat('-d', plot_format), filename);
end_try_catch
endfunction
......@@ -56,6 +56,37 @@
</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">
@EPS_PLOT_FORMAT_UI_ELEMENT@
<item>
<property name="text">
<string notr="true">png</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">svg</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">jpeg</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox">
<property name="title">
......
......@@ -33,6 +33,7 @@
#include "defaultvariablemodel.h"
#include "octaveexpression.h"
#include "settings.h"
#include <QDebug>
......@@ -207,7 +208,8 @@ void TestOctave::testVariableCreatingFromCodeWithPlot()
QVERIFY(e!=nullptr);
QCOMPARE(e->status(), Cantor::Expression::Done);
QVERIFY(e->result());
QVERIFY(e->result()->type() == OctavePlotResult::Type);
int plotType = (OctaveExpression::plotExtensions[OctaveSettings::inlinePlotFormat()] == QLatin1String("eps") ? (int)Cantor::EpsResult::Type : (int)Cantor::ImageResult::Type);
QVERIFY(e->result()->type() == plotType);
if(session()->status()==Cantor::Session::Running)
waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
......@@ -243,7 +245,8 @@ void TestOctave::testPlot()
int cnt=0;
//give some time to create the image, but at most 5sec
while(e->result()==nullptr||e->result()->type()!=OctavePlotResult::Type )
int plotType = (OctaveExpression::plotExtensions[OctaveSettings::inlinePlotFormat()] == QLatin1String("eps") ? (int)Cantor::EpsResult::Type : (int)Cantor::ImageResult::Type);
while(e->result()==nullptr||e->result()->type() != plotType )
{
QTest::qWait(250);
cnt+=250;
......@@ -254,7 +257,7 @@ void TestOctave::testPlot()
QVERIFY( e!=nullptr );
QVERIFY( e->result()!=nullptr );
QCOMPARE( e->result()->type(), (int) OctavePlotResult::Type );
QCOMPARE( e->result()->type(), plotType );
QVERIFY( !e->result()->data().isNull() );
}
......
<item>
<property name="text">
<string notr="true">eps</string>
</property>
</item>
\ No newline at end of file
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