GNU Octave  4.0.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-2015 John W. Eaton
4 Copyright (C) 2011-2015 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 ("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 
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  bool /* debug */,
232  const QString& scopes,
233  const QStringList& symbols,
234  const QStringList& class_names,
235  const QStringList& dimensions,
236  const QStringList& values,
237  const QIntList& complex_flags)
238 {
239  _top_level = top_level;
240  _scopes = scopes;
241  _symbols = symbols;
242  _class_names = class_names;
243  _dimensions = dimensions;
244  _values = values;
245  _complex_flags = complex_flags;
246 
247  update_table ();
248 }
249 
250 void
252 {
253  clear_data ();
254  update_table ();
255 }
256 
257 void
259 {
260  _top_level = false;
261  _scopes = QString ();
262  _symbols = QStringList ();
263  _class_names = QStringList ();
264  _dimensions = QStringList ();
265  _values = QStringList ();
267 }
268 
269 void
271 {
272  beginResetModel ();
273 
274  // Nothing to do except tell the world to recalc.
275 
276  endResetModel ();
277 
278  emit model_changed ();
279 }
280 
281 void
282 workspace_model::notice_settings (const QSettings *settings)
283 {
284  QList<QColor> default_colors =
286  QString class_chars = resource_manager::storage_class_chars ();
287 
288  for (int i = 0; i < class_chars.length (); i++)
289  {
290  QVariant default_var = default_colors.at (i);
291  QColor setting_color = settings->value ("workspaceview/color_"
292  + class_chars.mid (i,1),
293  default_var).value<QColor> ();
294  _storage_class_colors.replace (i,setting_color);
295  }
296 }
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
static string_vector names(const map_type &lst)
Definition: help.cc:782
QIntList _complex_flags
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:77
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
QStringList _dimensions
void notice_settings(const QSettings *)
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
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)
QStringList _class_names