File indexing completed on 2023-05-30 09:03:13

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2018 Nikita Sirgienko <warquark@gmail.com>
0004     SPDX-FileCopyrightText: 2022 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #include "pythonvariablemodel.h"
0008 #include "pythonsession.h"
0009 #include "result.h"
0010 
0011 #include <QDebug>
0012 #include <QString>
0013 
0014 #include "settings.h"
0015 
0016 using namespace Cantor;
0017 
0018 PythonVariableModel::PythonVariableModel(PythonSession* session):
0019     DefaultVariableModel(session)
0020 {
0021 }
0022 
0023 PythonVariableModel::~PythonVariableModel()
0024 {
0025     if (m_expression)
0026         m_expression->setFinishingBehavior(Cantor::Expression::FinishingBehavior::DeleteOnFinish);
0027 }
0028 
0029 void PythonVariableModel::update()
0030 {
0031     if (m_expression)
0032         return;
0033 
0034     int variableManagement = PythonSettings::variableManagement();
0035     const QString command = QString::fromLatin1("%variables %1").arg(variableManagement);
0036     m_expression = session()->evaluateExpression(command, Cantor::Expression::FinishingBehavior::DoNotDelete, true);
0037     connect(m_expression, &Cantor::Expression::statusChanged, this, &PythonVariableModel::extractVariables);
0038 }
0039 
0040 void PythonVariableModel::extractVariables(Cantor::Expression::Status status)
0041 {
0042     switch(status)
0043     {
0044         case Cantor::Expression::Done:
0045         {
0046             auto* result = m_expression->result();
0047             if (result)
0048             {
0049                 const QString data = result->data().toString();
0050                 // In Cantor server response DC2(18) is delimiter between variables
0051                 const QStringList& records = data.split(QChar(18), QString::SkipEmptyParts);
0052 
0053                 QList<Variable> variables;
0054                 for (const QString& record : records)
0055                 {
0056                     // every variable data has 4 parts/elements separated by DC1(17) - the name of the variable, its size, type and the actual value
0057                     const auto& elements = record.split(QChar(17), QString::SkipEmptyParts);
0058                     int count = elements.count();
0059                     if (count < 4)
0060                         continue;
0061 
0062                     const QString& name = elements.at(0);
0063                     const QString& value = elements.at(1);
0064                     const QString& size = elements.at(2);
0065                     const QString& type = elements.at(3);
0066                     variables << Variable(name, value, size.toULongLong(), type);
0067                 }
0068 
0069                 setVariables(variables);
0070             }
0071             break;
0072         }
0073         case Cantor::Expression::Interrupted:
0074         case Cantor::Expression::Error:
0075         {
0076             qDebug() << "python variable model update finished with status" << (status == Cantor::Expression::Error? "Error" : "Interrupted");
0077             if (status == Cantor::Expression::Error)
0078                 qDebug() << "error message: " << m_expression->errorMessage();
0079             break;
0080         }
0081         default:
0082             return;
0083     }
0084 
0085     m_expression->deleteLater();
0086     m_expression = nullptr;
0087 }