comparison libgui/src/workspace-view.cc @ 19572: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 2f0c21339e9d
children 77e58a7945b3
comparison
equal deleted inserted replaced
19568:9da61e3ecbe7 19572:e4b25475ef3d
33 #include <QHeaderView> 33 #include <QHeaderView>
34 #include <QHBoxLayout> 34 #include <QHBoxLayout>
35 #include <QVBoxLayout> 35 #include <QVBoxLayout>
36 #include <QPushButton> 36 #include <QPushButton>
37 #include <QMenu> 37 #include <QMenu>
38 #include <QLabel>
39 #include <QCompleter>
38 40
39 #include "workspace-view.h" 41 #include "workspace-view.h"
40 #include "resource-manager.h" 42 #include "resource-manager.h"
41 #include "symtab.h" 43 #include "symtab.h"
42 44
46 setObjectName ("WorkspaceView"); 48 setObjectName ("WorkspaceView");
47 setWindowIcon (QIcon (":/actions/icons/logo.png")); 49 setWindowIcon (QIcon (":/actions/icons/logo.png"));
48 set_title (tr ("Workspace")); 50 set_title (tr ("Workspace"));
49 setStatusTip (tr ("View the variables in the active workspace.")); 51 setStatusTip (tr ("View the variables in the active workspace."));
50 52
53 _filter = new QComboBox (this);
54 _filter->setToolTip (tr ("Enter the path or filename"));
55 _filter->setEditable (true);
56 _filter->setMaxCount (MaxFilterHistory);
57 _filter->setInsertPolicy (QComboBox::NoInsert);
58 _filter->setSizeAdjustPolicy (
59 QComboBox::AdjustToMinimumContentsLengthWithIcon);
60 QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Maximum);
61 _filter->setSizePolicy (sizePol);
62 _filter->completer ()->setCaseSensitivity (Qt::CaseSensitive);
63
64 QLabel *filter_label = new QLabel (tr ("Filter"));
65
66 _filter_checkbox = new QCheckBox ();
67
51 view->setWordWrap (false); 68 view->setWordWrap (false);
52 view->setContextMenuPolicy (Qt::CustomContextMenu); 69 view->setContextMenuPolicy (Qt::CustomContextMenu);
53 view->setShowGrid (false); 70 view->setShowGrid (false);
71 (view->verticalHeader) ()->hide ();
54 view->setAlternatingRowColors (true); 72 view->setAlternatingRowColors (true);
55 view_previous_row_count = 0; 73 view_previous_row_count = 0;
56 74
57 // Set an empty widget, so we can assign a layout to it. 75 // Set an empty widget, so we can assign a layout to it.
58 setWidget (new QWidget (this)); 76 setWidget (new QWidget (this));
59 77
60 // Create a new layout and add widgets to it. 78 // Create a new layout and add widgets to it.
61 QVBoxLayout *vbox_layout = new QVBoxLayout (); 79 QVBoxLayout *vbox_layout = new QVBoxLayout ();
80 QHBoxLayout *hbox_layout = new QHBoxLayout ();
81 hbox_layout->addWidget (filter_label);
82 hbox_layout->addWidget (_filter_checkbox);
83 hbox_layout->addWidget (_filter);
84 vbox_layout->addLayout (hbox_layout);
62 vbox_layout->addWidget (view); 85 vbox_layout->addWidget (view);
63 vbox_layout->setMargin (2); 86 vbox_layout->setMargin (2);
64 87
65 // Set the empty widget to have our layout. 88 // Set the empty widget to have our layout.
66 widget ()->setLayout (vbox_layout); 89 widget ()->setLayout (vbox_layout);
67 90
91 // Filter model
92 _filter_model = new QSortFilterProxyModel ();
93 _filter_model->setFilterKeyColumn(0);
94
68 // Initialize collapse/expand state of the workspace subcategories. 95 // Initialize collapse/expand state of the workspace subcategories.
69 96
70 QSettings *settings = resource_manager::get_settings (); 97 QSettings *settings = resource_manager::get_settings ();
71 98
72 // Initialize column order and width of the workspace 99 // Initialize column order and width of the workspace
73
74 view->horizontalHeader ()->restoreState ( 100 view->horizontalHeader ()->restoreState (
75 settings->value ("workspaceview/column_state").toByteArray ()); 101 settings->value ("workspaceview/column_state").toByteArray ());
76 102
103 // Init state of the filter
104 _filter->addItems (settings->value ("workspaceview/mru_list").toStringList ());
105
106 bool filter_state =
107 settings->value ("workspaceview/filter_active", false).toBool ();
108 _filter_checkbox->setChecked (filter_state);
109 filter_activate (filter_state);
110
77 // Connect signals and slots. 111 // Connect signals and slots.
112
113 connect (_filter, SIGNAL (editTextChanged (const QString&)),
114 this, SLOT (filter_update (const QString&)));
115 connect (_filter_checkbox, SIGNAL (toggled (bool)),
116 this, SLOT (filter_activate (bool)));
117 connect (_filter->lineEdit (), SIGNAL (editingFinished ()),
118 this, SLOT (update_filter_history ()));
78 119
79 connect (view, SIGNAL (customContextMenuRequested (const QPoint&)), 120 connect (view, SIGNAL (customContextMenuRequested (const QPoint&)),
80 this, SLOT (contextmenu_requested (const QPoint&))); 121 this, SLOT (contextmenu_requested (const QPoint&)));
81 122
82 connect (this, SIGNAL (command_requested (const QString&)), 123 connect (this, SIGNAL (command_requested (const QString&)),
88 { 129 {
89 QSettings *settings = resource_manager::get_settings (); 130 QSettings *settings = resource_manager::get_settings ();
90 131
91 settings->setValue ("workspaceview/column_state", 132 settings->setValue ("workspaceview/column_state",
92 view->horizontalHeader ()->saveState ()); 133 view->horizontalHeader ()->saveState ());
134 settings->setValue ("workspaceview/filter_active",
135 _filter_checkbox->isChecked ());
136
137 QStringList mru;
138 for (int i = 0; i < _filter->count (); i++)
139 mru.append (_filter->itemText (i));
140 settings->setValue ("workspaceview/mru_list", mru);
93 141
94 settings->sync (); 142 settings->sync ();
95 } 143 }
96 144
97 void workspace_view::setModel (workspace_model *model) 145 void workspace_view::setModel (workspace_model *model)
98 { 146 {
99 view->setModel (model); 147 _filter_model->setSourceModel (model);
148 view->setModel (_filter_model);
100 _model = model; 149 _model = model;
101 } 150 }
102 151
103 void 152 void
104 workspace_view::closeEvent (QCloseEvent *e) 153 workspace_view::closeEvent (QCloseEvent *e)
105 { 154 {
106 emit active_changed (false); 155 emit active_changed (false);
107 QDockWidget::closeEvent (e); 156 QDockWidget::closeEvent (e);
157 }
158
159 void
160 workspace_view::filter_update (const QString& expression)
161 {
162 _filter_model->setFilterRegExp (QRegExp (expression,
163 Qt::CaseSensitive, QRegExp::FixedString));
164 handle_model_changed ();
165 }
166
167 void
168 workspace_view::filter_activate (bool state)
169 {
170 _filter->setEnabled (state);
171 _filter_model->setDynamicSortFilter (state);
172
173 if (state)
174 filter_update (_filter->currentText ());
175 else
176 filter_update (QString ());
177 }
178
179 void
180 workspace_view::update_filter_history ()
181 {
182 QString text = _filter->currentText ();
183 if (! text.isEmpty () && _filter->findText (text) == -1)
184 _filter->insertItem (0, _filter->currentText ());
108 } 185 }
109 186
110 QString 187 QString
111 workspace_view::get_var_name (QModelIndex index) 188 workspace_view::get_var_name (QModelIndex index)
112 { 189 {
254 } 331 }
255 332
256 void 333 void
257 workspace_view::handle_model_changed (void) 334 workspace_view::handle_model_changed (void)
258 { 335 {
336 // view->resizeRowsToContents ();
259 // Just modify those rows that have been added rather than go through 337 // Just modify those rows that have been added rather than go through
260 // the whole list. For-loop test will handle when number of rows reduced. 338 // the whole list. For-loop test will handle when number of rows reduced.
261 QFontMetrics fm = view->fontMetrics (); 339 QFontMetrics fm = view->fontMetrics ();
262 int row_height = fm.height (); 340 int row_height = fm.height ();
263 int new_row_count = view->model ()->rowCount (); 341 int new_row_count = _filter_model->rowCount ();
264 for (int i = view_previous_row_count; i < new_row_count; i++) 342 for (int i = view_previous_row_count; i < new_row_count; i++)
265 view->setRowHeight (i, row_height); 343 view->setRowHeight (i, row_height);
266 view_previous_row_count = new_row_count; 344 view_previous_row_count = new_row_count;
267 } 345 }
268 346