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-view.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 <QInputDialog>
29 #include <QApplication>
30 #include <QClipboard>
31 #include <QMessageBox>
32 #include <QLineEdit>
33 #include <QHeaderView>
34 #include <QHBoxLayout>
35 #include <QVBoxLayout>
36 #include <QPushButton>
37 #include <QMenu>
38 
39 #include "workspace-view.h"
40 #include "resource-manager.h"
41 
43  : octave_dock_widget (p), view (new QTableView (this))
44 {
45  setObjectName ("WorkspaceView");
46  setWindowIcon (QIcon (":/actions/icons/logo.png"));
47  set_title (tr ("Workspace"));
48  setStatusTip (tr ("View the variables in the active workspace."));
49 
50  view->setWordWrap (false);
51  view->setContextMenuPolicy (Qt::CustomContextMenu);
53 
54  // Set an empty widget, so we can assign a layout to it.
55  setWidget (new QWidget (this));
56 
57  // Create a new layout and add widgets to it.
58  QVBoxLayout *vbox_layout = new QVBoxLayout ();
59  vbox_layout->addWidget (view);
60  vbox_layout->setMargin (2);
61 
62  // Set the empty widget to have our layout.
63  widget ()->setLayout (vbox_layout);
64 
65  // Initialize collapse/expand state of the workspace subcategories.
66 
67  QSettings *settings = resource_manager::get_settings ();
68 
69  // Initialize column order and width of the workspace
70 
71  view->horizontalHeader ()->restoreState (
72  settings->value ("workspaceview/column_state").toByteArray ());
73 
74  // Connect signals and slots.
75 
76  connect (view, SIGNAL (customContextMenuRequested (const QPoint&)),
77  this, SLOT (contextmenu_requested (const QPoint&)));
78 
79  connect (this, SIGNAL (command_requested (const QString&)),
80  p, SLOT (execute_command_in_terminal (const QString&)));
81 
82 }
83 
85 {
86  QSettings *settings = resource_manager::get_settings ();
87 
88  settings->setValue ("workspaceview/column_state",
89  view->horizontalHeader ()->saveState ());
90 
91  settings->sync ();
92 }
93 
95 {
96  view->setModel (model);
97  _model = model;
98 }
99 
100 void
102 {
103  emit active_changed (false);
105 }
106 
107 void
109 {
110  QMenu menu (this);
111 
112  QModelIndex index = view->indexAt (qpos);
113  QAbstractItemModel *m = view->model ();
114 
115  // if it isnt Local, Glocal etc, allow the ctx menu
116  if (index.isValid () && index.column () == 0)
117  {
118  index = index.sibling (index.row (), 0);
119 
120  QMap<int, QVariant> item_data = m->itemData (index);
121 
122  QString var_name = item_data[0].toString ();
123 
124  menu.addAction (tr ("Copy"), this,
125  SLOT (handle_contextmenu_copy ()));
126 
127  QAction *rename = menu.addAction (tr ("Rename"), this,
128  SLOT (handle_contextmenu_rename ()));
129 
130  const workspace_model *wm = static_cast<const workspace_model *> (m);
131 
132  if (! wm->is_top_level ())
133  {
134  rename->setDisabled (true);
135  rename->setToolTip (tr ("Only top-level symbols may be renamed."));
136  }
137 
138  menu.addSeparator ();
139 
140  menu.addAction ("disp (" + var_name + ")", this,
141  SLOT (handle_contextmenu_disp ()));
142 
143  menu.addAction ("plot (" + var_name + ")", this,
144  SLOT (handle_contextmenu_plot ()));
145 
146  menu.addAction ("stem (" + var_name + ")", this,
147  SLOT (handle_contextmenu_stem ()));
148 
149  menu.exec (view->mapToGlobal (qpos));
150  }
151 }
152 
153 void
155 {
156  QModelIndex index = view->currentIndex ();
157 
158  if (index.isValid ())
159  {
160  index = index.sibling (index.row (), 0);
161 
162  QAbstractItemModel *m = view->model ();
163 
164  QMap<int, QVariant> item_data = m->itemData (index);
165 
166  QString var_name = item_data[0].toString ();
167 
168  QClipboard *clipboard = QApplication::clipboard ();
169 
170  clipboard->setText (var_name);
171  }
172 }
173 
174 void
176 {
177  QModelIndex index = view->currentIndex ();
178 
179  if (index.isValid ())
180  {
181  index = index.sibling (index.row (), 0);
182 
183  QAbstractItemModel *m = view->model ();
184 
185  QMap<int, QVariant> item_data = m->itemData (index);
186 
187  QString var_name = item_data[0].toString ();
188 
189  QInputDialog* inputDialog = new QInputDialog ();
190 
191  inputDialog->setOptions (QInputDialog::NoButtons);
192 
193  bool ok = false;
194 
195  QString new_name
196  = inputDialog->getText (0, "Rename Variable", "New name:",
197  QLineEdit::Normal, var_name, &ok);
198 
199  if (ok && ! new_name.isEmpty ())
200  m->setData (index, new_name, Qt::EditRole);
201  }
202 }
203 
204 void
206 {
207  relay_contextmenu_command ("disp");
208 }
209 
210 void
212 {
213  relay_contextmenu_command ("figure (); plot");
214 }
215 
216 void
218 {
219  relay_contextmenu_command ("figure (); stem");
220 }
221 
222 void
224 {
225  QModelIndex index = view->currentIndex ();
226 
227  if (index.isValid ())
228  {
229  index = index.sibling (index.row (), 0);
230 
231  QAbstractItemModel *m = view->model ();
232 
233  QMap<int, QVariant> item_data = m->itemData (index);
234 
235  QString var_name = item_data[0].toString ();
236 
237  emit command_requested (cmdname + " (" + var_name + ");");
238  }
239 }
240 
241 void
243 {
244  // Just modify those rows that have been added rather than go through
245  // the whole list. For-loop test will handle when number of rows reduced.
246  QFontMetrics fm = view->fontMetrics ();
247  int row_height = fm.height ();
248  int new_row_count = view->model ()->rowCount ();
249  for (int i = view_previous_row_count; i < new_row_count; i++)
250  view->setRowHeight (i, row_height);
251  view_previous_row_count = new_row_count;
252 }
253 
254 void
255 workspace_view::notice_settings (const QSettings *settings)
256 {
257  _model->notice_settings (settings); // update colors of model first
258 
259  QString tool_tip;
260  tool_tip = QString (tr ("View the variables in the active workspace.<br>"));
261  tool_tip += QString (tr ("Colors for the storage class:"));
262  for (int i = 0; i < resource_manager::storage_class_chars ().length (); i++)
263  {
264  tool_tip +=
265  QString ("<div style=\"background-color:%1;color:#000000\">%2</div>")
266  .arg (_model->storage_class_color (i).name ())
268  }
269  setToolTip (tool_tip);
270 }
271 
272 void
274 {
275  if (view->hasFocus ())
277 }
278