changeset 16521:dbc7018be4be

use context menu for workspace viewer rename and copy to clipboard operations * workspace-model.cc (workspace_model::data): Handle Qt::ToolTipRole for first column. * workspace-view.h, workspace-view.cc (variable_name): Delete class. (workspace_view::var_name_editor): Delete data member and all uses. (workspace_view::contextmenu_requested): New menu items for rename and copy. (workspace_view::handle_contextmenu_rename, workspace_view::handle_contextmenu_copy): New functions.
author John W. Eaton <jwe@octave.org>
date Tue, 16 Apr 2013 01:49:20 -0400
parents 086b5e81245b
children bfff338c56d1
files libgui/src/workspace-model.cc libgui/src/workspace-view.cc libgui/src/workspace-view.h
diffstat 3 files changed, 85 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/workspace-model.cc	Mon Apr 15 21:48:27 2013 +0200
+++ b/libgui/src/workspace-model.cc	Tue Apr 16 01:49:20 2013 -0400
@@ -58,10 +58,12 @@
   Qt::ItemFlags retval = 0;
 
   if (idx.isValid ())
-    retval |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+    {
+      retval |= Qt::ItemIsEnabled;
 
-  if (idx.column () == 0)
-    retval |= Qt::ItemIsEditable;
+      if (_top_level && idx.column () == 0)
+        retval |= Qt::ItemIsSelectable;
+    }
 
   return retval;
 }
@@ -83,12 +85,16 @@
 
   if (idx.isValid ()
       && (role == Qt::DisplayRole
-          || (idx.column () == 0 && role == Qt::EditRole)))
+          || (idx.column () == 0 && (role == Qt::EditRole
+                                     || role == Qt::ToolTipRole))))
     {
       switch (idx.column ())
         {
         case 0:
-          retval = QVariant (_symbols[idx.row()]);
+          if (role == Qt::ToolTipRole)
+            retval = QVariant (tr ("Right click to copy, rename, or display"));
+          else
+            retval = QVariant (_symbols[idx.row()]);
           break;
 
         case 1:
--- a/libgui/src/workspace-view.cc	Mon Apr 15 21:48:27 2013 +0200
+++ b/libgui/src/workspace-view.cc	Tue Apr 16 01:49:20 2013 -0400
@@ -25,6 +25,9 @@
 #include <config.h>
 #endif
 
+#include <QInputDialog>
+#include <QApplication>
+#include <QClipboard>
 #include <QMessageBox>
 #include <QLineEdit>
 #include <QHeaderView>
@@ -36,68 +39,8 @@
 #include "workspace-view.h"
 #include "resource-manager.h"
 
-QWidget *
-variable_name_editor::createEditor (QWidget *p, const QStyleOptionViewItem&,
-                                    const QModelIndex& index) const
-{
-  QWidget *retval = 0;
-
-  const QAbstractItemModel *m = index.model ();
-
-  const workspace_model *wm = static_cast<const workspace_model *> (m);
-
-  if (wm->is_top_level ())
-    retval = new QLineEdit (p);
-  else
-    {
-      QMessageBox *msg_box
-        = new QMessageBox (QMessageBox::Critical,
-                           tr ("Workspace Viewer"),
-                           tr ("Only top-level symbols may be renamed.\n"),
-                           QMessageBox::Ok);
-
-      msg_box->setWindowModality (Qt::NonModal);
-      msg_box->setAttribute (Qt::WA_DeleteOnClose);
-      msg_box->show ();
-    }
-
-  return retval;
-}
-
-void
-variable_name_editor::setEditorData (QWidget *editor,
-                                     const QModelIndex& index) const
-{
-  QLineEdit *line_editor = static_cast<QLineEdit *> (editor);
-
-  const QAbstractItemModel *m = index.model ();
-
-  QVariant d =  m->data (index, Qt::EditRole);
-
-  line_editor->insert (d.toString ());
-}
-
-void
-variable_name_editor::setModelData (QWidget *editor,
-                                    QAbstractItemModel *model,
-                                    const QModelIndex& index) const
-{
-  QLineEdit *line_editor = static_cast<QLineEdit*> (editor);
-
-  model->setData (index, line_editor->text (), Qt::EditRole);
-}
-
-void
-variable_name_editor::updateEditorGeometry (QWidget *editor,
-                                            const QStyleOptionViewItem& option,
-                                            const QModelIndex&) const
-{
-  editor->setGeometry (option.rect);
-}
-
 workspace_view::workspace_view (QWidget *p)
-  : octave_dock_widget (p), view (new QTableView (this)),
-    var_name_editor (new variable_name_editor (this))
+  : octave_dock_widget (p), view (new QTableView (this))
 {
   setObjectName ("WorkspaceView");
   setWindowIcon (QIcon (":/actions/icons/logo.png"));
@@ -126,8 +69,6 @@
   
   view->horizontalHeader ()->restoreState (settings->value ("workspaceview/column_state").toByteArray ());
 
-  view->setItemDelegateForColumn (0, var_name_editor);
-
   // Connect signals and slots.
 
   connect (view, SIGNAL (customContextMenuRequested (const QPoint&)),
@@ -145,8 +86,6 @@
                      view->horizontalHeader ()->saveState ());
 
   settings->sync ();
-
-  delete var_name_editor;
 }
 
 void
@@ -165,7 +104,7 @@
   QAbstractItemModel *m = view->model ();
 
   // if it isnt Local, Glocal etc, allow the ctx menu
-  if (index.isValid())
+  if (index.isValid() && index.column () == 0)
     {
       index = index.sibling (index.row(), 0);
 
@@ -173,6 +112,22 @@
   
       QString var_name = item_data[0].toString ();
 
+      menu.addAction (tr ("Copy"), this,
+                      SLOT (handle_contextmenu_copy ()));
+
+      QAction *rename = menu.addAction (tr ("Rename"), this,
+                                        SLOT (handle_contextmenu_rename ()));
+
+      const workspace_model *wm = static_cast<const workspace_model *> (m);
+
+      if (! wm->is_top_level ())
+        {
+          rename->setDisabled (true);
+          rename->setToolTip (tr ("Only top-level symbols may be renamed."));
+        }
+
+      menu.addSeparator ();
+
       menu.addAction ("disp(" + var_name + ")", this,
                       SLOT (handle_contextmenu_disp ()));
 
@@ -187,6 +142,57 @@
 }
 
 void
+workspace_view::handle_contextmenu_copy (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 ();
+
+      QClipboard *clipboard = QApplication::clipboard ();
+
+      clipboard->setText (var_name);
+    }
+}
+
+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 ();
+
+      QInputDialog* inputDialog = new QInputDialog ();
+
+      inputDialog->setOptions (QInputDialog::NoButtons);
+
+      bool ok = false;
+
+      QString new_name
+        =  inputDialog->getText (0, "Rename Variable", "New name:",
+                                 QLineEdit::Normal, var_name, &ok);
+
+      if (ok && ! new_name.isEmpty ())
+        m->setData (index, new_name, Qt::EditRole);
+    }
+}
+
+void
 workspace_view::handle_contextmenu_disp (void)
 {
   relay_contextmenu_command ("disp"); 
--- a/libgui/src/workspace-view.h	Mon Apr 15 21:48:27 2013 +0200
+++ b/libgui/src/workspace-view.h	Tue Apr 16 01:49:20 2013 -0400
@@ -31,27 +31,6 @@
 #include "octave-dock-widget.h"
 #include "workspace-model.h"
 
-class variable_name_editor : public QItemDelegate
-{
-  Q_OBJECT
-
-public:
-
-  variable_name_editor (QObject *p = 0) : QItemDelegate (p) { }
-
-  QWidget *createEditor (QWidget *p, const QStyleOptionViewItem& option,
-                         const QModelIndex& index) const;
-
-  void setEditorData (QWidget *editor, const QModelIndex& index) const;
-
-  void setModelData (QWidget *editor, QAbstractItemModel *model,
-                     const QModelIndex& index) const;
-
-  void updateEditorGeometry (QWidget *editor,
-                             const QStyleOptionViewItem& option,
-                             const QModelIndex&) const;
-};
-
 class workspace_view : public octave_dock_widget
 {
   Q_OBJECT
@@ -80,6 +59,8 @@
   void contextmenu_requested (const QPoint& pos);
 
   // context menu slots
+  void handle_contextmenu_copy (void);
+  void handle_contextmenu_rename (void);
   void handle_contextmenu_disp (void);
   void handle_contextmenu_plot (void);
   void handle_contextmenu_stem (void);
@@ -89,8 +70,6 @@
   void relay_contextmenu_command (const QString& cmdname);
 
   QTableView *view;
-
-  variable_name_editor *var_name_editor;
 };
 
 #endif