# HG changeset patch
# User John Donoghue <john.donoghue@ieee.org>
# Date 1365362256 14400
# Node ID f65a8e3fd6aff81d9a7955054c552a77cbb4bf36
# Parent  094bd3627eadef2a2e07413b0c8426e00ac9b84e
Added context menu to workspace variable display

* libgui/src/workspace-view.h, libgui/src/workspace-view.cc
  (workspace_view::workspace_view): Added setup for context menu usage.
  (workspace_view::contextmenu_requested): New slot function.
  (workspace_view::handle_contextmenu_disp): New slot functon.
  (workspace_view::handle_contextmenu_plot): New slot function.
  (workspace_view::handle_contextmenu_stem): New slot function.
  (workspace_view::relay_contextmenu_command): New function.
  (workspace_view::command_requested): New signal

diff -r 094bd3627ead -r f65a8e3fd6af 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:17:36 2013 -0400
@@ -30,6 +30,7 @@
 #include <QHBoxLayout>
 #include <QVBoxLayout>
 #include <QPushButton>
+#include <QMenu>
 
 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,59 @@
   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<int, QVariant> 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()
+{
+  relay_contextmenu_command("disp"); 
+}
+
+void
+workspace_view::handle_contextmenu_plot()
+{
+  relay_contextmenu_command("figure;\nplot"); 
+}
+
+void
+workspace_view::handle_contextmenu_stem()
+{
+  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<int, QVariant> item_data = m->itemData (index);
+  
+    QString var_name = item_data[0].toString();
+
+    emit command_requested ( cmdname + "(" + var_name + ")\n" );
+  }
+}
diff -r 094bd3627ead -r f65a8e3fd6af 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:17:36 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
