GNU Octave  3.8.0
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 John W. Eaton
4 Copyright (C) 2011-2013 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 #ifdef 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 ("Storage Class"));
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 
68 QStringList
70 {
71  QStringList names;
72 
73  if (names.isEmpty ())
74  {
75  names << QObject::tr ("automatic")
76  << QObject::tr ("function")
77  << QObject::tr ("global")
78  << QObject::tr ("hidden")
79  << QObject::tr ("inherited")
80  << QObject::tr ("persistent");
81  }
82 
83  return names;
84 }
85 
86 int
87 workspace_model::rowCount (const QModelIndex&) const
88 {
89  return _symbols.size ();
90 }
91 
92 int
93 workspace_model::columnCount (const QModelIndex&) const
94 {
95  return _columnNames.size ();
96 }
97 
98 Qt::ItemFlags
99 workspace_model::flags (const QModelIndex& idx) const
100 {
101  Qt::ItemFlags retval = 0;
102 
103  if (idx.isValid ())
104  {
105  retval |= Qt::ItemIsEnabled;
106 
107  if (_top_level && idx.column () == 0)
108  retval |= Qt::ItemIsSelectable;
109  }
110 
111  return retval;
112 }
113 
114 QVariant
115 workspace_model::headerData (int section, Qt::Orientation orientation,
116  int role) const
117 {
118  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
119  return _columnNames[section];
120  else
121  return QVariant ();
122 }
123 
124 QVariant
125 workspace_model::data (const QModelIndex& idx, int role) const
126 {
127  QVariant retval;
128 
129  if (idx.isValid ())
130  {
131  if (role == Qt::BackgroundColorRole)
132  {
133  QString class_chars = resource_manager::storage_class_chars ();
134  int actual_class
135  = class_chars.indexOf (_scopes[idx.row ()].toAscii ());
136  if (actual_class >= 0)
137  return QVariant (_storage_class_colors.at (actual_class));
138  else
139  return retval;
140  }
141 
142  if (role == Qt::DisplayRole
143  || (idx.column () == 0 && role == Qt::EditRole)
144  || (idx.column () == 0 && role == Qt::ToolTipRole) )
145  {
146  switch (idx.column ())
147  {
148  case 0:
149  if (role == Qt::ToolTipRole)
150  retval
151  = QVariant (tr ("Right click to copy, rename, or display"));
152  else
153  retval = QVariant (_symbols[idx.row ()]);
154  break;
155 
156  case 1:
157  retval = QVariant (_class_names[idx.row ()]);
158  break;
159 
160  case 2:
161  retval = QVariant (_dimensions[idx.row ()]);
162  break;
163 
164  case 3:
165  retval = QVariant (_values[idx.row ()]);
166  break;
167 
168  case 4:
169  {
170  QString sclass;
171 
172  QString class_chars = resource_manager::storage_class_chars ();
173 
174  int actual_class
175  = class_chars.indexOf (_scopes[idx.row ()].toAscii ());
176 
177  if (actual_class >= 0)
178  {
179  QStringList class_names
181 
182  sclass = class_names.at (actual_class);
183  }
184 
185  if (_complex_flags[idx.row ()])
186  {
187  if (sclass.isEmpty ())
188  sclass = tr ("complex");
189  else
190  sclass += ", " + tr ("complex");
191  }
192 
193  retval = QVariant (sclass);
194  }
195  break;
196  }
197  }
198  }
199 
200  return retval;
201 }
202 
203 bool
204 workspace_model::setData (const QModelIndex& idx, const QVariant& value,
205  int role)
206 {
207  bool retval = false;
208 
209  if (idx.column () == 0 && role == Qt::EditRole)
210  {
211  QString qold_name = _symbols[idx.row ()];
212 
213  QString qnew_name = value.toString ();
214 
215  std::string new_name = qnew_name.toStdString ();
216 
217  if (valid_identifier (new_name))
218  {
219  emit rename_variable (qold_name, qnew_name);
220 
221  retval = true;
222  }
223  }
224 
225  return retval;
226 }
227 
228 
229 void
231  const QString& scopes,
232  const QStringList& symbols,
233  const QStringList& class_names,
234  const QStringList& dimensions,
235  const QStringList& values,
236  const QIntList& complex_flags)
237 {
238  _top_level = top_level;
239  _scopes = scopes;
240  _symbols = symbols;
241  _class_names = class_names;
242  _dimensions = dimensions;
243  _values = values;
244  _complex_flags = complex_flags;
245 
246  update_table ();
247 
248  emit model_changed ();
249 }
250 
251 void
253 {
254  clear_data ();
255  update_table ();
256 
257  emit model_changed ();
258 }
259 
260 void
262 {
263  _top_level = false;
264  _scopes = QString ();
265  _symbols = QStringList ();
266  _class_names = QStringList ();
267  _dimensions = QStringList ();
268  _values = QStringList ();
270 }
271 
272 void
274 {
275  beginResetModel ();
276 
277  // Nothing to do except tell the world to recalc.
278 
279  endResetModel ();
280 
281  emit model_changed ();
282 }
283 
284 void
285 workspace_model::notice_settings (const QSettings *settings)
286 {
287  QList<QColor> default_colors =
289  QString class_chars = resource_manager::storage_class_chars ();
290 
291  for (int i = 0; i < class_chars.length (); i++)
292  {
293  QVariant default_var = default_colors.at (i);
294  QColor setting_color = settings->value ("workspaceview/color_"
295  + class_chars.mid (i,1),
296  default_var).value<QColor> ();
297  _storage_class_colors.replace (i,setting_color);
298  }
299 }