view libgui/src/set-path-dialog.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 796f54d4ddbf
children ca7d58406f82
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019-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 <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QDirIterator>
#include <QFileDialog>
#include <QFileDialog>
#include <QFileInfo>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QListView>
#include <QMenu>
#include <QPushButton>
#include <QStatusBar>
#include <QTextStream>
#include <QTimer>
#include <QVBoxLayout>

#include "gui-preferences-pd.h"
#include "gui-settings.h"
#include "octave-qobject.h"
#include "octave-qtutils.h"
#include "set-path-dialog.h"
#include "set-path-model.h"

#include "ovl.h"

namespace octave
{
  set_path_dialog::set_path_dialog (QWidget *parent, base_qobject& oct_qobj)
    : QDialog (parent), m_octave_qobj (oct_qobj)
  {
    setWindowTitle (tr ("Set Path"));

    set_path_model *model = new set_path_model (this);

    m_info_label = new QLabel (tr ("All changes take effect immediately."));

    m_add_folder_button = new QPushButton (tr ("Add Folder..."));

    QMenu *add_dir_menu = new QMenu ();
    m_add_folder_button->setMenu (add_dir_menu);
    add_dir_menu->addAction (tr ("Single Folder"),
                             this, &set_path_dialog::add_dir);
    add_dir_menu->addAction (tr ("Folder With Subfolders"),
                             this, &set_path_dialog::add_dir_subdirs);

    m_move_to_top_button = new QPushButton (tr ("Move to Top"));
    m_move_to_bottom_button = new QPushButton (tr ("Move to Bottom"));
    m_move_up_button = new QPushButton (tr ("Move Up"));
    m_move_down_button = new QPushButton (tr ("Move Down"));
    m_remove_button = new QPushButton (tr ("Remove"));

    m_reload_button = new QPushButton (tr ("Reload"));
    m_save_button = new QPushButton (tr ("Save"));

    m_revert_button = new QPushButton (tr ("Revert"));

    QMenu *revert_menu = new QMenu ();
    m_revert_button->setMenu (revert_menu);
    revert_menu->addAction (tr ("Revert Last Change"),
                            model, &set_path_model::revert_last);
    revert_menu->addAction (tr ("Revert All Changes"),
                            model, &set_path_model::revert);

    m_save_button->setFocus ();

    connect (m_remove_button, &QPushButton::clicked,
             this, &set_path_dialog::rm_dir);

    connect (m_move_to_top_button, &QPushButton::clicked,
             this, &set_path_dialog::move_dir_top);

    connect (m_move_to_bottom_button, &QPushButton::clicked,
             this, &set_path_dialog::move_dir_bottom);

    connect (m_move_up_button, &QPushButton::clicked,
             this, &set_path_dialog::move_dir_up);

    connect (m_move_down_button, &QPushButton::clicked,
             this, &set_path_dialog::move_dir_down);

    connect (m_reload_button, &QPushButton::clicked,
             model, &set_path_model::path_to_model);

    connect (m_save_button, &QPushButton::clicked,
             model, &set_path_model::save);

    // Any interpreter_event signal from a set_path_model object is
    // handled the same as for the parent set_path_dialog object.

    connect (model, QOverload<const fcn_callback&>::of (&set_path_model::interpreter_event),
             this, QOverload<const fcn_callback&>::of (&set_path_dialog::interpreter_event));

    connect (model, QOverload<const meth_callback&>::of (&set_path_model::interpreter_event),
             this, QOverload<const meth_callback&>::of (&set_path_dialog::interpreter_event));

    m_path_list = new QListView (this);
    m_path_list->setWordWrap (false);
    m_path_list->setModel (model);
    m_path_list->setSelectionBehavior (QAbstractItemView::SelectRows);
    m_path_list->setSelectionMode (QAbstractItemView::ExtendedSelection);
    m_path_list->setAlternatingRowColors (true);
    m_path_list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // layout everything
    QDialogButtonBox *button_box = new QDialogButtonBox (Qt::Horizontal);
    button_box->addButton (m_save_button, QDialogButtonBox::ActionRole);
    button_box->addButton (m_reload_button, QDialogButtonBox::ActionRole);

    // add dialog close button
    m_close_button = button_box->addButton (QDialogButtonBox::Close);
    connect (button_box, &QDialogButtonBox::rejected,
             this, &set_path_dialog::close);

    button_box->addButton (m_revert_button, QDialogButtonBox::ActionRole);

    // path edit options
    QDialogButtonBox *path_edit_layout = new QDialogButtonBox (Qt::Vertical);
    path_edit_layout->addButton (m_add_folder_button, QDialogButtonBox::ActionRole);
    path_edit_layout->addButton (m_move_to_top_button, QDialogButtonBox::ActionRole);
    path_edit_layout->addButton (m_move_up_button, QDialogButtonBox::ActionRole);
    path_edit_layout->addButton (m_move_down_button, QDialogButtonBox::ActionRole);
    path_edit_layout->addButton (m_move_to_bottom_button, QDialogButtonBox::ActionRole);
    path_edit_layout->addButton (m_remove_button, QDialogButtonBox::ActionRole);

    // main layout
    QHBoxLayout *main_hboxlayout = new QHBoxLayout;
    main_hboxlayout->addWidget(path_edit_layout);
    main_hboxlayout->addWidget(m_path_list);

    QGridLayout *main_layout = new QGridLayout;
    main_layout->addWidget (m_info_label, 0, 0);
    main_layout->addLayout (main_hboxlayout, 1, 0);
    main_layout->addWidget (button_box, 2, 0);

    setLayout (main_layout);

    gui_settings settings;

    restoreGeometry (settings.value(pd_geometry.key).toByteArray());
  }

  void set_path_dialog::update_model (void)
  {
    set_path_model *m = static_cast<set_path_model *> (m_path_list->model ());
    m->path_to_model ();
  }

  void set_path_dialog::add_dir_common (bool subdirs)
  {
    QString dir
      = QFileDialog::getExistingDirectory (this, tr ("Open Directory"),
                                           "",
                                           (QFileDialog::ShowDirsOnly
                                            | QFileDialog::DontResolveSymlinks));

    if (! dir.isEmpty ())
      {
        if (subdirs)
          {
            // Use existing method mofifying load path and updating dialog
            // instead of adding string and updating load path
            emit modify_path_signal (QStringList (dir), false, true);
          }
        else
          {
            set_path_model *m
              = static_cast<set_path_model *> (m_path_list->model ());
            m->add_dir (dir);
          }
      }
  }

  void set_path_dialog::add_dir(void)
  {
    add_dir_common (false);
  }

  void set_path_dialog::add_dir_subdirs (void)
  {
    add_dir_common (true);
  }

  void set_path_dialog::rm_dir (void)
  {
    set_path_model *m = static_cast<set_path_model *> (m_path_list->model ());
    QItemSelectionModel *selmodel = m_path_list->selectionModel ();
    QModelIndexList indexlist = selmodel->selectedIndexes();
    m->rm_dir (indexlist);

    selmodel->clearSelection ();
  }

  void set_path_dialog::move_dir_up (void)
  {
    set_path_model *m = static_cast<set_path_model *> (m_path_list->model ());
    QItemSelectionModel *selmodel = m_path_list->selectionModel ();
    QModelIndexList indexlist = selmodel->selectedIndexes();
    m->move_dir_up (indexlist);

    // Update selection and view
    selmodel->clearSelection ();
    int min_row = m->rowCount () - 1;
    for (int i = 0; i < indexlist.length (); i++)
      {
        int new_row = std::max (indexlist.at (i).row () - 1, 0);
        min_row = std::min (min_row, new_row);
        selmodel->select (m->index (new_row), QItemSelectionModel::Select);
      }

    m_path_list->scrollTo (m->index (min_row));
  }

  void set_path_dialog::move_dir_down (void)
  {
    set_path_model *m = static_cast<set_path_model *> (m_path_list->model ());
    QItemSelectionModel *selmodel = m_path_list->selectionModel ();
    QModelIndexList indexlist = selmodel->selectedIndexes();
    m->move_dir_down (indexlist);

    // Update selection and view
    selmodel->clearSelection ();
    int max_row = 0;
    for (int i = 0; i < indexlist.length (); i++)
      {
        int new_row = std::min (indexlist.at (i).row () + 1, m->rowCount () - 1);
        max_row = std::max (max_row, new_row);
        selmodel->select (m->index (new_row), QItemSelectionModel::Select);
      }

    m_path_list->scrollTo (m->index (max_row));
  }

  void set_path_dialog::move_dir_top (void)
  {
    set_path_model *m = static_cast<set_path_model *> (m_path_list->model ());
    QItemSelectionModel *selmodel = m_path_list->selectionModel ();
    QModelIndexList indexlist = selmodel->selectedIndexes();
    m->move_dir_top (indexlist);

    // Update selection and view
    selmodel->clearSelection ();
    for (int i = 0; i < indexlist.length (); i++)
      selmodel->select (m->index (i), QItemSelectionModel::Select);

    m_path_list->scrollTo (m->index (0));
  }

  void set_path_dialog::move_dir_bottom (void)
  {
    set_path_model *m = static_cast<set_path_model *> (m_path_list->model ());
    QItemSelectionModel *selmodel = m_path_list->selectionModel ();
    QModelIndexList indexlist = selmodel->selectedIndexes();
    m->move_dir_bottom (indexlist);

    // Update selection and view
    selmodel->clearSelection ();
    int row_count = m->rowCount ();
    for (int i = 0; i < indexlist.length (); i++)
      selmodel->select (m->index (row_count - 1 - i),
                        QItemSelectionModel::Select);

    m_path_list->scrollTo (m->index (row_count - 1));
  }

  void set_path_dialog::save_settings (void)
  {
    gui_settings settings;

    settings.setValue (pd_geometry.key, saveGeometry ());
  }

  void set_path_dialog::closeEvent (QCloseEvent *e)
  {
    save_settings ();

    QWidget::closeEvent (e);
  }

}