forked from shundhammer/qdirstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionModel.h
290 lines (239 loc) · 8.71 KB
/
SelectionModel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* File name: SelectionModel.h
* Summary: Handling of selected items
* License: GPL V2 - See file LICENSE for details.
*
* Author: Stefan Hundhammer <[email protected]>
*/
#ifndef SelectionModel_h
#define SelectionModel_h
#include <QItemSelectionModel>
#include "FileInfoSet.h"
namespace QDirStat
{
class FileInfo;
class DirTreeModel;
/**
* Selection model that can translate between QModelIndex and FileInfo
* pointers for use with a QModelIndex based Qt item view (e.g., a
* TreeView) and any QDirStat::DirTree based view (e.g., the
* QDirStat::TreeMapView).
*
* This is only a thin wrapper around QItemSelectionModel. The
* QItemSelectionModel base class is the master with its QModelIndex based
* selection; this subclass fetches that QModelIndex selection and
* translates each item into a FileInfo pointer on demand.
**/
class SelectionModel: public QItemSelectionModel
{
Q_OBJECT
public:
/**
* Create a SelectionModel that uses the DirTree in 'dirTreeModel'.
* This object does not take ownership of 'dirTreeModel'.
**/
SelectionModel( DirTreeModel * dirTreeModel, QObject * parent = 0 );
/**
* Destructor.
**/
virtual ~SelectionModel();
/**
* Return all currently selected items as a set.
**/
FileInfoSet selectedItems();
/**
* Return the current item (the one that has the keyboard focus).
* This might return 0 if currently no item has the keyboard focus.
**/
FileInfo * currentItem() const { return _currentItem; }
/**
* Return the DirTreeModel of this object.
**/
DirTreeModel * dirTreeModel() const { return _dirTreeModel; }
/**
* Set 'verbose' mode: Log each selection change.
**/
void setVerbose( bool verbose ) { _verbose = verbose; }
/**
* Return 'true' if verbose mode is set.
**/
bool verbose() const { return _verbose; }
public slots:
/**
* Replace the current selection with one item.
* If this item is 0, everything is deselected.
* This does NOT change the current item.
**/
void selectItem( FileInfo * item );
/**
* Extend the current selection with one item: Add this item to the set
* of selected items. If this item is 0, the selection remains
* unchanged.
*
* This does NOT change the current item.
*
* If 'clear' is 'true', this will clear the old selection first, so
* this has the same effect as selectItem().
**/
void extendSelection( FileInfo * item, bool clear = false );
/**
* Set the selected items, i.e., replace the complete selection.
**/
void setSelectedItems( const FileInfoSet & selectedItems );
/**
* Make 'item' the current item. This is different from the selection:
* There is one current item (mostly for the keyboard focus), but there
* can be any number of selected items.
*
* The current item can change the selection: In the tree view in
* 'extended selection' mode, [Shift]+[Click] extends the range of
* selected items (and makes the clicked item the current item),
* [Ctrl]+[Click] toggles the selected state of an item (and makes it
* the current item).
*
* 'item' may be 0. In that case, there is no current item.
*
* If 'select' is 'true', this also implicitly replaces the selection
* with this item, i.e. only this item is selected afterwards. If
* 'select' is 'false', the selection remains unchanged.
**/
void setCurrentItem( FileInfo * item, bool select = false );
/**
* Search the dir tree for an item with the specified path and, if
* successful, make it the current item.
*
* See also setCurrentItem( FileInfo *, bool ).
**/
void setCurrentItem( const QString & path );
/**
* Make 'item' the current branch. This is meant to notify connected
* tree views to close all other branches. See also the
* currentBranchChanged() signal.
**/
void setCurrentBranch( FileInfo * item );
/**
* Return the current branch or 0 if there is none.
**/
FileInfo * currentBranch() const { return _currentBranch; }
/**
* Prepare refreshing a set of items: Select a suitable item that will
* still be in the tree after refreshing is finished. The idea is to
* avoid having no selected item or branch which means having the tree
* widget jumping wildly and thus disorienting the user.
*
* This is done in preparation of refreshing subtrees after cleanup
* actions are finished. Refreshing subtrees means deleting the items
* in the subtrees.
*
* 'refreshSet' is the set of items that will be refreshed. Depending
* on the refresh policy in a cleanup action, this might be the items
* on which the cleanup action is performed, or their respective
* parents.
**/
void prepareRefresh( const FileInfoSet & refreshSet );
/**
* For debugging: Dump the currently selected items and the current
* item to the log.
**/
void dumpSelectedItems();
signals:
/**
* Emitted when the current item changes. 'newCurrent' is the new
* current item, 'oldCurrent' the previous one. Any of them might be 0.
**/
void currentItemChanged( FileInfo * newCurrent, FileInfo * oldCurrent );
/**
* Emitted when the selection changes.
**/
void selectionChanged();
void selectionChanged( const FileInfoSet & selectedItems );
/**
* Emitted when the current branch changes. Tree views can use this to
* close all other branches.
**/
void currentBranchChanged( const QModelIndex & newCurrentBranch );
void currentBranchChanged( FileInfo * newCurrentBranch );
protected slots:
/**
* Propagate the QModelIndex based currentChanged() signal to
* the FileInfo * based one
**/
void propagateCurrentChanged( const QModelIndex & newCurrent,
const QModelIndex & oldCurrent );
/**
* Propagate the QModelIndex based selectionChanged() signal to
* the FileInfo * based one
**/
void propagateSelectionChanged( const QItemSelection & selected,
const QItemSelection & deselected );
/**
* Clear all old contents.
**/
void clear();
/**
* Notification that a child is about to be deleted.
**/
void deletingChildNotify( FileInfo *deletedChild );
protected:
// Data members
DirTreeModel * _dirTreeModel;
FileInfo * _currentItem;
FileInfo * _currentBranch;
FileInfoSet _selectedItems;
bool _selectedItemsDirty;
bool _verbose;
}; // class SelectionModel
/**
* Proxy class for SelectionModel: Forward the relevant selection signals
* to a receiver.
*
* The basic idea behind this is to avoid signal ping-pong between the
* SelectionModel and any number of conncected view widgets:
*
* View A sends a "selectionChanged()" signal to the SelectionModel, the
* SelectionModel sends that signal to all connected widgets - including
* back to view A which initiated it, which then sends the signal again to
* the model etc. etc.
*
* With this proxy class, the view connects the "changed" signals not from
* the SelectionModel to itself, but from the SelectionModelProxy (which in
* turn connects the signals transparently from the master SelectionModel).
*
* Now if view A sends the signal, it first blocks signals from its
* SelectionModelProxy (preferably using a SignalBlocker), sends the signal
* and unblocks signals again from the proxy. This means that view A does
* not receive its own signals, but all other connected widgets do.
*
* If we'd just block all signals from the SelectionModel, the other
* widgets would not get notified at all. With this approach, only the
* connections from one widget are disabled temporarily.
*
* Of course, each view has to create and set up its own proxy. They cannot
* be shared among views.
**/
class SelectionModelProxy: public QObject
{
Q_OBJECT
public:
/**
* Creates a SelectionModelProxy. This automatically connects the
* master SelectionModel's signals to the matching signals of this
* object.
*
* 'parent' is the QObject tree parent for automatic deletion
* of this object when the parent is deleted.
**/
SelectionModelProxy( SelectionModel * master, QObject * parent = 0 );
signals:
// From QItemSelectionModel
void selectionChanged( const QItemSelection & selected, const QItemSelection & deselected );
void currentChanged ( const QModelIndex & current, const QModelIndex & previous );
void currentColumnChanged( const QModelIndex & current, const QModelIndex & previous );
void currentRowChanged ( const QModelIndex & current, const QModelIndex & previous );
// from SelectionModel
void selectionChanged();
void selectionChanged( const FileInfoSet & selectedItems );
void currentItemChanged( FileInfo * newCurrent, FileInfo * oldCurrent );
}; // class SelectionModelProxy
} // namespace QDirStat
#endif // SelectionModel_h