GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
workspace-model.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2017 John W. Eaton
4 Copyright (C) 2011-2016 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 the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 <http://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 "utils.h"
32 #include "resource-manager.h"
33 #include "workspace-model.h"
34 
37 {
38  _columnNames.append (tr ("Name"));
39  _columnNames.append (tr ("Class"));
40  _columnNames.append (tr ("Dimension"));
41  _columnNames.append (tr ("Value"));
42  _columnNames.append (tr ("Attribute"));
43 
44  for (int i = 0; i < resource_manager::storage_class_chars ().length (); i++)
45  _storage_class_colors.append (QColor (Qt::white));
46 
47 }
48 
51 {
52  QList<QColor> colors;
53 
54  if (colors.isEmpty ())
55  {
56  colors << QColor (190,255,255)
57  << QColor (220,255,220)
58  << QColor (220,220,255)
59  << QColor (255,255,190)
60  << QColor (255,220,220)
61  << QColor (255,190,255);
62  }
63 
64  return colors;
65 }
66 
67 QStringList
69 {
70  QStringList names;
71 
72  if (names.isEmpty ())
73  {
74  names << QObject::tr ("automatic")
75  << QObject::tr ("function")
76  << QObject::tr ("global")
77  << QObject::tr ("hidden")
78  << QObject::tr ("inherited")
79  << QObject::tr ("persistent");
80  }
81 
82  return names;
83 }
84 
85 int
86 workspace_model::rowCount (const QModelIndex&) const
87 {
88  return _symbols.size ();
89 }
90 
91 int
92 workspace_model::columnCount (const QModelIndex&) const
93 {
94  return _columnNames.size ();
95 }
96 
97 Qt::ItemFlags
98 workspace_model::flags (const QModelIndex& idx) const
99 {
100  Qt::ItemFlags retval = 0;
101 
102  if (idx.isValid ())
103  {
104  retval |= Qt::ItemIsEnabled;
105 
106  if (_top_level && idx.column () == 0)
107  retval |= Qt::ItemIsSelectable;
108  }
109 
110  return retval;
111 }
112 
113 QVariant
114 workspace_model::headerData (int section, Qt::Orientation orientation,
115  int role) const
116 {
117  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
118  return _columnNames[section];
119  else
120  return QVariant ();
121 }
122 
123 QVariant
124 workspace_model::data (const QModelIndex& idx, int role) const
125 {
126  QVariant retval;
127 
128  if (idx.isValid ())
129  {
130  if (role == Qt::BackgroundColorRole)
131  {
132  QString class_chars = resource_manager::storage_class_chars ();
133  int actual_class
134  = class_chars.indexOf (_scopes[idx.row ()].toLatin1 ());
135  if (actual_class >= 0)
136  return QVariant (_storage_class_colors.at (actual_class));
137  else
138  return retval;
139  }
140 
141  if (role == Qt::DisplayRole
142  || (idx.column () == 0 && role == Qt::EditRole)
143  || (idx.column () == 0 && role == Qt::ToolTipRole))
144  {
145  switch (idx.column ())
146  {
147  case 0:
148  if (role == Qt::ToolTipRole)
149  retval
150  = QVariant (tr ("Right click to copy, rename, or display"));
151  else
152  retval = QVariant (_symbols[idx.row ()]);
153  break;
154 
155  case 1:
156  retval = QVariant (_class_names[idx.row ()]);
157  break;
158 
159  case 2:
160  retval = QVariant (_dimensions[idx.row ()]);
161  break;
162 
163  case 3:
164  retval = QVariant (_values[idx.row ()]);
165  break;
166 
167  case 4:
168  {
169  QString sclass;
170 
171  QString class_chars = resource_manager::storage_class_chars ();
172 
173  int actual_class
174  = class_chars.indexOf (_scopes[idx.row ()].toLatin1 ());
175 
176  if (actual_class >= 0)
177  {
178  QStringList class_names
180 
181  sclass = class_names.at (actual_class);
182  }
183 
184  if (_complex_flags[idx.row ()])
185  {
186  if (sclass.isEmpty ())
187  sclass = tr ("complex");
188  else
189  sclass += ", " + tr ("complex");
190  }
191 
192  retval = QVariant (sclass);
193  }
194  break;
195  }
196  }
197  }
198 
199  return retval;
200 }
201 
202 bool
203 workspace_model::setData (const QModelIndex& idx, const QVariant& value,
204  int role)
205 {
206  bool retval = false;
207 
208  if (idx.column () == 0 && role == Qt::EditRole)
209  {
210  QString qold_name = _symbols[idx.row ()];
211 
212  QString qnew_name = value.toString ();
213 
214  std::string new_name = qnew_name.toStdString ();
215 
216  if (valid_identifier (new_name))
217  {
218  emit rename_variable (qold_name, qnew_name);
219 
220  retval = true;
221  }
222  }
223 
224  return retval;
225 }
226 
227 void
229  bool /* debug */,
230  const QString& scopes,
231  const QStringList& symbols,
232  const QStringList& class_names,
233  const QStringList& dimensions,
234  const QStringList& values,
235  const QIntList& complex_flags)
236 {
237  _top_level = top_level;
238  _scopes = scopes;
239  _symbols = symbols;
240  _class_names = class_names;
241  _dimensions = dimensions;
242  _values = values;
243  _complex_flags = complex_flags;
244 
245  update_table ();
246 }
247 
248 void
250 {
251  clear_data ();
252  update_table ();
253 }
254 
255 void
257 {
258  _top_level = false;
259  _scopes = QString ();
260  _symbols = QStringList ();
261  _class_names = QStringList ();
262  _dimensions = QStringList ();
263  _values = QStringList ();
265 }
266 
267 void
269 {
270  beginResetModel ();
271 
272  // Nothing to do except tell the world to recalc.
273 
274  endResetModel ();
275 
276  emit model_changed ();
277 }
278 
279 void
280 workspace_model::notice_settings (const QSettings *settings)
281 {
282  QList<QColor> default_colors =
284  QString class_chars = resource_manager::storage_class_chars ();
285 
286  for (int i = 0; i < class_chars.length (); i++)
287  {
288  QVariant default_var = default_colors.at (i);
289  QColor setting_color = settings->value ("workspaceview/color_"
290  + class_chars.mid (i,1),
291  default_var).value<QColor> ();
292  _storage_class_colors.replace (i,setting_color);
293  }
294 }
static QStringList storage_class_names(void)
QList< QColor > _storage_class_colors
void rename_variable(const QString &old_name, const QString &new_name)
QStringList _symbols
Qt::ItemFlags flags(const QModelIndex &index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
QIntList _complex_flags
cell array If invoked with two or more scalar integer or a vector of integer values
Definition: ov-cell.cc:1205
static QList< QColor > storage_class_default_colors(void)
static QList< QColor > storage_class_default_colors(void)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
QStringList _columnNames
bool valid_identifier(const char *s)
Definition: utils.cc:74
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
QStringList _dimensions
void notice_settings(const QSettings *)
octave_value retval
Definition: data.cc:6294
void set_workspace(bool top_level, bool debug, const QString &scopes, const QStringList &symbols, const QStringList &class_names, const QStringList &dimensions, const QStringList &values, const QIntList &complex_flags)
QStringList _values
workspace_model(QObject *parent=0)
QVariant data(const QModelIndex &index, int role) const
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by nd tex zero divided by nd ifnottex and any operation involving another NaN value(5+NaN).Note that NaN always compares not equal to NaN(NaN!
void clear_data(void)
static QString storage_class_chars(void)
static QStringList storage_class_names(void)
QList< int > QIntList
Definition: dialog.h:38
void update_table(void)
int columnCount(const QModelIndex &parent=QModelIndex()) const
void clear_workspace(void)
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:854
QStringList _class_names