Mercurial > octave
changeset 24585:8a4aedbb3e5a
style fixes for variable editor
* variable-editor-model.h, variable-editor-model.cc,
variable-editor.h, variable-editor.cc: Style fixes.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 12 Dec 2017 17:30:50 -0500 |
parents | 7a18e02a516e |
children | c24b536df5d4 |
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, 452 insertions(+), 298 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/variable-editor-model.cc Wed Jan 10 16:09:53 2018 -0800 +++ b/libgui/src/variable-editor-model.cc Tue Dec 12 17:30:50 2017 -0500 @@ -1,8 +1,8 @@ /* +Copyright (C) 2013-2017 John W. Eaton Copyright (C) 2015 Michael Barnes Copyright (C) 2013 Rüdiger Sonderfeld -Copyright (C) 2013 John W. Eaton This file is part of Octave. @@ -41,7 +41,8 @@ #include "parse.h" #include "variables.h" -/// Pimpl/Dpointer for variable_editor_model. +// Pimpl/Dpointer for variable_editor_model. + struct variable_editor_model::impl { struct cell @@ -54,7 +55,7 @@ unset }; - cell () + cell (void) : m_state (unset) { } @@ -69,10 +70,15 @@ { } state_t m_state; + QVariant m_data; + QVariant m_status_tip; + QVariant m_tool_tip; + QVariant m_background; + bool m_requires_sub_editor; sub_editor_types m_editor_type; @@ -94,20 +100,20 @@ bool is_set (const QModelIndex& idx) const { - return idx.isValid () - && m_table[model_to_index (idx)].m_state == cell::avail; + return (idx.isValid () + && m_table[model_to_index (idx)].m_state == cell::avail); } bool is_notavail (const QModelIndex& idx) const { - return idx.isValid () - && m_table[model_to_index (idx)].m_state == cell::notavail; + return (idx.isValid () + && m_table[model_to_index (idx)].m_state == cell::notavail); } bool is_pending (const QModelIndex& idx) const { - return idx.isValid () - && m_table[model_to_index (idx)].m_state == cell::pending; + return (idx.isValid () + && m_table[model_to_index (idx)].m_state == cell::pending); } void pending (const QModelIndex& idx) @@ -124,13 +130,14 @@ bool requires_sub_editor (const QModelIndex& idx) { - return idx.isValid () - && m_table[model_to_index (idx)].m_requires_sub_editor; + return (idx.isValid () + && m_table[model_to_index (idx)].m_requires_sub_editor); } sub_editor_types sub_editor_type (const QModelIndex& idx) { - return idx.isValid () ? m_table[model_to_index (idx)].m_editor_type : sub_none; + return (idx.isValid () + ? m_table[model_to_index (idx)].m_editor_type : sub_none); } void unset (int r, int c) @@ -150,19 +157,24 @@ if (idx.isValid ()) { const int i = model_to_index (idx); + switch (role) { case Qt::DisplayRole: case Qt::EditRole: return m_table[i].m_data; + case Qt::StatusTipRole: return m_table[i].m_status_tip; + case Qt::ToolTipRole: return m_table[i].m_tool_tip; + case Qt::BackgroundRole: return m_table[i].m_background; } } + return QVariant (); } @@ -182,12 +194,19 @@ { } const std::string m_name; + std::string m_type; + octave_idx_type m_rows; + octave_idx_type m_cols; + QVector<cell> m_table; + QLabel *m_label; + bool m_validity; + QString m_validtext; }; @@ -202,12 +221,16 @@ this, SLOT (received_data (int, int, const QString&, const QString&, int, int))); + connect (this, SIGNAL (no_data (int, int)), this, SLOT (received_no_data (int, int))); + connect (this, SIGNAL (unset_data (int, int)), this, SLOT (received_unset_data (int, int))); + connect (this, SIGNAL (user_error (const QString&, const QString&)), this, SLOT (received_user_error (const QString&, const QString&))); + connect (this, SIGNAL (initialize_data (const QString&, const QString&, int, int)), this, SLOT (received_initialize_data (const QString&, @@ -253,28 +276,19 @@ QString variable_editor_model::parens (void) const { - if (m_d->m_type == "{") - return "{%1, %2}"; - else - return "(%1, %2)"; + return m_d->m_type == "{" ? "{%1, %2}" : "(%1, %2)"; } int variable_editor_model::rowCount (const QModelIndex&) const { - if (m_d->m_validity) - return m_d->rows (); - - return 1; + return m_d->m_validity ? m_d->rows () : 1; } int variable_editor_model::columnCount (const QModelIndex&) const { - if (m_d->m_validity) - return m_d->columns (); - - return 1; + return m_d->m_validity ? m_d->columns () : 1; } QVariant @@ -288,6 +302,7 @@ return QVariant (QString ("Variable %d not found") .arg (QString::fromStdString (m_d->m_name))); } + return QVariant (QString ("x")); } @@ -304,8 +319,10 @@ (const_cast<variable_editor_model *> (this), &variable_editor_model::get_data_oct, idx.row (), idx.column (), m_d->m_name); + m_d->pending (idx); } + if (role == Qt::DisplayRole) return QVariant (QString (m_d->is_notavail (idx) ? "⌛" : "✗")); else @@ -313,7 +330,8 @@ } } - return QVariant (); // invalid + // Invalid. + return QVariant (); } bool @@ -327,11 +345,13 @@ qDebug () << v.typeName () << " Expected String!"; return false; } + octave_link::post_event<variable_editor_model, std::string, int, int, std::string> (this, &variable_editor_model::set_data_oct, m_d->m_name, idx.row (), idx.column (), v.toString ().toStdString ()); + return true; } else @@ -419,14 +439,17 @@ if (editor_type (idx) != sub_string) return QAbstractTableModel::flags (idx); } + return QAbstractTableModel::flags (idx) | Qt::ItemIsEditable; - //return requires_sub_editor(idx) ? QAbstractTableModel::flags (idx) : QAbstractTableModel::flags (idx) | Qt::ItemIsEditable; + + // FIXME: ??? + // return requires_sub_editor(idx) ? QAbstractTableModel::flags (idx) : QAbstractTableModel::flags (idx) | Qt::ItemIsEditable; } return Qt::NoItemFlags; } -// private slots +// Private slots. void variable_editor_model::received_data (int r, int c, @@ -434,15 +457,18 @@ const QString& class_info, int rows, int cols) { - // trim data + // Trim data. + const QString status_tip; - const QString tool_tip = class_info + - QString (": %1x%2").arg (rows).arg (cols); + + const QString tool_tip + = class_info + QString (": %1x%2").arg (rows).arg (cols); bool subedit = rows != 1 || cols != 1 || class_info == QString ("struct"); sub_editor_types edittype; - if (!subedit) + + if (! subedit) edittype = sub_none; else { @@ -451,14 +477,13 @@ else edittype = sub_matrix; } + if (class_info == QString ("struct")) edittype = sub_struct; - - m_d->set (r, c, impl::cell (dat, status_tip, tool_tip, - rows > 1 || cols > 1 - || class_info == QString ("struct"), + (rows > 1 || cols > 1 + || class_info == QString ("struct")), edittype)); QModelIndex idx = QAbstractTableModel::index (r, c); @@ -482,7 +507,7 @@ variable_editor_model::received_user_error (const QString& title, const QString& msg) { - QMessageBox::critical (0x0, title, msg); + QMessageBox::critical (nullptr, title, msg); } void @@ -490,10 +515,9 @@ const QString& paren, int rows, int cols) { - if (!(m_d->m_validity)) - { + if (! (m_d->m_validity)) return; - } + m_d->m_type = paren.toStdString (); const int r = m_d->m_rows - rows; @@ -524,19 +548,24 @@ emit endInsertRows (); emit dataChanged (QAbstractTableModel::index (0, 0), - QAbstractTableModel::index (m_d->m_rows - 1, m_d->m_cols - 1)); + QAbstractTableModel::index (m_d->m_rows - 1, + m_d->m_cols - 1)); m_d->m_label->setTextFormat (Qt::PlainText); - QString description = QString ("%1: %2 %3x%4") - .arg (QString::fromStdString (m_d->m_name)) - .arg (class_name) - .arg (rows) - .arg (cols); + + QString description + = (QString ("%1: %2 %3x%4") + .arg (QString::fromStdString (m_d->m_name)) + .arg (class_name) + .arg (rows) + .arg (cols)); + m_d->m_label->setText (description); + m_d->m_validtext = description; } -// private +// Private. void variable_editor_model::get_data_oct (const int& row, const int& col, @@ -545,8 +574,10 @@ int parse_status = 0; octave_value v = retrieve_variable (x, parse_status); - //eval_string (x, true, parse_status);//retrieve_variable(x, parse_status); - //symbol_exist(x,"var") > 0 ? eval_string (x, true, parse_status) : octave_value(); + + // FIXME: ??? + // eval_string (x, true, parse_status);//retrieve_variable(x, parse_status); + // symbol_exist(x,"var") > 0 ? eval_string (x, true, parse_status) : octave_value(); if (parse_status != 0 || ! v.is_defined ()) { @@ -577,18 +608,25 @@ } // val has to be copied! + void variable_editor_model::set_data_oct (const std::string& x, const int& row, const int& col, const std::string& val) { m_d->m_validity = true; - int parse_status = 0; + // Accessing directly since // 1) retrieve_variable does not support writeback, and // 2) we can be reasonably sure that this variable exists. + + int parse_status = 0; + octave_value ret = octave::eval_string (val, true, parse_status); - //retrieve_variable(x, parse_status);//eval_string (val, true, parse_status); + + // FIXME: ??? + // retrieve_variable(x, parse_status);//eval_string (val, true, parse_status); + if (parse_status != 0 || ret.is_undefined ()) { emit user_error ("Invalid expression", @@ -598,8 +636,12 @@ } parse_status = 0; + octave_value v = retrieve_variable (x, parse_status); - //eval_string (x, true, parse_status); + + // FIXME: ??? + // eval_string (x, true, parse_status); + if (parse_status != 0 || ! v.is_defined ()) { m_d->m_validity = false; @@ -615,11 +657,12 @@ v.subsasgn (m_d->m_type, idxl, ret); emit unset_data (row, col); QModelIndex idx = QAbstractTableModel::index (row, col); + emit dataChanged (idx, idx); } -// If the variable exists, load it into the data model. If it doesn't exist, -// flag the data model as referring to a nonexistent variable. +// If the variable exists, load it into the data model. If it doesn't +// exist, flag the data model as referring to a nonexistent variable. // This allows the variable to be opened before it is created. octave_value variable_editor_model::retrieve_variable (const std::string& x, @@ -643,20 +686,28 @@ variable_editor_model::init_from_oct (const std::string& x) { int parse_status = 0; - const octave_value ov = retrieve_variable (x, parse_status);//eval_string (x, true, parse_status); + + const octave_value ov = retrieve_variable (x, parse_status); + + // FIXME: ??? + // eval_string (x, true, parse_status); + m_d->m_validity = true; + if (parse_status != 0 || ! ov.is_defined ()) { m_d->m_validity = false; display_invalid (); return; } + const QString class_name = QString::fromStdString (ov.class_name ()); const QString paren = ov.iscell () ? "{" : "("; // FIXME: cells? const octave_idx_type rows = ov.rows (); const octave_idx_type cols = ov.columns (); display_valid (); + emit initialize_data (class_name, paren, rows, cols); } @@ -664,11 +715,14 @@ variable_editor_model::eval_oct (const std::string& name, const std::string& x) { int parse_status = 0; + octave::eval_string (x, true, parse_status); + if (parse_status != 0) emit user_error ("Evaluation failed", QString ("Evaluation of `%s' failed") .arg (QString::fromStdString (x))); + init_from_oct (name); } @@ -676,9 +730,12 @@ variable_editor_model::display_invalid (void) { m_d->m_label->setTextFormat (Qt::PlainText); + QString description = QString ("%1: [not found or out-of-scope]") .arg (QString::fromStdString (m_d->m_name)); + m_d->m_label->setText (description); + dynamic_cast<QWidget *> (m_p)->setVisible (false); } @@ -686,7 +743,9 @@ variable_editor_model::display_valid (void) { m_d->m_label->setTextFormat (Qt::PlainText); + m_d->m_label->setText (m_d->m_validtext); + dynamic_cast<QWidget *> (m_p)->setVisible (true); }
--- a/libgui/src/variable-editor-model.h Wed Jan 10 16:09:53 2018 -0800 +++ b/libgui/src/variable-editor-model.h Tue Dec 12 17:30:50 2017 -0500 @@ -1,8 +1,8 @@ /* +Copyright (C) 2013-2017 John W. Eaton Copyright (C) 2015 Michael Barnes Copyright (C) 2013 Rüdiger Sonderfeld -Copyright (C) 2013 John W. Eaton This file is part of Octave. @@ -81,6 +81,7 @@ // If a sub editor is required, is it a standard type? bool editor_type_matrix (const QModelIndex& idx) const; + bool editor_type_string (const QModelIndex& idx) const; // Return the proper parens to access the data structure. @@ -120,7 +121,8 @@ private: // Get data for ov(row, col). This must be executed in the octave thread! - void get_data_oct (const int& row, const int& col, const std::string& v) /*const*/; + void get_data_oct (const int& row, const int& col, + const std::string& v) /*const*/; void set_data_oct (const std::string& v, const int& row, const int& col, const std::string& val); @@ -142,7 +144,9 @@ void display_valid (void); QObject *m_p; + struct impl; + impl *m_d; };
--- a/libgui/src/variable-editor.cc Wed Jan 10 16:09:53 2018 -0800 +++ b/libgui/src/variable-editor.cc Tue Dec 12 17:30:50 2017 -0500 @@ -1,8 +1,8 @@ /* +Copyright (C) 2013-2017 John W. Eaton Copyright (C) 2015 Michael Barnes Copyright (C) 2013 Rüdiger Sonderfeld -Copyright (C) 2013 John W. Eaton This file is part of Octave. @@ -78,48 +78,44 @@ Q_DECLARE_METATYPE (table_data) -static -QString idx_to_expr (int32_t from, int32_t to) +static QString +idx_to_expr (int32_t from, int32_t to) { - if (from == to) - return QString ("%1").arg (from + 1); - else - return QString ("%1:%2").arg (from + 1).arg (to + 1); + return (from == to + ? QString ("%1").arg (from + 1) + : QString ("%1:%2").arg (from + 1).arg (to + 1)); } variable_editor::variable_editor (QWidget *p) - : octave_dock_widget (p), - m_main (new QMainWindow ()), + : octave_dock_widget (p), m_main (new QMainWindow ()), m_tool_bar (new QToolBar (m_main)), m_tab_widget (new QTabWidget (m_main)), - m_default_width (20), m_default_height (100), - m_add_font_height (0), - m_autofit (false), m_autofit_max (false), - m_use_terminal_font (true), m_alternate_rows (true), - m_stylesheet (""), m_font (), m_sel_font (), + m_default_width (20), m_default_height (100), m_add_font_height (0), + m_autofit (false), m_autofit_max (false), m_use_terminal_font (true), + m_alternate_rows (true), m_stylesheet (""), m_font (), m_sel_font (), m_table_colors () { - // Use a MainWindow + // Use a MainWindow. setObjectName ("variable_editor"); set_title (tr ("Variable Editor")); setStatusTip (tr ("Edit variables.")); setWindowIcon (QIcon (":/actions/icons/logo.png")); - // Tool Bar + // Tool Bar. construct_tool_bar (); m_main->addToolBar (m_tool_bar); for (int i = 0; i < resource_manager::varedit_color_chars ().length (); i++) m_table_colors.append (QColor (Qt::white)); - // Tab Widget + // Tab Widget. m_tab_widget->setTabsClosable (true); m_tab_widget->setMovable (true); connect (m_tab_widget, SIGNAL (tabCloseRequested (int)), this, SLOT (closeTab (int))); m_main->setCentralWidget (m_tab_widget); - // Main + // Main. m_main->setParent (this); setWidget (m_main); @@ -129,12 +125,13 @@ variable_editor::~variable_editor (void) { - // m_tool_bar and m_tab_widget are contained within m_main + // m_tool_bar and m_tab_widget are contained within m_main. delete m_main; } -// Returns the real variable name from the tab addressed by 'index', +// Return the real variable name from the tab addressed by 'index', // cleaned of any '&' possibly inserted by KDE. + QString variable_editor::real_var_name (int index) { @@ -154,11 +151,13 @@ const int tab_count = m_tab_widget->count (); for (int i = 0; i < tab_count; ++i) - if (real_var_name (i) == name) - { - m_tab_widget->setCurrentIndex (i); - return; // already open - } + { + if (real_var_name (i) == name) + { + m_tab_widget->setCurrentIndex (i); + return; // Already open. + } + } QWidget *page = new QWidget; // Do not set parent. @@ -186,13 +185,17 @@ connect (table->horizontalHeader (), SIGNAL (customContextMenuRequested (const QPoint&)), this, SLOT (columnmenu_requested (const QPoint&))); + connect (table->verticalHeader (), SIGNAL (customContextMenuRequested (const QPoint&)), this, SLOT (rowmenu_requested (const QPoint&))); + connect (table, SIGNAL (customContextMenuRequested (const QPoint&)), this, SLOT (contextmenu_requested (const QPoint&))); + connect (table, SIGNAL (doubleClicked (const QModelIndex&)), this, SLOT (double_click (const QModelIndex&))); + connect (model, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)), this, SLOT (callUpdate (const QModelIndex&, const QModelIndex&))); @@ -203,26 +206,28 @@ m_tab_widget->setCurrentIndex (tab_idx); if (m_tab_widget->count () == 1) - m_tool_bar->setEnabled (true); // This is the first tab -> enable tool bar + m_tool_bar->setEnabled (true); // This is the first tab -> enable tool bar. if (m_autofit) { table->resizeColumnsToContents (); + if (m_autofit_max) { int mx = 0; + for (int i = 0; i < table->model ()->columnCount (); i++) { if (table->columnWidth (i) > mx) mx = table->columnWidth (i); } + table->horizontalHeader ()->setDefaultSectionSize (mx); } } else - { - table->horizontalHeader ()->setDefaultSectionSize (m_default_width); - } + table->horizontalHeader ()->setDefaultSectionSize (m_default_width); + table->setFont (m_font); table->setStyleSheet (m_stylesheet); table->setAlternatingRowColors (m_alternate_rows); @@ -235,7 +240,8 @@ + m_add_font_height); } -void variable_editor::clear_data_cache (void) +void +variable_editor::clear_data_cache (void) { for (int i = 0; i < m_tab_widget->count (); ++i) { @@ -250,16 +256,15 @@ { // FIXME: This only generates exceptions in certain circumstances. // Get a definitive list and eliminate the need to handle exceptions. + if (m_tab_widget->currentIndex () == -1) - return false; // No tabs + return false; // No tabs. try { QTableView *view = get_table_data (m_tab_widget).m_table; - if (view) - return view->hasFocus (); - return false; + return view ? view->hasFocus () : false; } catch (...) { @@ -269,7 +274,8 @@ return false; } -QList<QColor> variable_editor::default_colors (void) +QList<QColor> +variable_editor::default_colors (void) { QList<QColor> colorlist; @@ -282,7 +288,8 @@ return colorlist; } -QStringList variable_editor::color_names (void) +QStringList +variable_editor::color_names (void) { QStringList output; @@ -301,30 +308,36 @@ if (m_autofit) { QTableView *view = get_table_data (m_tab_widget).m_table; + view->resizeColumnsToContents (); + if (m_autofit_max) { int mx = 0; + for (int i = 0; i < view->model ()->columnCount (); i++) { if (view->columnWidth (i) > mx) mx = view->columnWidth (i); } + view->horizontalHeader ()->setDefaultSectionSize (mx); } - } emit updated (); } -void variable_editor::notice_settings (const QSettings *settings) +void +variable_editor::notice_settings (const QSettings *settings) { // FIXME: Why use object->tostring->toint? Why not just 100? m_default_width = settings->value ("variable_editor/column_width", QVariant ("100")).toString ().toInt (); + m_autofit = settings->value ("variable_editor/autofit_column_width", QVariant (false)).toBool (); + // FIXME: Magic Number 1 here, why not use enum? if (m_autofit) { @@ -333,11 +346,13 @@ } m_default_height = settings->value ("variable_editor/row_height", - QVariant ("10")).toString ().toInt (); + QVariant ("10")).toString ().toInt (); + m_alternate_rows = settings->value ("variable_editor/alternate_rows", - QVariant (false)).toBool (); + QVariant (false)).toBool (); QList<QColor> _default_colors = resource_manager::varedit_default_colors (); + QString class_chars = resource_manager::varedit_color_chars (); m_use_terminal_font = settings->value ("variable_editor/use_terminal_font", @@ -356,11 +371,13 @@ font_name = settings->value ("variable_editor/font_name", settings->value ("terminal/fontName", "Courier New")).toString (); font_size = settings->value ("variable_editor/font_size", 10).toInt (); } + m_font = QFont (font_name, font_size); if (settings->value ("variable_editor/autofit_row_height", true).toBool ()) { QFontMetrics fm (m_font); + m_add_font_height = fm.height (); } else @@ -372,11 +389,13 @@ QColor setting_color = settings->value ("variable_editor/color_" + class_chars.mid (i, 1), default_var).value<QColor> (); + m_table_colors.replace (i, setting_color); } + update_colors (); - // Icon size in the toolbar + // Icon size in the toolbar. int icon_size_settings = settings->value ("toolbar_icon_size", 0).toInt (); QStyle *st = style (); int icon_size = st->pixelMetric (QStyle::PM_ToolBarIconSize); @@ -394,6 +413,7 @@ variable_editor::closeEvent (QCloseEvent *e) { emit finished (); + octave_dock_widget::closeEvent (e); } @@ -420,62 +440,73 @@ if (index.isValid ()) { QMenu *menu = new QMenu (this); - menu->addAction (resource_manager::icon ("edit-cut"), tr ("Cut"), - this, SLOT (cutClipboard ())); - menu->addAction (resource_manager::icon ("edit-copy"), tr ("Copy"), - this, SLOT (copyClipboard ())); - menu->addAction (resource_manager::icon ("edit-paste"), tr ("Paste"), - this, SLOT (pasteClipboard ())); + + menu->addAction (resource_manager::icon ("edit-cut"), + tr ("Cut"), this, SLOT (cutClipboard ())); + + menu->addAction (resource_manager::icon ("edit-copy"), + tr ("Copy"), this, SLOT (copyClipboard ())); + + menu->addAction (resource_manager::icon ("edit-paste"), + tr ("Paste"), this, SLOT (pasteClipboard ())); + // FIXME: need different icon for paste table separate from paste? - menu->addAction (resource_manager::icon ("edit-paste"), tr ("Paste Table"), - this, SLOT (pasteTableClipboard ())); + menu->addAction (resource_manager::icon ("edit-paste"), + tr ("Paste Table"), this, + SLOT (pasteTableClipboard ())); menu->addSeparator (); - menu->addAction (resource_manager::icon ("edit-delete"), tr ("Clear"), - this, SLOT (clearContent ())); + menu->addAction (resource_manager::icon ("edit-delete"), + tr ("Clear"), this, SLOT (clearContent ())); + menu->addAction (resource_manager::icon ("document-new"), - tr ("Variable from Selection"), - this, SLOT (createVariable ())); + tr ("Variable from Selection"), this, + SLOT (createVariable ())); + // FIXME: addAction for sort? menu->addAction ( //QIcon (), FIXME: Add icon for transpose - tr ("Transpose"), - this, SLOT (transposeContent ())); + tr ("Transpose"), this, + SLOT (transposeContent ())); QItemSelectionModel *sel = view->selectionModel (); + QList<QModelIndex> indices = sel->selectedIndexes (); + if (! indices.isEmpty ()) { menu->addSeparator (); + QSignalMapper *plot_mapper = new QSignalMapper (menu); - plot_mapper->setMapping (menu->addAction ("plot", - plot_mapper, - SLOT (map ())), - "figure (); plot (%1);"); - plot_mapper->setMapping (menu->addAction ("bar", - plot_mapper, - SLOT (map ())), - "figure (); bar (%1);"); - plot_mapper->setMapping (menu->addAction ("stem", - plot_mapper, - SLOT (map ())), - "figure (); stem (%1);"); - plot_mapper->setMapping (menu->addAction ("stairs", - plot_mapper, - SLOT (map ())), - "figure (); stairs (%1);"); - plot_mapper->setMapping (menu->addAction ("area", - plot_mapper, - SLOT (map ())), - "figure (); area (%1);"); - plot_mapper->setMapping (menu->addAction ("pie", - plot_mapper, - SLOT (map ())), - "figure (); pie (%1);"); - plot_mapper->setMapping (menu->addAction ("hist", - plot_mapper, - SLOT (map ())), - "figure (); hist (%1);"); + + plot_mapper->setMapping + (menu->addAction ("plot", plot_mapper, SLOT (map ())), + "figure (); plot (%1);"); + + plot_mapper->setMapping + (menu->addAction ("bar", plot_mapper, SLOT (map ())), + "figure (); bar (%1);"); + + plot_mapper->setMapping + (menu->addAction ("stem", plot_mapper, SLOT (map ())), + "figure (); stem (%1);"); + + plot_mapper->setMapping + (menu->addAction ("stairs", plot_mapper, SLOT (map ())), + "figure (); stairs (%1);"); + + plot_mapper->setMapping + (menu->addAction ("area", plot_mapper, SLOT (map ())), + "figure (); area (%1);"); + + plot_mapper->setMapping + (menu->addAction ("pie", plot_mapper, SLOT (map ())), + "figure (); pie (%1);"); + + plot_mapper->setMapping + (menu->addAction ("hist", plot_mapper, SLOT (map ())), + "figure (); hist (%1);"); + connect (plot_mapper, SIGNAL (mapped (const QString&)), this, SLOT (relay_command (const QString&))); } @@ -491,28 +522,28 @@ int index = view->horizontalHeader ()->logicalIndexAt (pt); - //emit command_requested (QString ("disp ('") + QString::number (index) + "');"); + // FIXME: what was the intent here? + // emit command_requested (QString ("disp ('") + QString::number (index) + "');"); if (index < 0 || index > view->model ()->columnCount ()) return; QString selection = selected_to_octave (); + QList<int> coords = octave_to_coords (selection); - bool nothingSelected = false; - if (coords.isEmpty ()) - nothingSelected = true; + bool nothingSelected = coords.isEmpty (); - bool whole_columns_selected = - nothingSelected ? false - : (coords[0] == 1 - && coords[1] == view->model ()->rowCount ()); + bool whole_columns_selected + = (nothingSelected + ? false + : (coords[0] == 1 && coords[1] == view->model ()->rowCount ())); - bool current_column_selected = - nothingSelected ? false : (coords[2] <= index+1 && coords[3] > index); + bool current_column_selected + = nothingSelected ? false : (coords[2] <= index+1 && coords[3] > index); - int column_selection_count = - nothingSelected ? 0 : (coords[3] - coords[2] + 1); + int column_selection_count + = nothingSelected ? 0 : (coords[3] - coords[2] + 1); if (! whole_columns_selected || ! current_column_selected) { @@ -522,19 +553,23 @@ whole_columns_selected = true; } - QString column_string = tr (column_selection_count > 1 ? " columns" - : " column"); + QString column_string + = tr (column_selection_count > 1 ? " columns" : " column"); QMenu *menu = new QMenu (this); + menu->addAction (resource_manager::icon ("edit-cut"), tr ("Cut") + column_string, this, SLOT (cutClipboard ())); + menu->addAction (resource_manager::icon ("edit-copy"), tr ("Copy") + column_string, this, SLOT (copyClipboard ())); + menu->addAction (resource_manager::icon ("edit-paste"), tr ("Paste"), this, SLOT (pasteClipboard ())); + // FIXME: different icon for Paste Table? menu->addAction (resource_manager::icon ("edit-paste"), tr ("Paste Table"), @@ -545,9 +580,11 @@ menu->addAction (resource_manager::icon ("edit-delete"), tr ("Clear") + column_string, this, SLOT (clearContent ())); + menu->addAction (resource_manager::icon ("edit-delete"), tr ("Delete") + column_string, this, SLOT (delete_selected ())); + menu->addAction (resource_manager::icon ("document-new"), tr ("Variable from Selection"), this, SLOT (createVariable ())); @@ -555,34 +592,35 @@ menu->addSeparator (); QSignalMapper *plot_mapper = new QSignalMapper (menu); - plot_mapper->setMapping (menu->addAction ("plot", - plot_mapper, - SLOT (map ())), - "figure (); plot (%1);"); - plot_mapper->setMapping (menu->addAction ("bar", - plot_mapper, - SLOT (map ())), - "figure (); bar (%1);"); - plot_mapper->setMapping (menu->addAction ("stem", - plot_mapper, - SLOT (map ())), - "figure (); stem (%1);"); - plot_mapper->setMapping (menu->addAction ("stairs", - plot_mapper, - SLOT (map ())), - "figure (); stairs (%1);"); - plot_mapper->setMapping (menu->addAction ("area", - plot_mapper, - SLOT (map ())), - "figure (); area (%1);"); - plot_mapper->setMapping (menu->addAction ("pie", - plot_mapper, - SLOT (map ())), - "figure (); pie (%1);"); - plot_mapper->setMapping (menu->addAction ("hist", - plot_mapper, - SLOT (map ())), - "figure (); hist (%1);"); + + plot_mapper->setMapping + (menu->addAction ("plot", plot_mapper, SLOT (map ())), + "figure (); plot (%1);"); + + plot_mapper->setMapping + (menu->addAction ("bar", plot_mapper, SLOT (map ())), + "figure (); bar (%1);"); + + plot_mapper->setMapping + (menu->addAction ("stem", plot_mapper, SLOT (map ())), + "figure (); stem (%1);"); + + plot_mapper->setMapping + (menu->addAction ("stairs", plot_mapper, SLOT (map ())), + "figure (); stairs (%1);"); + + plot_mapper->setMapping + (menu->addAction ("area", plot_mapper, SLOT (map ())), + "figure (); area (%1);"); + + plot_mapper->setMapping + (menu->addAction ("pie", plot_mapper, SLOT (map ())), + "figure (); pie (%1);"); + + plot_mapper->setMapping + (menu->addAction ("hist", plot_mapper, SLOT (map ())), + "figure (); hist (%1);"); + connect (plot_mapper, SIGNAL (mapped (const QString&)), this, SLOT (relay_command (const QString&))); @@ -599,28 +637,25 @@ int index = view->verticalHeader ()->logicalIndexAt (pt); + // FIXME: what was the intent here? //emit command_requested (QString ("disp ('") + QString::number (index) + "');"); if (index < 0 || index > view->model ()->columnCount ()) return; QString selection = selected_to_octave (); + QList<int> coords = octave_to_coords (selection); - bool nothingSelected; - if (coords.isEmpty ()) - nothingSelected = true; - else - nothingSelected = false; + bool nothingSelected = coords.isEmpty (); - bool whole_rows_selected = - nothingSelected ? false - : (coords[2] == 1 - && coords[3] == view->model ()->columnCount ()); + bool whole_rows_selected + = (nothingSelected + ? false + : (coords[2] == 1 && coords[3] == view->model ()->columnCount ())); - bool current_row_selected = - nothingSelected ? false - : (coords[0] <= index+1 && coords[1] > index); + bool current_row_selected + = (nothingSelected ? false : (coords[0] <= index+1 && coords[1] > index)); int rowselection_count = nothingSelected ? 0 : (coords[3] - coords[2] + 1); @@ -635,15 +670,19 @@ QString row_string = tr (rowselection_count > 1 ? " rows" : " row"); QMenu *menu = new QMenu (this); + menu->addAction (resource_manager::icon ("edit-cut"), tr ("Cut") + row_string, this, SLOT (cutClipboard ())); + menu->addAction (resource_manager::icon ("edit-copy"), tr ("Copy") + row_string, this, SLOT (copyClipboard ())); + menu->addAction (resource_manager::icon ("edit-paste"), tr ("Paste"), this, SLOT (pasteClipboard ())); + // FIXME: better icon for Paste Table? menu->addAction (resource_manager::icon ("edit-paste"), tr ("Paste Table"), @@ -654,9 +693,11 @@ menu->addAction (resource_manager::icon ("edit-delete"), tr ("Clear") + row_string, this, SLOT (clearContent ())); + menu->addAction (resource_manager::icon ("edit-delete"), tr ("Delete") + row_string, this, SLOT (delete_selected ())); + menu->addAction (resource_manager::icon ("document-new"), tr ("Variable from Selection"), this, SLOT (createVariable ())); @@ -664,34 +705,35 @@ menu->addSeparator (); QSignalMapper *plot_mapper = new QSignalMapper (menu); - plot_mapper->setMapping (menu->addAction ("plot", - plot_mapper, - SLOT (map ())), - "figure (); plot (%1);"); - plot_mapper->setMapping (menu->addAction ("bar", - plot_mapper, - SLOT (map ())), - "figure (); bar (%1);"); - plot_mapper->setMapping (menu->addAction ("stem", - plot_mapper, - SLOT (map ())), - "figure (); stem (%1);"); - plot_mapper->setMapping (menu->addAction ("stairs", - plot_mapper, - SLOT (map ())), - "figure (); stairs (%1);"); - plot_mapper->setMapping (menu->addAction ("area", - plot_mapper, - SLOT (map ())), - "figure (); area (%1);"); - plot_mapper->setMapping (menu->addAction ("pie", - plot_mapper, - SLOT (map ())), - "figure (); pie (%1);"); - plot_mapper->setMapping (menu->addAction ("hist", - plot_mapper, - SLOT (map ())), - "figure (); hist (%1);"); + + plot_mapper->setMapping + (menu->addAction ("plot", plot_mapper, SLOT (map ())), + "figure (); plot (%1);"); + + plot_mapper->setMapping + (menu->addAction ("bar", plot_mapper, SLOT (map ())), + "figure (); bar (%1);"); + + plot_mapper->setMapping + (menu->addAction ("stem", plot_mapper, SLOT (map ())), + "figure (); stem (%1);"); + + plot_mapper->setMapping + (menu->addAction ("stairs", plot_mapper, SLOT (map ())), + "figure (); stairs (%1);"); + + plot_mapper->setMapping + (menu->addAction ("area", plot_mapper, SLOT (map ())), + "figure (); area (%1);"); + + plot_mapper->setMapping + (menu->addAction ("pie", plot_mapper, SLOT (map ())), + "figure (); pie (%1);"); + + plot_mapper->setMapping + (menu->addAction ("hist", plot_mapper, SLOT (map ())), + "figure (); hist (%1);"); + connect (plot_mapper, SIGNAL (mapped (const QString&)), this, SLOT (relay_command (const QString&))); @@ -707,16 +749,18 @@ variable_editor::double_click (const QModelIndex& idx) { QString name = real_var_name (m_tab_widget->currentIndex ()); + QTableView *const table = get_table_data (m_tab_widget).m_table; - variable_editor_model *const model = - qobject_cast<variable_editor_model *> (table->model ()); + + variable_editor_model *const model + = qobject_cast<variable_editor_model *> (table->model ()); + if (model->requires_sub_editor (idx)) { if (model ->editor_type_matrix (idx)) - edit_variable (name + - model->parens () - .arg (idx.row () + 1) + edit_variable (name + model->parens () .arg (idx.row () + 1) .arg (idx.column () + 1)); + /* emit command_requested ("openvar ('" + name + model->parens () .arg (idx.row () + 1) @@ -731,11 +775,12 @@ variable_editor::save (void) { QString name = real_var_name (m_tab_widget->currentIndex ()); - QString file = - QFileDialog::getSaveFileName (this, - tr ("Save Variable %1 As").arg (name), - ".", 0, 0, - QFileDialog::DontUseNativeDialog); + QString file + = QFileDialog::getSaveFileName (this, + tr ("Save Variable %1 As").arg (name), + ".", 0, 0, + QFileDialog::DontUseNativeDialog); + // FIXME: Type? binary, float-binary, ascii, text, hdf5, matlab format? if (! file.isEmpty ()) // FIXME: Call octave_value::save_* directly? @@ -752,6 +797,7 @@ QAbstractItemModel *model = view->model (); QItemSelectionModel *sel = view->selectionModel (); QList<QModelIndex> indices = sel->selectedIndexes (); + for (const auto& idx : indices) model->setData (idx, QVariant ("0")); // FIXME: Use [] for empty cells } @@ -763,6 +809,7 @@ return; copyClipboard (); + clearContent (); } @@ -816,9 +863,7 @@ if (indices.isEmpty ()) { if (view->size () == QSize (1,1)) - { - model->setData (view->model ()->index (0,0), text.toDouble ()); - } + model->setData (view->model ()->index (0,0), text.toDouble ()); else if (view->size () == QSize (0,0)) { model->insertColumn (0); @@ -847,8 +892,8 @@ QItemSelectionModel *sel = view->selectionModel (); QList<QModelIndex> indices = sel->selectedIndexes (); - variable_editor_model *model = - static_cast<variable_editor_model *> (view->model ()); + variable_editor_model *model + = static_cast<variable_editor_model *> (view->model ()); QPoint start, end; @@ -882,7 +927,6 @@ if (indices[i].row () > end.x ()) end.setX (indices[i].column ()); - } } @@ -910,9 +954,10 @@ colnum + start.y ()), QVariant (col)); - // relay_command ("disp ('" + QString::number (colnum+start.y ()) + "," + QString::number (rownum+start.x ()) +"');"); + // relay_command ("disp ('" + QString::number (colnum+start.y ()) + "," + QString::number (rownum+start.x ()) +"');"); colnum++; } + colnum = 0; rownum++; } @@ -931,7 +976,9 @@ variable_editor::transposeContent (void) { QString name = real_var_name (m_tab_widget->currentIndex ()); + emit command_requested (QString ("%1 = %1';").arg (name)); + emit updated (); } @@ -939,6 +986,7 @@ variable_editor::up (void) { QString name = real_var_name (m_tab_widget->currentIndex ()); + // FIXME: is there a better way? if (name.endsWith (')') || name.endsWith ('}')) { @@ -959,10 +1007,11 @@ if (coords.isEmpty ()) return; - bool whole_columns_selected = coords[0] == 1 - && coords[1] == view->model ()->rowCount (); - bool whole_rows_selected = coords[2] == 1 - && coords[3] == view->model ()->columnCount (); + bool whole_columns_selected + = coords[0] == 1 && coords[1] == view->model ()->rowCount (); + + bool whole_rows_selected + = coords[2] == 1 && coords[3] == view->model ()->columnCount (); emit command_requested (QString ("disp ('") + QString::number (coords[0]) + "," @@ -971,7 +1020,7 @@ + QString::number (coords[3]) + "');"); // Must be deleting whole columns or whole rows, and not the whole thing. - if (whole_columns_selected == whole_rows_selected) // all or nothing + if (whole_columns_selected == whole_rows_selected) return; if (whole_rows_selected) @@ -995,22 +1044,24 @@ // FIXME: Is this necessary or would it be quicker to clone the function // that gives us the QString? - // sanity check + // Sanity check. if (selection.count (",") != 1) return QList<int> (); QList<int> output; output.clear (); // FIXME: Why clear if object has just been created? - // remove braces + + // Remove braces. int firstbracket = std::max (selection.indexOf ("("), selection.indexOf ("{")); + selection = selection.mid (firstbracket + 1, selection.length () - (firstbracket + 2)); QString rows = selection.left (selection.indexOf (",")); if (! rows.contains (":")) { - // Only one row + // Only one row. output.push_back (rows.toInt ()); output.push_back (output.last ()); } @@ -1028,7 +1079,7 @@ if (! cols.contains (":")) { - // Only one row + // Only one row. output.push_back (cols.toInt ()); output.push_back (output.last ()); } @@ -1052,7 +1103,7 @@ if (! sel->hasSelection ()) return name; // Nothing selected - QList<QModelIndex> indices = sel->selectedIndexes (); // it's indices! + QList<QModelIndex> indices = sel->selectedIndexes (); // FIXME: Shouldn't this be keyed to octave_idx_type? // If octave_idx_type is 64-bit then one could have 2^64,1 vector which @@ -1073,28 +1124,36 @@ QString rows = idx_to_expr (from_row, to_row); QString cols = idx_to_expr (from_col, to_col); - // FIXME: Do cell needs separate handling? Maybe use '{.,.}'? + // FIXME: Does cell need separate handling? Maybe use '{.,.}'? return QString ("%1(%2, %3)").arg (name).arg (rows).arg (cols); } -/// Also updates the font +/// Also updates the font. void variable_editor::update_colors (void) { m_stylesheet = ""; + m_stylesheet += "QTableView::item{ foreground-color: " - + m_table_colors[0].name () +" }"; + + m_table_colors[0].name () +" }"; + m_stylesheet += "QTableView::item{ background-color: " - + m_table_colors[1].name () +" }"; + + m_table_colors[1].name () +" }"; + m_stylesheet += "QTableView::item{ selection-color: " - + m_table_colors[2].name () +" }"; + + m_table_colors[2].name () +" }"; + m_stylesheet += "QTableView::item:selected{ background-color: " - + m_table_colors[3].name () +" }"; + + m_table_colors[3].name () +" }"; + if (m_table_colors.length () > 4 && m_alternate_rows) { - m_stylesheet += "QTableView::item:alternate{ background-color: " - + m_table_colors[4].name () +" }"; - m_stylesheet += "QTableView::item:alternate:selected{ background-color: " - + m_table_colors[3].name () +" }"; + m_stylesheet + += "QTableView::item:alternate{ background-color: " + + m_table_colors[4].name () +" }"; + + m_stylesheet + += "QTableView::item:alternate:selected{ background-color: " + + m_table_colors[3].name () +" }"; } if (m_tab_widget->count () < 1) @@ -1114,21 +1173,26 @@ variable_editor::construct_tool_bar (void) { m_tool_bar->setObjectName ("VariableEditorToolBar"); + m_tool_bar->setWindowTitle (tr ("Variable Editor Toolbar")); - m_tool_bar->addAction (resource_manager::icon ("document-save"), tr ("Save"), - this, SLOT (save ())); + m_tool_bar->addAction (resource_manager::icon ("document-save"), + tr ("Save"), this, SLOT (save ())); m_tool_bar->addSeparator (); - m_tool_bar->addAction (resource_manager::icon ("edit-cut"), tr ("Cut"), - this, SLOT (cutClipboard ())); - m_tool_bar->addAction (resource_manager::icon ("edit-copy"), tr ("Copy"), - this, SLOT (copyClipboard ())); - m_tool_bar->addAction (resource_manager::icon ("edit-paste"), tr ("Paste"), - this, SLOT (pasteClipboard ())); + m_tool_bar->addAction (resource_manager::icon ("edit-cut"), + tr ("Cut"), this, SLOT (cutClipboard ())); + + m_tool_bar->addAction (resource_manager::icon ("edit-copy"), + tr ("Copy"), this, SLOT (copyClipboard ())); + + m_tool_bar->addAction (resource_manager::icon ("edit-paste"), + tr ("Paste"), this, SLOT (pasteClipboard ())); + // FIXME: Different icon for Paste Table? - m_tool_bar->addAction (resource_manager::icon ("edit-paste"), tr ("Paste Table"), + m_tool_bar->addAction (resource_manager::icon ("edit-paste"), + tr ("Paste Table"), this, SLOT (pasteTableClipboard ())); m_tool_bar->addSeparator (); @@ -1144,45 +1208,51 @@ plot_tool_button->setPopupMode (QToolButton::InstantPopup); QMenu *plot_menu = new QMenu (tr ("Plot"), plot_tool_button); + plot_menu->setSeparatorsCollapsible (false); + QSignalMapper *plot_mapper = new QSignalMapper (plot_menu); - plot_mapper->setMapping (plot_menu->addAction ("plot", - plot_mapper, - SLOT (map ())), - "figure (); plot (%1);"); - plot_mapper->setMapping (plot_menu->addAction ("bar", - plot_mapper, - SLOT (map ())), - "figure (); bar (%1);"); - plot_mapper->setMapping (plot_menu->addAction ("stem", - plot_mapper, - SLOT (map ())), - "figure (); stem (%1);"); - plot_mapper->setMapping (plot_menu->addAction ("stairs", - plot_mapper, - SLOT (map ())), - "figure (); stairs (%1);"); - plot_mapper->setMapping (plot_menu->addAction ("area", - plot_mapper, - SLOT (map ())), - "figure (); area (%1);"); - plot_mapper->setMapping (plot_menu->addAction ("pie", - plot_mapper, - SLOT (map ())), - "figure (); pie (%1);"); - plot_mapper->setMapping (plot_menu->addAction ("hist", - plot_mapper, - SLOT (map ())), - "figure (); hist (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("plot", plot_mapper, SLOT (map ())), + "figure (); plot (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("bar", plot_mapper, SLOT (map ())), + "figure (); bar (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("stem", plot_mapper, SLOT (map ())), + "figure (); stem (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("stairs", plot_mapper, SLOT (map ())), + "figure (); stairs (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("area", plot_mapper, SLOT (map ())), + "figure (); area (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("pie", plot_mapper, SLOT (map ())), + "figure (); pie (%1);"); + + plot_mapper->setMapping + (plot_menu->addAction ("hist", plot_mapper, SLOT (map ())), + "figure (); hist (%1);"); + connect (plot_mapper, SIGNAL (mapped (const QString&)), this, SLOT (relay_command (const QString&))); plot_tool_button->setMenu (plot_menu); + m_tool_bar->addWidget (plot_tool_button); m_tool_bar->addSeparator (); + m_tool_bar->addAction (QIcon (resource_manager::icon ("go-up")), tr ("Up"), this, SLOT (up ())); - m_tool_bar->setEnabled (false); // Disabled when no tab is present + // Disabled when no tab is present. + m_tool_bar->setEnabled (false); }
--- a/libgui/src/variable-editor.h Wed Jan 10 16:09:53 2018 -0800 +++ b/libgui/src/variable-editor.h Tue Dec 12 17:30:50 2017 -0500 @@ -1,8 +1,8 @@ /* +Copyright (C) 2013-2017 John W. Eaton Copyright (C) 2015 Michael Barnes Copyright (C) 2013 Rüdiger Sonderfeld -Copyright (C) 2013 John W. Eaton This file is part of Octave. @@ -48,12 +48,13 @@ void edit_variable (const QString& name); - /// Clear all the models' data cache + // Clear all the models' data cache. void clear_data_cache (void); bool has_focus (void); static QList<QColor> default_colors (void); + static QStringList color_names (void); public slots: @@ -69,19 +70,29 @@ void closeTab (int idx); void contextmenu_requested (const QPoint& pt); + void columnmenu_requested (const QPoint& pt); + void rowmenu_requested (const QPoint& pt); void double_click (const QModelIndex& idx); void save (void); + void clearContent (void); + void cutClipboard (void); + void copyClipboard (void); + void pasteClipboard (void); + void pasteTableClipboard (void); + void createVariable (void); + void transposeContent (void); + void up (void); void delete_selected (void); @@ -93,22 +104,31 @@ signals: void updated (void); + void finished (void); + void command_requested (const QString& cmd); private: QMainWindow *m_main; + QToolBar *m_tool_bar; + QTabWidget *m_tab_widget; int m_default_width; + int m_default_height; + int m_add_font_height; bool m_autofit; + bool m_autofit_max; + bool m_use_terminal_font; + bool m_alternate_rows; QString m_stylesheet; @@ -118,6 +138,7 @@ // If use_terminal_font is true then this will be different since // "font" will contain the terminal font. QFont m_sel_font; + QList<QColor> m_table_colors; QList<int> octave_to_coords (QString&);