GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
workspace-model.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <iostream>
31 
32 #include <QTreeWidget>
33 
34 #include "gui-preferences-ws.h"
35 #include "gui-settings.h"
36 #include "octave-qobject.h"
37 #include "workspace-model.h"
38 
39 #include "syminfo.h"
40 #include "utils.h"
41 
43 
46 {
47  // The header names. Use tr () again when accessing them since
48  // the translator si not yet initialized when this ctor is called
49  m_columnNames.append (tr ("Name"));
50  m_columnNames.append (tr ("Class"));
51  m_columnNames.append (tr ("Dimension"));
52  m_columnNames.append (tr ("Value"));
53  m_columnNames.append (tr ("Attribute"));
54 
55  // Initialize the background and foreground colors of special
56  // classes in the workspace view. The structure is
57  // m_storage_class_colors(1,2,...,colors): background colors
58  // m_storage_class_colors(colors+1,...,2*colors): foreground colors
59  for (unsigned int i = 0; i < 2*ws_colors_count; i++)
60  m_storage_class_colors.append (QColor (Qt::white));
61 
62 }
63 
64 int
65 workspace_model::rowCount (const QModelIndex&) const
66 {
67  return m_symbols.size ();
68 }
69 
70 int
71 workspace_model::columnCount (const QModelIndex&) const
72 {
73  return m_columnNames.size ();
74 }
75 
76 Qt::ItemFlags
77 workspace_model::flags (const QModelIndex& idx) const
78 {
79  Qt::ItemFlags retval = Qt::NoItemFlags;
80 
81  if (idx.isValid ())
82  {
83  retval |= Qt::ItemIsEnabled;
84 
85  if (m_top_level && idx.column () == 0)
86  retval |= Qt::ItemIsSelectable;
87  }
88 
89  return retval;
90 }
91 
92 QVariant
93 workspace_model::headerData (int section, Qt::Orientation orientation,
94  int role) const
95 {
96  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
97  return tr (m_columnNames[section].toStdString ().data ());
98  else
99  return QVariant ();
100 }
101 
102 QVariant
103 workspace_model::data (const QModelIndex& idx, int role) const
104 {
105  QVariant retval;
106 
107  if (idx.isValid ())
108  {
109  if ((role == Qt::BackgroundRole || role == Qt::ForegroundRole)
110  && m_enable_colors)
111  {
112  int actual_class
113  = ws_class_chars.indexOf (m_scopes[idx.row ()].toLatin1 ());
114  if (actual_class >= 0)
115  {
116  // Valid class: Get background (normal indexes) or foreground
117  // color (indexes with offset)
118  if (role == Qt::ForegroundRole)
119  actual_class += ws_colors_count;
120 
121  return QVariant (m_storage_class_colors.at (actual_class));
122  }
123  else
124  return retval;
125  }
126 
127  if (role == Qt::DisplayRole
128  || (idx.column () == 0 && role == Qt::EditRole)
129  || (idx.column () == 0 && role == Qt::ToolTipRole))
130  {
131  switch (idx.column ())
132  {
133  case 0:
134  if (role == Qt::ToolTipRole)
135  retval
136  = QVariant (tr ("Right click to copy, rename, or display"));
137  else
138  retval = QVariant (m_symbols[idx.row ()]);
139  break;
140 
141  case 1:
142  retval = QVariant (m_class_names[idx.row ()]);
143  break;
144 
145  case 2:
146  retval = QVariant (m_dimensions[idx.row ()]);
147  break;
148 
149  case 3:
150  retval = QVariant (m_values[idx.row ()]);
151  break;
152 
153  case 4:
154  {
155  QString sclass;
156 
157  int actual_class
158  = ws_class_chars.indexOf (m_scopes[idx.row ()].toLatin1 ());
159 
160  if (actual_class >= 0)
161  sclass = ws_color_names.at (actual_class);
162 
163  if (m_complex_flags[idx.row ()])
164  {
165  if (sclass.isEmpty ())
166  sclass = tr ("complex");
167  else
168  sclass += ", " + tr ("complex");
169  }
170 
171  retval = QVariant (sclass);
172  }
173  break;
174  }
175  }
176  }
177 
178  return retval;
179 }
180 
181 void
182 workspace_model::set_workspace (bool top_level, bool /* debug */,
183  const symbol_info_list& syminfo)
184 {
185  clear_data ();
186 
187  m_top_level = top_level;
188  m_syminfo_list = syminfo;
189 
190  update_table ();
191 }
192 
193 void
195 {
196  clear_data ();
197  update_table ();
198 }
199 
200 void
202 {
203  m_enable_colors = settings->value (ws_enable_colors).toBool ();
204 
205  int mode = settings->value (ws_color_mode).toInt ();
206 
207  for (int i = 0; i < ws_colors_count; i++)
208  {
209  QColor setting_color = settings->color_value (ws_colors[i], mode);
210 
211  QPalette p (setting_color);
212  m_storage_class_colors.replace (i, setting_color);
213 
214  QColor fg_color = p.color (QPalette::WindowText);
215  m_storage_class_colors.replace (i + ws_colors_count, fg_color);
216 
217  }
218 }
219 
220 void
222 {
223  m_top_level = false;
225  m_scopes = QString ();
226  m_symbols = QStringList ();
227  m_class_names = QStringList ();
228  m_dimensions = QStringList ();
229  m_values = QStringList ();
231 }
232 
233 void
235 {
236  beginResetModel ();
237 
238  for (const auto& syminfo : m_syminfo_list)
239  {
240  std::string nm = syminfo.name ();
241 
242  octave_value val = syminfo.value ();
243 
244  // FIXME: fix size for objects, see kluge in ov.cc
245  Matrix sz = val.size ();
246  dim_vector dv = dim_vector::alloc (sz.numel ());
247  for (octave_idx_type i = 0; i < dv.ndims (); i++)
248  dv(i) = sz(i);
249 
250  char storage = ' ';
251  if (syminfo.is_formal ())
252  storage = 'a';
253  else if (syminfo.is_global ())
254  storage = 'g';
255  else if (syminfo.is_persistent ())
256  storage = 'p';
257 
258  std::ostringstream buf;
259  val.short_disp (buf);
260  std::string short_disp_str = buf.str ();
261 
262  m_scopes.append (storage);
263  m_symbols.append (QString::fromStdString (nm));
265  m_dimensions.append (QString::fromStdString (dv.str ()));
266  m_values.append (QString::fromStdString (short_disp_str));
267  m_complex_flags.append (val.iscomplex ());
268  }
269 
270  endResetModel ();
271 
272  emit model_changed ();
273 }
274 
OCTAVE_END_NAMESPACE(octave)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTAVE_API std::string str(char sep='x') const
Definition: dim-vector.cc:68
static dim_vector alloc(int n)
Definition: dim-vector.h:202
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:257
void short_disp(std::ostream &os) const
Definition: ov.h:1440
Matrix size(void)
Definition: ov.h:511
std::string class_name(void) const
Definition: ov.h:1454
bool iscomplex(void) const
Definition: ov.h:786
int rowCount(const QModelIndex &parent=QModelIndex()) const
QStringList m_columnNames
void model_changed(void)
symbol_info_list m_syminfo_list
int columnCount(const QModelIndex &parent=QModelIndex()) const
QVariant data(const QModelIndex &index, int role) const
QStringList m_class_names
QStringList m_values
void notice_settings(const gui_settings *)
QList< QColor > m_storage_class_colors
QStringList m_dimensions
Qt::ItemFlags flags(const QModelIndex &index) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
void clear_data(void)
void clear_workspace(void)
QStringList m_symbols
void update_table(void)
QIntList m_complex_flags
void set_workspace(bool top_level, bool debug, const symbol_info_list &syminfo)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
QList< int > QIntList
Definition: dialog.h:40
const QString ws_class_chars("agp")
const gui_pref ws_colors[2 *ws_colors_count]
const gui_pref ws_enable_colors("workspaceview/enable_colors", QVariant(false))
const gui_pref ws_color_mode("workspaceview/color_mode", QVariant(0))
const QStringList ws_color_names
const int ws_colors_count
std::string toStdString(const QString &s)
QString fromStdString(const std::string &s)