-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sidebarmodel.cpp
507 lines (452 loc) · 17.2 KB
/
sidebarmodel.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include "library/sidebarmodel.h"
#include <QApplication>
#include <QUrl>
#include <QtDebug>
#include "library/browse/browsefeature.h"
#include "library/libraryfeature.h"
#include "library/treeitem.h"
#include "moc_sidebarmodel.cpp"
#include "util/assert.h"
namespace {
// The time between selecting and activating (= clicking) a feature item
// in the sidebar tree. This is essential to allow smooth scrolling through
// a list of items with an encoder or the keyboard! A value of 300 ms has
// been chosen as a compromise between usability and responsiveness.
const int kPressedUntilClickedTimeoutMillis = 300;
} // anonymous namespace
SidebarModel::SidebarModel(
QObject* parent)
: QAbstractItemModel(parent),
m_iDefaultSelectedIndex(0),
m_pressedUntilClickedTimer(new QTimer(this)) {
m_pressedUntilClickedTimer->setSingleShot(true);
connect(m_pressedUntilClickedTimer,
&QTimer::timeout,
this,
&SidebarModel::slotPressedUntilClickedTimeout);
}
void SidebarModel::addLibraryFeature(LibraryFeature* feature) {
m_sFeatures.push_back(feature);
connect(feature,
&LibraryFeature::featureIsLoading,
this,
&SidebarModel::slotFeatureIsLoading);
connect(feature,
&LibraryFeature::featureLoadingFinished,
this,
&SidebarModel::slotFeatureLoadingFinished);
connect(feature,
&LibraryFeature::featureSelect,
this,
&SidebarModel::slotFeatureSelect);
QAbstractItemModel* model = feature->getChildModel();
connect(model,
&QAbstractItemModel::modelAboutToBeReset,
this,
&SidebarModel::slotModelAboutToBeReset);
connect(model,
&QAbstractItemModel::modelReset,
this,
&SidebarModel::slotModelReset);
connect(model,
&QAbstractItemModel::dataChanged,
this,
&SidebarModel::slotDataChanged);
connect(model,
&QAbstractItemModel::rowsAboutToBeInserted,
this,
&SidebarModel::slotRowsAboutToBeInserted);
connect(model,
&QAbstractItemModel::rowsAboutToBeRemoved,
this,
&SidebarModel::slotRowsAboutToBeRemoved);
connect(model,
&QAbstractItemModel::rowsInserted,
this,
&SidebarModel::slotRowsInserted);
connect(model,
&QAbstractItemModel::rowsRemoved,
this,
&SidebarModel::slotRowsRemoved);
}
QModelIndex SidebarModel::getDefaultSelection() {
if (m_sFeatures.size() == 0) {
return QModelIndex();
}
return createIndex(m_iDefaultSelectedIndex, 0, (void*)this);
}
void SidebarModel::setDefaultSelection(unsigned int index) {
m_iDefaultSelectedIndex = index;
}
void SidebarModel::activateDefaultSelection() {
if (m_iDefaultSelectedIndex <
static_cast<unsigned int>(m_sFeatures.size())) {
emit selectIndex(getDefaultSelection());
// Selecting an index does not activate it.
m_sFeatures[m_iDefaultSelectedIndex]->activate();
}
}
QModelIndex SidebarModel::index(int row, int column,
const QModelIndex& parent) const {
// qDebug() << "SidebarModel::index row=" << row
// << "column=" << column << "parent=" << parent.getData();
if (parent.isValid()) {
/* If we have selected the root of a library feature at position 'row'
* its internal pointer is the current sidebar object model
* we return its associated childmodel
*/
if (parent.internalPointer() == this) {
const QAbstractItemModel* childModel = m_sFeatures[parent.row()]->getChildModel();
QModelIndex childIndex = childModel->index(row, column);
TreeItem* tree_item = (TreeItem*)childIndex.internalPointer();
if (tree_item && childIndex.isValid()) {
return createIndex(childIndex.row(), childIndex.column(), (void*)tree_item);
} else {
return QModelIndex();
}
} else {
// We have selected an item within the childmodel
// This item has always an internal pointer of (sub)type TreeItem
TreeItem* tree_item = (TreeItem*)parent.internalPointer();
if (row < tree_item->childRows()) {
return createIndex(row, column, (void*) tree_item->child(row));
} else {
// Otherwise this row might have been removed just now
// (just a dirty workaround for unmaintainable GUI code)
return QModelIndex();
}
}
}
return createIndex(row, column, (void*)this);
}
QModelIndex SidebarModel::parent(const QModelIndex& index) const {
//qDebug() << "SidebarModel::parent index=" << index.getData();
if (index.isValid()) {
// If we have selected the root of a library feature
// its internal pointer is the current sidebar object model
// A root library feature has no parent and thus we return
// an invalid QModelIndex
if (index.internalPointer() == this) {
return QModelIndex();
} else {
TreeItem* tree_item = (TreeItem*)index.internalPointer();
if (tree_item == nullptr) {
return QModelIndex();
}
TreeItem* tree_item_parent = tree_item->parent();
// if we have selected an item at the first level of a childnode
if (tree_item_parent) {
if (tree_item_parent->isRoot()) {
LibraryFeature* feature = tree_item->feature();
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (feature == m_sFeatures[i]) {
// create a ModelIndex for parent 'this' having a
// library feature at position 'i'
return createIndex(i, 0, (void*)this);
}
}
}
// if we have selected an item at some deeper level of a childnode
return createIndex(tree_item_parent->parentRow(), 0 , tree_item_parent);
}
}
}
return QModelIndex();
}
int SidebarModel::rowCount(const QModelIndex& parent) const {
//qDebug() << "SidebarModel::rowCount parent=" << parent.getData();
if (parent.isValid()) {
if (parent.internalPointer() == this) {
return m_sFeatures[parent.row()]->getChildModel()->rowCount();
} else {
// We support tree models deeper than 1 level
TreeItem* tree_item = (TreeItem*)parent.internalPointer();
if (tree_item) {
return tree_item->childRows();
}
return 0;
}
}
return m_sFeatures.size();
}
int SidebarModel::columnCount(const QModelIndex& parent) const {
Q_UNUSED(parent);
//qDebug() << "SidebarModel::columnCount parent=" << parent;
// TODO(rryan) will we ever have columns? I don't think so.
return 1;
}
bool SidebarModel::hasChildren(const QModelIndex& parent) const {
if (parent.isValid()) {
if (parent.internalPointer() == this) {
return QAbstractItemModel::hasChildren(parent);
}
else
{
TreeItem* tree_item = (TreeItem*)parent.internalPointer();
if (tree_item) {
LibraryFeature* feature = tree_item->feature();
return feature->getChildModel()->hasChildren(parent);
}
}
}
return QAbstractItemModel::hasChildren(parent);
}
QVariant SidebarModel::data(const QModelIndex& index, int role) const {
// qDebug("SidebarModel::data row=%d column=%d pointer=%8x, role=%d",
// index.row(), index.column(), index.internalPointer(), role);
if (!index.isValid()) {
return QVariant();
}
if (index.internalPointer() == this) {
//If it points to SidebarModel
if (role == Qt::DisplayRole) {
return m_sFeatures[index.row()]->title();
} else if (role == Qt::DecorationRole) {
return m_sFeatures[index.row()]->getIcon();
}
}
if (index.internalPointer() != this) {
// If it points to a TreeItem
TreeItem* tree_item = (TreeItem*)index.internalPointer();
if (tree_item) {
if (role == Qt::DisplayRole) {
return tree_item->getLabel();
} else if (role == Qt::ToolTipRole) {
// If it's the "Quick Links" node, display it's name
if (tree_item->getData().toString() == QUICK_LINK_NODE) {
return tree_item->getLabel();
} else {
return tree_item->getData();
}
} else if (role == TreeItemModel::kDataRole) {
// We use Qt::UserRole to ask for the datapath.
return tree_item->getData();
} else if (role == Qt::FontRole) {
QFont font;
font.setBold(tree_item->isBold());
return font;
} else if (role == Qt::DecorationRole) {
return tree_item->getIcon();
}
}
}
return QVariant();
}
void SidebarModel::startPressedUntilClickedTimer(const QModelIndex& pressedIndex) {
m_pressedIndex = pressedIndex;
m_pressedUntilClickedTimer->start(kPressedUntilClickedTimeoutMillis);
}
void SidebarModel::stopPressedUntilClickedTimer() {
m_pressedUntilClickedTimer->stop();
m_pressedIndex = QModelIndex();
}
void SidebarModel::slotPressedUntilClickedTimeout() {
if (m_pressedIndex.isValid()) {
QModelIndex clickedIndex = m_pressedIndex;
stopPressedUntilClickedTimer();
clicked(clickedIndex);
}
}
void SidebarModel::pressed(const QModelIndex& index) {
stopPressedUntilClickedTimer();
if (index.isValid()) {
startPressedUntilClickedTimer(index);
}
}
void SidebarModel::clicked(const QModelIndex& index) {
// When triggered by a mouse event pressed() has been
// invoked immediately before. That doesn't matter,
// because we stop any running timer before handling
// this event.
stopPressedUntilClickedTimer();
if (index.isValid()) {
if (index.internalPointer() == this) {
m_sFeatures[index.row()]->activate();
} else {
TreeItem* tree_item = static_cast<TreeItem*>(index.internalPointer());
if (tree_item) {
LibraryFeature* feature = tree_item->feature();
DEBUG_ASSERT(feature);
feature->activateChild(index);
}
}
}
}
void SidebarModel::doubleClicked(const QModelIndex& index) {
stopPressedUntilClickedTimer();
if (index.isValid()) {
if (index.internalPointer() == this) {
return;
} else {
TreeItem* tree_item = (TreeItem*)index.internalPointer();
if (tree_item) {
LibraryFeature* feature = tree_item->feature();
feature->onLazyChildExpandation(index);
}
}
}
}
void SidebarModel::rightClicked(const QPoint& globalPos, const QModelIndex& index) {
stopPressedUntilClickedTimer();
if (index.isValid()) {
if (index.internalPointer() == this) {
m_sFeatures[index.row()]->onRightClick(globalPos);
}
else
{
TreeItem* tree_item = (TreeItem*)index.internalPointer();
if (tree_item) {
LibraryFeature* feature = tree_item->feature();
feature->onRightClickChild(globalPos, index);
}
}
}
}
bool SidebarModel::dropAccept(const QModelIndex& index, const QList<QUrl>& urls, QObject* pSource) {
//qDebug() << "SidebarModel::dropAccept() index=" << index << url;
bool result = false;
if (index.isValid()) {
if (index.internalPointer() == this) {
result = m_sFeatures[index.row()]->dropAccept(urls, pSource);
} else {
TreeItem* tree_item = (TreeItem*)index.internalPointer();
if (tree_item) {
LibraryFeature* feature = tree_item->feature();
result = feature->dropAcceptChild(index, urls, pSource);
}
}
}
return result;
}
bool SidebarModel::hasTrackTable(const QModelIndex& index) const {
if (index.internalPointer() == this) {
return m_sFeatures[index.row()]->hasTrackTable();
}
return false;
}
bool SidebarModel::dragMoveAccept(const QModelIndex& index, const QUrl& url) {
//qDebug() << "SidebarModel::dragMoveAccept() index=" << index << url;
bool result = false;
if (index.isValid()) {
if (index.internalPointer() == this) {
result = m_sFeatures[index.row()]->dragMoveAccept(url);
} else {
TreeItem* tree_item = (TreeItem*)index.internalPointer();
if (tree_item) {
LibraryFeature* feature = tree_item->feature();
result = feature->dragMoveAcceptChild(index, url);
}
}
}
return result;
}
// Translates an index from the child models to an index of the sidebar models
QModelIndex SidebarModel::translateSourceIndex(const QModelIndex& index) {
/* These method is called from the slot functions below.
* QObject::sender() return the object which emitted the signal
* handled by the slot functions.
* For child models, this always the child models itself
*/
const QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
VERIFY_OR_DEBUG_ASSERT(model != nullptr) {
return QModelIndex();
}
return translateIndex(index, model);
}
QModelIndex SidebarModel::translateIndex(
const QModelIndex& index, const QAbstractItemModel* model) {
QModelIndex translatedIndex;
if (index.isValid()) {
TreeItem* item = (TreeItem*)index.internalPointer();
translatedIndex = createIndex(index.row(), index.column(), item);
}
else
{
//Comment from Tobias Rafreider --> Dead Code????
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i]->getChildModel() == model) {
translatedIndex = createIndex(i, 0, (void*)this);
}
}
}
return translatedIndex;
}
void SidebarModel::slotDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) {
//qDebug() << "slotDataChanged topLeft:" << topLeft << "bottomRight:" << bottomRight;
QModelIndex topLeftTranslated = translateSourceIndex(topLeft);
QModelIndex bottomRightTranslated = translateSourceIndex(bottomRight);
emit dataChanged(topLeftTranslated, bottomRightTranslated);
}
void SidebarModel::slotRowsAboutToBeInserted(const QModelIndex& parent, int start, int end) {
//qDebug() << "slotRowsABoutToBeInserted" << parent << start << end;
QModelIndex newParent = translateSourceIndex(parent);
beginInsertRows(newParent, start, end);
}
void SidebarModel::slotRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end) {
//qDebug() << "slotRowsABoutToBeRemoved" << parent << start << end;
QModelIndex newParent = translateSourceIndex(parent);
beginRemoveRows(newParent, start, end);
}
void SidebarModel::slotRowsInserted(const QModelIndex& parent, int start, int end) {
Q_UNUSED(parent);
Q_UNUSED(start);
Q_UNUSED(end);
//qDebug() << "slotRowsInserted" << parent << start << end;
//QModelIndex newParent = translateSourceIndex(parent);
endInsertRows();
}
void SidebarModel::slotRowsRemoved(const QModelIndex& parent, int start, int end) {
Q_UNUSED(parent);
Q_UNUSED(start);
Q_UNUSED(end);
//qDebug() << "slotRowsRemoved" << parent << start << end;
//QModelIndex newParent = translateSourceIndex(parent);
endRemoveRows();
}
void SidebarModel::slotModelAboutToBeReset() {
beginResetModel();
}
void SidebarModel::slotModelReset() {
endResetModel();
}
/*
* Call this slot whenever the title of the feature has changed.
* See RhythmboxFeature for an example, in which the title becomes '(loading) Rhythmbox'
* If selectFeature is true, the feature is selected when the title change occurs.
*/
void SidebarModel::slotFeatureIsLoading(LibraryFeature * feature, bool selectFeature) {
featureRenamed(feature);
if (selectFeature) {
slotFeatureSelect(feature);
}
}
/* Tobias: This slot is somewhat redundant but I decided
* to leave it for code readability reasons
*/
void SidebarModel::slotFeatureLoadingFinished(LibraryFeature * feature) {
featureRenamed(feature);
slotFeatureSelect(feature);
}
void SidebarModel::featureRenamed(LibraryFeature* pFeature) {
for (int i=0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i] == pFeature) {
QModelIndex ind = index(i, 0);
emit dataChanged(ind, ind);
}
}
}
void SidebarModel::slotFeatureSelect(LibraryFeature* pFeature, const QModelIndex& featureIndex) {
QModelIndex ind;
if (featureIndex.isValid()) {
TreeItem* item = (TreeItem*)featureIndex.internalPointer();
ind = createIndex(featureIndex.row(), featureIndex.column(), item);
} else {
for (int i=0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i] == pFeature) {
ind = index(i, 0);
break;
}
}
}
emit selectIndex(ind);
}