File indexing completed on 2024-04-14 10:50:04

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2018 Nikita Sirgienko <warquark@gmail.com>
0004 */
0005 
0006 #include "juliavariablemodel.h"
0007 #include "juliaextensions.h"
0008 #include "juliasession.h"
0009 
0010 #include <QDebug>
0011 #include <QDBusReply>
0012 #include <QDBusInterface>
0013 #include <QString>
0014 
0015 #include "settings.h"
0016 
0017 using namespace Cantor;
0018 
0019 const QRegularExpression JuliaVariableModel::typeVariableInfo = QRegularExpression(QLatin1String("\\w+\\["));
0020 const QStringList JuliaVariableModel::internalCantorJuliaVariables = {QLatin1String("__cantor_gr_gks_need_restore__")};
0021 
0022 JuliaVariableModel::JuliaVariableModel(JuliaSession* session):
0023     DefaultVariableModel(session),
0024     m_interface(nullptr)
0025 {
0026 }
0027 
0028 void JuliaVariableModel::setJuliaServer(QDBusInterface* interface)
0029 {
0030     m_interface = interface;
0031 }
0032 
0033 void JuliaVariableModel::update()
0034 {
0035     if (!m_interface)
0036         return;
0037 
0038     m_interface->call(QLatin1String("parseModules"), JuliaSettings::variableManagement());
0039 
0040     const auto& variables =
0041         static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("variablesList"))).value();
0042 
0043     QList<Variable> vars;
0044     if (JuliaSettings::variableManagement())
0045     {
0046         const auto& values =
0047             static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("variableValuesList"))).value();
0048         const auto& sizes =
0049             static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("variableSizesList"))).value();
0050         const auto& types =
0051             static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("variableTypesList"))).value();
0052 
0053         for (int i = 0; i < variables.size(); i++)
0054         {
0055             if (i >= values.size())
0056             {
0057                 qWarning() << "Don't have value for variable from julia server response, something wrong!";
0058                 continue;
0059             }
0060 
0061             const auto& name = variables.at(i);
0062             auto value = values.at(i);
0063             size_t size = sizes.at(i).toULongLong();
0064             const auto& type = types.at(i);
0065             if (!internalCantorJuliaVariables.contains(name) && value != JuliaVariableManagementExtension::REMOVED_VARIABLE_MARKER)
0066             {
0067                 // Register variable
0068                 // We use replace here, because julia return data type for some variables, and we need
0069                 // remove it to make variable view more consistent with the other backends
0070                 // More info: https://bugs.kde.org/show_bug.cgi?id=377771
0071                 vars << Variable(name, value.replace(typeVariableInfo, QLatin1String("[")), size, type);
0072             }
0073         }
0074     }
0075     else
0076     {
0077         for (int i = 0; i < variables.size(); i++)
0078             vars << Variable(variables[i], QString());
0079     }
0080     setVariables(vars);
0081 
0082     const QStringList& newFuncs =
0083         static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("functionsList"))).value();
0084     setFunctions(newFuncs);
0085 }