This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathitemview.cpp
1919 lines (1755 loc) · 65.3 KB
/
itemview.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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "itemview.h"
#include "groupedview.h"
#include "tableview.h"
#ifdef ENABLE_CATEGORIZED_VIEW
#include "categorizedview.h"
#endif
#include "messageoverlay.h"
#include "models/roles.h"
#include "gui/covers.h"
#include "gui/stdactions.h"
#include "models/proxymodel.h"
#include "actionitemdelegate.h"
#include "basicitemdelegate.h"
#include "models/actionmodel.h"
#include "support/icon.h"
#include "config.h"
#include "support/gtkstyle.h"
#include "support/proxystyle.h"
#include "support/spinner.h"
#include "support/action.h"
#include "support/actioncollection.h"
#include "support/configuration.h"
#include "support/flattoolbutton.h"
#include "support/monoicon.h"
#include <QStyleOption>
#include <QStyle>
#include <QPainter>
#include <QAction>
#include <QTimer>
#include <QKeyEvent>
#include <QProxyStyle>
#include <QScrollBar>
#include <QDebug>
#define COVERS_DBUG if (Covers::verboseDebugEnabled()) qWarning() << metaObject()->className() << QThread::currentThread()->objectName() << __FUNCTION__
#define RESPONSIVE_LAYOUT
static int detailedViewDecorationSize=22;
static int simpleViewDecorationSize=16;
static int listCoverSize=22;
static int gridCoverSize=22;
static inline int adjust(int v)
{
if (v>48) {
static const int constStep=4;
return (((int)(v/constStep))*constStep)+((v%constStep) ? constStep : 0);
} else {
return Icon::stdSize(v);
}
}
void ItemView::setup()
{
int height=QApplication::fontMetrics().height();
if (height>22) {
detailedViewDecorationSize=Icon::stdSize(height*1.4);
simpleViewDecorationSize=Icon::stdSize(height);
} else {
detailedViewDecorationSize=22;
simpleViewDecorationSize=16;
}
listCoverSize=qMax(32, adjust(2*height));
gridCoverSize=qMax(128, adjust(8*height));
}
KeyEventHandler::KeyEventHandler(QAbstractItemView *v, QAction *a)
: QObject(v)
, view(v)
, deleteAct(a)
, interceptBackspace(qobject_cast<ListView *>(view))
{
}
bool KeyEventHandler::eventFilter(QObject *obj, QEvent *event)
{
if (view->hasFocus()) {
if (QEvent::KeyRelease==event->type()) {
QKeyEvent *keyEvent=static_cast<QKeyEvent *>(event);
if (deleteAct && Qt::Key_Delete==keyEvent->key() && Qt::NoModifier==keyEvent->modifiers()) {
deleteAct->trigger();
return true;
}
} else if (QEvent::KeyPress==event->type()) {
QKeyEvent *keyEvent=static_cast<QKeyEvent *>(event);
if (interceptBackspace && Qt::Key_Backspace==keyEvent->key() && Qt::NoModifier==keyEvent->modifiers()) {
emit backspacePressed();
}
}
}
return QObject::eventFilter(obj, event);
}
ViewEventHandler::ViewEventHandler(ActionItemDelegate *d, QAbstractItemView *v)
: KeyEventHandler(v, nullptr)
, delegate(d)
{
}
// HACK time. For some reason, IconView is not always re-drawn when mouse leaves the view.
// We sometimes get an item that is left in the mouse-over state. So, work-around this by
// keeping track of when mouse is over listview.
bool ViewEventHandler::eventFilter(QObject *obj, QEvent *event)
{
if (delegate) {
if (QEvent::Enter==event->type()) {
delegate->setUnderMouse(true);
view->viewport()->update();
} else if (QEvent::Leave==event->type()) {
delegate->setUnderMouse(false);
view->viewport()->update();
}
}
return KeyEventHandler::eventFilter(obj, event);
}
static const int constDevImageSize=32;
static inline double subTextAlpha(bool selected)
{
return selected ? 0.7 : 0.5;
}
static int zoomedSize(QListView *view, int size) {
return view
? qobject_cast<ListView *>(view)
? static_cast<ListView *>(view)->zoom()*size
#ifdef ENABLE_CATEGORIZED_VIEW
: qobject_cast<CategorizedView *>(view)
? static_cast<CategorizedView *>(view)->zoom()*size
#endif
: size
: size;
}
static QSize zoomedSize(QListView *view, QSize size) {
return view
? qobject_cast<ListView *>(view)
? static_cast<ListView *>(view)->zoom()*size
#ifdef ENABLE_CATEGORIZED_VIEW
: qobject_cast<CategorizedView *>(view)
? static_cast<CategorizedView *>(view)->zoom()*size
#endif
: size
: size;
}
class ListDelegate : public ActionItemDelegate
{
public:
ListDelegate(QListView *v, QAbstractItemView *p)
: ActionItemDelegate(p)
, view(v)
#ifdef RESPONSIVE_LAYOUT
, viewGap(Utils::scaleForDpi(8))
#endif
#ifdef ENABLE_CATEGORIZED_VIEW
, isCategorizedView(v && qobject_cast<CategorizedView *>(v))
#else
, isCategorizedView(false)
#endif
{
}
~ListDelegate() override
{
}
#ifdef RESPONSIVE_LAYOUT
// Calculate width for each item in IconMode. The idea is to have the icons evenly spaced out.
int calcItemWidth() const
{
int viewWidth = view->viewport()->width();
// KVantum returns a -ve number for spacing if using overlay scrollbars. I /think/ this
// is what messes the layout code. The subtracion below seems to work-around this - but
// give a larger right-hand margin!
int sbarSpacing = view->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing);
if (sbarSpacing<0) {
viewWidth-=3*viewGap;
} else {
QScrollBar *sb=view->verticalScrollBar();
int sbarWidth=sb && sb->isVisible() ? 0 : view->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
viewWidth-=sbarSpacing+sbarWidth;
}
int standardWidth = zoomedSize(view, gridCoverSize)+viewGap;
int iconWidth = standardWidth;
int viewCount = view->model() ? view->model()->rowCount(view->rootIndex()) : -1;
int numItems = viewWidth/iconWidth;
if (numItems>1 && viewCount>1 && (viewCount+1)>numItems) {
iconWidth = qMax(iconWidth-1, (int)(viewWidth/numItems)-1);
} else if (numItems==1) {
iconWidth = viewWidth;
}
return iconWidth;
}
#endif
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
Q_UNUSED(option)
if (view && QListView::IconMode==view->viewMode()) {
double textSpace = !isCategorizedView || view->model()->data(QModelIndex(), Cantata::Role_CatergizedHasSubText).toBool() ? 2.5 : 1.75;
#ifdef RESPONSIVE_LAYOUT
if (!isCategorizedView) {
return QSize(calcItemWidth(), zoomedSize(view, gridCoverSize)+(QApplication::fontMetrics().height()*textSpace));
}
#endif
return QSize(zoomedSize(view, gridCoverSize)+8, zoomedSize(view, gridCoverSize)+(QApplication::fontMetrics().height()*textSpace));
} else {
int imageSize = index.data(Cantata::Role_ListImage).toBool() ? listCoverSize : 0;
// TODO: Any point to checking one-line here? All models return sub-text...
// Things will be quicker if we dont call SubText here...
bool oneLine = false ; // index.data(Cantata::Role_SubText).toString().isEmpty();
bool showCapacity = !index.data(Cantata::Role_CapacityText).toString().isEmpty();
int textHeight = QApplication::fontMetrics().height()*(oneLine ? 1 : 2);
if (showCapacity) {
imageSize=constDevImageSize;
}
return QSize(qMax(64, imageSize) + (constBorder * 2),
qMax(textHeight, imageSize) + (constBorder*2) + (int)((showCapacity ? textHeight*Utils::smallFontFactor(QApplication::font()) : 0)+0.5));
}
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (!index.isValid()) {
return;
}
bool mouseOver=option.state&QStyle::State_MouseOver;
bool gtk=mouseOver && GtkStyle::isActive();
bool selected=option.state&QStyle::State_Selected;
bool active=option.state&QStyle::State_Active;
bool drawBgnd=true;
bool iconMode = view && QListView::IconMode==view->viewMode();
bool iconSubText = iconMode && (!isCategorizedView || view->model()->data(QModelIndex(), Cantata::Role_CatergizedHasSubText).toBool());
QStyleOptionViewItem opt(option);
opt.showDecorationSelected=true;
if (!isCategorizedView && !underMouse) {
if (mouseOver && !selected) {
drawBgnd=false;
}
mouseOver=false;
}
if (drawBgnd) {
if (mouseOver && gtk) {
GtkStyle::drawSelection(opt, painter, selected ? 0.75 : 0.25);
} else {
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, itemView());
}
}
QString capacityText=index.data(Cantata::Role_CapacityText).toString();
bool showCapacity = !capacityText.isEmpty();
QString text = iconMode ? index.data(Cantata::Role_BriefMainText).toString() : QString();
if (text.isEmpty()) {
text=index.data(Cantata::Role_MainText).toString();
}
if (text.isEmpty()) {
text=index.data(Qt::DisplayRole).toString();
}
#ifdef Q_OS_WIN
QColor textColor(option.palette.color(active ? QPalette::Active : QPalette::Inactive, QPalette::Text));
#else
QColor textColor(option.palette.color(active ? QPalette::Active : QPalette::Inactive, selected ? QPalette::HighlightedText : QPalette::Text));
#endif
QRect r(option.rect);
QRect r2(r);
QString childText = index.data(Cantata::Role_SubText).toString();
QPixmap pix;
if (showCapacity) {
const_cast<QAbstractItemModel *>(index.model())->setData(index, iconMode ? zoomedSize(view, gridCoverSize) : listCoverSize, Cantata::Role_Image);
pix=index.data(Cantata::Role_Image).value<QPixmap>();
}
if (pix.isNull() && (iconMode || index.data(Cantata::Role_ListImage).toBool())) {
Song cSong=index.data(iconMode ? Cantata::Role_GridCoverSong : Cantata::Role_CoverSong).value<Song>();
COVERS_DBUG << "Cover song" << cSong.albumArtist() << cSong.album << cSong.file << index.data().toString() << iconMode;
if (!cSong.isEmpty()) {
QPixmap *cp=Covers::self()->get(cSong, iconMode ? zoomedSize(view, gridCoverSize) : listCoverSize, getCoverInUiThread(index));
if (cp) {
pix=*cp;
}
}
}
if (pix.isNull()) {
COVERS_DBUG << "No cover image, use decoration";
int size=iconMode ? zoomedSize(view, gridCoverSize) : detailedViewDecorationSize;
pix=index.data(Qt::DecorationRole).value<QIcon>().pixmap(size, size, selected &&
textColor==qApp->palette().color(QPalette::HighlightedText)
? QIcon::Selected : QIcon::Normal);
}
bool oneLine = (iconMode && !iconSubText) || childText.isEmpty();
ActionPos actionPos = iconMode ? AP_VTop : AP_HMiddle;
bool rtl = QApplication::isRightToLeft();
if (childText==QLatin1String("-")) {
childText.clear();
}
painter->save();
//painter->setClipRect(r);
QFont textFont(index.data(Qt::FontRole).value<QFont>());
QFontMetrics textMetrics(textFont);
int textHeight=textMetrics.height();
if (showCapacity) {
r.adjust(2, 0, 0, -(textHeight+4));
}
if (AP_VTop==actionPos) {
r.adjust(constBorder, constBorder*4, -constBorder, -constBorder);
r2=r;
} else {
r.adjust(constBorder, 0, -constBorder, 0);
}
if (!pix.isNull()) {
QSize layoutSize = pix.size() / pix.DEVICE_PIXEL_RATIO();
int adjust=qMax(layoutSize.width(), layoutSize.height());
if (AP_VTop==actionPos) {
int xpos=r.x()+((r.width()-layoutSize.width())/2);
painter->drawPixmap(xpos, r.y(), layoutSize.width(), layoutSize.height(), pix);
QColor color(option.palette.color(active ? QPalette::Active : QPalette::Inactive, QPalette::Text));
double alphas[]={0.25, 0.125, 0.061};
QRect border(xpos, r.y(), layoutSize.width(), layoutSize.height());
QRect shadow(border);
for (int i=0; i<3; ++i) {
shadow.adjust(1, 1, 1, 1);
color.setAlphaF(alphas[i]);
painter->setPen(color);
painter->drawLine(shadow.bottomLeft()+QPoint(i+1, 0),
shadow.bottomRight()+QPoint(-((i*2)+2), 0));
painter->drawLine(shadow.bottomRight()+QPoint(0, -((i*2)+2)),
shadow.topRight()+QPoint(0, i+1));
if (1==i) {
painter->drawPoint(shadow.bottomRight()-QPoint(2, 1));
painter->drawPoint(shadow.bottomRight()-QPoint(1, 2));
painter->drawPoint(shadow.bottomLeft()-QPoint(1, 1));
painter->drawPoint(shadow.topRight()-QPoint(1, 1));
} else if (2==i) {
painter->drawPoint(shadow.bottomRight()-QPoint(4, 1));
painter->drawPoint(shadow.bottomRight()-QPoint(1, 4));
painter->drawPoint(shadow.bottomLeft()-QPoint(0, 1));
painter->drawPoint(shadow.topRight()-QPoint(1, 0));
painter->drawPoint(shadow.bottomRight()-QPoint(2, 2));
}
}
color.setAlphaF(0.4);
painter->setPen(color);
painter->drawRect(border.adjusted(0, 0, -1, -1));
r.adjust(0, adjust+3, 0, -3);
} else {
if (rtl) {
painter->drawPixmap(r.x()+r.width()-layoutSize.width(), r.y()+((r.height()-layoutSize.height())/2), layoutSize.width(), layoutSize.height(), pix);
r.adjust(3, 0, -(3+adjust), 0);
} else {
painter->drawPixmap(r.x()+2, r.y()+((r.height()-layoutSize.height())/2), layoutSize.width(), layoutSize.height(), pix);
r.adjust(adjust+5, 0, -3, 0);
}
}
}
QRect textRect;
QTextOption textOpt(AP_VTop==actionPos ? Qt::AlignHCenter|Qt::AlignVCenter : Qt::AlignVCenter);
QVariant col = index.data(Cantata::Role_TextColor);
if (col.isValid()) {
textColor = col.value<QColor>();
}
textOpt.setWrapMode(QTextOption::NoWrap);
if (oneLine) {
textRect=QRect(r.x(), r.y()+((r.height()-textHeight)/2), r.width(), textHeight);
text = textMetrics.elidedText(text, Qt::ElideRight, textRect.width(), QPalette::WindowText);
painter->setPen(textColor);
painter->setFont(textFont);
painter->drawText(textRect, text, textOpt);
} else {
QFont childFont(Utils::smallFont(textFont));
QFontMetrics childMetrics(childFont);
QRect childRect;
int childHeight=childMetrics.height();
int totalHeight=textHeight+childHeight;
textRect=QRect(r.x(), r.y()+((r.height()-totalHeight)/2), r.width(), textHeight);
childRect=QRect(r.x(), r.y()+textHeight+((r.height()-totalHeight)/2), r.width(),
(iconMode ? childHeight-(2*constBorder) : childHeight));
text = textMetrics.elidedText(text, Qt::ElideRight, textRect.width(), QPalette::WindowText);
painter->setPen(textColor);
painter->setFont(textFont);
painter->drawText(textRect, text, textOpt);
if (!childText.isEmpty()) {
childText = childMetrics.elidedText(childText, Qt::ElideRight, childRect.width(), QPalette::WindowText);
textColor.setAlphaF(subTextAlpha(selected));
painter->setPen(textColor);
painter->setFont(childFont);
painter->drawText(childRect, childText, textOpt);
}
}
if (showCapacity) {
QColor col(Qt::white);
col.setAlphaF(0.25);
painter->fillRect(QRect(r2.x(), r2.bottom()-(textHeight+8), r2.width(), textHeight+8), col);
QStyleOptionProgressBar opt;
double capacity=index.data(Cantata::Role_Capacity).toDouble();
if (capacity<0.0) {
capacity=0.0;
} else if (capacity>1.0) {
capacity=1.0;
}
opt.minimum=0;
opt.maximum=1000;
opt.progress=capacity*1000;
opt.textVisible=true;
opt.text=capacityText;
opt.rect=QRect(r2.x()+4, r2.bottom()-(textHeight+4), r2.width()-8, textHeight);
opt.state=QStyle::State_Enabled;
opt.palette=option.palette;
opt.direction=QApplication::layoutDirection();
opt.fontMetrics=textMetrics;
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, nullptr);
}
if (drawBgnd && mouseOver) {
QRect rect(AP_VTop==actionPos ? option.rect : r);
#ifdef RESPONSIVE_LAYOUT
if (!isCategorizedView && AP_VTop==actionPos) {
rect.adjust(0, 0, actionPosAdjust(), 0);
}
#endif
drawIcons(painter, rect, mouseOver, rtl, actionPos, index);
}
if (!iconMode) {
BasicItemDelegate::drawLine(painter, option.rect, textColor);
}
painter->restore();
}
virtual bool getCoverInUiThread(const QModelIndex &) const { return false; }
virtual QWidget * itemView() const { return view; }
#ifdef RESPONSIVE_LAYOUT
int actionPosAdjust() const {
return !isCategorizedView && view && QListView::IconMode==view->viewMode() ? -(((calcItemWidth()-(zoomedSize(view, gridCoverSize)+viewGap))/2.0)+0.5) : 0;
}
#endif
protected:
QListView *view;
#ifdef RESPONSIVE_LAYOUT
int viewGap;
#endif
bool isCategorizedView;
};
class TreeDelegate : public ListDelegate
{
public:
TreeDelegate(QAbstractItemView *p)
: ListDelegate(nullptr, p)
, simpleStyle(true)
, treeView(p)
{
}
~TreeDelegate() override
{
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (noIcons) {
return QStyledItemDelegate::sizeHint(option, index);
}
if (!simpleStyle || !index.data(Cantata::Role_CapacityText).toString().isEmpty()) {
return ListDelegate::sizeHint(option, index);
}
QSize sz(QStyledItemDelegate::sizeHint(option, index));
if (index.data(Cantata::Role_ListImage).toBool()) {
sz.setHeight(qMax(sz.height(), listCoverSize));
}
int textHeight = QApplication::fontMetrics().height()*1.25;
sz.setHeight(qMax(sz.height(), textHeight)+(constBorder*2));
return sz;
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (!index.isValid()) {
return;
}
if (!simpleStyle || !index.data(Cantata::Role_CapacityText).toString().isEmpty()) {
ListDelegate::paint(painter, option, index);
return;
}
QStringList text=index.data(Qt::DisplayRole).toString().split(Song::constFieldSep);
bool gtk=GtkStyle::isActive();
bool rtl = QApplication::isRightToLeft();
bool selected=option.state&QStyle::State_Selected;
bool active=option.state&QStyle::State_Active;
bool mouseOver=underMouse && option.state&QStyle::State_MouseOver;
#ifdef Q_OS_WIN
QColor textColor(option.palette.color(active ? QPalette::Active : QPalette::Inactive, QPalette::Text));
#else
QColor textColor(option.palette.color(active ? QPalette::Active : QPalette::Inactive, selected ? QPalette::HighlightedText : QPalette::Text));
#endif
if (mouseOver && gtk) {
GtkStyle::drawSelection(option, painter, selected ? 0.75 : 0.25);
} else {
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, itemView());
}
QRect r(option.rect);
r.adjust(4, 0, -4, 0);
if (!noIcons) {
QPixmap pix;
if (index.data(Cantata::Role_ListImage).toBool()) {
Song cSong=index.data(Cantata::Role_CoverSong).value<Song>();
if (!cSong.isEmpty()) {
QPixmap *cp=Covers::self()->get(cSong, listCoverSize);
if (cp) {
pix=*cp;
}
}
}
if (pix.isNull()) {
pix=index.data(Qt::DecorationRole).value<QIcon>().pixmap(simpleViewDecorationSize, simpleViewDecorationSize,
selected &&
textColor==qApp->palette().color(QPalette::HighlightedText)
? QIcon::Selected : QIcon::Normal);
}
if (!pix.isNull()) {
QSize layoutSize = pix.size() / pix.DEVICE_PIXEL_RATIO();
int adjust=qMax(layoutSize.width(), layoutSize.height());
if (rtl) {
painter->drawPixmap(r.x()+r.width()-layoutSize.width(), r.y()+((r.height()-layoutSize.height())/2), layoutSize.width(), layoutSize.height(), pix);
r.adjust(3, 0, -(3+adjust), 0);
} else {
painter->drawPixmap(r.x(), r.y()+((r.height()-layoutSize.height())/2), layoutSize.width(), layoutSize.height(), pix);
r.adjust(adjust+3, 0, -3, 0);
}
}
}
QVariant col = index.data(Cantata::Role_TextColor);
if (col.isValid()) {
textColor = col.value<QColor>();
}
if (text.count()>0) {
QFont textFont(QApplication::font());
QFontMetrics textMetrics(textFont);
int textHeight=textMetrics.height();
QTextOption textOpt(Qt::AlignVCenter);
QRect textRect(r.x(), r.y()+((r.height()-textHeight)/2), r.width(), textHeight);
QString str=textMetrics.elidedText(text.at(0), Qt::ElideRight, textRect.width(), QPalette::WindowText);
painter->save();
painter->setPen(textColor);
painter->setFont(textFont);
painter->drawText(textRect, str, textOpt);
if (text.count()>1) {
int mainWidth=textMetrics.horizontalAdvance(str);
text.takeFirst();
str=text.join(Song::constSep);
textRect=QRect(r.x()+(mainWidth+8), r.y()+((r.height()-textHeight)/2), r.width()-(mainWidth+8), textHeight);
if (textRect.width()>4) {
str = textMetrics.elidedText(str, Qt::ElideRight, textRect.width(), QPalette::WindowText);
textColor.setAlphaF(subTextAlpha(selected));
painter->setPen(textColor);
painter->drawText(textRect, str, textOpt/*QTextOption(Qt::AlignVCenter|Qt::AlignRight)*/);
}
}
painter->restore();
}
if (mouseOver) {
drawIcons(painter, option.rect, mouseOver, rtl, AP_HMiddle, index);
}
#ifdef Q_OS_WIN
BasicItemDelegate::drawLine(painter, option.rect, option.palette.color(active ? QPalette::Active : QPalette::Inactive,
QPalette::Text));
#else
BasicItemDelegate::drawLine(painter, option.rect, option.palette.color(active ? QPalette::Active : QPalette::Inactive,
selected ? QPalette::HighlightedText : QPalette::Text));
#endif
}
void setSimple(bool s) { simpleStyle=s; }
void setNoIcons(bool n) { noIcons=n; }
bool getCoverInUiThread(const QModelIndex &idx) const override
{
// Want album covers in artists view to load quickly...
return idx.isValid() && idx.data(Cantata::Role_LoadCoverInUIThread).toBool();
}
QWidget * itemView() const override { return treeView; }
bool simpleStyle;
bool noIcons;
QAbstractItemView *treeView;
};
ItemView::Mode ItemView::toMode(const QString &str)
{
for (int i=0; i<Mode_Count; ++i) {
if (modeStr((Mode)i)==str) {
return (Mode)i;
}
}
// Older versions of Cantata saved mode as an integer!!!
int i=str.toInt();
switch (i) {
default:
case 0: return Mode_SimpleTree;
case 1: return Mode_List;
case 2: return Mode_IconTop;
case 3: return Mode_GroupedTree;
}
}
QString ItemView::modeStr(Mode m)
{
switch (m) {
default:
case Mode_SimpleTree: return QLatin1String("simpletree");
case Mode_BasicTree: return QLatin1String("basictree");
case Mode_DetailedTree: return QLatin1String("detailedtree");
case Mode_List: return QLatin1String("list");
case Mode_IconTop: return QLatin1String("icontop");
case Mode_GroupedTree: return QLatin1String("grouped");
case Mode_Table: return QLatin1String("table");
#ifdef ENABLE_CATEGORIZED_VIEW
case Mode_Categorized: return QLatin1String("categorized");
#endif
}
}
static const char *constPageProp="page";
static const char *constAlwaysShowProp="always";
static Action *backAction=nullptr;
static const QLatin1String constZoomKey("gridZoom");
const QLatin1String ItemView::constSearchActiveKey("searchActive");
const QLatin1String ItemView::constViewModeKey("viewMode");
const QLatin1String ItemView::constStartClosedKey("startClosed");
const QLatin1String ItemView::constSearchCategoryKey("searchCategory");
static const double constMinZoom = 0.5;
static const double constMaxZoom = 4.0;
static const double constZoomStep = 0.25;
ItemView::ItemView(QWidget *p)
: QWidget(p)
, searchTimer(nullptr)
, itemModel(nullptr)
, currentLevel(0)
, mode(Mode_SimpleTree)
, groupedView(nullptr)
, tableView(nullptr)
#ifdef ENABLE_CATEGORIZED_VIEW
, categorizedView(nullptr)
#endif
, spinner(nullptr)
, msgOverlay(nullptr)
, performedSearch(false)
, searchResetLevel(0)
, openFirstLevelAfterSearch(false)
, initialised(false)
, minSearchDebounce(250)
{
setupUi(this);
if (!backAction) {
backAction=ActionCollection::get()->createAction("itemview-goback", tr("Go Back"));
backAction->setShortcut(Qt::AltModifier+(Qt::LeftToRight==layoutDirection() ? Qt::Key_Left : Qt::Key_Right));
}
title->addAction(backAction);
title->setVisible(false);
Action::updateToolTip(backAction);
QAction *sep=new QAction(this);
sep->setSeparator(true);
listView->addAction(sep);
treeView->setPageDefaults();
// Some styles, eg Cleanlooks/Plastique require that we explicitly set mouse tracking on the treeview.
treeView->setAttribute(Qt::WA_MouseTracking);
iconGridSize=listGridSize=listView->gridSize();
ListDelegate *ld=new ListDelegate(listView, listView);
TreeDelegate *td=new TreeDelegate(treeView);
listView->setItemDelegate(ld);
treeView->setItemDelegate(td);
listView->setProperty(ProxyStyle::constModifyFrameProp, ProxyStyle::VF_Side|ProxyStyle::VF_Top);
treeView->setProperty(ProxyStyle::constModifyFrameProp, ProxyStyle::VF_Side|ProxyStyle::VF_Top);
ViewEventHandler *listViewEventHandler=new ViewEventHandler(ld, listView);
ViewEventHandler *treeViewEventHandler=new ViewEventHandler(td, treeView);
listView->installFilter(listViewEventHandler);
treeView->installFilter(treeViewEventHandler);
connect(searchWidget, SIGNAL(returnPressed()), this, SLOT(doSearch()));
connect(searchWidget, SIGNAL(textChanged(const QString)), this, SLOT(delaySearchItems()));
connect(searchWidget, SIGNAL(active(bool)), this, SLOT(searchActive(bool)));
connect(treeView, SIGNAL(itemsSelected(bool)), this, SIGNAL(itemsSelected(bool)));
connect(treeView, SIGNAL(itemActivated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
connect(treeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SIGNAL(doubleClicked(const QModelIndex &)));
connect(treeView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
connect(listView, SIGNAL(itemsSelected(bool)), this, SIGNAL(itemsSelected(bool)));
connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(activateItem(const QModelIndex &)));
connect(listView, SIGNAL(itemDoubleClicked(const QModelIndex &)), this, SIGNAL(doubleClicked(const QModelIndex &)));
connect(listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
connect(backAction, SIGNAL(triggered()), this, SLOT(backActivated()));
connect(listViewEventHandler, SIGNAL(backspacePressed()), this, SLOT(backActivated()));
connect(title, SIGNAL(addToPlayQueue()), this, SLOT(addTitleButtonClicked()));
connect(title, SIGNAL(replacePlayQueue()), this, SLOT(replaceTitleButtonClicked()));
connect(Covers::self(), SIGNAL(loaded(Song,int)), this, SLOT(coverLoaded(Song,int)));
searchWidget->setVisible(false);
#ifdef Q_OS_MAC
treeView->setAttribute(Qt::WA_MacShowFocusRect, 0);
listView->setAttribute(Qt::WA_MacShowFocusRect, 0);
#endif
QWidget::addAction(StdActions::self()->zoomInAction);
QWidget::addAction(StdActions::self()->zoomOutAction);
connect(StdActions::self()->zoomInAction, SIGNAL(triggered()), SLOT(zoomIn()));
connect(StdActions::self()->zoomOutAction, SIGNAL(triggered()), SLOT(zoomOut()));
}
ItemView::~ItemView()
{
}
void ItemView::alwaysShowHeader()
{
title->setVisible(true);
title->setProperty(constAlwaysShowProp, true);
setTitle();
controlViewFrame();
}
void ItemView::load(Configuration &config)
{
if (config.get(constSearchActiveKey, false)) {
focusSearch();
}
setMode(toMode(config.get(constViewModeKey, modeStr(mode))));
setStartClosed(config.get(constStartClosedKey, isStartClosed()));
setSearchCategory(config.get(constSearchCategoryKey, searchCategory()));
int zoom=config.get(constZoomKey, 100);
if (zoom>(constMinZoom*100)) {
listView->setZoom(zoom/100.0);
}
}
void ItemView::save(Configuration &config)
{
config.set(constSearchActiveKey, searchWidget->isActive());
config.set(constViewModeKey, modeStr(mode));
config.set(constZoomKey, (int)(listView->zoom()*100));
if (groupedView) {
config.set(constStartClosedKey, isStartClosed());
}
TableView *tv=qobject_cast<TableView *>(view());
if (tv) {
tv->saveHeader();
}
QString cat=searchCategory();
if (!cat.isEmpty()) {
config.set(constSearchCategoryKey, cat);
}
}
void ItemView::allowGroupedView()
{
if (!groupedView) {
groupedView=new GroupedView(stackedWidget);
stackedWidget->addWidget(groupedView);
groupedView->setProperty(constPageProp, stackedWidget->count()-1);
// Some styles, eg Cleanlooks/Plastique require that we explicitly set mouse tracking on the treeview.
groupedView->setAttribute(Qt::WA_MouseTracking, true);
ViewEventHandler *viewHandler=new ViewEventHandler(qobject_cast<ActionItemDelegate *>(groupedView->itemDelegate()), groupedView);
groupedView->installFilter(viewHandler);
groupedView->setAutoExpand(false);
groupedView->setMultiLevel(true);
connect(groupedView, SIGNAL(itemsSelected(bool)), this, SIGNAL(itemsSelected(bool)));
connect(groupedView, SIGNAL(itemActivated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
connect(groupedView, SIGNAL(doubleClicked(const QModelIndex &)), this, SIGNAL(doubleClicked(const QModelIndex &)));
connect(groupedView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
groupedView->setProperty(ProxyStyle::constModifyFrameProp, ProxyStyle::VF_Side|ProxyStyle::VF_Top);
#ifdef Q_OS_MAC
groupedView->setAttribute(Qt::WA_MacShowFocusRect, 0);
#endif
}
}
void ItemView::allowTableView(TableView *v)
{
if (!tableView) {
tableView=v;
tableView->setParent(stackedWidget);
stackedWidget->addWidget(tableView);
tableView->setProperty(constPageProp, stackedWidget->count()-1);
ViewEventHandler *viewHandler=new ViewEventHandler(nullptr, tableView);
tableView->installFilter(viewHandler);
connect(tableView, SIGNAL(itemsSelected(bool)), this, SIGNAL(itemsSelected(bool)));
connect(tableView, SIGNAL(itemActivated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
connect(tableView, SIGNAL(doubleClicked(const QModelIndex &)), this, SIGNAL(doubleClicked(const QModelIndex &)));
connect(tableView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
tableView->setProperty(ProxyStyle::constModifyFrameProp, ProxyStyle::VF_Side|ProxyStyle::VF_Top);
#ifdef Q_OS_MAC
tableView->setAttribute(Qt::WA_MacShowFocusRect, 0);
#endif
}
}
void ItemView::allowCategorized()
{
#ifdef ENABLE_CATEGORIZED_VIEW
if (!categorizedView) {
categorizedView=new CategorizedView(stackedWidget);
categorizedView->setParent(stackedWidget);
stackedWidget->addWidget(categorizedView);
categorizedView->setProperty(constPageProp, stackedWidget->count()-1);
//KCategorizedView seems to handle mouse-over events better
//ViewEventHandler *viewHandler=new ViewEventHandler(nullptr, categorizedView);
//categorizedView->installFilter(viewHandler);
connect(categorizedView, SIGNAL(itemsSelected(bool)), this, SIGNAL(itemsSelected(bool)));
connect(categorizedView, SIGNAL(itemActivated(const QModelIndex &)), this, SLOT(activateItem(const QModelIndex &)));
connect(categorizedView, SIGNAL(itemDoubleClicked(const QModelIndex &)), this, SIGNAL(doubleClicked(const QModelIndex &)));
connect(categorizedView, SIGNAL(itemClicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
categorizedView->setProperty(ProxyStyle::constModifyFrameProp, ProxyStyle::VF_Side|ProxyStyle::VF_Top);
#ifdef Q_OS_MAC
categorizedView->setAttribute(Qt::WA_MacShowFocusRect, 0);
#endif
categorizedView->setItemDelegate(new ListDelegate(categorizedView, categorizedView));
}
#endif
}
void ItemView::addAction(QAction *act)
{
treeView->addAction(act);
listView->addAction(act);
if (groupedView) {
groupedView->addAction(act);
}
if (tableView) {
tableView->addAction(act);
}
#ifdef ENABLE_CATEGORIZED_VIEW
if (categorizedView) {
categorizedView->addAction(act);
}
#endif
}
void ItemView::addSeparator()
{
QAction *act=new QAction(this);
act->setSeparator(true);
addAction(act);
}
void ItemView::setMode(Mode m)
{
initialised=true;
if (m<0 || m>=Mode_Count || (Mode_GroupedTree==m && !groupedView) || (Mode_Table==m && !tableView)
#ifdef ENABLE_CATEGORIZED_VIEW
|| (Mode_Categorized==m && !categorizedView)
#endif
) {
m=Mode_SimpleTree;
}
if (m==mode) {
return;
}
prevTopIndex.clear();
searchWidget->setText(QString());
if (!title->property(constAlwaysShowProp).toBool()) {
title->setVisible(false);
controlViewFrame();
}
QIcon oldBgndIcon=bgndIcon;
if (!bgndIcon.isNull()) {
setBackgroundImage(QIcon());
}
bool wasListView = usingListView();
mode=m;
int stackIndex=0;
if (usingTreeView()) {
listView->setModel(nullptr);
if (groupedView) {
groupedView->setModel(nullptr);
}
if (tableView) {
tableView->saveHeader();
tableView->setModel(nullptr);
}
#ifdef ENABLE_CATEGORIZED_VIEW
if (categorizedView) {
categorizedView->setModel(nullptr);
}
#endif
treeView->setModel(itemModel);
treeView->setHidden(false);
static_cast<TreeDelegate *>(treeView->itemDelegate())->setSimple(Mode_SimpleTree==mode || Mode_BasicTree==mode);
static_cast<TreeDelegate *>(treeView->itemDelegate())->setNoIcons(Mode_BasicTree==mode);
if (dynamic_cast<ProxyModel *>(itemModel)) {
static_cast<ProxyModel *>(itemModel)->setRootIndex(QModelIndex());
}
treeView->reset();
} else if (Mode_GroupedTree==mode) {
treeView->setModel(nullptr);
listView->setModel(nullptr);
if (tableView) {
tableView->saveHeader();
tableView->setModel(nullptr);
}
#ifdef ENABLE_CATEGORIZED_VIEW
if (categorizedView) {
categorizedView->setModel(nullptr);
}
#endif
groupedView->setHidden(false);
treeView->setHidden(true);
groupedView->setModel(itemModel);
if (dynamic_cast<ProxyModel *>(itemModel)) {
static_cast<ProxyModel *>(itemModel)->setRootIndex(QModelIndex());
}
stackIndex=groupedView->property(constPageProp).toInt();
} else if (Mode_Table==mode) {
int w=view()->width();
treeView->setModel(nullptr);
listView->setModel(nullptr);
if (groupedView) {
groupedView->setModel(nullptr);
}
#ifdef ENABLE_CATEGORIZED_VIEW
if (categorizedView) {
categorizedView->setModel(nullptr);
}
#endif
tableView->setHidden(false);
treeView->setHidden(true);
tableView->setModel(itemModel);
tableView->initHeader();
if (dynamic_cast<ProxyModel *>(itemModel)) {
static_cast<ProxyModel *>(itemModel)->setRootIndex(QModelIndex());
}
tableView->resize(w, tableView->height());
stackIndex=tableView->property(constPageProp).toInt();
#ifdef ENABLE_CATEGORIZED_VIEW
} else if (Mode_Categorized==mode) {
//int w=view()->width();
treeView->setModel(nullptr);
listView->setModel(nullptr);
if (groupedView) {
groupedView->setModel(nullptr);
}