# HG changeset patch # User Torsten Lilge # Date 1591120959 -7200 # Node ID b8d9dff6218c26ed39ebcae20f0fb62c4d5c4c6f # Parent d34558c873dc027576d4b3bb5ab3d7c88ed90a80 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; diff -r d34558c873dc -r b8d9dff6218c libgui/src/variable-editor-model.h --- a/libgui/src/variable-editor-model.h Fri Nov 27 11:16:43 2020 +0100 +++ 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); diff -r d34558c873dc -r b8d9dff6218c libgui/src/variable-editor.cc --- a/libgui/src/variable-editor.cc Fri Nov 27 11:16:43 2020 +0100 +++ b/libgui/src/variable-editor.cc Tue Jun 02 20:02:39 2020 +0200 @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -63,14 +62,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) { @@ -80,11 +71,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; } @@ -567,30 +555,39 @@ return range; } - QString - variable_editor_view::selected_to_octave (void) - { - QList 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 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 @@ -765,7 +762,7 @@ { // FIXME: Create unnamed1..n if exist ('unnamed', 'var') is true. - selected_command_requested ("unnamed = %1"); + selected_command_requested ("create"); } void @@ -1064,6 +1061,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) { @@ -1244,6 +1242,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 ()), @@ -1254,8 +1255,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&))); @@ -1523,12 +1522,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) @@ -1647,10 +1640,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); diff -r d34558c873dc -r b8d9dff6218c libgui/src/variable-editor.h --- a/libgui/src/variable-editor.h Fri Nov 27 11:16:43 2020 +0100 +++ 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 +#include #include #include @@ -207,9 +208,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: @@ -321,8 +319,6 @@ void delete_selected_signal (void); - void selected_command_signal (const QString& cmd); - public slots: void callUpdate (const QModelIndex&, const QModelIndex&); @@ -353,10 +349,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); @@ -401,6 +393,8 @@ QString m_hovered_focus_vname; + QSignalMapper *m_plot_mapper; + QWidget *m_focus_widget; variable_dock_widget *m_focus_widget_vdw;