# HG changeset patch # User John Donoghue # Date 1365364091 14400 # Node ID 8c666c7b0e5d9825a11343bb2ee9c20fb5773448 # Parent 094bd3627eadef2a2e07413b0c8426e00ac9b84e 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. diff -r 094bd3627ead -r 8c666c7b0e5d libgui/src/workspace-view.cc --- a/libgui/src/workspace-view.cc Sun Apr 07 12:36:07 2013 -0400 +++ b/libgui/src/workspace-view.cc Sun Apr 07 15:48:11 2013 -0400 @@ -30,6 +30,7 @@ #include #include #include +#include workspace_view::workspace_view (QWidget *p) : QDockWidget (p) @@ -45,6 +46,7 @@ view->setAnimated (false); // Deactivate animations because of strange glitches. view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells. view->setWordWrap (false); // No wordwrapping in cells. + view->setContextMenuPolicy (Qt::CustomContextMenu); // Set an empty widget, so we can assign a layout to it. setWidget (new QWidget (this)); @@ -81,6 +83,12 @@ connect (view, SIGNAL (doubleClicked (QModelIndex)), this, SLOT (item_double_clicked (QModelIndex))); + connect (view, SIGNAL (customContextMenuRequested(const QPoint&)), + this, SLOT(contextmenu_requested (const QPoint&))); + + connect (this, SIGNAL (command_requested (const QString&)), + p, SLOT (handle_command_double_clicked(const QString&))); + // topLevelChanged is emitted when floating property changes (floating = true) connect (this, SIGNAL (topLevelChanged(bool)), this, SLOT(top_level_changed(bool))); @@ -245,3 +253,66 @@ if (visible && ! isFloating ()) focus (); } + +void +workspace_view::contextmenu_requested (const QPoint& pos) +{ + QMenu menu (this); + + QModelIndex index = view->indexAt (pos); + QAbstractItemModel *m = view->model (); + + // if it isnt Local, Glocal etc, allow the ctx menu + if (index.parent() != QModelIndex()) + { + QMap item_data = m->itemData (index); + + QString var_name = item_data[0].toString (); + + menu.addAction ("disp(" + var_name + ")", this, + SLOT (handle_contextmenu_disp ())); + + menu.addAction ("plot(" + var_name + ")", this, + SLOT (handle_contextmenu_plot ())); + + menu.addAction ("stem(" + var_name + ")", this, + SLOT (handle_contextmenu_stem ())); + + menu.exec (view->mapToGlobal (pos)); + } +} + +void +workspace_view::handle_contextmenu_disp (void) +{ + relay_contextmenu_command ("disp"); +} + +void +workspace_view::handle_contextmenu_plot (void) +{ + relay_contextmenu_command("figure;\nplot"); +} + +void +workspace_view::handle_contextmenu_stem (void) +{ + relay_contextmenu_command ("figure;\nstem"); +} + +void +workspace_view::relay_contextmenu_command (const QString& cmdname) +{ + QModelIndex index = view->currentIndex (); + + if (index.parent () != QModelIndex ()) + { + QAbstractItemModel *m = view->model (); + + QMap item_data = m->itemData (index); + + QString var_name = item_data[0].toString (); + + emit command_requested (cmdname + "(" + var_name + ")\n"); + } +} diff -r 094bd3627ead -r 8c666c7b0e5d libgui/src/workspace-view.h --- a/libgui/src/workspace-view.h Sun Apr 07 12:36:07 2013 -0400 +++ b/libgui/src/workspace-view.h Sun Apr 07 15:48:11 2013 -0400 @@ -59,6 +59,8 @@ signals: /** Custom signal that tells if a user has clicke away that dock widget. */ void active_changed (bool active); + /** signal that user had requested a command on a variable */ + void command_requested (const QString& cmd); protected: void closeEvent (QCloseEvent *event); @@ -67,8 +69,14 @@ void collapse_requested (QModelIndex index); void expand_requested (QModelIndex index); void item_double_clicked (QModelIndex index); + void contextmenu_requested (const QPoint& pos); + // context menu slots + void handle_contextmenu_disp (); + void handle_contextmenu_plot (); + void handle_contextmenu_stem (); +private: + void relay_contextmenu_command (const QString& cmdname); -private: QTreeView *view; struct