forked from linuxdeepin/dtkwidget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlistview.cpp
More file actions
996 lines (775 loc) · 26.3 KB
/
dlistview.cpp
File metadata and controls
996 lines (775 loc) · 26.3 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
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
// SPDX-FileCopyrightText: 2015 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <QDebug>
#include <QScrollBar>
#include "dboxwidget.h"
#include "dlistview.h"
#include "private/dlistview_p.h"
#include "dflowlayout.h"
#include "dstyleoption.h"
#include "dstyleditemdelegate.h"
#include "dstyle.h"
#include <DBounceAnimation>
DWIDGET_BEGIN_NAMESPACE
DVariantListModel::DVariantListModel(QObject *parent) :
QAbstractListModel(parent)
{
}
int DVariantListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return dataList.count();
}
QVariant DVariantListModel::data(const QModelIndex &index, int role) const
{
Q_UNUSED(role);
return dataList.value(index.row());
}
bool DVariantListModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (index.row() >= 0 && index.row() < dataList.size()) {
dataList.replace(index.row(), value);
Q_EMIT dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
bool DVariantListModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (count < 1 || row < 0 || row > rowCount(parent))
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
dataList.insert(row, QVariant());
endInsertRows();
return true;
}
bool DVariantListModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
dataList.removeAt(row);
endRemoveRows();
return true;
}
DListViewPrivate::DListViewPrivate(DListView *qq) :
DObjectPrivate(qq)
{
}
DListViewPrivate::~DListViewPrivate()
{
}
void DListViewPrivate::init()
{
D_Q(DListView);
q->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
q->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
DStyledItemDelegate *delegate = new DStyledItemDelegate(q);
q->setItemDelegate(delegate);
q->setFrameShape(QFrame::NoFrame);
q->viewport()->setAutoFillBackground(false);
q->setAutoFillBackground(false);
q->setBackgroundRole(q->viewport()->backgroundRole());
// 默认背景类型为圆角背景
q->setBackgroundType(DStyledItemDelegate::RoundedBackground);
}
void DListViewPrivate::onOrientationChanged()
{
D_Q(DListView);
bool isVerticalLayout = (q->orientation() == Qt::Vertical);
if (headerLayout) {
headerLayout->setDirection(isVerticalLayout
? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight);
if (isVerticalLayout) {
headerLayout->resize(q->width(), headerLayout->sizeHint().height());
} else {
headerLayout->resize(headerLayout->sizeHint().width(), q->height());
}
headerLayout->layout()->update();
}
if (footerLayout) {
footerLayout->setDirection(isVerticalLayout
? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight);
if (isVerticalLayout) {
footerLayout->resize(q->width(), footerLayout->sizeHint().height());
footerLayout->move(0, q->height() - footerLayout->height());
} else {
footerLayout->resize(footerLayout->sizeHint().width(), q->height());
footerLayout->move(q->width() - footerLayout->width(), 0);
}
footerLayout->layout()->update();
}
}
// ====================Signals begin====================
/*!
@~english
\fn void DListView::currentChanged(const QModelIndex &previous)
\brief This signal is called when the current item changes
\details ListView will have a QModelIndex object that always represents the current Item index,
This signal is called when the location of this QModelIndex object changes, not the content of the current ITEM content changes.
When a mouse ordered machine to switch item with a keyboard
\param[in] previous It is the index object of the previous item
\sa QModelIndex QListView::currentChanged
*/
/*!
@~english
\fn void DListView::triggerEdit(const QModelIndex &index)
\brief This signal is called when a new item is edited
\param[in] index the indexing object of item that is being edited
\sa QModelIndex QAbstractItemView::EditTrigger
*/
// ====================Signals end====================
/*!
@~english
\class Dtk::Widget::DListView
\inmodule dtkwidget
\brief A control for displaying a column of data.
\details DListView is similar to QListView a class that belongs to QT's Model/View Framework.
It is often used to display a column of data. When the data is more, you can roll the control
to display and multiple contents.
But it is also different from QLISTView. DListView provides top controls and bottom controls.
They are always displayed in ListView, which will not be visible because of rolling.
The method of data, such as: additem, additems, insertitem, takeItem, removeItem, and some
commonly used signals in development.
*/
/*!
@~english
\brief Get a dlistView instance
\param[in] parent Parents controlled by the DListView instance
*/
DListView::DListView(QWidget *parent) :
QListView(parent),
DObject(*new DListViewPrivate(this))
{
d_func()->init();
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SEARCH) {
auto ani = new DBounceAnimation(this);
ani->setAnimationTarget(this);
ani->setAniMationEnable(true);
}
}
/*!
@~english
\brief Get the current state of the control
The control can be dragged, edited by editor, and is playing animation and other states. You can check in detail:QAbstractItemView::State
\return the current state of control
\sa QAbstractItemView::State
*/
QAbstractItemView::State DListView::state() const
{
return QListView::state();
}
void DListView::setFlow(QListView::Flow flow)
{
QListView::setFlow(flow);
}
void DListView::setWrapping(bool enable)
{
QListView::setWrapping(enable);
}
QSize DListView::viewportSizeHint() const
{
return sizeAdjustPolicy() == QAbstractScrollArea::AdjustToContents ? QSize(0, 0) : QListView::viewportSizeHint();
}
int DListView::horizontalOffset() const
{
if (viewMode() != IconMode || flow() != LeftToRight || !isWrapping()
|| (!gridSize().isValid() && !itemSize().isValid())) {
return QListView::horizontalOffset();
}
int width = contentsSize().width();
int offset = gridSize().isValid() ? 0 : spacing() / 2;
return -(viewport()->width() - width) / 2 + offset;
}
QSize DListView::minimumSizeHint() const
{
QSize size = QListView::minimumSizeHint();
QSize content_size = QListView::contentsSize();
if (horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
size.setWidth(content_size.width());
}
if (verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
size.setHeight(content_size.height());
}
return size;
}
/*!
@~english
\brief Get a top control
\details The top control will be displayed in ListView, and the top control will always be above all item.
That is to say, the difference between the top control and item is that the top control is always displayed in the layout, not because the mouse rolling is not visible.
In addition, there are multiple top controls. Their layout (direction) is the same as the layout direction of item
\param[in] index Specify the index of the top control to be obtained
\return Return to the top control object at the designated index
\note Note that the top control is not like the head of GridLayout, and the header is always deployed in the horizontal direction
\sa DListView::getFooterWidget DListView::addHeaderWidget DListView::removeHeaderWidget DListView::takeHeaderWidget
*/
QWidget *DListView::getHeaderWidget(int index) const
{
return d_func()->headerList.value(index);
}
/*!
@~english
\brief Get a bottom control
\param[in] index Specify the index of the bottom control to be obtained
\return return to the bottom control object at the specified index
\sa DListView::getHeaderWidget
*/
QWidget *DListView::getFooterWidget(int index) const
{
return d_func()->footerList.value(index);
}
/*!
@~english
\brief Determine whether the given QRect is overlapped with the item of ListView.
\details The Item of ListView shows the ViewPort of ListView. item can only be displayed in ViewPort. item beyond this area will be invisible.
\param[in] rect QRct to compare
\return Whether the two rectangles have overlapping areas
\sa DListView::isVisualRect
*/
bool DListView::isActiveRect(const QRect &rect) const
{
// D_DC(DListView);
const QRect &area = viewport()->geometry();
return area.intersects(rect);
}
/*!
@~english
\brief Same as dlistView::IsvisualRect
\param[in] rect The position rectangle for judging.
\return Successfully contains rectangular return true,Otherwise, return false.
\sa DListView::isVisualRect
*/
bool DListView::isVisualRect(const QRect &rect) const
{
const QRect &area = viewport()->geometry();
return area.intersects(rect);
}
/*!
@~english
\fn void DListView::rowCountChanged()
\sa DListView::count
*/
/*!
@~english
\property DListView::count
\brief How many lines of data is there in this attribute preservation
\details Getter: DListView::count , Signal: DListView::rowCountChanged
*/
int DListView::count() const
{
return model()->rowCount(rootIndex());
}
/*!
@~english
\fn void DListView::orientationChanged(Qt::Orientation orientation)
\param[in] orientation The direction value of the change.
\sa DListView::orientation
*/
/*!
@~english
\property DListView::orientation
\brief This attribute saves the layout of Item in ListView
\details Getter: DListView::orientation , Setter: DListView::setOrientation , Signal: DListView::orientationChanged
\sa Qt::Orientation
*/
Qt::Orientation DListView::orientation() const
{
bool isVerticalLayout = isWrapping()
? flow() == QListView::LeftToRight
: flow() == QListView::TopToBottom;
return isVerticalLayout ? Qt::Vertical : Qt::Horizontal;
}
/*!
@~english
\brief Set the model you want to use dlistView
\details The model is used to provide data for ListView to achieve the structure of the separation of the data layer and the interface layer. For details, please refer to Qt's Model/View Framework
\param[in] model Model object
\sa QListView::setModel
*/
void DListView::setModel(QAbstractItemModel *model)
{
QAbstractItemModel *old_model = this->model();
if (old_model) {
disconnect(old_model, &QAbstractItemModel::rowsInserted, this, &DListView::rowCountChanged);
disconnect(old_model, &QAbstractItemModel::rowsRemoved, this, &DListView::rowCountChanged);
}
QListView::setModel(model);
model = this->model();
if (model) {
connect(model, &QAbstractItemModel::rowsInserted, this, &DListView::rowCountChanged);
connect(model, &QAbstractItemModel::rowsRemoved, this, &DListView::rowCountChanged);
}
}
DStyledItemDelegate::BackgroundType DListView::backgroundType() const
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
return d->backgroundType();
}
return DStyledItemDelegate::NoBackground;
}
QMargins DListView::itemMargins() const
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
return d->margins();
}
return QMargins();
}
QSize DListView::itemSize() const
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
return d->itemSize();
}
return QSize();
}
/*!
@~english
\brief Add an Item at the bottom of the list
\param[in] data New data to be added
\return return whether it is successful to add
*/
bool DListView::addItem(const QVariant &data)
{
return insertItem(count(), data);
}
/*!
@~english
\brief Add more item at the bottom of the list at one time
\param[in] datas List of new data composition
\return Whether it is successful
*/
bool DListView::addItems(const QVariantList &datas)
{
return insertItems(count(), datas);
}
/*!
@~english
\brief Add a new item at the designated bank
\param[in] index To increase the line number of Item
\param[in] data Item data to be added
\return Whether it is successful
*/
bool DListView::insertItem(int index, const QVariant &data)
{
if (!model()->insertRow(index))
return false;
return model()->setData(model()->index(index, 0, rootIndex()), data);
}
/*!
@~english
\brief Add more item at the designated bank
\param[in] index To increase the line number of Item
\param[in] datas List of data composition of items data
\return Whether it is successful
*/
bool DListView::insertItems(int index, const QVariantList &datas)
{
if (!model()->insertRows(index, datas.count()))
return false;
for (int i = 0; i < datas.count(); ++i)
model()->setData(model()->index(index + i, 0, rootIndex()), datas.at(i));
return true;
}
/*!
@~english
\brief Remove the designated position item
\param[in] index The line number of it to remove
\return Whether it is remove successfully
*/
bool DListView::removeItem(int index)
{
return model()->removeRow(index);
}
/*!
@~english
\brief Remove multiple item at a time
\param[in] index Start removing the line number of Item
\param[in] count Remove from the line number specified by index, remove the county item
\return Whether to return to remove success
*/
bool DListView::removeItems(int index, int count)
{
return model()->removeRows(index, count);
}
/*!
@~english
\brief This function is used to add top controls.
Similar to dlistView::GetHeaderWidget, but returns the object of the top control to be removed.
\param[in] widget Head control instance.
\return Successfully adds to the index value of adding to dlistview, and the corresponding control has been returned
The index value.
\sa DListView::getHeaderWidget
*/
int DListView::addHeaderWidget(QWidget *widget)
{
D_D(DListView);
int index = d->headerList.indexOf(widget);
if (index >= 0)
return index;
if (!d->headerLayout) {
bool isVerticalLayout = (orientation() == Qt::Vertical);
d->headerLayout = new DBoxWidget(isVerticalLayout
? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight, this);
if (isVerticalLayout)
d->headerLayout->resize(width(), d->headerLayout->height());
else
d->headerLayout->resize(d->headerLayout->width(), height());
connect(d->headerLayout, &DBoxWidget::sizeChanged,
this, [this](const QSize & size) {
bool isVerticalLayout = (orientation() == Qt::Vertical);
QMargins margins = this->viewportMargins();
if (isVerticalLayout)
margins.setTop(size.height());
else
margins.setLeft(size.width());
setViewportMargins(margins);
});
d->headerLayout->show();
}
d->headerLayout->addWidget(widget);
d->headerList << widget;
return d->headerList.count() - 1;
}
/*!
@~english
\brief This function is used to remove the head control small control.
\param[in] index Add a small head control in dlistview
The index value is the return value of dlistView::addheaderWidget.
\sa DListView::addFooterWidget
*/
void DListView::removeHeaderWidget(int index)
{
QWidget *widget = takeHeaderWidget(index);
if (widget)
widget->deleteLater();
}
/*!
@~english
\brief This function is used to remove the small control of the head and return the control.
\details Similar to Dlistview::GetHeaderWidget, but returns the object of the top control to be removed
\param[in] index Add a small head control in dlistview
The index value is the return value of dlistView::addheaderWidget.
\return Successfully remove the small head control obtained, otherwise return nullptr.
\sa DListView::getHeaderWidget
*/
QWidget *DListView::takeHeaderWidget(int index)
{
D_D(DListView);
QWidget *widget = d->headerList.takeAt(index);
d->headerLayout->layout()->removeWidget(widget);
if (d->headerList.isEmpty()) {
d->headerLayout->deleteLater();
d->headerLayout = nullptr;
}
return widget;
}
/*!
@~english
\brief This function is used to add a small control small control.
\param[in] widget the instance of control in footer
\return Successfully add the corresponding index value, if it exists, return
Corresponding index value.
\sa DListView::getFooterWidget
*/
int DListView::addFooterWidget(QWidget *widget)
{
D_D(DListView);
int index = d->footerList.indexOf(widget);
if (index >= 0)
return index;
if (!d->footerLayout) {
bool isVerticalLayout = (orientation() == Qt::Vertical);
d->footerLayout = new DBoxWidget(isVerticalLayout
? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight, this);
if (isVerticalLayout)
d->footerLayout->resize(width(), d->footerLayout->height());
else
d->footerLayout->resize(d->footerLayout->width(), height());
connect(d->footerLayout, &DBoxWidget::sizeChanged,
this, [this](const QSize & size) {
bool isVerticalLayout = (orientation() == Qt::Vertical);
QMargins margins = this->viewportMargins();
D_D(DListView);
if (isVerticalLayout) {
margins.setBottom(size.height());
d->footerLayout->move(0, height() - d->footerLayout->height());
} else {
margins.setRight(size.width());
d->footerLayout->move(width() - d->footerLayout->width(), 0);
}
setViewportMargins(margins);
});
d->footerLayout->show();
}
d->footerLayout->addWidget(widget);
d->footerList << widget;
return d->footerList.count() - 1;
}
/*!
@~english
\brief This function is used to remove the underlying foot-footed control.
\param[in] index Add to dlistview mid-layer footer control
Index value,It is the return value of dlistView::addfooterWidget.
\sa DListView::addFooterWidget
*/
void DListView::removeFooterWidget(int index)
{
QWidget *widget = takeFooterWidget(index);
if (widget)
widget->deleteLater();
}
/*!
@~english
\brief Remove the bottom foot control and return to the control.
\param[in] index Add the index value of the bottom -footed control control in dlistview,
It is the return value of dlistView::addfooterWidget.
\sa DListView::getFooterWidget DListView::takeHeaderWidget
*/
QWidget *DListView::takeFooterWidget(int index)
{
D_D(DListView);
QWidget *widget = d->footerList.takeAt(index);
d->footerLayout->layout()->removeWidget(widget);
if (d->footerList.isEmpty()) {
d->footerLayout->deleteLater();
d->footerLayout = nullptr;
}
return widget;
}
/*!
@~english
\brief This function is used to set the direction of dlistview
\param[in] flow The direction of dlistview,have QListView::Flow::LeftToRight and
QListView::Flow::TopToBottom
\param[in] wrapping Used to control whether the layout is automatically changed.
True indicates that automatic changes are replaced, and False indicates non -automatic bank.
\sa DListView::orientation
*/
void DListView::setOrientation(QListView::Flow flow, bool wrapping)
{
Qt::Orientation old_orientation = this->orientation();
setFlow(flow);
setWrapping(wrapping);
Qt::Orientation orientation = this->orientation();
if (old_orientation != orientation) {
D_D(DListView);
QMargins margins = viewportMargins();
if (old_orientation == Qt::Vertical) {
if (d->headerLayout)
margins.setTop(0);
if (d->footerLayout)
margins.setBottom(0);
} else {
if (d->headerLayout)
margins.setLeft(0);
if (d->footerLayout)
margins.setRight(0);
}
setViewportMargins(margins);
d->onOrientationChanged();
Q_EMIT orientationChanged(orientation);
}
}
/*!
@~english
\brief Edit a item.
\param[in] index Specify the location of the Item to edit
*/
void DListView::edit(const QModelIndex &index)
{
QListView::edit(index);
}
/*!
@~english
\brief Set the background color type of item.
\param[in] backgroundType Background color
*/
void DListView::setBackgroundType(DStyledItemDelegate::BackgroundType backgroundType)
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
d->setBackgroundType(backgroundType);
if (d->spacing() < 0) {
if (d->backgroundType() == DStyledItemDelegate::RoundedBackground) {
d->setItemSpacing(10);
} else if (d->backgroundType() == DStyledItemDelegate::ClipCornerBackground) {
d->setItemSpacing(1);
}
}
}
}
/*!
@~english
\brief Set the content of the item Margin.
\param[in] itemMargins margin value
*/
void DListView::setItemMargins(const QMargins &itemMargins)
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
d->setMargins(itemMargins);
}
}
/*!
@~english
\brief Set the size of Item.
\param[in] itemSize itemSize
*/
void DListView::setItemSize(QSize itemSize)
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
return d->setItemSize(itemSize);
}
}
/*!
@~english
\brief Set the spacing size of Item.
\param[in] spacing Spacing value
*/
void DListView::setItemSpacing(int spacing)
{
if (DStyledItemDelegate *d = qobject_cast<DStyledItemDelegate *>(itemDelegate())) {
return d->setItemSpacing(spacing);
}
}
/*!
@~english
\brief Set the rounded corner size of Item.
\param[in] radius Rounded corner size value
*/
void DListView::setItemRadius(int radius)
{
DStyle::setFrameRadius(this, radius);
update();
}
#if(QT_VERSION < 0x050500)
void DListView::setViewportMargins(int left, int top, int right, int bottom)
{
D_D(DListView);
d->left = left;
d->top = top;
d->right = right;
d->bottom = bottom;
QListView::setViewportMargins(left, top, right, bottom);
}
void DListView::setViewportMargins(const QMargins &margins)
{
setViewportMargins(margins.left(), margins.top(), margins.right(), margins.bottom());
}
QMargins DListView::viewportMargins() const
{
D_DC(DListView);
return QMargins(d->left, d->top, d->right, d->bottom);
}
#endif
void DListView::resizeEvent(QResizeEvent *event)
{
QListView::resizeEvent(event);
D_D(DListView);
bool isVerticalLayout = (orientation() == Qt::Vertical);
if (d->headerLayout) {
if (isVerticalLayout)
d->headerLayout->resize(width(), d->headerLayout->height());
else
d->headerLayout->resize(d->headerLayout->width(), height());
}
if (d->footerLayout) {
if (isVerticalLayout) {
d->footerLayout->resize(width(), d->footerLayout->height());
d->footerLayout->move(0, height() - d->footerLayout->height());
} else {
d->footerLayout->resize(d->footerLayout->width(), height());
d->footerLayout->move(width() - d->footerLayout->width(), 0);
}
}
}
void DListView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
QListView::currentChanged(current, previous);
Q_EMIT currentChanged(previous);
}
bool DListView::edit(const QModelIndex &index, QAbstractItemView::EditTrigger trigger, QEvent *event)
{
if (QWidget *w = indexWidget(index)) {
Qt::ItemFlags flags = model()->flags(index);
if (((flags & Qt::ItemIsEditable) == 0) || ((flags & Qt::ItemIsEnabled) == 0))
return false;
if (state() == QAbstractItemView::EditingState)
return false;
if (trigger == QAbstractItemView::AllEditTriggers) // force editing
return true;
if ((trigger & editTriggers()) == QAbstractItemView::SelectedClicked
&& !selectionModel()->isSelected(index))
return false;
if (trigger & editTriggers()) {
w->setFocus();
Q_EMIT triggerEdit(index);
return true;
}
}
bool tmp = QListView::edit(index, trigger, event);
if (tmp)
Q_EMIT triggerEdit(index);
return tmp;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QStyleOptionViewItem DListView::viewOptions() const
{
QStyleOptionViewItem item = QListView::viewOptions();
item.showDecorationSelected = true;
// 列表项不会变为Inactive状态
item.state |= QStyle::State_Active;
if (viewMode() == QListView::ListMode) {
item.decorationAlignment = Qt::AlignVCenter;
}
return item;
}
#else
/*!
\reimp
*/
void DListView::initViewItemOption(QStyleOptionViewItem *option) const
{
QListView::initViewItemOption(option);
option->showDecorationSelected = true;
// 列表项不会变为Inactive状态
option->state |= QStyle::State_Active;
if (viewMode() == QListView::ListMode) {
option->decorationAlignment = Qt::AlignVCenter;
}
}
#endif
QModelIndex DListView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
switch (cursorAction) {
case MoveNext: {
auto curIdx = currentIndex();
auto curCol = modelColumn();
auto nextRow = 0;
if (curIdx.isValid()) {
nextRow = (curIdx.row() + 1) % model()->rowCount();
}
int hiddenCount = 0;
while (isRowHidden(nextRow)) {
nextRow = (nextRow + 1) % model()->rowCount();
//防止所有列都是隐藏的死循环
++hiddenCount;
if (hiddenCount >= count()) {
return currentIndex();
}
}
return model()->index(nextRow, curCol);
}
default:
break;
}
return QListView::moveCursor(cursorAction, modifiers);
}
DWIDGET_END_NAMESPACE
#include "moc_dlistview.cpp"