Changeset View
Changeset View
Standalone View
Standalone View
src/backends/octave/octavevariablemodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This program is free software; you can redistribute it and/or | ||||
3 | modify it under the terms of the GNU General Public License | ||||
4 | as published by the Free Software Foundation; either version 2 | ||||
5 | of the License, or (at your option) any later version. | ||||
6 | | ||||
7 | This program is distributed in the hope that it will be useful, | ||||
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
10 | GNU General Public License for more details. | ||||
11 | | ||||
12 | You should have received a copy of the GNU General Public License | ||||
13 | along with this program; if not, write to the Free Software | ||||
14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
15 | Boston, MA 02110-1301, USA. | ||||
16 | | ||||
17 | --- | ||||
18 | Copyright (C) 2018 Nikita Sirgienko <warquark@gmail.com> | ||||
19 | */ | ||||
20 | | ||||
21 | #include "octavevariablemodel.h" | ||||
22 | #include "octavesession.h" | ||||
23 | #include "textresult.h" | ||||
24 | | ||||
25 | #include <QDebug> | ||||
26 | | ||||
27 | #include "settings.h" | ||||
28 | | ||||
29 | using namespace Cantor; | ||||
30 | | ||||
31 | OctaveVariableModel::OctaveVariableModel(OctaveSession* session): | ||||
32 | DefaultVariableModel(session), | ||||
33 | m_expr(nullptr) | ||||
34 | { | ||||
35 | } | ||||
36 | | ||||
37 | void OctaveVariableModel::update() | ||||
38 | { | ||||
39 | static const QString& cmd = QLatin1String( | ||||
40 | "printf('__cantor_delimiter_line__\\n');" | ||||
41 | "__cantor_list__ = who();" | ||||
42 | "for __cantor_index__ = 1:length(__cantor_list__)" | ||||
43 | " __cantor_varname__ = char(__cantor_list__{__cantor_index__});" | ||||
44 | " printf([__cantor_varname__ '\\n']);" | ||||
45 | " eval(['disp(' __cantor_varname__ ')']);" | ||||
46 | " printf('__cantor_delimiter_line__\\n')" | ||||
47 | "endfor;" | ||||
48 | "clear __cantor_list__;" | ||||
49 | "clear __cantor_index__;" | ||||
50 | "clear __cantor_varname__;" | ||||
51 | ); | ||||
52 | | ||||
53 | if (m_expr) | ||||
54 | return; | ||||
55 | | ||||
56 | m_expr = session()->evaluateExpression(cmd, Expression::FinishingBehavior::DoNotDelete, true); | ||||
57 | connect(m_expr, &Expression::statusChanged, this, &OctaveVariableModel::parseNewVariables); | ||||
58 | } | ||||
59 | | ||||
60 | void OctaveVariableModel::parseNewVariables(Expression::Status status) | ||||
61 | { | ||||
62 | switch(status) | ||||
63 | { | ||||
64 | case Expression::Status::Done: | ||||
65 | { | ||||
66 | static const QLatin1String delimiter("__cantor_delimiter_line__"); | ||||
67 | | ||||
68 | // Result always must be, if we done, so don't check it | ||||
69 | QString text = static_cast<Cantor::TextResult*>(m_expr->result())->plain(); | ||||
70 | const QStringList& lines = text.split(delimiter, QString::SkipEmptyParts); | ||||
71 | | ||||
72 | QList<Variable> vars; | ||||
73 | for (QString line : lines) | ||||
74 | { | ||||
75 | line = line.trimmed(); | ||||
76 | const QString& name = line.section(QLatin1String("\n"), 0, 0); | ||||
77 | QString value; | ||||
78 | if (OctaveSettings::self()->variableManagement()) | ||||
79 | value = line.section(QLatin1String("\n"), 1); | ||||
80 | vars << Variable{name, value}; | ||||
81 | } | ||||
82 | | ||||
83 | setVariables(vars); | ||||
84 | break; | ||||
85 | } | ||||
86 | default: | ||||
87 | return; | ||||
88 | } | ||||
89 | | ||||
90 | m_expr->deleteLater(); | ||||
91 | m_expr = nullptr; | ||||
92 | } |