Mercurial > octave
changeset 24758:09364b41ddb6
allow dynamic resizing of display rows and columns in variable editor
* variable-editor-model.h, variable-editor-model.cc
(ve_base_model::maybe_resize_rows, ve_base_model::maybe_resize_columns,
numeric_model::maybe_resize_rows, numeric_model::maybe_resize_columns,
cell_model::maybe_resize_rows, cell_model::maybe_resize_columns,
struct_vector_model::maybe_resize_rows,
struct_model::maybe_resize_rows, struct_model::maybe_resize_columns,
variable_editor_model::change_display_size): New functions.
(variable_editor_model::update_data): Call change_display_size if row
or column size changes.
(variable_editor_model::maybe_resize_rows,
variable_editor_model::maybe_resize_columns): New functions.
* variable-editor.h, variable-editor.cc
(variable_editor_tab::variable_editor_tab): Initialize all data
members.
(variable_editor_tab::keyPressEvent): New function.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 14 Feb 2018 19:02:58 -0500 |
parents | 3897b81979d5 |
children | 6fe8e8a2a8e8 |
files | libgui/src/variable-editor-model.cc libgui/src/variable-editor-model.h libgui/src/variable-editor.cc libgui/src/variable-editor.h |
diffstat | 4 files changed, 170 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/variable-editor-model.cc Wed Feb 14 16:21:53 2018 -0500 +++ b/libgui/src/variable-editor-model.cc Wed Feb 14 19:02:58 2018 -0500 @@ -330,8 +330,8 @@ // FIXME: should fill the window and expand on scrolling or // resizing. - m_display_rows = m_data_rows + 16; - m_display_cols = m_data_cols + 16; + maybe_resize_rows (m_data_rows + 16); + maybe_resize_columns (m_data_cols + 16); } ~numeric_model (void) = default; @@ -342,6 +342,18 @@ numeric_model& operator = (const numeric_model&) = delete; + void maybe_resize_rows (int rows) + { + if (rows > m_display_rows) + m_display_rows = rows; + } + + void maybe_resize_columns (int cols) + { + if (cols > m_display_cols) + m_display_cols = cols; + } + QVariant edit_display (const QModelIndex& idx, int role) const { int row; @@ -419,8 +431,8 @@ // FIXME: should fill the window and expand on scrolling or // resizing. - m_display_rows = m_data_rows + 16; - m_display_cols = m_data_cols + 16; + maybe_resize_rows (m_data_rows + 16); + maybe_resize_columns (m_data_cols + 16); } ~cell_model (void) = default; @@ -431,6 +443,18 @@ cell_model& operator = (const cell_model&) = delete; + void maybe_resize_rows (int rows) + { + if (rows > m_display_rows) + m_display_rows = rows; + } + + void maybe_resize_columns (int cols) + { + if (cols > m_display_cols) + m_display_cols = cols; + } + QVariant edit_display (const QModelIndex& idx, int role) const { int row; @@ -682,8 +706,7 @@ m_data_rows = val.numel (); m_data_cols = val.nfields (); - m_display_rows = m_data_rows + 16; - m_display_cols = m_data_cols; + maybe_resize_rows (m_data_rows + 16); } ~vector_struct_model (void) = default; @@ -694,6 +717,12 @@ vector_struct_model& operator = (const vector_struct_model&) = delete; + void maybe_resize_rows (int rows) + { + if (rows > m_display_rows) + m_display_rows = rows; + } + QVariant edit_display (const QModelIndex& idx, int role) const { int row; @@ -809,8 +838,8 @@ // FIXME: should fill the window and expand on scrolling or // resizing. - m_display_rows = m_data_rows + 16; - m_display_cols = m_data_cols + 16; + maybe_resize_rows (m_data_rows + 16); + maybe_resize_columns (m_data_cols + 16); } ~struct_model (void) = default; @@ -821,6 +850,18 @@ struct_model& operator = (const struct_model&) = delete; + void maybe_resize_rows (int rows) + { + if (rows > m_display_rows) + m_display_rows = rows; + } + + void maybe_resize_columns (int cols) + { + if (cols > m_display_cols) + m_display_cols = cols; + } + QVariant edit_display (const QModelIndex& idx, int) const { int row; @@ -1218,40 +1259,81 @@ // Add or remove rows and columns when the size changes. - int old_display_rows = display_rows (); - int old_display_cols = display_columns (); + int old_rows = display_rows (); + int old_cols = display_columns (); reset (val); - int new_display_rows = display_rows (); - int new_display_cols = display_columns (); + int new_rows = display_rows (); + int new_cols = display_columns (); + + if (new_rows != old_rows || new_cols != old_cols) + change_display_size (old_rows, old_cols, new_rows, new_cols); + + // Even if the size doesn't change, we still need to update here + // because the data may have changed. - if (new_display_rows < old_display_rows) + emit dataChanged (QAbstractTableModel::index (0, 0), + QAbstractTableModel::index (new_rows-1, new_cols-1)); + + clear_update_pending (); +} + +void +variable_editor_model::change_display_size (int old_rows, int old_cols, + int new_rows, int new_cols) +{ + if (new_rows < old_rows) { - beginRemoveRows (QModelIndex (), new_display_rows, old_display_rows-1); + beginRemoveRows (QModelIndex (), new_rows, old_rows-1); endRemoveRows (); } - else if (new_display_rows > old_display_rows) + else if (new_rows > old_rows) { - beginInsertRows (QModelIndex (), old_display_rows, new_display_rows-1); + beginInsertRows (QModelIndex (), old_rows, new_rows-1); endInsertRows (); } - if (new_display_cols < old_display_cols) + if (new_cols < old_cols) { - beginRemoveColumns (QModelIndex (), new_display_cols, old_display_cols-1); + beginRemoveColumns (QModelIndex (), new_cols, old_cols-1); endRemoveColumns (); } - else if (new_display_cols > old_display_cols) + else if (new_cols > old_cols) { - beginInsertColumns (QModelIndex (), old_display_cols, new_display_cols-1); + beginInsertColumns (QModelIndex (), old_cols, new_cols-1); endInsertColumns (); } +} - clear_update_pending (); +void +variable_editor_model::maybe_resize_rows (int rows) +{ + int old_rows = display_rows (); + int old_cols = display_columns (); + + rep->maybe_resize_rows (rows); + + int new_rows = display_rows (); + int new_cols = display_columns (); - emit dataChanged (QAbstractTableModel::index (0, 0), - QAbstractTableModel::index (new_display_rows-1, new_display_cols-1)); + if (new_rows != old_rows) + change_display_size (old_rows, old_cols, new_rows, new_cols); +} + +void +variable_editor_model::maybe_resize_columns (int cols) +{ + int old_rows = display_rows (); + int old_cols = display_columns (); + + rep->maybe_resize_columns (cols); + + int new_rows = display_rows (); + int new_cols = display_columns (); + + if (new_cols != old_cols) + change_display_size (old_rows, old_cols, new_rows, new_cols); } void
--- a/libgui/src/variable-editor-model.h Wed Feb 14 16:21:53 2018 -0500 +++ b/libgui/src/variable-editor-model.h Wed Feb 14 19:02:58 2018 -0500 @@ -47,6 +47,10 @@ base_ve_model& operator = (const base_ve_model&) = delete; + virtual void maybe_resize_rows (int) { } + + virtual void maybe_resize_columns (int) { } + std::string name (void) const; bool index_ok (const QModelIndex& idx, int& row, int& col) const; @@ -249,7 +253,21 @@ return rep->subscript_expression (idx); } -signals: // private + int display_rows (void) const + { + return rep->display_rows (); + } + + int display_columns (void) const + { + return rep->display_columns (); + } + + void maybe_resize_rows (int rows); + + void maybe_resize_columns (int cols); + +signals: void update_data_signal (const octave_value& val); @@ -299,15 +317,8 @@ return rep->data_columns (); } - int display_rows (void) const - { - return rep->display_rows (); - } - - int display_columns (void) const - { - return rep->display_columns (); - } + void change_display_size (int old_rows, int old_cols, + int new_rows, int new_cols); QString make_description_text (void) const {
--- a/libgui/src/variable-editor.cc Wed Feb 14 16:21:53 2018 -0500 +++ b/libgui/src/variable-editor.cc Wed Feb 14 19:02:58 2018 -0500 @@ -85,7 +85,6 @@ m_disp_view_idx = m_widget_stack->addWidget (disp_view); } - bool var_editor_tab::has_focus (void) const { @@ -97,6 +96,46 @@ } void +var_editor_tab::keyPressEvent (QKeyEvent *event) +{ + QTableView *edit_view = get_edit_view (); + + if (edit_view) + { + int key = event->key (); + + if (key == Qt::Key_Right || key == Qt::Key_Tab) + { + QModelIndex idx = edit_view->currentIndex (); + + int curr_row = idx.row (); + int next_col = idx.column () + 1; + + if (next_col == m_model->display_columns ()) + { + m_model->maybe_resize_columns (next_col + 16); + + edit_view->setCurrentIndex (m_model->index (curr_row, next_col)); + } + } + else if (key == Qt::Key_Down || key == Qt::Key_PageDown) + { + QModelIndex idx = edit_view->currentIndex (); + + int next_row = idx.row () + 1; + int curr_col = idx.column (); + + if (next_row == m_model->display_rows ()) + { + m_model->maybe_resize_rows (next_row + 16); + + edit_view->setCurrentIndex (m_model->index (next_row, curr_col)); + } + } + } +} + +void var_editor_tab::set_editable (bool editable) { int idx = (editable ? m_edit_view_idx : m_disp_view_idx);
--- a/libgui/src/variable-editor.h Wed Feb 14 16:21:53 2018 -0500 +++ b/libgui/src/variable-editor.h Wed Feb 14 19:02:58 2018 -0500 @@ -49,7 +49,8 @@ public: var_editor_tab (QStackedWidget *widget_stack, QWidget *p = nullptr) - : QWidget (p), m_widget_stack (widget_stack) + : QWidget (p), m_model (nullptr), m_widget_stack (widget_stack), + m_edit_view_idx (-1), m_disp_view_idx (-1) { } ~var_editor_tab (void) = default; @@ -73,6 +74,8 @@ bool has_focus (void) const; + void keyPressEvent (QKeyEvent *event); + public slots: void set_editable (bool);