GNU Octave  4.4.1
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) 2013-2018 John W. Eaton
4 Copyright (C) 2011-2018 Jacob Dawid
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include <QTreeWidget>
29 #include <QSettings>
30 
31 #include "symscope.h"
32 #include "utils.h"
33 
34 #include "resource-manager.h"
35 #include "workspace-model.h"
36 
37 namespace octave
38 {
41  {
42  m_columnNames.append (tr ("Name"));
43  m_columnNames.append (tr ("Class"));
44  m_columnNames.append (tr ("Dimension"));
45  m_columnNames.append (tr ("Value"));
46  m_columnNames.append (tr ("Attribute"));
47 
48  for (int i = 0; i < resource_manager::storage_class_chars ().length (); i++)
49  m_storage_class_colors.append (QColor (Qt::white));
50 
51  }
52 
55  {
56  QList<QColor> colors;
57 
58  if (colors.isEmpty ())
59  {
60  colors << QColor (190, 255, 255)
61  << QColor (220, 255, 220)
62  << QColor (220, 220, 255)
63  << QColor (255, 255, 190)
64  << QColor (255, 220, 220)
65  << QColor (255, 190, 255);
66  }
67 
68  return colors;
69  }
70 
71  QStringList
73  {
74  QStringList names;
75 
76  if (names.isEmpty ())
77  {
78  names << QObject::tr ("automatic")
79  << QObject::tr ("function")
80  << QObject::tr ("global")
81  << QObject::tr ("hidden")
82  << QObject::tr ("inherited")
83  << QObject::tr ("persistent");
84  }
85 
86  return names;
87  }
88 
89  int
90  workspace_model::rowCount (const QModelIndex&) const
91  {
92  return m_symbols.size ();
93  }
94 
95  int
96  workspace_model::columnCount (const QModelIndex&) const
97  {
98  return m_columnNames.size ();
99  }
100 
101  Qt::ItemFlags
102  workspace_model::flags (const QModelIndex& idx) const
103  {
104  Qt::ItemFlags retval = Qt::NoItemFlags;
105 
106  if (idx.isValid ())
107  {
108  retval |= Qt::ItemIsEnabled;
109 
110  if (m_top_level && idx.column () == 0)
111  retval |= Qt::ItemIsSelectable;
112  }
113 
114  return retval;
115  }
116 
117  QVariant
118  workspace_model::headerData (int section, Qt::Orientation orientation,
119  int role) const
120  {
121  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
122  return m_columnNames[section];
123  else
124  return QVariant ();
125  }
126 
127  QVariant
128  workspace_model::data (const QModelIndex& idx, int role) const
129  {
130  QVariant retval;
131 
132  if (idx.isValid ())
133  {
134  if (role == Qt::BackgroundColorRole)
135  {
136  QString class_chars = resource_manager::storage_class_chars ();
137  int actual_class
138  = class_chars.indexOf (m_scopes[idx.row ()].toLatin1 ());
139  if (actual_class >= 0)
140  return QVariant (m_storage_class_colors.at (actual_class));
141  else
142  return retval;
143  }
144 
145  if (role == Qt::DisplayRole
146  || (idx.column () == 0 && role == Qt::EditRole)
147  || (idx.column () == 0 && role == Qt::ToolTipRole))
148  {
149  switch (idx.column ())
150  {
151  case 0:
152  if (role == Qt::ToolTipRole)
153  retval
154  = QVariant (tr ("Right click to copy, rename, or display"));
155  else
156  retval = QVariant (m_symbols[idx.row ()]);
157  break;
158 
159  case 1:
160  retval = QVariant (m_class_names[idx.row ()]);
161  break;
162 
163  case 2:
164  retval = QVariant (m_dimensions[idx.row ()]);
165  break;
166 
167  case 3:
168  retval = QVariant (m_values[idx.row ()]);
169  break;
170 
171  case 4:
172  {
173  QString sclass;
174 
175  QString class_chars = resource_manager::storage_class_chars ();
176 
177  int actual_class
178  = class_chars.indexOf (m_scopes[idx.row ()].toLatin1 ());
179 
180  if (actual_class >= 0)
181  {
182  QStringList class_names
184 
185  sclass = class_names.at (actual_class);
186  }
187 
188  if (m_complex_flags[idx.row ()])
189  {
190  if (sclass.isEmpty ())
191  sclass = tr ("complex");
192  else
193  sclass += ", " + tr ("complex");
194  }
195 
196  retval = QVariant (sclass);
197  }
198  break;
199  }
200  }
201  }
202 
203  return retval;
204  }
205 
206  bool
207  workspace_model::setData (const QModelIndex& idx, const QVariant& value,
208  int role)
209  {
210  bool retval = false;
211 
212  if (idx.column () == 0 && role == Qt::EditRole)
213  {
214  QString qold_name = m_symbols[idx.row ()];
215 
216  QString qnew_name = value.toString ();
217 
218  std::string new_name = qnew_name.toStdString ();
219 
220  if (valid_identifier (new_name))
221  {
222  emit rename_variable (qold_name, qnew_name);
223 
224  retval = true;
225  }
226  }
227 
228  return retval;
229  }
230 
231  void
232  workspace_model::set_workspace (bool top_level, bool /* debug */,
233  const symbol_scope& scope)
234  {
235  clear_data ();
236 
237  m_top_level = top_level;
238  m_scope = scope;
239 
240  update_table ();
241  }
242 
243  void
245  {
246  clear_data ();
247  update_table ();
248  }
249 
250  void
251  workspace_model::notice_settings (const QSettings *settings)
252  {
253  QList<QColor> default_colors =
255  QString class_chars = resource_manager::storage_class_chars ();
256 
257  for (int i = 0; i < class_chars.length (); i++)
258  {
259  QVariant default_var = default_colors.at (i);
260  QColor setting_color = settings->value ("workspaceview/color_"
261  + class_chars.mid (i,1),
262  default_var).value<QColor> ();
263  m_storage_class_colors.replace (i,setting_color);
264  }
265  }
266 
267  void
269  {
270  m_top_level = false;
271  m_scope = symbol_scope ();
272  m_scopes = QString ();
273  m_symbols = QStringList ();
274  m_class_names = QStringList ();
275  m_dimensions = QStringList ();
276  m_values = QStringList ();
278  }
279 
280  void
282  {
283  beginResetModel ();
284 
285  std::list<symbol_record> sr_list = m_scope.all_variables ();
286 
288 
289  for (const auto& sr : sr_list)
290  {
291  std::string nm = sr.name ();
292 
293  octave_value val = sr.varval (context);
294 
295  // FIXME: fix size for objects, see kluge in variables.cc
296  //dim_vector dv = val.dims ();
297  octave_value tmp = val;
298  Matrix sz = tmp.size ();
299  dim_vector dv = dim_vector::alloc (sz.numel ());
300  for (octave_idx_type i = 0; i < dv.ndims (); i++)
301  dv(i) = sz(i);
302 
303  char storage = ' ';
304  if (sr.is_global ())
305  storage = 'g';
306  else if (sr.is_persistent ())
307  storage = 'p';
308  else if (sr.is_automatic ())
309  storage = 'a';
310  else if (sr.is_formal ())
311  storage = 'f';
312  else if (sr.is_hidden ())
313  storage = 'h';
314  else if (sr.is_inherited ())
315  storage = 'i';
316 
317  std::ostringstream buf;
318  val.short_disp (buf);
319  std::string short_disp_str = buf.str ();
320 
321  m_scopes.append (storage);
323  m_class_names.append (QString::fromStdString (val.class_name ()));
325  m_values.append (QString::fromStdString (short_disp_str));
326  m_complex_flags.append (val.iscomplex ());
327  }
328 
329  endResetModel ();
330 
331  emit model_changed ();
332  }
333 }
std::string str(char sep='x') const
Definition: dim-vector.cc:73
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
symbol_scope scope(void) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
QString fromStdString(const std::string &s)
static llvm::LLVMContext & context
Definition: jit-typeinfo.cc:79
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
QList< QColor > m_storage_class_colors
std::list< symbol_record > all_variables(bool defined_only=true, unsigned int exclude=symbol_record::hidden) const
Definition: symscope.h:808
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
workspace_model(QObject *parent=nullptr)
bool valid_identifier(const char *s)
Definition: utils.cc:74
symbol_record::context_id current_context(void) const
Definition: symscope.h:669
int columnCount(const QModelIndex &parent=QModelIndex()) const
static QString storage_class_chars(void)
void set_workspace(bool top_level, bool debug, const symbol_scope &scope)
Qt::ItemFlags flags(const QModelIndex &index) const
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
void rename_variable(const QString &old_name, const QString &new_name)
Definition: dMatrix.h:36
static QList< QColor > storage_class_default_colors(void)
sz
Definition: data.cc:5264
static dim_vector alloc(int n)
Definition: dim-vector.h:264
static QStringList storage_class_names(void)
QVariant data(const QModelIndex &index, int role) const
static QList< QColor > storage_class_default_colors(void)
static QStringList storage_class_names(void)
p
Definition: lu.cc:138
for i
Definition: data.cc:5264
QList< int > QIntList
Definition: dialog.h:38
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:295
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
nd group nd example For each display the value
Definition: sysdep.cc:866
dim_vector dv
Definition: sub2ind.cc:263
int rowCount(const QModelIndex &parent=QModelIndex()) const
void notice_settings(const QSettings *)