changeset 28397:37c5532e558c

check object size before plotting from variiable editor (bug #56685) * variable-editor-model.h: make data_rows and data_columns public * variable-editor.cc: do not include QSignalMapper here; (idx_to_expr): remove obsolete static function; (make_plot_mapper): only add the plot command to the mapper; (selected_to_octave): remove obsolete function; (selected_command_requested): get selected range and the object size from the model for comparing object size and selection, generate command string and emit signal for executing it in the console; (createVariable): only pass keywoard for variable creation instead of complete complete, which is now generate in selected_command_requested; (variable_editor): initialize new class variable for the edtiors plot signal mapper; (edit_variable): connect signal mapper to selected_command_requested and remove connection from previously used relay signal; (relay_selected_command): removed, direct connection is used; (construct_tool_bar): create plot mapper and store it for later connecting its signal, remove connection here * variable-editor.h: include QSignalMapper here; removed selected_to_octave; removed selected_command_signal; removed relay_selected_command; added class variable m_plot_mapper;
author Torsten Lilge <ttl-octave@mailbox.org>
date Tue, 02 Jun 2020 20:02:39 +0200
parents 4ea54a38c500
children 287ffa8cfeae
files libgui/src/variable-editor-model.h libgui/src/variable-editor.cc libgui/src/variable-editor.h
diffstat 3 files changed, 48 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/variable-editor-model.h	Tue Jun 02 14:57:56 2020 +0200
+++ b/libgui/src/variable-editor-model.h	Tue Jun 02 20:02:39 2020 +0200
@@ -263,11 +263,21 @@
       return rep->display_rows ();
     }
 
+    octave_idx_type data_rows (void) const
+    {
+      return rep->data_rows ();
+    }
+
     int display_columns (void) const
     {
       return rep->display_columns ();
     }
 
+    octave_idx_type data_columns (void) const
+    {
+      return rep->data_columns ();
+    }
+
     void maybe_resize_rows (int rows);
 
     void maybe_resize_columns (int cols);
@@ -318,16 +328,6 @@
       return rep->is_valid ();
     }
 
-    octave_idx_type data_rows (void) const
-    {
-      return rep->data_rows ();
-    }
-
-    octave_idx_type data_columns (void) const
-    {
-      return rep->data_columns ();
-    }
-
     void change_display_size (int old_rows, int old_cols,
                               int new_rows, int new_cols);
 
--- a/libgui/src/variable-editor.cc	Tue Jun 02 14:57:56 2020 +0200
+++ b/libgui/src/variable-editor.cc	Tue Jun 02 20:02:39 2020 +0200
@@ -39,7 +39,6 @@
 #include <QPalette>
 #include <QScreen>
 #include <QScrollBar>
-#include <QSignalMapper>
 #include <QStackedWidget>
 #include <QTabWidget>
 #include <QTableView>
@@ -61,14 +60,6 @@
 {
   // Code reuse functions
 
-  static QString
-  idx_to_expr (int32_t from, int32_t to)
-  {
-    return (from == to
-            ? QString ("%1").arg (from)
-            : QString ("%1:%2").arg (from).arg (to));
-  }
-
   static QSignalMapper *
   make_plot_mapper (QMenu *menu)
   {
@@ -78,11 +69,8 @@
     QSignalMapper *plot_mapper = new QSignalMapper (menu);
 
     for (int i = 0; i < list.size(); ++i)
-      {
-        plot_mapper->setMapping
-          (menu->addAction (list.at (i), plot_mapper, SLOT (map ())),
-           "figure (); " + list.at (i) + " (%1); title (\"%1\");");
-      }
+      plot_mapper->setMapping
+        (menu->addAction (list.at (i), plot_mapper, SLOT (map ())), list.at (i));
 
     return plot_mapper;
   }
@@ -550,30 +538,39 @@
     return range;
   }
 
-  QString
-  variable_editor_view::selected_to_octave (void)
-  {
-    QList<int> range = range_selected ();
-    if (range.isEmpty ())
-      return objectName ();
-
-    QString rows = idx_to_expr (range.at (0), range.at (1));
-    QString cols = idx_to_expr (range.at (2), range.at (3));
-
-    // FIXME: Does cell need separate handling?  Maybe use '{.,.}'?
-
-    return QString ("%1(%2, %3)").arg (objectName ()).arg (rows).arg (cols);
-  }
-
   void
   variable_editor_view::selected_command_requested (const QString& cmd)
   {
     if (! hasFocus ())
       return;
 
-    QString selarg = selected_to_octave ();
-    if (! selarg.isEmpty ())
-      emit command_signal (cmd.arg (selarg));
+    QList<int> range = range_selected ();
+    if (range.isEmpty ())
+      return;
+
+    int s1 = m_var_model->data_rows ();
+    int s2 = m_var_model->data_columns ();
+    if (s1 < range.at (0) || s2 < range.at (2))
+      return; // Selected range does not contain data
+
+    s1 = std::min (s1, range.at (1));
+    s2 = std::min (s2, range.at (3));
+
+    // Variable with desired range as string
+    QString variable = QString ("%1(%2:%3,%4:%5)")
+                                .arg (objectName ())
+                                .arg (range.at (0)).arg (s1)
+                                .arg (range.at (2)).arg (s2);
+
+    // Desired command as string
+    QString command;
+    if (cmd == "create")
+      command = QString ("unnamed = %1;").arg (variable);
+    else
+      command = QString ("figure (); %1 (%2); title ('%2');")
+                          .arg (cmd).arg (variable);
+
+    emit command_signal (command);
   }
 
   void
@@ -748,7 +745,7 @@
   {
     // FIXME: Create unnamed1..n if exist ('unnamed', 'var') is true.
 
-    selected_command_requested ("unnamed = %1");
+    selected_command_requested ("create");
   }
 
   void
@@ -1047,6 +1044,7 @@
       m_table_colors (),
       m_current_focus_vname (""),
       m_hovered_focus_vname (""),
+      m_plot_mapper (nullptr),
       m_focus_widget (nullptr),
       m_focus_widget_vdw (nullptr)
   {
@@ -1227,6 +1225,9 @@
     edit_view->verticalHeader ()->setDefaultSectionSize (m_default_height
                                                          + m_add_font_height);
 
+    connect (m_plot_mapper, SIGNAL (mapped (const QString&)),
+             edit_view, SLOT (selected_command_requested (const QString&)));
+
     connect (edit_view, SIGNAL (command_signal (const QString&)),
              this, SIGNAL (command_signal (const QString&)));
     connect (this, SIGNAL (delete_selected_signal ()),
@@ -1237,8 +1238,6 @@
              edit_view, SLOT (copyClipboard ()));
     connect (this, SIGNAL (paste_clipboard_signal ()),
              edit_view, SLOT (pasteClipboard ()));
-    connect (this, SIGNAL (selected_command_signal (const QString&)),
-             edit_view, SLOT (selected_command_requested (const QString&)));
     connect (edit_view->horizontalHeader (),
              SIGNAL (customContextMenuRequested (const QPoint&)),
              edit_view, SLOT (createColumnMenu (const QPoint&)));
@@ -1506,12 +1505,6 @@
     emit level_up_signal ();
   }
 
-  void
-  variable_editor::relay_selected_command (const QString& cmd)
-  {
-    emit selected_command_signal (cmd);
-  }
-
   // Also updates the font.
 
   void variable_editor::update_colors (void)
@@ -1630,10 +1623,7 @@
     plot_menu->setTitle (tr ("Plot"));
     plot_menu->setSeparatorsCollapsible (false);
 
-    QSignalMapper *plot_mapper = make_plot_mapper (plot_menu);
-
-    connect (plot_mapper, SIGNAL (mapped (const QString&)),
-             this, SLOT (relay_selected_command (const QString&)));
+    m_plot_mapper = make_plot_mapper (plot_menu);
 
     plot_tool_button->setMenu (plot_menu);
 
--- a/libgui/src/variable-editor.h	Tue Jun 02 14:57:56 2020 +0200
+++ b/libgui/src/variable-editor.h	Tue Jun 02 20:02:39 2020 +0200
@@ -27,6 +27,7 @@
 #define octave_variable_editor_h 1
 
 #include <QHeaderView>
+#include <QSignalMapper>
 #include <QStackedWidget>
 #include <QTableView>
 
@@ -203,9 +204,6 @@
 
     void createRowMenu (const QPoint& pt);
 
-    // Convert selection to an Octave expression.
-    QString selected_to_octave (void);
-
     void selected_command_requested (const QString& cmd);
 
   private:
@@ -317,8 +315,6 @@
 
     void delete_selected_signal (void);
 
-    void selected_command_signal (const QString& cmd);
-
   public slots:
 
     void callUpdate (const QModelIndex&, const QModelIndex&);
@@ -349,10 +345,6 @@
 
     void levelUp (void);
 
-    // Send command to Octave interpreter.
-    // %1 in CMD is replaced with the value of selected_to_octave.
-    void relay_selected_command (const QString& cmd);
-
   protected:
 
     void focusInEvent (QFocusEvent *ev);
@@ -397,6 +389,8 @@
 
     QString m_hovered_focus_vname;
 
+    QSignalMapper *m_plot_mapper;
+
     QWidget *m_focus_widget;
 
     variable_dock_widget *m_focus_widget_vdw;