File indexing completed on 2024-05-19 11:21:33

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2011 Filipe Saraiva <filipe@kde.org>
0004 */
0005 
0006 #include "scilabexpression.h"
0007 
0008 #include <config-cantorlib.h>
0009 
0010 #include "textresult.h"
0011 #include "imageresult.h"
0012 #include "helpresult.h"
0013 
0014 #include <QDebug>
0015 #include <QDir>
0016 #include <QFile>
0017 
0018 #include <KIconLoader>
0019 
0020 #include "settings.h"
0021 #include "defaultvariablemodel.h"
0022 
0023 using ScilabPlotResult = Cantor::ImageResult;
0024 
0025 ScilabExpression::ScilabExpression( Cantor::Session* session, bool internal ) : Cantor::Expression(session, internal),
0026 m_finished(false),
0027 m_plotPending(false)
0028 {
0029     qDebug() << "ScilabExpression constructor";
0030 }
0031 
0032 void ScilabExpression::evaluate()
0033 {
0034     if((ScilabSettings::integratePlots()) && (command().contains(QLatin1String("plot")))){
0035 
0036         qDebug() << "Preparing export figures property";
0037 
0038         QString exportCommand;
0039 
0040         QStringList commandList = command().split(QLatin1String("\n"));
0041 
0042         for(int count = 0; count < commandList.size(); count++){
0043 
0044             if(commandList.at(count).toLocal8Bit().contains("plot")){
0045 
0046                 exportCommand = QString::fromLatin1("\nxs2png(gcf(), 'cantor-export-scilab-figure-%1.png');\ndelete(gcf());").arg(qrand());
0047 
0048                 commandList[count].append(exportCommand);
0049 
0050                 exportCommand.clear();
0051             }
0052 
0053             qDebug() << "Command " << count << ": " << commandList.at(count).toLocal8Bit().constData();
0054         }
0055 
0056         QString newCommand = commandList.join(QLatin1String("\n"));
0057         newCommand.prepend(QLatin1String("clf();\n"));
0058         newCommand.append(QLatin1String("\n"));
0059 
0060         this->setCommand(newCommand);
0061 
0062         qDebug() << "New Command " << command();
0063 
0064     }
0065 
0066     session()->enqueueExpression(this);
0067 }
0068 
0069 void ScilabExpression::parseOutput(const QString& output)
0070 {
0071     qDebug() << "output: " << output;
0072     const QStringList lines = output.split(QLatin1String("\n"));
0073     bool isPrefixLines = true;
0074     for (const QString& line : lines)
0075     {
0076         if (isPrefixLines && line.isEmpty())
0077             continue;
0078 
0079         m_output += line + QLatin1String("\n");
0080         isPrefixLines = false;
0081     }
0082 
0083     if (!m_output.simplified().isEmpty())
0084         setResult(new Cantor::TextResult(m_output));
0085 
0086     evalFinished();
0087     setStatus(Cantor::Expression::Done);
0088 }
0089 
0090 void ScilabExpression::parseError(const QString& error)
0091 {
0092     qDebug() << "error" << error;
0093 
0094     setErrorMessage(error);
0095 
0096     evalFinished();
0097     setStatus(Cantor::Expression::Error);
0098 }
0099 
0100 void ScilabExpression::parsePlotFile(QString filename)
0101 {
0102     qDebug() << "parsePlotFile";
0103 
0104     qDebug() << "ScilabExpression::parsePlotFile: " << filename;
0105 
0106     setResult(new ScilabPlotResult(QUrl::fromLocalFile(filename)));
0107 
0108     setPlotPending(false);
0109 
0110     if (m_finished){
0111         qDebug() << "ScilabExpression::parsePlotFile: done";
0112         setStatus(Done);
0113     }
0114 }
0115 
0116 void ScilabExpression::evalFinished()
0117 {
0118     qDebug()<<"evaluation finished";
0119 
0120     foreach (const QString& line, m_output.simplified().split(QLatin1Char('\n'), QString::SkipEmptyParts)){
0121         if (m_output.contains(QLatin1Char('='))){
0122 
0123             qDebug() << line;
0124 
0125             QStringList parts = line.split(QLatin1Char('='));
0126 
0127             if (parts.size() >= 2){
0128                 Cantor::DefaultVariableModel* model = dynamic_cast<Cantor::DefaultVariableModel*>(session()->variableModel());
0129 
0130                 if (model){
0131                     model->addVariable(parts.first().trimmed(), parts.last().trimmed());
0132                 }
0133             }
0134         }
0135     }
0136 }
0137 
0138 void ScilabExpression::setPlotPending(bool plot)
0139 {
0140     m_plotPending = plot;
0141 }