-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathqtreeview.cpp
4079 lines (3594 loc) · 134 KB
/
qtreeview.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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtreeview.h"
#include <qheaderview.h>
#include <qitemdelegate.h>
#include <qapplication.h>
#include <qscrollbar.h>
#include <qpainter.h>
#include <qstack.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qevent.h>
#include <qpen.h>
#include <qdebug.h>
#include <QMetaMethod>
#include <private/qscrollbar_p.h>
#ifndef QT_NO_ACCESSIBILITY
#include <qaccessible.h>
#endif
#include <private/qapplication_p.h>
#include <private/qtreeview_p.h>
#include <private/qheaderview_p.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
/*!
\class QTreeView
\brief The QTreeView class provides a default model/view implementation of a tree view.
\ingroup model-view
\ingroup advanced
\inmodule QtWidgets
\image windows-treeview.png
A QTreeView implements a tree representation of items from a
model. This class is used to provide standard hierarchical lists that
were previously provided by the \c QListView class, but using the more
flexible approach provided by Qt's model/view architecture.
The QTreeView class is one of the \l{Model/View Classes} and is part of
Qt's \l{Model/View Programming}{model/view framework}.
QTreeView implements the interfaces defined by the
QAbstractItemView class to allow it to display data provided by
models derived from the QAbstractItemModel class.
It is simple to construct a tree view displaying data from a
model. In the following example, the contents of a directory are
supplied by a QFileSystemModel and displayed as a tree:
\snippet shareddirmodel/main.cpp 3
\snippet shareddirmodel/main.cpp 6
The model/view architecture ensures that the contents of the tree view
are updated as the model changes.
Items that have children can be in an expanded (children are
visible) or collapsed (children are hidden) state. When this state
changes a collapsed() or expanded() signal is emitted with the
model index of the relevant item.
The amount of indentation used to indicate levels of hierarchy is
controlled by the \l indentation property.
Headers in tree views are constructed using the QHeaderView class and can
be hidden using \c{header()->hide()}. Note that each header is configured
with its \l{QHeaderView::}{stretchLastSection} property set to true,
ensuring that the view does not waste any of the space assigned to it for
its header. If this value is set to true, this property will override the
resize mode set on the last section in the header.
By default, all columns in a tree view are movable except the first. To
disable movement of these columns, use QHeaderView's
\l {QHeaderView::}{setSectionsMovable()} function. For more information
about rearranging sections, see \l {Moving Header Sections}.
\section1 Key Bindings
QTreeView supports a set of key bindings that enable the user to
navigate in the view and interact with the contents of items:
\table
\header \li Key \li Action
\row \li Up \li Moves the cursor to the item in the same column on
the previous row. If the parent of the current item has no more rows to
navigate to, the cursor moves to the relevant item in the last row
of the sibling that precedes the parent.
\row \li Down \li Moves the cursor to the item in the same column on
the next row. If the parent of the current item has no more rows to
navigate to, the cursor moves to the relevant item in the first row
of the sibling that follows the parent.
\row \li Left \li Hides the children of the current item (if present)
by collapsing a branch.
\row \li Minus \li Same as Left.
\row \li Right \li Reveals the children of the current item (if present)
by expanding a branch.
\row \li Plus \li Same as Right.
\row \li Asterisk \li Expands the current item and all its children
(if present).
\row \li PageUp \li Moves the cursor up one page.
\row \li PageDown \li Moves the cursor down one page.
\row \li Home \li Moves the cursor to an item in the same column of the first
row of the first top-level item in the model.
\row \li End \li Moves the cursor to an item in the same column of the last
row of the last top-level item in the model.
\row \li F2 \li In editable models, this opens the current item for editing.
The Escape key can be used to cancel the editing process and revert
any changes to the data displayed.
\endtable
\omit
Describe the expanding/collapsing concept if not covered elsewhere.
\endomit
\section1 Improving Performance
It is possible to give the view hints about the data it is handling in order
to improve its performance when displaying large numbers of items. One approach
that can be taken for views that are intended to display items with equal heights
is to set the \l uniformRowHeights property to true.
\sa QListView, QTreeWidget, {View Classes}, QAbstractItemModel, QAbstractItemView,
{Dir View Example}
*/
/*!
\fn void QTreeView::expanded(const QModelIndex &index)
This signal is emitted when the item specified by \a index is expanded.
*/
/*!
\fn void QTreeView::collapsed(const QModelIndex &index)
This signal is emitted when the item specified by \a index is collapsed.
*/
/*!
Constructs a tree view with a \a parent to represent a model's
data. Use setModel() to set the model.
\sa QAbstractItemModel
*/
QTreeView::QTreeView(QWidget *parent)
: QAbstractItemView(*new QTreeViewPrivate, parent)
{
Q_D(QTreeView);
d->initialize();
}
/*!
\internal
*/
QTreeView::QTreeView(QTreeViewPrivate &dd, QWidget *parent)
: QAbstractItemView(dd, parent)
{
Q_D(QTreeView);
d->initialize();
}
/*!
Destroys the tree view.
*/
QTreeView::~QTreeView()
{
}
/*!
\reimp
*/
void QTreeView::setModel(QAbstractItemModel *model)
{
Q_D(QTreeView);
if (model == d->model)
return;
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(rowsRemoved(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));
}
if (d->selectionModel) { // support row editing
disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
d->model, SLOT(submit()));
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(rowsRemoved(QModelIndex,int,int)));
disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));
}
d->viewItems.clear();
d->expandedIndexes.clear();
d->hiddenIndexes.clear();
d->geometryRecursionBlock = true; // do not update geometries due to signals from the headers
d->header->setModel(model);
d->geometryRecursionBlock = false;
QAbstractItemView::setModel(model);
// QAbstractItemView connects to a private slot
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
// do header layout after the tree
disconnect(d->model, SIGNAL(layoutChanged()),
d->header, SLOT(_q_layoutChanged()));
// QTreeView has a public slot for this
connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(rowsRemoved(QModelIndex,int,int)));
connect(d->model, SIGNAL(modelAboutToBeReset()), SLOT(_q_modelAboutToBeReset()));
if (d->sortingEnabled)
d->_q_sortIndicatorChanged(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
}
/*!
\reimp
*/
void QTreeView::setRootIndex(const QModelIndex &index)
{
Q_D(QTreeView);
d->header->setRootIndex(index);
QAbstractItemView::setRootIndex(index);
}
/*!
\reimp
*/
void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
{
Q_D(QTreeView);
Q_ASSERT(selectionModel);
if (d->selectionModel) {
// support row editing
disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
d->model, SLOT(submit()));
}
d->header->setSelectionModel(selectionModel);
QAbstractItemView::setSelectionModel(selectionModel);
if (d->selectionModel) {
// support row editing
connect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
d->model, SLOT(submit()));
}
}
/*!
Returns the header for the tree view.
\sa QAbstractItemModel::headerData()
*/
QHeaderView *QTreeView::header() const
{
Q_D(const QTreeView);
return d->header;
}
/*!
Sets the header for the tree view, to the given \a header.
The view takes ownership over the given \a header and deletes it
when a new header is set.
\sa QAbstractItemModel::headerData()
*/
void QTreeView::setHeader(QHeaderView *header)
{
Q_D(QTreeView);
if (header == d->header || !header)
return;
if (d->header && d->header->parent() == this)
delete d->header;
d->header = header;
d->header->setParent(this);
d->header->setFirstSectionMovable(false);
if (!d->header->model()) {
d->header->setModel(d->model);
if (d->selectionModel)
d->header->setSelectionModel(d->selectionModel);
}
connect(d->header, SIGNAL(sectionResized(int,int,int)),
this, SLOT(columnResized(int,int,int)));
connect(d->header, SIGNAL(sectionMoved(int,int,int)),
this, SLOT(columnMoved()));
connect(d->header, SIGNAL(sectionCountChanged(int,int)),
this, SLOT(columnCountChanged(int,int)));
connect(d->header, SIGNAL(sectionHandleDoubleClicked(int)),
this, SLOT(resizeColumnToContents(int)));
connect(d->header, SIGNAL(geometriesChanged()),
this, SLOT(updateGeometries()));
setSortingEnabled(d->sortingEnabled);
d->updateGeometry();
}
/*!
\property QTreeView::autoExpandDelay
\brief The delay time before items in a tree are opened during a drag and drop operation.
\since 4.3
This property holds the amount of time in milliseconds that the user must wait over
a node before that node will automatically open or close. If the time is
set to less then 0 then it will not be activated.
By default, this property has a value of -1, meaning that auto-expansion is disabled.
*/
int QTreeView::autoExpandDelay() const
{
Q_D(const QTreeView);
return d->autoExpandDelay;
}
void QTreeView::setAutoExpandDelay(int delay)
{
Q_D(QTreeView);
d->autoExpandDelay = delay;
}
/*!
\property QTreeView::indentation
\brief indentation of the items in the tree view.
This property holds the indentation measured in pixels of the items for each
level in the tree view. For top-level items, the indentation specifies the
horizontal distance from the viewport edge to the items in the first column;
for child items, it specifies their indentation from their parent items.
By default, the value of this property is style dependent. Thus, when the style
changes, this property updates from it. Calling setIndentation() stops the updates,
calling resetIndentation() will restore default behavior.
*/
int QTreeView::indentation() const
{
Q_D(const QTreeView);
return d->indent;
}
void QTreeView::setIndentation(int i)
{
Q_D(QTreeView);
if (!d->customIndent || (i != d->indent)) {
d->indent = i;
d->customIndent = true;
d->viewport->update();
}
}
void QTreeView::resetIndentation()
{
Q_D(QTreeView);
if (d->customIndent) {
d->updateIndentationFromStyle();
d->customIndent = false;
}
}
/*!
\property QTreeView::rootIsDecorated
\brief whether to show controls for expanding and collapsing top-level items
Items with children are typically shown with controls to expand and collapse
them, allowing their children to be shown or hidden. If this property is
false, these controls are not shown for top-level items. This can be used to
make a single level tree structure appear like a simple list of items.
By default, this property is \c true.
*/
bool QTreeView::rootIsDecorated() const
{
Q_D(const QTreeView);
return d->rootDecoration;
}
void QTreeView::setRootIsDecorated(bool show)
{
Q_D(QTreeView);
if (show != d->rootDecoration) {
d->rootDecoration = show;
d->viewport->update();
}
}
/*!
\property QTreeView::uniformRowHeights
\brief whether all items in the treeview have the same height
This property should only be set to true if it is guaranteed that all items
in the view has the same height. This enables the view to do some
optimizations.
The height is obtained from the first item in the view. It is updated
when the data changes on that item.
\note If the editor size hint is bigger than the cell size hint, then the
size hint of the editor will be used.
By default, this property is \c false.
*/
bool QTreeView::uniformRowHeights() const
{
Q_D(const QTreeView);
return d->uniformRowHeights;
}
void QTreeView::setUniformRowHeights(bool uniform)
{
Q_D(QTreeView);
d->uniformRowHeights = uniform;
}
/*!
\property QTreeView::itemsExpandable
\brief whether the items are expandable by the user.
This property holds whether the user can expand and collapse items
interactively.
By default, this property is \c true.
*/
bool QTreeView::itemsExpandable() const
{
Q_D(const QTreeView);
return d->itemsExpandable;
}
void QTreeView::setItemsExpandable(bool enable)
{
Q_D(QTreeView);
d->itemsExpandable = enable;
}
/*!
\property QTreeView::expandsOnDoubleClick
\since 4.4
\brief whether the items can be expanded by double-clicking.
This property holds whether the user can expand and collapse items
by double-clicking. The default value is true.
\sa itemsExpandable
*/
bool QTreeView::expandsOnDoubleClick() const
{
Q_D(const QTreeView);
return d->expandsOnDoubleClick;
}
void QTreeView::setExpandsOnDoubleClick(bool enable)
{
Q_D(QTreeView);
d->expandsOnDoubleClick = enable;
}
/*!
Returns the horizontal position of the \a column in the viewport.
*/
int QTreeView::columnViewportPosition(int column) const
{
Q_D(const QTreeView);
return d->header->sectionViewportPosition(column);
}
/*!
Returns the width of the \a column.
\sa resizeColumnToContents(), setColumnWidth()
*/
int QTreeView::columnWidth(int column) const
{
Q_D(const QTreeView);
return d->header->sectionSize(column);
}
/*!
\since 4.2
Sets the width of the given \a column to the \a width specified.
\sa columnWidth(), resizeColumnToContents()
*/
void QTreeView::setColumnWidth(int column, int width)
{
Q_D(QTreeView);
d->header->resizeSection(column, width);
}
/*!
Returns the column in the tree view whose header covers the \a x
coordinate given.
*/
int QTreeView::columnAt(int x) const
{
Q_D(const QTreeView);
return d->header->logicalIndexAt(x);
}
/*!
Returns \c true if the \a column is hidden; otherwise returns \c false.
\sa hideColumn(), isRowHidden()
*/
bool QTreeView::isColumnHidden(int column) const
{
Q_D(const QTreeView);
return d->header->isSectionHidden(column);
}
/*!
If \a hide is true the \a column is hidden, otherwise the \a column is shown.
\sa hideColumn(), setRowHidden()
*/
void QTreeView::setColumnHidden(int column, bool hide)
{
Q_D(QTreeView);
if (column < 0 || column >= d->header->count())
return;
d->header->setSectionHidden(column, hide);
}
/*!
\property QTreeView::headerHidden
\brief whether the header is shown or not.
\since 4.4
If this property is \c true, the header is not shown otherwise it is.
The default value is false.
\sa header()
*/
bool QTreeView::isHeaderHidden() const
{
Q_D(const QTreeView);
return d->header->isHidden();
}
void QTreeView::setHeaderHidden(bool hide)
{
Q_D(QTreeView);
d->header->setHidden(hide);
}
/*!
Returns \c true if the item in the given \a row of the \a parent is hidden;
otherwise returns \c false.
\sa setRowHidden(), isColumnHidden()
*/
bool QTreeView::isRowHidden(int row, const QModelIndex &parent) const
{
Q_D(const QTreeView);
if (!d->model)
return false;
return d->isRowHidden(d->model->index(row, 0, parent));
}
/*!
If \a hide is true the \a row with the given \a parent is hidden, otherwise the \a row is shown.
\sa isRowHidden(), setColumnHidden()
*/
void QTreeView::setRowHidden(int row, const QModelIndex &parent, bool hide)
{
Q_D(QTreeView);
if (!d->model)
return;
QModelIndex index = d->model->index(row, 0, parent);
if (!index.isValid())
return;
if (hide) {
d->hiddenIndexes.insert(index);
} else if(d->isPersistent(index)) { //if the index is not persistent, it cannot be in the set
d->hiddenIndexes.remove(index);
}
d->doDelayedItemsLayout();
}
/*!
\since 4.3
Returns \c true if the item in first column in the given \a row
of the \a parent is spanning all the columns; otherwise returns \c false.
\sa setFirstColumnSpanned()
*/
bool QTreeView::isFirstColumnSpanned(int row, const QModelIndex &parent) const
{
Q_D(const QTreeView);
if (d->spanningIndexes.isEmpty() || !d->model)
return false;
const QModelIndex index = d->model->index(row, 0, parent);
return d->spanningIndexes.contains(index);
}
/*!
\since 4.3
If \a span is true the item in the first column in the \a row
with the given \a parent is set to span all columns, otherwise all items
on the \a row are shown.
\sa isFirstColumnSpanned()
*/
void QTreeView::setFirstColumnSpanned(int row, const QModelIndex &parent, bool span)
{
Q_D(QTreeView);
if (!d->model)
return;
const QModelIndex index = d->model->index(row, 0, parent);
if (!index.isValid())
return;
if (span)
d->spanningIndexes.insert(index);
else
d->spanningIndexes.remove(index);
d->executePostedLayout();
int i = d->viewIndex(index);
if (i >= 0)
d->viewItems[i].spanning = span;
d->viewport->update();
}
/*!
\reimp
*/
void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
Q_D(QTreeView);
// if we are going to do a complete relayout anyway, there is no need to update
if (d->delayedPendingLayout)
return;
// refresh the height cache here; we don't really lose anything by getting the size hint,
// since QAbstractItemView::dataChanged() will get the visualRect for the items anyway
bool sizeChanged = false;
int topViewIndex = d->viewIndex(topLeft);
if (topViewIndex == 0) {
int newDefaultItemHeight = indexRowSizeHint(topLeft);
sizeChanged = d->defaultItemHeight != newDefaultItemHeight;
d->defaultItemHeight = newDefaultItemHeight;
}
if (topViewIndex != -1) {
if (topLeft.row() == bottomRight.row()) {
int oldHeight = d->itemHeight(topViewIndex);
d->invalidateHeightCache(topViewIndex);
sizeChanged |= (oldHeight != d->itemHeight(topViewIndex));
if (topLeft.column() == 0)
d->viewItems[topViewIndex].hasChildren = d->hasVisibleChildren(topLeft);
} else {
int bottomViewIndex = d->viewIndex(bottomRight);
for (int i = topViewIndex; i <= bottomViewIndex; ++i) {
int oldHeight = d->itemHeight(i);
d->invalidateHeightCache(i);
sizeChanged |= (oldHeight != d->itemHeight(i));
if (topLeft.column() == 0)
d->viewItems[i].hasChildren = d->hasVisibleChildren(d->viewItems.at(i).index);
}
}
}
if (sizeChanged) {
d->updateScrollBars();
d->viewport->update();
}
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
}
/*!
Hides the \a column given.
\note This function should only be called after the model has been
initialized, as the view needs to know the number of columns in order to
hide \a column.
\sa showColumn(), setColumnHidden()
*/
void QTreeView::hideColumn(int column)
{
Q_D(QTreeView);
if (d->header->isSectionHidden(column))
return;
d->header->hideSection(column);
doItemsLayout();
}
/*!
Shows the given \a column in the tree view.
\sa hideColumn(), setColumnHidden()
*/
void QTreeView::showColumn(int column)
{
Q_D(QTreeView);
if (!d->header->isSectionHidden(column))
return;
d->header->showSection(column);
doItemsLayout();
}
/*!
\fn void QTreeView::expand(const QModelIndex &index)
Expands the model item specified by the \a index.
\sa expanded()
*/
void QTreeView::expand(const QModelIndex &index)
{
Q_D(QTreeView);
if (!d->isIndexValid(index))
return;
if (index.flags() & Qt::ItemNeverHasChildren)
return;
if (d->isIndexExpanded(index))
return;
if (d->delayedPendingLayout) {
//A complete relayout is going to be performed, just store the expanded index, no need to layout.
if (d->storeExpanded(index))
emit expanded(index);
return;
}
int i = d->viewIndex(index);
if (i != -1) { // is visible
d->expand(i, true);
if (!d->isAnimating()) {
updateGeometries();
d->viewport->update();
}
} else if (d->storeExpanded(index)) {
emit expanded(index);
}
}
/*!
\fn void QTreeView::collapse(const QModelIndex &index)
Collapses the model item specified by the \a index.
\sa collapsed()
*/
void QTreeView::collapse(const QModelIndex &index)
{
Q_D(QTreeView);
if (!d->isIndexValid(index))
return;
if (!d->isIndexExpanded(index))
return;
//if the current item is now invisible, the autoscroll will expand the tree to see it, so disable the autoscroll
d->delayedAutoScroll.stop();
if (d->delayedPendingLayout) {
//A complete relayout is going to be performed, just un-store the expanded index, no need to layout.
if (d->isPersistent(index) && d->expandedIndexes.remove(index))
emit collapsed(index);
return;
}
int i = d->viewIndex(index);
if (i != -1) { // is visible
d->collapse(i, true);
if (!d->isAnimating()) {
updateGeometries();
viewport()->update();
}
} else {
if (d->isPersistent(index) && d->expandedIndexes.remove(index))
emit collapsed(index);
}
}
/*!
\fn bool QTreeView::isExpanded(const QModelIndex &index) const
Returns \c true if the model item \a index is expanded; otherwise returns
false.
\sa expand(), expanded(), setExpanded()
*/
bool QTreeView::isExpanded(const QModelIndex &index) const
{
Q_D(const QTreeView);
return d->isIndexExpanded(index);
}
/*!
Sets the item referred to by \a index to either collapse or expanded,
depending on the value of \a expanded.
\sa expanded(), expand(), isExpanded()
*/
void QTreeView::setExpanded(const QModelIndex &index, bool expanded)
{
if (expanded)
this->expand(index);
else
this->collapse(index);
}
/*!
\since 4.2
\property QTreeView::sortingEnabled
\brief whether sorting is enabled
If this property is \c true, sorting is enabled for the tree; if the property
is false, sorting is not enabled. The default value is false.
\note In order to avoid performance issues, it is recommended that
sorting is enabled \e after inserting the items into the tree.
Alternatively, you could also insert the items into a list before inserting
the items into the tree.
\sa sortByColumn()
*/
void QTreeView::setSortingEnabled(bool enable)
{
Q_D(QTreeView);
header()->setSortIndicatorShown(enable);
header()->setSectionsClickable(enable);
if (enable) {
//sortByColumn has to be called before we connect or set the sortingEnabled flag
// because otherwise it will not call sort on the model.
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
connect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)), Qt::UniqueConnection);
} else {
disconnect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
this, SLOT(_q_sortIndicatorChanged(int,Qt::SortOrder)));
}
d->sortingEnabled = enable;
}
bool QTreeView::isSortingEnabled() const
{
Q_D(const QTreeView);
return d->sortingEnabled;
}
/*!
\since 4.2
\property QTreeView::animated
\brief whether animations are enabled
If this property is \c true the treeview will animate expansion
and collapsing of branches. If this property is \c false, the treeview
will expand or collapse branches immediately without showing
the animation.
By default, this property is \c false.
*/
void QTreeView::setAnimated(bool animate)
{
Q_D(QTreeView);
d->animationsEnabled = animate;
}
bool QTreeView::isAnimated() const
{
Q_D(const QTreeView);
return d->animationsEnabled;
}
/*!
\since 4.2
\property QTreeView::allColumnsShowFocus
\brief whether items should show keyboard focus using all columns
If this property is \c true all columns will show focus, otherwise only
one column will show focus.
The default is false.
*/
void QTreeView::setAllColumnsShowFocus(bool enable)
{
Q_D(QTreeView);
if (d->allColumnsShowFocus == enable)
return;
d->allColumnsShowFocus = enable;
d->viewport->update();
}
bool QTreeView::allColumnsShowFocus() const
{
Q_D(const QTreeView);
return d->allColumnsShowFocus;
}
/*!
\property QTreeView::wordWrap
\brief the item text word-wrapping policy
\since 4.3
If this property is \c true then the item text is wrapped where
necessary at word-breaks; otherwise it is not wrapped at all.
This property is \c false by default.
Note that even if wrapping is enabled, the cell will not be
expanded to fit all text. Ellipsis will be inserted according to
the current \l{QAbstractItemView::}{textElideMode}.
*/
void QTreeView::setWordWrap(bool on)
{
Q_D(QTreeView);
if (d->wrapItemText == on)
return;
d->wrapItemText = on;
d->doDelayedItemsLayout();
}
bool QTreeView::wordWrap() const
{
Q_D(const QTreeView);
return d->wrapItemText;
}
/*!
\since 5.2
This specifies that the tree structure should be placed at logical index \a index.
If \index is set to -1 then the tree will always follow visual index 0.
\sa treePosition(), QHeaderView::swapSections(), QHeaderView::moveSection()
*/
void QTreeView::setTreePosition(int index)
{
Q_D(QTreeView);
d->treePosition = index;
d->viewport->update();
}
/*!
\since 5.2
Return the logical index the tree is set on. If the return value is -1 then the
tree is placed on the visual index 0.
\sa setTreePosition()
*/
int QTreeView::treePosition() const
{
Q_D(const QTreeView);
return d->treePosition;