Mercurial > hg > octave-nkf
changeset 19735:e4b25475ef3d gui-release
provide a filter for the workspace view (bug #41222)
* workspace_view.cc (constructor): new combo-box for filter expressions,
new check box for enable/disable filter, connect related signals, get
last state of filter and check box from settings file;
(destructor): write state of filter and check box into settings file;
(setModel): use a filter proxy model between model and table view;
(filter_update): new slot for updating the filter expression;
(filter_activate): new slot enabling or disabling the filter;
(update_filter_history): new slot for saving the filter expression when
its edtitign has finished;
(handle_model_changed): actual row count is now taken from the filter model
* workspace_view.h: new slots filter_activate, filter_update, and
update_filter_history; new class variables for check box, filter combo box,
and filter model
author | Torsten <ttl@justmail.de> |
---|---|
date | Wed, 07 Jan 2015 18:16:28 +0100 |
parents | 9da61e3ecbe7 |
children | 77e58a7945b3 |
files | libgui/src/workspace-view.cc libgui/src/workspace-view.h |
diffstat | 2 files changed, 93 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/workspace-view.cc +++ b/libgui/src/workspace-view.cc @@ -35,6 +35,8 @@ #include <QVBoxLayout> #include <QPushButton> #include <QMenu> +#include <QLabel> +#include <QCompleter> #include "workspace-view.h" #include "resource-manager.h" @@ -48,9 +50,25 @@ set_title (tr ("Workspace")); setStatusTip (tr ("View the variables in the active workspace.")); + _filter = new QComboBox (this); + _filter->setToolTip (tr ("Enter the path or filename")); + _filter->setEditable (true); + _filter->setMaxCount (MaxFilterHistory); + _filter->setInsertPolicy (QComboBox::NoInsert); + _filter->setSizeAdjustPolicy ( + QComboBox::AdjustToMinimumContentsLengthWithIcon); + QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Maximum); + _filter->setSizePolicy (sizePol); + _filter->completer ()->setCaseSensitivity (Qt::CaseSensitive); + + QLabel *filter_label = new QLabel (tr ("Filter")); + + _filter_checkbox = new QCheckBox (); + view->setWordWrap (false); view->setContextMenuPolicy (Qt::CustomContextMenu); view->setShowGrid (false); + (view->verticalHeader) ()->hide (); view->setAlternatingRowColors (true); view_previous_row_count = 0; @@ -59,23 +77,46 @@ // Create a new layout and add widgets to it. QVBoxLayout *vbox_layout = new QVBoxLayout (); + QHBoxLayout *hbox_layout = new QHBoxLayout (); + hbox_layout->addWidget (filter_label); + hbox_layout->addWidget (_filter_checkbox); + hbox_layout->addWidget (_filter); + vbox_layout->addLayout (hbox_layout); vbox_layout->addWidget (view); vbox_layout->setMargin (2); // Set the empty widget to have our layout. widget ()->setLayout (vbox_layout); + // Filter model + _filter_model = new QSortFilterProxyModel (); + _filter_model->setFilterKeyColumn(0); + // Initialize collapse/expand state of the workspace subcategories. QSettings *settings = resource_manager::get_settings (); // Initialize column order and width of the workspace - view->horizontalHeader ()->restoreState ( settings->value ("workspaceview/column_state").toByteArray ()); + // Init state of the filter + _filter->addItems (settings->value ("workspaceview/mru_list").toStringList ()); + + bool filter_state = + settings->value ("workspaceview/filter_active", false).toBool (); + _filter_checkbox->setChecked (filter_state); + filter_activate (filter_state); + // Connect signals and slots. + connect (_filter, SIGNAL (editTextChanged (const QString&)), + this, SLOT (filter_update (const QString&))); + connect (_filter_checkbox, SIGNAL (toggled (bool)), + this, SLOT (filter_activate (bool))); + connect (_filter->lineEdit (), SIGNAL (editingFinished ()), + this, SLOT (update_filter_history ())); + connect (view, SIGNAL (customContextMenuRequested (const QPoint&)), this, SLOT (contextmenu_requested (const QPoint&))); @@ -90,13 +131,21 @@ settings->setValue ("workspaceview/column_state", view->horizontalHeader ()->saveState ()); + settings->setValue ("workspaceview/filter_active", + _filter_checkbox->isChecked ()); + + QStringList mru; + for (int i = 0; i < _filter->count (); i++) + mru.append (_filter->itemText (i)); + settings->setValue ("workspaceview/mru_list", mru); settings->sync (); } void workspace_view::setModel (workspace_model *model) { - view->setModel (model); + _filter_model->setSourceModel (model); + view->setModel (_filter_model); _model = model; } @@ -107,6 +156,34 @@ QDockWidget::closeEvent (e); } +void +workspace_view::filter_update (const QString& expression) +{ + _filter_model->setFilterRegExp (QRegExp (expression, + Qt::CaseSensitive, QRegExp::FixedString)); + handle_model_changed (); +} + +void +workspace_view::filter_activate (bool state) +{ + _filter->setEnabled (state); + _filter_model->setDynamicSortFilter (state); + + if (state) + filter_update (_filter->currentText ()); + else + filter_update (QString ()); +} + +void +workspace_view::update_filter_history () +{ + QString text = _filter->currentText (); + if (! text.isEmpty () && _filter->findText (text) == -1) + _filter->insertItem (0, _filter->currentText ()); +} + QString workspace_view::get_var_name (QModelIndex index) { @@ -256,11 +333,12 @@ void workspace_view::handle_model_changed (void) { +// 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 = view->fontMetrics (); int row_height = fm.height (); - int new_row_count = view->model ()->rowCount (); + int new_row_count = _filter_model->rowCount (); for (int i = view_previous_row_count; i < new_row_count; i++) view->setRowHeight (i, row_height); view_previous_row_count = new_row_count;
--- a/libgui/src/workspace-view.h +++ b/libgui/src/workspace-view.h @@ -27,6 +27,9 @@ #include <QItemDelegate> #include <QTableView> #include <QSemaphore> +#include <QComboBox> +#include <QSortFilterProxyModel> +#include <QCheckBox> #include "octave-dock-widget.h" #include "workspace-model.h" @@ -73,6 +76,10 @@ void copyClipboard (); void selectAll (); + void filter_update (const QString& expression); + void filter_activate (bool enable); + void update_filter_history (); + private: void relay_contextmenu_command (const QString& cmdname); @@ -81,6 +88,11 @@ QTableView *view; int view_previous_row_count; workspace_model *_model; + + QSortFilterProxyModel *_filter_model; + QCheckBox *_filter_checkbox; + QComboBox *_filter; + enum { MaxFilterHistory = 10 }; }; #endif