File indexing completed on 2024-05-26 11:22:03

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2016 Ivan Lakhtanov <ivan.lakhtanov@gmail.com>
0004 */
0005 #include "juliaexpression.h"
0006 
0007 #include <QDir>
0008 #include <QUuid>
0009 
0010 #include "settings.h"
0011 #include "juliasession.h"
0012 #include "juliakeywords.h"
0013 #include "textresult.h"
0014 #include "imageresult.h"
0015 
0016 const QStringList JuliaExpression::plotExtensions({
0017     QLatin1String("svg"),
0018     QLatin1String("png")
0019 });
0020 
0021 JuliaExpression::JuliaExpression(Cantor::Session *session, bool internal)
0022     : Cantor::Expression(session, internal)
0023 {
0024 }
0025 
0026 void JuliaExpression::evaluate()
0027 {
0028     auto juliaSession = static_cast<JuliaSession *>(session());
0029 
0030     juliaSession->enqueueExpression(this);
0031 }
0032 
0033 QString JuliaExpression::internalCommand()
0034 {
0035     QString cmd = command();
0036     auto juliaSession = static_cast<JuliaSession *>(session());
0037 
0038     // Plots integration
0039     m_plot_filename.clear();
0040     // Not sure about how this code will work with two graphic packages activated in the same time (they both will save to one file?)...
0041     if (!session()->enabledGraphicPackages().isEmpty() && !isInternal())
0042     {
0043         QStringList cmdWords = cmd.split(QRegularExpression(QStringLiteral("\\b")), QString::SkipEmptyParts);
0044         for (const Cantor::GraphicPackage& package : session()->enabledGraphicPackages())
0045         {
0046             for (const QString& plotCmd : package.plotCommandPrecentsKeywords())
0047                 if (cmdWords.contains(plotCmd))
0048                 {
0049                     if (package.isHavePlotCommand())
0050                     {
0051                         m_plot_filename = juliaSession->plotFilePrefixPath() + QString::number(id()) + QLatin1String(".") + plotExtensions[JuliaSettings::inlinePlotFormat()];
0052                         cmd.append(QLatin1String("\n"));
0053                         cmd.append(package.savePlotCommand(juliaSession->plotFilePrefixPath(), id(), plotExtensions[JuliaSettings::inlinePlotFormat()]));
0054                     }
0055                     break;
0056                 }
0057         }
0058     }
0059 
0060     qDebug() << "expression internal command:" << cmd;
0061 
0062     return cmd;
0063 }
0064 
0065 void JuliaExpression::finalize(const QString& output, const QString& error, bool wasException)
0066 {
0067     if (wasException) {
0068         setErrorMessage(error);
0069         if (!output.isEmpty())
0070             setResult(new Cantor::TextResult(output));
0071         setStatus(Cantor::Expression::Error);
0072     } else {
0073         if (!m_plot_filename.isEmpty() && QFileInfo(m_plot_filename).exists()) {
0074             // If we have plot in result, show it
0075             setResult(new Cantor::ImageResult(QUrl::fromLocalFile(m_plot_filename)));
0076         } else {
0077             if (!output.isEmpty())
0078                 setResult(new Cantor::TextResult(output));
0079         }
0080         setStatus(Cantor::Expression::Done);
0081     }
0082 }