comparison libgui/src/workspace-view.cc @ 16462:8c666c7b0e5d

Added context menu to workspace variable display * libgui/src/workspace-view.h, libgui/src/workspace-view.cc (workspace_view::workspace_view): Connect signals for context menu. (workspace_view::contextmenu_requested, workspace_view::handle_contextmenu_disp, workspace_view::handle_contextmenu_plot, workspace_view::handle_contextmenu_stem): New slot functions. (workspace_view::relay_contextmenu_command): New function. (workspace_view::command_requested): New signal.
author John Donoghue <john.donoghue@ieee.org>
date Sun, 07 Apr 2013 15:48:11 -0400
parents cbc39a3d0c42
children 8e2a853cdd7d
comparison
equal deleted inserted replaced
16461:094bd3627ead 16462:8c666c7b0e5d
28 #include "resource-manager.h" 28 #include "resource-manager.h"
29 #include <QHeaderView> 29 #include <QHeaderView>
30 #include <QHBoxLayout> 30 #include <QHBoxLayout>
31 #include <QVBoxLayout> 31 #include <QVBoxLayout>
32 #include <QPushButton> 32 #include <QPushButton>
33 #include <QMenu>
33 34
34 workspace_view::workspace_view (QWidget *p) 35 workspace_view::workspace_view (QWidget *p)
35 : QDockWidget (p) 36 : QDockWidget (p)
36 { 37 {
37 setObjectName ("WorkspaceView"); 38 setObjectName ("WorkspaceView");
43 view->setHeaderHidden (false); // Do not show header columns. 44 view->setHeaderHidden (false); // Do not show header columns.
44 view->setAlternatingRowColors (true); // Activate alternating row colors. 45 view->setAlternatingRowColors (true); // Activate alternating row colors.
45 view->setAnimated (false); // Deactivate animations because of strange glitches. 46 view->setAnimated (false); // Deactivate animations because of strange glitches.
46 view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells. 47 view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells.
47 view->setWordWrap (false); // No wordwrapping in cells. 48 view->setWordWrap (false); // No wordwrapping in cells.
49 view->setContextMenuPolicy (Qt::CustomContextMenu);
48 50
49 // Set an empty widget, so we can assign a layout to it. 51 // Set an empty widget, so we can assign a layout to it.
50 setWidget (new QWidget (this)); 52 setWidget (new QWidget (this));
51 53
52 // Create a new layout and add widgets to it. 54 // Create a new layout and add widgets to it.
78 connect (view, SIGNAL (expanded (QModelIndex)), 80 connect (view, SIGNAL (expanded (QModelIndex)),
79 this, SLOT (expand_requested (QModelIndex))); 81 this, SLOT (expand_requested (QModelIndex)));
80 82
81 connect (view, SIGNAL (doubleClicked (QModelIndex)), 83 connect (view, SIGNAL (doubleClicked (QModelIndex)),
82 this, SLOT (item_double_clicked (QModelIndex))); 84 this, SLOT (item_double_clicked (QModelIndex)));
85
86 connect (view, SIGNAL (customContextMenuRequested(const QPoint&)),
87 this, SLOT(contextmenu_requested (const QPoint&)));
88
89 connect (this, SIGNAL (command_requested (const QString&)),
90 p, SLOT (handle_command_double_clicked(const QString&)));
83 91
84 // topLevelChanged is emitted when floating property changes (floating = true) 92 // topLevelChanged is emitted when floating property changes (floating = true)
85 connect (this, SIGNAL (topLevelChanged(bool)), this, SLOT(top_level_changed(bool))); 93 connect (this, SIGNAL (topLevelChanged(bool)), this, SLOT(top_level_changed(bool)));
86 94
87 } 95 }
243 { 251 {
244 // if changed to visible and widget is not floating 252 // if changed to visible and widget is not floating
245 if (visible && ! isFloating ()) 253 if (visible && ! isFloating ())
246 focus (); 254 focus ();
247 } 255 }
256
257 void
258 workspace_view::contextmenu_requested (const QPoint& pos)
259 {
260 QMenu menu (this);
261
262 QModelIndex index = view->indexAt (pos);
263 QAbstractItemModel *m = view->model ();
264
265 // if it isnt Local, Glocal etc, allow the ctx menu
266 if (index.parent() != QModelIndex())
267 {
268 QMap<int, QVariant> item_data = m->itemData (index);
269
270 QString var_name = item_data[0].toString ();
271
272 menu.addAction ("disp(" + var_name + ")", this,
273 SLOT (handle_contextmenu_disp ()));
274
275 menu.addAction ("plot(" + var_name + ")", this,
276 SLOT (handle_contextmenu_plot ()));
277
278 menu.addAction ("stem(" + var_name + ")", this,
279 SLOT (handle_contextmenu_stem ()));
280
281 menu.exec (view->mapToGlobal (pos));
282 }
283 }
284
285 void
286 workspace_view::handle_contextmenu_disp (void)
287 {
288 relay_contextmenu_command ("disp");
289 }
290
291 void
292 workspace_view::handle_contextmenu_plot (void)
293 {
294 relay_contextmenu_command("figure;\nplot");
295 }
296
297 void
298 workspace_view::handle_contextmenu_stem (void)
299 {
300 relay_contextmenu_command ("figure;\nstem");
301 }
302
303 void
304 workspace_view::relay_contextmenu_command (const QString& cmdname)
305 {
306 QModelIndex index = view->currentIndex ();
307
308 if (index.parent () != QModelIndex ())
309 {
310 QAbstractItemModel *m = view->model ();
311
312 QMap<int, QVariant> item_data = m->itemData (index);
313
314 QString var_name = item_data[0].toString ();
315
316 emit command_requested (cmdname + "(" + var_name + ")\n");
317 }
318 }