changeset 19467:2f0c21339e9d gui-release

values of variables in workspace view copyable (bug #43674) * workspace-view.cc (get_var_name): new function returning the name of the variable related to a specific index of the workspace view; (handle_contextmenu_copy, handle_contextmenu_rename, relay_contextmenu_command): using the new function get_var_name; (contextmenu_requested): new menu entry for copying the value, using get_var_name; (handle_contextmenu_copy_value): new action slot for copying the value * workspace-view.h: new functions get_var_name and handle_contextmenu_copy_value
author Torsten <ttl@justmail.de>
date Sat, 27 Dec 2014 19:32:42 +0100
parents 0f5670b40d94
children d57edbd761aa
files libgui/src/workspace-view.cc libgui/src/workspace-view.h
diffstat 2 files changed, 44 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/workspace-view.cc	Sat Dec 27 13:13:48 2014 +0100
+++ b/libgui/src/workspace-view.cc	Sat Dec 27 19:32:42 2014 +0100
@@ -38,6 +38,7 @@
 
 #include "workspace-view.h"
 #include "resource-manager.h"
+#include "symtab.h"
 
 workspace_view::workspace_view (QWidget *p)
   : octave_dock_widget (p), view (new QTableView (this))
@@ -106,29 +107,38 @@
   QDockWidget::closeEvent (e);
 }
 
+QString
+workspace_view::get_var_name (QModelIndex index)
+{
+  index = index.sibling (index.row (), 0);
+  QAbstractItemModel *m = view->model ();
+  QMap<int, QVariant> item_data = m->itemData (index);
+
+  return item_data[0].toString ();
+}
+
 void
 workspace_view::contextmenu_requested (const QPoint& qpos)
 {
   QMenu menu (this);
 
   QModelIndex index = view->indexAt (qpos);
-  QAbstractItemModel *m = view->model ();
 
   // if it isnt Local, Glocal etc, allow the ctx menu
   if (index.isValid () && index.column () == 0)
     {
-      index = index.sibling (index.row (), 0);
-
-      QMap<int, QVariant> item_data = m->itemData (index);
+      QString var_name = get_var_name (index);
 
-      QString var_name = item_data[0].toString ();
+      menu.addAction (tr ("Copy name"), this,
+                      SLOT (handle_contextmenu_copy ()));
 
-      menu.addAction (tr ("Copy"), this,
-                      SLOT (handle_contextmenu_copy ()));
+      menu.addAction (tr ("Copy value"), this,
+                      SLOT (handle_contextmenu_copy_value ()));
 
       QAction *rename = menu.addAction (tr ("Rename"), this,
                                         SLOT (handle_contextmenu_rename ()));
 
+      QAbstractItemModel *m = view->model ();
       const workspace_model *wm = static_cast<const workspace_model *> (m);
 
       if (! wm->is_top_level ())
@@ -159,13 +169,7 @@
 
   if (index.isValid ())
     {
-      index = index.sibling (index.row (), 0);
-
-      QAbstractItemModel *m = view->model ();
-
-      QMap<int, QVariant> item_data = m->itemData (index);
-
-      QString var_name = item_data[0].toString ();
+      QString var_name = get_var_name (index);
 
       QClipboard *clipboard = QApplication::clipboard ();
 
@@ -174,19 +178,31 @@
 }
 
 void
+workspace_view::handle_contextmenu_copy_value (void)
+{
+  QModelIndex index = view->currentIndex ();
+
+  if (index.isValid ())
+    {
+      QString var_name = get_var_name (index);
+
+      octave_value val = symbol_table::varval (var_name.toStdString ());
+      std::ostringstream buf;
+      val.print_raw (buf, true);
+
+      QClipboard *clipboard = QApplication::clipboard ();
+      clipboard->setText (QString::fromStdString (buf.str ()));
+    }
+}
+
+void
 workspace_view::handle_contextmenu_rename (void)
 {
   QModelIndex index = view->currentIndex ();
 
   if (index.isValid ())
     {
-      index = index.sibling (index.row (), 0);
-
-      QAbstractItemModel *m = view->model ();
-
-      QMap<int, QVariant> item_data = m->itemData (index);
-
-      QString var_name = item_data[0].toString ();
+      QString var_name = get_var_name (index);
 
       QInputDialog* inputDialog = new QInputDialog ();
 
@@ -199,7 +215,10 @@
                                  QLineEdit::Normal, var_name, &ok);
 
       if (ok && ! new_name.isEmpty ())
-        m->setData (index, new_name, Qt::EditRole);
+        {
+          QAbstractItemModel *m = view->model ();
+          m->setData (index, new_name, Qt::EditRole);
+        }
     }
 }
 
@@ -228,13 +247,7 @@
 
   if (index.isValid ())
     {
-      index = index.sibling (index.row (), 0);
-
-      QAbstractItemModel *m = view->model ();
-
-      QMap<int, QVariant> item_data = m->itemData (index);
-
-      QString var_name = item_data[0].toString ();
+      QString var_name = get_var_name (index);
 
       emit command_requested (cmdname + " (" + var_name + ");");
     }
--- a/libgui/src/workspace-view.h	Sat Dec 27 13:13:48 2014 +0100
+++ b/libgui/src/workspace-view.h	Sat Dec 27 19:32:42 2014 +0100
@@ -62,6 +62,7 @@
 
   // context menu slots
   void handle_contextmenu_copy (void);
+  void handle_contextmenu_copy_value (void);
   void handle_contextmenu_rename (void);
   void handle_contextmenu_disp (void);
   void handle_contextmenu_plot (void);
@@ -76,6 +77,7 @@
 
   void relay_contextmenu_command (const QString& cmdname);
 
+  QString get_var_name (QModelIndex index);
   QTableView *view;
   int view_previous_row_count;
   workspace_model *_model;