forked from gustavosbarreto/qcategorizedview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqcategorizedview.cpp
1533 lines (1332 loc) · 58.2 KB
/
qcategorizedview.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
/**
* This file is part of the KDE project
* Copyright (C) 2007, 2009 Rafael Fernández López <ereslibre@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* IMPLEMENTATION NOTES:
*
* QListView::setRowHidden() and QListView::isRowHidden() are not taken into
* account. This methods should actually not exist. This effect should be handled
* by an hypothetical QSortFilterProxyModel which filters out the desired rows.
*
* In case this needs to be implemented, contact me, but I consider this a faulty
* design.
*/
#include "qcategorizedview.h"
#include "qcategorizedview_p.h"
#include <math.h> // trunc on C99 compliant systems
//#include <kdefakes.h> // trunc for not C99 compliant systems
#include <QPainter>
#include <QScrollBar>
#include <QPaintEvent>
#include "qcategorydrawer.h"
#include "qcategorizedsortfilterproxymodel.h"
//BEGIN: Private part
struct QCategorizedView::Private::Item
{
Item()
: topLeft(QPoint())
, size(QSize())
{
}
QPoint topLeft;
QSize size;
};
struct QCategorizedView::Private::Block
{
Block()
: topLeft(QPoint())
, height(-1)
, firstIndex(QModelIndex())
, quarantineStart(QModelIndex())
, items(QList<Item>())
, outOfQuarantine(false)
, alternate(false)
, collapsed(false)
{
}
bool operator!=(const Block &rhs) const
{
return firstIndex != rhs.firstIndex;
}
static bool lessThan(const Block &left, const Block &right)
{
Q_ASSERT(left.firstIndex.isValid());
Q_ASSERT(right.firstIndex.isValid());
return left.firstIndex.row() < right.firstIndex.row();
}
QPoint topLeft;
int height;
QPersistentModelIndex firstIndex;
// if we have n elements on this block, and we inserted an element at position i. The quarantine
// will start at index (i, column, parent). This means that for all elements j where i <= j <= n, the
// visual rect position of item j will have to be recomputed (cannot use the cached point). The quarantine
// will only affect the current block, since the rest of blocks can be affected only in the way
// that the whole block will have different offset, but items will keep the same relative position
// in terms of their parent blocks.
QPersistentModelIndex quarantineStart;
QList<Item> items;
// this affects the whole block, not items separately. items contain the topLeft point relative
// to the block. Because of insertions or removals a whole block can be moved, so the whole block
// will enter in quarantine, what is faster than moving all items in absolute terms.
bool outOfQuarantine;
// should we alternate its color ? is just a hint, could not be used
bool alternate;
bool collapsed;
};
QCategorizedView::Private::Private(QCategorizedView *q)
: q(q)
, proxyModel(0)
, categoryDrawer(0)
, categoryDrawerV2(0)
, categoryDrawerV3(0)
, categorySpacing(5)
, alternatingBlockColors(false)
, collapsibleBlocks(false)
, hoveredBlock(new Block())
, hoveredIndex(QModelIndex())
, pressedPosition(QPoint())
, rubberBandRect(QRect())
{
}
QCategorizedView::Private::~Private()
{
delete hoveredBlock;
}
bool QCategorizedView::Private::isCategorized() const
{
return proxyModel && categoryDrawer && proxyModel->isCategorizedModel();
}
QStyleOptionViewItemV4 QCategorizedView::Private::blockRect(const QModelIndex &representative)
{
QStyleOptionViewItemV4 option(q->viewOptions());
const int height = categoryDrawer->categoryHeight(representative, option);
const QString categoryDisplay = representative.data(QCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
QPoint pos = blockPosition(categoryDisplay);
pos.ry() -= height;
option.rect.setTopLeft(pos);
option.rect.setWidth(viewportWidth() + categoryDrawer->leftMargin() + categoryDrawer->rightMargin());
option.rect.setHeight(height + blockHeight(categoryDisplay));
option.rect = mapToViewport(option.rect);
return option;
}
QPair<QModelIndex, QModelIndex> QCategorizedView::Private::intersectingIndexesWithRect(const QRect &_rect) const
{
const int rowCount = proxyModel->rowCount();
const QRect rect = _rect.normalized();
// binary search to find out the top border
int bottom = 0;
int top = rowCount - 1;
while (bottom <= top) {
const int middle = (bottom + top) / 2;
const QModelIndex index = proxyModel->index(middle, q->modelColumn(), q->rootIndex());
QRect itemRect = q->visualRect(index);
const int verticalOff = q->verticalOffset();
const int horizontalOff = q->horizontalOffset();
itemRect.topLeft().ry() += verticalOff;
itemRect.topLeft().rx() += horizontalOff;
itemRect.bottomRight().ry() += verticalOff;
itemRect.bottomRight().rx() += horizontalOff;
if (itemRect.bottomRight().y() <= rect.topLeft().y()) {
bottom = middle + 1;
} else {
top = middle - 1;
}
}
const QModelIndex bottomIndex = proxyModel->index(bottom, q->modelColumn(), q->rootIndex());
// binary search to find out the bottom border
bottom = 0;
top = rowCount - 1;
while (bottom <= top) {
const int middle = (bottom + top) / 2;
const QModelIndex index = proxyModel->index(middle, q->modelColumn(), q->rootIndex());
QRect itemRect = q->visualRect(index);
const int verticalOff = q->verticalOffset();
const int horizontalOff = q->horizontalOffset();
itemRect.topLeft().ry() += verticalOff;
itemRect.topLeft().rx() += horizontalOff;
itemRect.bottomRight().ry() += verticalOff;
itemRect.bottomRight().rx() += horizontalOff;
if (itemRect.topLeft().y() <= rect.bottomRight().y()) {
bottom = middle + 1;
} else {
top = middle - 1;
}
}
const QModelIndex topIndex = proxyModel->index(top, q->modelColumn(), q->rootIndex());
return qMakePair(bottomIndex, topIndex);
}
QPoint QCategorizedView::Private::blockPosition(const QString &category)
{
Block &block = blocks[category];
if (block.outOfQuarantine && !block.topLeft.isNull()) {
return block.topLeft;
}
QPoint res(categorySpacing, 0);
const QModelIndex index = block.firstIndex;
for (QHash<QString, Private::Block>::Iterator it = blocks.begin(); it != blocks.end(); ++it) {
Block &block = *it;
const QModelIndex categoryIndex = block.firstIndex;
if (index.row() < categoryIndex.row()) {
continue;
}
res.ry() += categoryDrawer->categoryHeight(categoryIndex, q->viewOptions()) + categorySpacing;
if (index.row() == categoryIndex.row()) {
continue;
}
res.ry() += blockHeight(it.key());
}
block.outOfQuarantine = true;
block.topLeft = res;
return res;
}
int QCategorizedView::Private::blockHeight(const QString &category)
{
Block &block = blocks[category];
if (block.collapsed) {
return 0;
}
if (block.height > -1) {
return block.height;
}
const QModelIndex firstIndex = block.firstIndex;
const QModelIndex lastIndex = proxyModel->index(firstIndex.row() + block.items.count() - 1, q->modelColumn(), q->rootIndex());
const QRect topLeft = q->visualRect(firstIndex);
QRect bottomRight = q->visualRect(lastIndex);
if (hasGrid()) {
bottomRight.setHeight(qMax(bottomRight.height(), q->gridSize().height()));
} else {
if (!q->uniformItemSizes()) {
bottomRight.setHeight(highestElementInLastRow(block) + q->spacing() * 2);
}
}
const int height = bottomRight.bottomRight().y() - topLeft.topLeft().y() + 1;
block.height = height;
return height;
}
int QCategorizedView::Private::viewportWidth() const
{
return q->viewport()->width() - categorySpacing * 2 - categoryDrawer->leftMargin() - categoryDrawer->rightMargin();
}
void QCategorizedView::Private::regenerateAllElements()
{
for (QHash<QString, Block>::Iterator it = blocks.begin(); it != blocks.end(); ++it) {
Block &block = *it;
block.outOfQuarantine = false;
block.quarantineStart = block.firstIndex;
block.height = -1;
}
}
void QCategorizedView::Private::rowsInserted(const QModelIndex &parent, int start, int end)
{
if (!isCategorized()) {
return;
}
for (int i = start; i <= end; ++i) {
const QModelIndex index = proxyModel->index(i, q->modelColumn(), parent);
Q_ASSERT(index.isValid());
const QString category = categoryForIndex(index);
Block &block = blocks[category];
//BEGIN: update firstIndex
// save as firstIndex in block if
// - it forced the category creation (first element on this category)
// - it is before the first row on that category
const QModelIndex firstIndex = block.firstIndex;
if (!firstIndex.isValid() || index.row() < firstIndex.row()) {
block.firstIndex = index;
}
//END: update firstIndex
Q_ASSERT(block.firstIndex.isValid());
const int firstIndexRow = block.firstIndex.row();
block.items.insert(index.row() - firstIndexRow, Private::Item());
block.height = -1;
q->visualRect(index);
q->viewport()->update();
}
//BEGIN: update the items that are in quarantine in affected categories
{
const QModelIndex lastIndex = proxyModel->index(end, q->modelColumn(), parent);
const QString category = categoryForIndex(lastIndex);
Private::Block &block = blocks[category];
block.quarantineStart = block.firstIndex;
}
//END: update the items that are in quarantine in affected categories
//BEGIN: mark as in quarantine those categories that are under the affected ones
{
const QModelIndex firstIndex = proxyModel->index(start, q->modelColumn(), parent);
const QString category = categoryForIndex(firstIndex);
const QModelIndex firstAffectedCategory = blocks[category].firstIndex;
//BEGIN: order for marking as alternate those blocks that are alternate
QList<Block> blockList = blocks.values();
qSort(blockList.begin(), blockList.end(), Block::lessThan);
QList<int> firstIndexesRows;
foreach (const Block &block, blockList) {
firstIndexesRows << block.firstIndex.row();
}
//END: order for marking as alternate those blocks that are alternate
for (QHash<QString, Private::Block>::Iterator it = blocks.begin(); it != blocks.end(); ++it) {
Private::Block &block = *it;
if (block.firstIndex.row() > firstAffectedCategory.row()) {
block.outOfQuarantine = false;
block.alternate = firstIndexesRows.indexOf(block.firstIndex.row()) % 2;
} else if (block.firstIndex.row() == firstAffectedCategory.row()) {
block.alternate = firstIndexesRows.indexOf(block.firstIndex.row()) % 2;
}
}
}
//END: mark as in quarantine those categories that are under the affected ones
}
QRect QCategorizedView::Private::mapToViewport(const QRect &rect) const
{
const int dx = -q->horizontalOffset();
const int dy = -q->verticalOffset();
return rect.adjusted(dx, dy, dx, dy);
}
QRect QCategorizedView::Private::mapFromViewport(const QRect &rect) const
{
const int dx = q->horizontalOffset();
const int dy = q->verticalOffset();
return rect.adjusted(dx, dy, dx, dy);
}
int QCategorizedView::Private::highestElementInLastRow(const Block &block) const
{
//Find the highest element in the last row
const QModelIndex lastIndex = proxyModel->index(block.firstIndex.row() + block.items.count() - 1, q->modelColumn(), q->rootIndex());
const QRect prevRect = q->visualRect(lastIndex);
int res = prevRect.height();
QModelIndex prevIndex = proxyModel->index(lastIndex.row() - 1, q->modelColumn(), q->rootIndex());
if (!prevIndex.isValid()) {
return res;
}
Q_FOREVER {
const QRect tempRect = q->visualRect(prevIndex);
if (tempRect.topLeft().y() < prevRect.topLeft().y()) {
break;
}
res = qMax(res, tempRect.height());
if (prevIndex == block.firstIndex) {
break;
}
prevIndex = proxyModel->index(prevIndex.row() - 1, q->modelColumn(), q->rootIndex());
}
return res;
}
bool QCategorizedView::Private::hasGrid() const
{
const QSize gridSize = q->gridSize();
return gridSize.isValid() && !gridSize.isNull();
}
QString QCategorizedView::Private::categoryForIndex(const QModelIndex &index) const
{
const QModelIndex categoryIndex = index.model()->index(index.row(), proxyModel->sortColumn(), index.parent());
return categoryIndex.data(QCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
}
void QCategorizedView::Private::leftToRightVisualRect(const QModelIndex &index, Item &item,
const Block &block, const QPoint &blockPos) const
{
const int firstIndexRow = block.firstIndex.row();
if (hasGrid()) {
const int relativeRow = index.row() - firstIndexRow;
const int maxItemsPerRow = qMax(viewportWidth() / q->gridSize().width(), 1);
if (q->layoutDirection() == Qt::LeftToRight) {
item.topLeft.rx() = (relativeRow % maxItemsPerRow) * q->gridSize().width() + blockPos.x() + categoryDrawer->leftMargin();
} else {
item.topLeft.rx() = viewportWidth() - ((relativeRow % maxItemsPerRow) + 1) * q->gridSize().width() + categoryDrawer->leftMargin() + categorySpacing;
}
item.topLeft.ry() = (relativeRow / maxItemsPerRow) * q->gridSize().height();
} else {
if (q->uniformItemSizes()) {
const int relativeRow = index.row() - firstIndexRow;
const QSize itemSize = q->sizeHintForIndex(index);
const int maxItemsPerRow = qMax((viewportWidth() - q->spacing()) / (itemSize.width() + q->spacing()), 1);
if (q->layoutDirection() == Qt::LeftToRight) {
item.topLeft.rx() = (relativeRow % maxItemsPerRow) * itemSize.width() + blockPos.x() + categoryDrawer->leftMargin();
} else {
item.topLeft.rx() = viewportWidth() - (relativeRow % maxItemsPerRow) * itemSize.width() + categoryDrawer->leftMargin() + categorySpacing;
}
item.topLeft.ry() = (relativeRow / maxItemsPerRow) * itemSize.height();
} else {
const QSize currSize = q->sizeHintForIndex(index);
if (index != block.firstIndex) {
const int viewportW = viewportWidth() - q->spacing();
QModelIndex prevIndex = proxyModel->index(index.row() - 1, q->modelColumn(), q->rootIndex());
QRect prevRect = q->visualRect(prevIndex);
prevRect = mapFromViewport(prevRect);
if ((prevRect.bottomRight().x() + 1) + currSize.width() - blockPos.x() + q->spacing() > viewportW) {
// we have to check the whole previous row, and see which one was the
// highest.
Q_FOREVER {
prevIndex = proxyModel->index(prevIndex.row() - 1, q->modelColumn(), q->rootIndex());
const QRect tempRect = q->visualRect(prevIndex);
if (tempRect.topLeft().y() < prevRect.topLeft().y()) {
break;
}
if (tempRect.bottomRight().y() > prevRect.bottomRight().y()) {
prevRect = tempRect;
}
if (prevIndex == block.firstIndex) {
break;
}
}
if (q->layoutDirection() == Qt::LeftToRight) {
item.topLeft.rx() = categoryDrawer->leftMargin() + blockPos.x() + q->spacing();
} else {
item.topLeft.rx() = viewportWidth() - currSize.width() + categoryDrawer->leftMargin() + categorySpacing;
}
item.topLeft.ry() = (prevRect.bottomRight().y() + 1) + q->spacing() - blockPos.y();
} else {
if (q->layoutDirection() == Qt::LeftToRight) {
item.topLeft.rx() = (prevRect.bottomRight().x() + 1) + q->spacing();
} else {
item.topLeft.rx() = (prevRect.bottomLeft().x() - 1) - q->spacing() - item.size.width() + categoryDrawer->leftMargin() + categorySpacing;
}
item.topLeft.ry() = prevRect.topLeft().y() - blockPos.y();
}
} else {
if (q->layoutDirection() == Qt::LeftToRight) {
item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin() + q->spacing();
} else {
item.topLeft.rx() = viewportWidth() - currSize.width() + categoryDrawer->leftMargin() + categorySpacing;
}
item.topLeft.ry() = q->spacing();
}
}
}
item.size = q->sizeHintForIndex(index);
}
void QCategorizedView::Private::topToBottomVisualRect(const QModelIndex &index, Item &item,
const Block &block, const QPoint &blockPos) const
{
const int firstIndexRow = block.firstIndex.row();
if (hasGrid()) {
const int relativeRow = index.row() - firstIndexRow;
item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin();
item.topLeft.ry() = relativeRow * q->gridSize().height();
} else {
if (q->uniformItemSizes()) {
const int relativeRow = index.row() - firstIndexRow;
const QSize itemSize = q->sizeHintForIndex(index);
item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin();
item.topLeft.ry() = relativeRow * itemSize.height();
} else {
if (index != block.firstIndex) {
QModelIndex prevIndex = proxyModel->index(index.row() - 1, q->modelColumn(), q->rootIndex());
QRect prevRect = q->visualRect(prevIndex);
prevRect = mapFromViewport(prevRect);
item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin() + q->spacing();
item.topLeft.ry() = (prevRect.bottomRight().y() + 1) + q->spacing() - blockPos.y();
} else {
item.topLeft.rx() = blockPos.x() + categoryDrawer->leftMargin() + q->spacing();
item.topLeft.ry() = q->spacing();
}
}
}
item.size = q->sizeHintForIndex(index);
item.size.setWidth(viewportWidth());
}
void QCategorizedView::Private::_k_slotCollapseOrExpandClicked(QModelIndex)
{
}
//END: Private part
//BEGIN: Public part
QCategorizedView::QCategorizedView(QWidget *parent)
: QListView(parent)
, d(new Private(this))
{
}
QCategorizedView::~QCategorizedView()
{
delete d;
}
void QCategorizedView::setModel(QAbstractItemModel *model)
{
if (d->proxyModel == model) {
return;
}
d->blocks.clear();
if (d->proxyModel) {
disconnect(d->proxyModel, SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()));
}
d->proxyModel = dynamic_cast<QCategorizedSortFilterProxyModel*>(model);
if (d->proxyModel) {
connect(d->proxyModel, SIGNAL(layoutChanged()), this, SLOT(slotLayoutChanged()));
}
QListView::setModel(model);
// if the model already had information inserted, update our data structures to it
if (model->rowCount()) {
slotLayoutChanged();
}
}
void QCategorizedView::setGridSize(const QSize &size)
{
setGridSizeOwn(size);
}
void QCategorizedView::setGridSizeOwn(const QSize &size)
{
d->regenerateAllElements();
QListView::setGridSize(size);
}
QRect QCategorizedView::visualRect(const QModelIndex &index) const
{
if (!d->isCategorized()) {
return QListView::visualRect(index);
}
if (!index.isValid()) {
return QRect();
}
const QString category = d->categoryForIndex(index);
if (!d->blocks.contains(category)) {
return QRect();
}
Private::Block &block = d->blocks[category];
const int firstIndexRow = block.firstIndex.row();
Q_ASSERT(block.firstIndex.isValid());
if (index.row() - firstIndexRow < 0 || index.row() - firstIndexRow >= block.items.count()) {
return QRect();
}
const QPoint blockPos = d->blockPosition(category);
Private::Item &ritem = block.items[index.row() - firstIndexRow];
if (ritem.topLeft.isNull() || (block.quarantineStart.isValid() &&
index.row() >= block.quarantineStart.row())) {
if (flow() == LeftToRight) {
d->leftToRightVisualRect(index, ritem, block, blockPos);
} else {
d->topToBottomVisualRect(index, ritem, block, blockPos);
}
//BEGIN: update the quarantine start
const bool wasLastIndex = (index.row() == (block.firstIndex.row() + block.items.count() - 1));
if (index.row() == block.quarantineStart.row()) {
if (wasLastIndex) {
block.quarantineStart = QModelIndex();
} else {
const QModelIndex nextIndex = d->proxyModel->index(index.row() + 1, modelColumn(), rootIndex());
block.quarantineStart = nextIndex;
}
}
//END: update the quarantine start
}
// we get now the absolute position through the relative position of the parent block. do not
// save this on ritem, since this would override the item relative position in block terms.
Private::Item item(ritem);
item.topLeft.ry() += blockPos.y();
const QSize sizeHint = item.size;
if (d->hasGrid()) {
const QSize sizeGrid = gridSize();
const QSize resultingSize = sizeHint.boundedTo(sizeGrid);
QRect res(item.topLeft.x() + ((sizeGrid.width() - resultingSize.width()) / 2),
item.topLeft.y(), resultingSize.width(), resultingSize.height());
if (block.collapsed) {
// we can still do binary search, while we "hide" items. We move those items in collapsed
// blocks to the left and set a 0 height.
res.setLeft(-resultingSize.width());
res.setHeight(0);
}
return d->mapToViewport(res);
}
QRect res(item.topLeft.x(), item.topLeft.y(), sizeHint.width(), sizeHint.height());
if (block.collapsed) {
// we can still do binary search, while we "hide" items. We move those items in collapsed
// blocks to the left and set a 0 height.
res.setLeft(-sizeHint.width());
res.setHeight(0);
}
return d->mapToViewport(res);
}
QCategoryDrawer *QCategorizedView::categoryDrawer() const
{
return d->categoryDrawer;
}
void QCategorizedView::setCategoryDrawer(QCategoryDrawer *categoryDrawer)
{
if (d->categoryDrawerV2) {
disconnect(d->categoryDrawerV2, SIGNAL(collapseOrExpandClicked(QModelIndex)),
this, SLOT(_k_slotCollapseOrExpandClicked(QModelIndex)));
}
d->categoryDrawer = categoryDrawer;
d->categoryDrawerV2 = dynamic_cast<QCategoryDrawerV2*>(categoryDrawer);
d->categoryDrawerV3 = dynamic_cast<QCategoryDrawerV3*>(categoryDrawer);
if (d->categoryDrawerV2) {
connect(d->categoryDrawerV2, SIGNAL(collapseOrExpandClicked(QModelIndex)),
this, SLOT(_k_slotCollapseOrExpandClicked(QModelIndex)));
}
}
int QCategorizedView::categorySpacing() const
{
return d->categorySpacing;
}
void QCategorizedView::setCategorySpacing(int categorySpacing)
{
if (d->categorySpacing == categorySpacing) {
return;
}
d->categorySpacing = categorySpacing;
for (QHash<QString, Private::Block>::Iterator it = d->blocks.begin(); it != d->blocks.end(); ++it) {
Private::Block &block = *it;
block.outOfQuarantine = false;
}
}
bool QCategorizedView::alternatingBlockColors() const
{
return d->alternatingBlockColors;
}
void QCategorizedView::setAlternatingBlockColors(bool enable)
{
d->alternatingBlockColors = enable;
}
bool QCategorizedView::collapsibleBlocks() const
{
return d->collapsibleBlocks;
}
void QCategorizedView::setCollapsibleBlocks(bool enable)
{
d->collapsibleBlocks = enable;
}
QModelIndexList QCategorizedView::block(const QString &category)
{
QModelIndexList res;
const Private::Block &block = d->blocks[category];
if (block.height == -1) {
return res;
}
QModelIndex current = block.firstIndex;
const int first = current.row();
for (int i = 1; i <= block.items.count(); ++i) {
if (current.isValid()) {
res << current;
}
current = d->proxyModel->index(first + i, modelColumn(), rootIndex());
}
return res;
}
QModelIndexList QCategorizedView::block(const QModelIndex &representative)
{
return block(representative.data(QCategorizedSortFilterProxyModel::CategoryDisplayRole).toString());
}
QModelIndex QCategorizedView::indexAt(const QPoint &point) const
{
if (!d->isCategorized()) {
return QListView::indexAt(point);
}
const int rowCount = d->proxyModel->rowCount();
if (!rowCount) {
return QModelIndex();
}
// Binary search that will try to spot if there is an index under point
int bottom = 0;
int top = rowCount - 1;
while (bottom <= top) {
const int middle = (bottom + top) / 2;
const QModelIndex index = d->proxyModel->index(middle, modelColumn(), rootIndex());
QRect rect = visualRect(index);
const int verticalOff = verticalOffset();
int horizontalOff = horizontalOffset();
if (layoutDirection() == Qt::RightToLeft) {
horizontalOff *= -1;
}
rect.topLeft().ry() += verticalOff;
rect.topLeft().rx() += horizontalOff;
rect.bottomRight().ry() += verticalOff;
rect.bottomRight().rx() += horizontalOff;
if (rect.contains(point)) {
if (index.model()->flags(index) & Qt::ItemIsEnabled) {
return index;
}
return QModelIndex();
}
bool directionCondition;
if (layoutDirection() == Qt::LeftToRight) {
directionCondition = point.x() > rect.bottomRight().x();
} else {
directionCondition = point.x() < rect.bottomLeft().x();
}
if (point.y() > rect.bottomRight().y() ||
(point.y() > rect.topLeft().y() && point.y() < rect.bottomRight().y() && directionCondition)) {
bottom = middle + 1;
} else {
top = middle - 1;
}
}
return QModelIndex();
}
void QCategorizedView::reset()
{
d->blocks.clear();
QListView::reset();
}
void QCategorizedView::paintEvent(QPaintEvent *event)
{
if (!d->isCategorized()) {
QListView::paintEvent(event);
return;
}
const QPair<QModelIndex, QModelIndex> intersecting = d->intersectingIndexesWithRect(viewport()->rect().intersected(event->rect()));
QPainter p(viewport());
p.save();
Q_ASSERT(selectionModel()->model() == d->proxyModel);
//BEGIN: draw categories
QHash<QString, Private::Block>::ConstIterator it(d->blocks.constBegin());
while (it != d->blocks.constEnd()) {
const Private::Block &block = *it;
const QModelIndex categoryIndex = d->proxyModel->index(block.firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
QStyleOptionViewItemV4 option(viewOptions());
option.features |= d->alternatingBlockColors && block.alternate ? QStyleOptionViewItemV4::Alternate
: QStyleOptionViewItemV4::None;
option.state |= !d->collapsibleBlocks || !block.collapsed ? QStyle::State_Open
: QStyle::State_None;
const int height = d->categoryDrawer->categoryHeight(categoryIndex, option);
QPoint pos = d->blockPosition(it.key());
pos.ry() -= height;
option.rect.setTopLeft(pos);
option.rect.setWidth(d->viewportWidth() + d->categoryDrawer->leftMargin() + d->categoryDrawer->rightMargin());
option.rect.setHeight(height + d->blockHeight(it.key()));
option.rect = d->mapToViewport(option.rect);
if (!option.rect.intersects(viewport()->rect())) {
++it;
continue;
}
d->categoryDrawer->drawCategory(categoryIndex, d->proxyModel->sortRole(), option, &p);
++it;
}
//END: draw categories
if (intersecting.first.isValid() && intersecting.second.isValid()) {
//BEGIN: draw items
int i = intersecting.first.row();
int indexToCheckIfBlockCollapsed = i;
QModelIndex categoryIndex;
QString category;
Private::Block *block = 0;
while (i <= intersecting.second.row()) {
//BEGIN: first check if the block is collapsed. if so, we have to skip the item painting
if (i == indexToCheckIfBlockCollapsed) {
categoryIndex = d->proxyModel->index(i, d->proxyModel->sortColumn(), rootIndex());
category = categoryIndex.data(QCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
block = &d->blocks[category];
indexToCheckIfBlockCollapsed = block->firstIndex.row() + block->items.count();
if (block->collapsed) {
i = indexToCheckIfBlockCollapsed;
continue;
}
}
//END: first check if the block is collapsed. if so, we have to skip the item painting
Q_ASSERT(block);
const bool alternateItem = (i - block->firstIndex.row()) % 2;
const QModelIndex index = d->proxyModel->index(i, modelColumn(), rootIndex());
const Qt::ItemFlags flags = d->proxyModel->flags(index);
QStyleOptionViewItemV4 option(viewOptions());
option.rect = visualRect(index);
option.widget = this;
option.features |= wordWrap() ? QStyleOptionViewItemV2::WrapText
: QStyleOptionViewItemV2::None;
option.features |= alternatingRowColors() && alternateItem ? QStyleOptionViewItemV4::Alternate
: QStyleOptionViewItemV4::None;
if (flags & Qt::ItemIsSelectable) {
option.state |= selectionModel()->isSelected(index) ? QStyle::State_Selected
: QStyle::State_None;
} else {
option.state &= ~QStyle::State_Selected;
}
option.state |= (index == currentIndex()) ? QStyle::State_HasFocus
: QStyle::State_None;
if (!(flags & Qt::ItemIsEnabled)) {
option.state &= ~QStyle::State_Enabled;
} else {
option.state |= (index == d->hoveredIndex) ? QStyle::State_MouseOver
: QStyle::State_None;
}
itemDelegate(index)->paint(&p, option, index);
++i;
}
//END: draw items
}
//BEGIN: draw selection rect
if (isSelectionRectVisible() && d->rubberBandRect.isValid()) {
QStyleOptionRubberBand opt;
opt.initFrom(this);
opt.shape = QRubberBand::Rectangle;
opt.opaque = false;
opt.rect = d->mapToViewport(d->rubberBandRect).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
p.save();
style()->drawControl(QStyle::CE_RubberBand, &opt, &p);
p.restore();
}
//END: draw selection rect
p.restore();
}
void QCategorizedView::resizeEvent(QResizeEvent *event)
{
d->regenerateAllElements();
QListView::resizeEvent(event);
}
void QCategorizedView::setSelection(const QRect &rect,
QItemSelectionModel::SelectionFlags flags)
{
if (!d->isCategorized()) {
QListView::setSelection(rect, flags);
return;
}
if (rect.topLeft() == rect.bottomRight()) {
const QModelIndex index = indexAt(rect.topLeft());
selectionModel()->select(index, flags);
return;
}
const QPair<QModelIndex, QModelIndex> intersecting = d->intersectingIndexesWithRect(rect);
QItemSelection selection;
//TODO: think of a faster implementation
QModelIndex firstIndex;
QModelIndex lastIndex;
for (int i = intersecting.first.row(); i <= intersecting.second.row(); ++i) {
const QModelIndex index = d->proxyModel->index(i, modelColumn(), rootIndex());
const bool visualRectIntersects = visualRect(index).intersects(rect);
if (firstIndex.isValid()) {
if (visualRectIntersects) {
lastIndex = index;
} else {
selection << QItemSelectionRange(firstIndex, lastIndex);
firstIndex = QModelIndex();
}
} else if (visualRectIntersects) {
firstIndex = index;
lastIndex = index;
}
}
if (firstIndex.isValid()) {
selection << QItemSelectionRange(firstIndex, lastIndex);
}
selectionModel()->select(selection, flags);
}
void QCategorizedView::mouseMoveEvent(QMouseEvent *event)
{
QListView::mouseMoveEvent(event);
d->hoveredIndex = indexAt(event->pos());
const SelectionMode itemViewSelectionMode = selectionMode();
if (state() == DragSelectingState && isSelectionRectVisible() && itemViewSelectionMode != SingleSelection
&& itemViewSelectionMode != NoSelection) {
QRect rect(d->pressedPosition, event->pos() + QPoint(horizontalOffset(), verticalOffset()));
rect = rect.normalized();
update(rect.united(d->rubberBandRect));
d->rubberBandRect = rect;
}
if (!d->categoryDrawerV2) {
return;
}
QHash<QString, Private::Block>::ConstIterator it(d->blocks.constBegin());
while (it != d->blocks.constEnd()) {
const Private::Block &block = *it;
const QModelIndex categoryIndex = d->proxyModel->index(block.firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
QStyleOptionViewItemV4 option(viewOptions());
const int height = d->categoryDrawer->categoryHeight(categoryIndex, option);
QPoint pos = d->blockPosition(it.key());
pos.ry() -= height;
option.rect.setTopLeft(pos);
option.rect.setWidth(d->viewportWidth() + d->categoryDrawer->leftMargin() + d->categoryDrawer->rightMargin());
option.rect.setHeight(height + d->blockHeight(it.key()));
option.rect = d->mapToViewport(option.rect);
const QPoint mousePos = viewport()->mapFromGlobal(QCursor::pos());
if (option.rect.contains(mousePos)) {
if (d->categoryDrawerV3 && d->hoveredBlock->height != -1 && *d->hoveredBlock != block) {
const QModelIndex categoryIndex = d->proxyModel->index(d->hoveredBlock->firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
d->categoryDrawerV3->mouseLeft(categoryIndex, option.rect);
*d->hoveredBlock = block;
d->hoveredCategory = it.key();
viewport()->update(option.rect);
} else if (d->hoveredBlock->height == -1) {
*d->hoveredBlock = block;
d->hoveredCategory = it.key();
} else if (d->categoryDrawerV3) {
d->categoryDrawerV3->mouseMoved(categoryIndex, option.rect, event);
} else {
d->categoryDrawerV2->mouseButtonMoved(categoryIndex, event);
}
viewport()->update(option.rect);
return;
}
++it;
}
if (d->categoryDrawerV3 && d->hoveredBlock->height != -1) {
const QModelIndex categoryIndex = d->proxyModel->index(d->hoveredBlock->firstIndex.row(), d->proxyModel->sortColumn(), rootIndex());
const QStyleOptionViewItemV4 option = d->blockRect(categoryIndex);
d->categoryDrawerV3->mouseLeft(categoryIndex, option.rect);
*d->hoveredBlock = Private::Block();
d->hoveredCategory = QString();
viewport()->update(option.rect);
}