Mercurial > octave
view libgui/src/workspace-view.cc @ 31619:ad014fc78bd6
use individual local gui_settings objects
Previously, we created a single gui_settings object (derived from
QSettings) and accessed it from the resource_manager object. That
design is not necessary and is not the way QSettings was designed to
be used. Instead of managing a single object, we should be using
individual QSettings objects where needed. Each individual QSettings
object manages thread-safe access to a single global collection of
settings. The Qt docs say that operations on QSettings are not thread
safe, but that means that you can't create a QSettings object in one
thread and use it in another without some locking. I'm not sure
whether we were doing that correctly, but with this change it no
longer matters. Each QSettings object does perform locking when
reading or writing the underlying global data.
* resource-manager.h, resource-manager.cc
(resource_manager::m_settings): Delete data member.
(resource_manager::get_settings): Delete.
* annotation-dialog.cc, QTerminal.cc, QTerminal.h, command-widget.cc,
command-widget.h, community-news.cc, dialog.cc,
documentation-bookmarks.cc, documentation-bookmarks.h,
documentation-dock-widget.cc, documentation-dock-widget.h,
documentation.cc, documentation.h, dw-main-window.cc,
dw-main-window.h, external-editor-interface.cc, files-dock-widget.cc,
files-dock-widget.h, find-files-dialog.cc, history-dock-widget.cc,
history-dock-widget.h, file-editor-interface.h, file-editor-tab.cc,
file-editor-tab.h, file-editor.cc, file-editor.h, find-dialog.cc,
octave-qscintilla.cc, main-window.cc, main-window.h, news-reader.cc,
octave-dock-widget.cc, octave-dock-widget.h, qt-interpreter-events.cc,
qt-interpreter-events.h, release-notes.cc, resource-manager.cc,
resource-manager.h, set-path-dialog.cc, settings-dialog.cc,
settings-dialog.h, shortcut-manager.cc, shortcut-manager.h,
terminal-dock-widget.cc, terminal-dock-widget.h, variable-editor.cc,
variable-editor.h, welcome-wizard.cc, workspace-model.cc,
workspace-model.h, workspace-view.cc: Use local gui_settings objects
instead of accessing a pointer to a single gui_settings object owned
by the resource_manager object.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 02 Dec 2022 14:23:53 -0500 |
parents | 04601f6c47f4 |
children | 431f80aba37a |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// // // Copyright (C) 2011-2022 The Octave Project Developers // // See the file COPYRIGHT.md in the top-level directory of this // distribution or <https://octave.org/copyright/>. // // This file is part of Octave. // // Octave is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Octave is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Octave; see the file COPYING. If not, see // <https://www.gnu.org/licenses/>. // //////////////////////////////////////////////////////////////////////// #if defined (HAVE_CONFIG_H) # include "config.h" #endif #include <QApplication> #include <QClipboard> #include <QCompleter> #include <QHBoxLayout> #include <QHeaderView> #include <QInputDialog> #include <QLabel> #include <QLineEdit> #include <QMenu> #include <QMessageBox> #include <QPushButton> #include <QSignalMapper> #include <QVBoxLayout> #include "gui-preferences-ws.h" #include "gui-settings.h" #include "octave-qobject.h" #include "octave-qtutils.h" #include "workspace-view.h" namespace octave { workspace_view::workspace_view (QWidget *p, base_qobject& oct_qobj) : octave_dock_widget ("WorkspaceView", p, oct_qobj), m_view (new QTableView (this)), m_filter_checkbox (new QCheckBox ()), m_filter (new QComboBox (this)), m_filter_widget (new QWidget (this)) { set_title (tr ("Workspace")); setStatusTip (tr ("View the variables in the active workspace.")); m_filter->setToolTip (tr ("Enter text to filter the workspace")); m_filter->setEditable (true); m_filter->setMaxCount (ws_max_filter_history.def.toInt ()); m_filter->setInsertPolicy (QComboBox::NoInsert); m_filter->setSizeAdjustPolicy (QComboBox::AdjustToMinimumContentsLengthWithIcon); QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred); m_filter->setSizePolicy (sizePol); m_filter->completer ()->setCaseSensitivity (Qt::CaseSensitive); QLabel *filter_label = new QLabel (tr ("Filter")); m_view->setWordWrap (false); m_view->setContextMenuPolicy (Qt::CustomContextMenu); m_view->setShowGrid (false); (m_view->verticalHeader) ()->hide (); m_view->setAlternatingRowColors (true); m_view_previous_row_count = 0; // Set an empty widget, so we can assign a layout to it. setWidget (new QWidget (this)); // Create the layouts QHBoxLayout *filter_layout = new QHBoxLayout (); filter_layout->addWidget (filter_label); filter_layout->addWidget (m_filter_checkbox); filter_layout->addWidget (m_filter); filter_layout->setMargin (0); m_filter_widget->setLayout (filter_layout); QVBoxLayout *ws_layout = new QVBoxLayout (); ws_layout->addWidget (m_filter_widget); ws_layout->addWidget (m_view); ws_layout->setSpacing (0); gui_settings settings; m_filter_shown = settings.value (ws_filter_shown).toBool (); m_filter_widget->setVisible (m_filter_shown); ws_layout->setMargin (2); // Set the empty widget to have our layout. widget ()->setLayout (ws_layout); // Initialize collapse/expand state of the workspace subcategories. //enable sorting (setting column and order after model was set) m_view->setSortingEnabled (true); // Initialize column order and width of the workspace m_view->horizontalHeader ()->restoreState (settings.value (ws_column_state.key).toByteArray ()); // Set header properties for sorting m_view->horizontalHeader ()->setSectionsClickable (true); m_view->horizontalHeader ()->setSectionsMovable (true); m_view->horizontalHeader ()->setSortIndicator ( settings.value (ws_sort_column).toInt (), static_cast<Qt::SortOrder> (settings.value (ws_sort_order).toUInt ())); // FIXME: use value<Qt::SortOrder> instead of static cast after // dropping support of Qt 5.4 m_view->horizontalHeader ()->setSortIndicatorShown (true); m_view->horizontalHeader ()->setContextMenuPolicy (Qt::CustomContextMenu); connect (m_view->horizontalHeader (), &QTableView::customContextMenuRequested, this, &workspace_view::header_contextmenu_requested); // Init state of the filter m_filter->addItems (settings.value (ws_mru_list.key).toStringList ()); bool filter_state = settings.value (ws_filter_active).toBool (); m_filter_checkbox->setChecked (filter_state); filter_activate (filter_state); // Connect signals and slots. connect (m_filter, &QComboBox::editTextChanged, this, &workspace_view::filter_update); connect (m_filter_checkbox, &QCheckBox::toggled, this, &workspace_view::filter_activate); connect (m_filter->lineEdit (), &QLineEdit::editingFinished, this, &workspace_view::update_filter_history); connect (m_view, &QTableView::customContextMenuRequested, this, &workspace_view::contextmenu_requested); connect (m_view, &QTableView::activated, this, &workspace_view::handle_contextmenu_edit); if (! p) make_window (); } void workspace_view::setModel (workspace_model *model) { m_filter_model.setSourceModel (model); m_filter_model.setFilterKeyColumn(0); m_view->setModel (&m_filter_model); // set the sorting after the model is set, it would be ignored otherwise gui_settings settings; m_view->sortByColumn ( settings.value (ws_sort_column).toInt (), static_cast<Qt::SortOrder> (settings.value (ws_sort_order).toUInt ())); // FIXME: use value<Qt::SortOrder> instead of static cast after // dropping support of Qt 5.4 m_model = model; } void workspace_view::notice_settings (void) { gui_settings settings; m_model->notice_settings (); // update colors of model first for (int i = 0; i < ws_columns_shown.length (); i++) m_view->setColumnHidden (i + 1, ! settings.value (ws_columns_shown_keys.at (i), true).toBool ()); QString tool_tip; if (settings.value (ws_enable_colors).toBool () && ! settings.value (ws_hide_tool_tips).toBool ()) { tool_tip = QString (tr ("View the variables in the active workspace.<br>")); tool_tip += QString (tr ("Colors for variable attributes:")); for (int i = 0; i < ws_colors_count; i++) { tool_tip += QString (R"(<div style="background-color:%1;color:%2">%3</div>)") .arg (m_model->storage_class_color (i).name ()) .arg (m_model->storage_class_color (i + ws_colors_count).name ()) .arg (QCoreApplication::translate ("octave::settings_dialog", ws_color_names.at (i).toStdString ().data ())); } } setToolTip (tool_tip); } void workspace_view::save_settings (void) { gui_settings settings; settings.setValue (ws_column_state.key, m_view->horizontalHeader ()->saveState ()); int sort_column = m_view->horizontalHeader ()->sortIndicatorSection (); Qt::SortOrder sort_order = m_view->horizontalHeader ()->sortIndicatorOrder (); settings.setValue (ws_sort_column.key, sort_column); settings.setValue (ws_sort_order.key, sort_order); settings.setValue (ws_filter_active.key, m_filter_checkbox->isChecked ()); settings.setValue (ws_filter_shown.key, m_filter_shown); QStringList mru; for (int i = 0; i < m_filter->count (); i++) mru.append (m_filter->itemText (i)); settings.setValue (ws_mru_list.key, mru); settings.sync (); octave_dock_widget::save_settings (); } void workspace_view::set_filter_focus (bool focus) { if (focus) { m_filter->setFocus (); setFocusProxy (m_filter); } else { m_view->setFocus (); setFocusProxy (m_view); } } void workspace_view::filter_update (const QString& expression) { m_filter_model.setFilterWildcard (expression); handle_model_changed (); } void workspace_view::filter_activate (bool state) { m_filter->setEnabled (state); m_filter_model.setDynamicSortFilter (state); if (state) filter_update (m_filter->currentText ()); else filter_update (QString ()); set_filter_focus (state); } void workspace_view::update_filter_history (void) { QString text = m_filter->currentText (); // get current text int index = m_filter->findText (text); // and its actual index if (index > -1) m_filter->removeItem (index); // remove if already existing m_filter->insertItem (0, text); // (re)insert at beginning m_filter->setCurrentIndex (0); } void workspace_view::header_contextmenu_requested (const QPoint& mpos) { QMenu menu (this); QSignalMapper sig_mapper (this); gui_settings settings; for (int i = 0; i < ws_columns_shown.length (); i++) { QAction *action = menu.addAction (tr (ws_columns_shown.at (i).toStdString ().data ()), &sig_mapper, SLOT (map ())); sig_mapper.setMapping (action, i); action->setCheckable (true); action->setChecked (settings.value (ws_columns_shown_keys.at (i), true).toBool ()); } // FIXME: We could use // // connect (&m_sig_mapper, QOverload<int>::of (&QSignalMapper::mapped), // this, &workspace_view::toggle_header); // // but referring to QSignalMapper::mapped will generate deprecated // function warnings from GCC. We could also use // // connect (&m_sig_mapper, &QSignalMapper::mappedInt, // this, &workspace_view::toggle_header); // // but the function mappedInt was not introduced until Qt 5.15 so // we'll need a feature test. connect (&sig_mapper, SIGNAL (mapped (int)), this, SLOT (toggle_header (int))); menu.exec (m_view->mapToGlobal (mpos)); } void workspace_view::toggle_header (int col) { gui_settings settings; QString key = ws_columns_shown_keys.at (col); bool shown = settings.value (key, true).toBool (); m_view->setColumnHidden (col + 1, shown); settings.setValue (key, ! shown); settings.sync (); octave_dock_widget::save_settings (); } void workspace_view::contextmenu_requested (const QPoint& qpos) { QMenu menu (this); QModelIndex index = m_view->indexAt (qpos); // if it isn't Local, Global etc, allow the ctx menu if (index.isValid () && index.column () == 0) { QString var_name = get_var_name (index); menu.addAction (tr ("Open in Variable Editor"), this, &workspace_view::handle_contextmenu_edit); menu.addAction (tr ("Copy name"), this, &workspace_view::handle_contextmenu_copy); menu.addAction (tr ("Copy value"), this, &workspace_view::handle_contextmenu_copy_value); QAction *rename = menu.addAction (tr ("Rename"), this, &workspace_view::handle_contextmenu_rename); // Use m_model here instead of using "m_view->model ()" because // that points to the proxy model. if (! m_model->is_top_level ()) { rename->setDisabled (true); rename->setToolTip (tr ("Only top-level symbols may be renamed")); } menu.addAction ("Clear " + var_name, this, &workspace_view::handle_contextmenu_clear); menu.addSeparator (); menu.addAction ("disp (" + var_name + ')', this, &workspace_view::handle_contextmenu_disp); menu.addAction ("plot (" + var_name + ')', this, &workspace_view::handle_contextmenu_plot); menu.addAction ("stem (" + var_name + ')', this, &workspace_view::handle_contextmenu_stem); menu.addSeparator (); } if (m_filter_shown) menu.addAction (tr ("Hide filter"), this, &workspace_view::handle_contextmenu_filter); else menu.addAction (tr ("Show filter"), this, &workspace_view::handle_contextmenu_filter); menu.exec (m_view->mapToGlobal (qpos)); } void workspace_view::handle_contextmenu_copy (void) { QModelIndex index = m_view->currentIndex (); if (index.isValid ()) { QString var_name = get_var_name (index); QClipboard *clipboard = QApplication::clipboard (); clipboard->setText (var_name); } } void workspace_view::handle_contextmenu_copy_value (void) { QModelIndex index = m_view->currentIndex (); if (index.isValid ()) emit copy_variable_value_to_clipboard (get_var_name (index)); } void workspace_view::handle_contextmenu_rename (void) { QModelIndex index = m_view->currentIndex (); if (index.isValid ()) { QString var_name = get_var_name (index); QInputDialog *inputDialog = new QInputDialog (); inputDialog->setOptions (QInputDialog::NoButtons); bool ok = false; QString new_name = inputDialog->getText (nullptr, "Rename Variable", "New name:", QLineEdit::Normal, var_name, &ok); if (ok && ! new_name.isEmpty ()) emit rename_variable_signal (var_name, new_name); } } void workspace_view::handle_contextmenu_edit (void) { QModelIndex index = m_view->currentIndex (); if (index.isValid ()) emit edit_variable_signal (get_var_name (index)); } void workspace_view::handle_contextmenu_clear (void) { relay_contextmenu_command ("clear", true); } void workspace_view::handle_contextmenu_disp (void) { relay_contextmenu_command ("disp"); } void workspace_view::handle_contextmenu_plot (void) { relay_contextmenu_command ("figure (); plot"); } void workspace_view::handle_contextmenu_stem (void) { relay_contextmenu_command ("figure (); stem"); } void workspace_view::handle_contextmenu_filter (void) { m_filter_shown = ! m_filter_shown; m_filter_widget->setVisible (m_filter_shown); set_filter_focus (m_filter_shown && m_filter_checkbox->isChecked ()); } void workspace_view::handle_model_changed (void) { // m_view->resizeRowsToContents (); // Just modify those rows that have been added rather than go through // the whole list. For-loop test will handle when number of rows reduced. QFontMetrics fm = m_view->fontMetrics (); int row_height = fm.height (); int new_row_count = m_filter_model.rowCount (); for (int i = m_view_previous_row_count; i < new_row_count; i++) m_view->setRowHeight (i, row_height); m_view_previous_row_count = new_row_count; } void workspace_view::copyClipboard () { if (m_view->hasFocus ()) handle_contextmenu_copy (); } void workspace_view::selectAll () { if (m_view->hasFocus ()) m_view->selectAll (); } void workspace_view::relay_contextmenu_command (const QString& cmdname, bool str) { QModelIndex index = m_view->currentIndex (); if (index.isValid ()) { QString var_name; if (str) var_name = "\'" + get_var_name (index) + "\'"; else var_name = get_var_name (index); emit command_requested (cmdname + " (" + var_name + ");"); } } QString workspace_view::get_var_name (const QModelIndex& index) { // We are using a sort model proxy so m_model won't provide the // correct ordering. QAbstractItemModel *m = m_view->model (); QMap<int, QVariant> item_data = m->itemData (index.sibling (index.row (), 0)); return item_data[0].toString (); } }