-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsidebarmodel.cpp
More file actions
770 lines (685 loc) · 25.2 KB
/
Copy pathsidebarmodel.cpp
File metadata and controls
770 lines (685 loc) · 25.2 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include "library/sidebarmodel.h"
#include <QTimer>
#include <QUrl>
#include "library/libraryfeature.h"
#include "library/treeitem.h"
#include "library/treeitemmodel.h"
#include "moc_sidebarmodel.cpp"
#include "util/assert.h"
#include "util/cmdlineargs.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.
constexpr int kPressedUntilClickedTimeoutMillis = 300;
/// Debounce delay for saving sidebar selection to config. Avoids
/// excessive config writes while scrolling through items.
constexpr int kSaveDebounceMillis = 3000;
/// Enables additional debugging output.
constexpr bool kDebug = false;
const ConfigKey kLastSelectedFeatureConfigKey =
ConfigKey(QStringLiteral("[Library]"), QStringLiteral("last_selected_feature"));
const ConfigKey kLastSelectedChildConfigKey =
ConfigKey(QStringLiteral("[Library]"), QStringLiteral("last_selected_child"));
} // anonymous namespace
SidebarModel::SidebarModel(
UserSettingsPointer pConfig,
QObject* parent)
: QAbstractItemModel(parent),
m_iDefaultSelectedIndex(0),
m_pressedUntilClickedTimer(new QTimer(this)),
m_pConfig(pConfig),
m_saveTimer(new QTimer(this)) {
m_pressedUntilClickedTimer->setSingleShot(true);
connect(m_pressedUntilClickedTimer,
&QTimer::timeout,
this,
&SidebarModel::slotPressedUntilClickedTimeout);
m_saveTimer->setSingleShot(true);
connect(m_saveTimer,
&QTimer::timeout,
this,
&SidebarModel::performSave);
}
void SidebarModel::addLibraryFeature(LibraryFeature* pFeature) {
m_sFeatures.push_back(pFeature);
connect(pFeature,
&LibraryFeature::featureIsLoading,
this,
&SidebarModel::slotFeatureIsLoading);
connect(pFeature,
&LibraryFeature::featureLoadingFinished,
this,
&SidebarModel::slotFeatureLoadingFinished);
connect(pFeature,
&LibraryFeature::featureSelect,
this,
&SidebarModel::slotFeatureSelect);
QAbstractItemModel* model = pFeature->sidebarModel();
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, 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(), true /* scrollTo */);
// Selecting an index does not activate it.
m_sFeatures[m_iDefaultSelectedIndex]->activate();
}
}
QModelIndex SidebarModel::index(int row, int column,
const QModelIndex& parent) const {
if constexpr (kDebug) {
qDebug() << "SidebarModel::index row=" << row
<< "column=" << column << "parent=" << parent;
}
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()]->sidebarModel();
QModelIndex childIndex = childModel->index(row, column);
TreeItem* pTreeItem = static_cast<TreeItem*>(childIndex.internalPointer());
if (pTreeItem && childIndex.isValid()) {
return createIndex(childIndex.row(), childIndex.column(), pTreeItem);
} else {
return QModelIndex();
}
} else {
// We have selected an item within the childmodel
// This item has always an internal pointer of (sub)type TreeItem
TreeItem* pTreeItem = static_cast<TreeItem*>(parent.internalPointer());
if (row < pTreeItem->childRows()) {
return createIndex(row, column, pTreeItem->child(row));
} else {
// Otherwise this row might have been removed just now
// (just a dirty workaround for unmaintainable GUI code)
return QModelIndex();
}
}
}
// `this` is const, but the function expects a non-const pointer.
// TODO: Check if we can get rid of this const cast somehow.
return createIndex(row, column, const_cast<SidebarModel*>(this));
}
QModelIndex SidebarModel::getFeatureRootIndex(LibraryFeature* pFeature) {
if constexpr (kDebug) {
qDebug() << "SidebarModel::getFeatureRootIndex for" << pFeature->title().toString();
}
QModelIndex ind;
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i] == pFeature) {
ind = index(i, 0);
break;
}
}
return ind;
}
void SidebarModel::clear(const QModelIndex& index) {
if (index.internalPointer() == this) {
m_sFeatures[index.row()]->clear();
}
}
void SidebarModel::paste(const QModelIndex& index) {
if (index.internalPointer() == this) {
m_sFeatures[index.row()]->paste();
} else {
TreeItem* pTreeItem = (TreeItem*)index.internalPointer();
if (pTreeItem) {
LibraryFeature* feature = pTreeItem->feature();
feature->pasteChild(index);
}
}
}
QModelIndex SidebarModel::parent(const QModelIndex& index) const {
if constexpr (kDebug) {
qDebug() << "SidebarModel::parent index=" << index;
}
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* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (pTreeItem == nullptr) {
return QModelIndex();
}
TreeItem* pTreeItemParent = pTreeItem->parent();
// if we have selected an item at the first level of a childnode
if (pTreeItemParent) {
if (pTreeItemParent->isRoot()) {
LibraryFeature* pFeature = pTreeItem->feature();
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (pFeature == m_sFeatures[i]) {
// create a ModelIndex for parent 'this' having a
// library feature at position 'i'
// `this` is const, but the function expects a
// non-const pointer.
// TODO: Check if we can get rid of this const cast
// somehow.
return createIndex(i, 0, const_cast<SidebarModel*>(this));
}
}
}
// if we have selected an item at some deeper level of a childnode
return createIndex(pTreeItemParent->parentRow(), 0, pTreeItemParent);
}
}
}
return QModelIndex();
}
int SidebarModel::rowCount(const QModelIndex& parent) const {
if constexpr (kDebug) {
qDebug() << "SidebarModel::rowCount parent=" << parent;
}
if (parent.isValid()) {
if (parent.internalPointer() == this) {
return m_sFeatures[parent.row()]->sidebarModel()->rowCount();
} else {
// We support tree models deeper than 1 level
TreeItem* pTreeItem = static_cast<TreeItem*>(parent.internalPointer());
if (pTreeItem) {
return pTreeItem->childRows();
}
return 0;
}
}
return m_sFeatures.size();
}
int SidebarModel::columnCount(const QModelIndex& parent) const {
Q_UNUSED(parent);
if constexpr (kDebug) {
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* pTreeItem = static_cast<TreeItem*>(parent.internalPointer());
if (pTreeItem) {
LibraryFeature* pFeature = pTreeItem->feature();
return pFeature->sidebarModel()->hasChildren(parent);
}
}
}
return QAbstractItemModel::hasChildren(parent);
}
QVariant SidebarModel::data(const QModelIndex& index, int role) const {
if constexpr (kDebug) {
qDebug("SidebarModel::data() row=%d column=%d pointer=%p, role=%d",
index.row(),
index.column(),
index.internalPointer(),
role);
}
if (!index.isValid()) {
return QVariant();
}
if (index.internalPointer() == this) {
//If it points to SidebarModel
switch (role) {
case Qt::DisplayRole:
return m_sFeatures[index.row()]->title();
case Qt::DecorationRole:
return m_sFeatures[index.row()]->icon();
case SidebarModel::IconNameRole:
return m_sFeatures[index.row()]->iconName();
case Qt::FontRole: {
auto* pFeature = m_sFeatures[index.row()];
TreeItem* pTreeItem = nullptr;
auto* pChildModel = pFeature->sidebarModel();
if (pChildModel) {
pTreeItem = pChildModel->getRootItem();
}
QFont font;
if (pTreeItem) {
font.setBold(pTreeItem->isBold());
}
return font;
}
default:
return QVariant();
}
} else {
// If it points to a TreeItem
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (!pTreeItem) {
return QVariant();
}
switch (role) {
case Qt::DisplayRole:
return pTreeItem->getLabel();
case Qt::ToolTipRole: {
if (CmdlineArgs::Instance().getDeveloper()) {
// Display the internal data for debugging
return pTreeItem->getData();
}
// Show the label. Helpful for long names with a narrow sidebar.
return pTreeItem->getLabel();
}
case Qt::FontRole: {
QFont font;
font.setBold(pTreeItem->isBold());
return font;
}
case Qt::DecorationRole:
return pTreeItem->getIcon();
case SidebarModel::DataRole:
return pTreeItem->getData();
case SidebarModel::IconNameRole:
// TODO: Add support for icon names in tree items
default:
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);
}
}
/// Connected to WLibrarySidebar::pressed signal, called after left click and
/// selection change via keyboard or controller
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* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (pTreeItem) {
LibraryFeature* pFeature = pTreeItem->feature();
DEBUG_ASSERT(pFeature);
pFeature->activateChild(index);
}
}
scheduleSelectionSave(index);
}
}
/// Invoked by double click and click on tree node expand icons
void SidebarModel::doubleClicked(const QModelIndex& index) {
stopPressedUntilClickedTimer();
if (index.isValid()) {
if (index.internalPointer() == this) {
return;
} else {
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (pTreeItem) {
LibraryFeature* pFeature = pTreeItem->feature();
pFeature->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* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (pTreeItem) {
LibraryFeature* pFeature = pTreeItem->feature();
pFeature->onRightClickChild(globalPos, index);
}
}
}
}
void SidebarModel::renameItem(const QModelIndex& index) {
stopPressedUntilClickedTimer();
m_pressedIndex = index;
if (!index.isValid()) {
return;
}
if (index.internalPointer() == this) {
// can't rename root features
return;
} else {
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (pTreeItem) {
LibraryFeature* pFeature = pTreeItem->feature();
pFeature->renameItem(index);
}
}
}
void SidebarModel::deleteItem(const QModelIndex& index) {
stopPressedUntilClickedTimer();
m_pressedIndex = index;
if (!index.isValid()) {
return;
}
if (index.internalPointer() == this) {
// Used only to call AutoDJFeature::clear()
m_sFeatures[index.row()]->clear();
return;
} else {
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (pTreeItem) {
LibraryFeature* pFeature = pTreeItem->feature();
pFeature->deleteItem(index);
}
}
}
bool SidebarModel::dropAccept(const QModelIndex& index, const QList<QUrl>& urls, QObject* pSource) {
if constexpr (kDebug) {
qDebug() << "SidebarModel::dropAccept() index=" << index << urls;
}
if (!index.isValid()) {
return false;
}
if (index.internalPointer() == this) {
return m_sFeatures[index.row()]->dropAccept(urls, pSource);
} else {
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (!pTreeItem) {
return false;
}
LibraryFeature* pFeature = pTreeItem->feature();
return pFeature->dropAcceptChild(index, urls, pSource);
}
}
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 QList<QUrl>& urls) const {
if constexpr (kDebug) {
qDebug() << "SidebarModel::dragMoveAccept() index=" << index << urls;
}
if (!index.isValid()) {
return false;
}
if (index.internalPointer() == this) {
return m_sFeatures[index.row()]->dragMoveAccept(urls);
} else {
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
if (!pTreeItem) {
return false;
}
LibraryFeature* pFeature = pTreeItem->feature();
VERIFY_OR_DEBUG_ASSERT(pFeature) {
return false;
}
return pFeature->dragMoveAcceptChild(index, urls);
}
}
/// 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* pModel) {
QModelIndex translatedIndex;
if (index.isValid()) {
if (!index.parent().isValid()) {
// This is the top-level root item of the child model.
// Find the feature it belongs to
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i]->sidebarModel() == pModel) {
return createIndex(i, index.column(), this);
}
}
}
TreeItem* pItem = static_cast<TreeItem*>(index.internalPointer());
translatedIndex = createIndex(index.row(), index.column(), pItem);
} else {
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i]->sidebarModel() == pModel) {
translatedIndex = createIndex(i, 0, 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* pFeature, bool selectFeature) {
featureRenamed(pFeature);
if (selectFeature) {
slotFeatureSelect(pFeature);
}
}
/* Tobias: This slot is somewhat redundant but I decided
* to leave it for code readability reasons
*/
void SidebarModel::slotFeatureLoadingFinished(LibraryFeature* pFeature) {
featureRenamed(pFeature);
slotFeatureSelect(pFeature);
}
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,
bool scrollTo) {
QModelIndex ind;
if (featureIndex.isValid()) {
TreeItem* pTreeItem = static_cast<TreeItem*>(featureIndex.internalPointer());
ind = createIndex(featureIndex.row(), featureIndex.column(), pTreeItem);
} else {
for (int i=0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i] == pFeature) {
ind = index(i, 0);
break;
}
}
}
emit selectIndex(ind, scrollTo);
}
void SidebarModel::scheduleSelectionSave(const QModelIndex& index) {
if (!index.isValid()) {
return;
}
m_pendingSelection = index;
m_saveTimer->stop();
m_saveTimer->start(kSaveDebounceMillis);
}
void SidebarModel::performSave() {
if (m_pendingSelection.isValid()) {
saveSelectionToConfig(m_pendingSelection);
emit selectionSaved();
}
}
void SidebarModel::saveSelectionToConfig(const QModelIndex& index) {
if (!index.isValid() || !m_pConfig) {
return;
}
LibraryFeature* pFeature = nullptr;
if (index.internalPointer() == this) {
// Top-level feature row
pFeature = m_sFeatures[index.row()];
} else {
TreeItem* pTreeItem = static_cast<TreeItem*>(index.internalPointer());
VERIFY_OR_DEBUG_ASSERT(pTreeItem) {
return;
}
pFeature = pTreeItem->feature();
}
VERIFY_OR_DEBUG_ASSERT(pFeature) {
return;
}
// Save feature icon name for robust matching across sessions
m_pConfig->setValue(kLastSelectedFeatureConfigKey, pFeature->iconName());
// Save child data if it's a child item, clear otherwise
if (index.parent().isValid()) {
QVariant childData = index.data(DataRole);
if (childData.isValid()) {
m_pConfig->set(kLastSelectedChildConfigKey,
ConfigValue(childData.toString()));
} else {
m_pConfig->set(kLastSelectedChildConfigKey, ConfigValue());
}
} else {
m_pConfig->set(kLastSelectedChildConfigKey, ConfigValue());
}
}
bool SidebarModel::restoreLastSelection() {
if (!m_pConfig) {
return false;
}
QString savedFeatureIcon = m_pConfig->getValue(kLastSelectedFeatureConfigKey);
if (savedFeatureIcon.isEmpty()) {
return false;
}
// Find the feature by icon name
LibraryFeature* pTargetFeature = nullptr;
for (int i = 0; i < m_sFeatures.size(); ++i) {
if (m_sFeatures[i]->iconName() == savedFeatureIcon) {
pTargetFeature = m_sFeatures[i];
break;
}
}
if (!pTargetFeature) {
return false;
}
QString savedChildDataStr = m_pConfig->getValue(kLastSelectedChildConfigKey);
if (!savedChildDataStr.isEmpty() && pTargetFeature->sidebarModel()) {
// Try to convert to int first (for playlist/crate IDs), fallback to string
QVariant savedChildData;
bool ok;
int intValue = savedChildDataStr.toInt(&ok);
if (ok) {
savedChildData = intValue;
} else {
savedChildData = savedChildDataStr;
}
TreeItemModel* pChildModel = pTargetFeature->sidebarModel();
const QModelIndexList matches = pChildModel->match(
pChildModel->index(0, 0),
TreeItemModel::kDataRole,
savedChildData,
1,
Qt::MatchExactly | Qt::MatchRecursive);
if (!matches.isEmpty() && matches.first().isValid()) {
// selectAndActivate handles sidebar selection, tree expansion,
// scrolling, and library pane activation
pTargetFeature->selectAndActivate(matches.first());
return true;
}
}
// No child data or child not found — select and activate the feature root
pTargetFeature->selectAndActivate();
return true;
}