aboutsummaryrefslogtreecommitdiffstats
path: root/src/qquicktreeview.cpp
blob: a604571d18e13fca4340ff513cb1be621de9ad56 (plain)
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
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of TreeView.
**
** $QT_BEGIN_LICENSE:GPL-MARKETPLACE-QT$
**
** Marketplace License Usage
** Users, who have licensed the Software under the Qt Marketplace license
** agreement, may use this file in accordance with the Qt Marketplace license
** agreement provided with the Software or, alternatively, in accordance with
** the terms contained in a written agreement between the licensee and The Qt
** Company. For licensing terms and conditions see
** https://www.qt.io/terms-conditions/#marketplace and
** https://www.qt.io/terms-conditions. For further information use the contact
** form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) 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.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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/qobject.h>
#include <QtQml/qqmlcontext.h>

#include "qquicktreemodeladaptor_p.h"
#include "qquicktreeview_p_p.h"

/*!
    \qmltype TreeView
    \inqmlmodule QtQuick.TreeView
    \since 5.15
    \inherits TableView
    \brief Provides a tree view to display data from a QAbstractItemModel.
*/

/*!
    \qmlproperty int QtQuick::TreeView::currentIndex

    This property holds the view index that has been selected as current. Selecting
    an index to be current is done by either using the arrow keys, clicking on a row
    (or a cell) with the mouse, or assign \l currentIndex a value explicitly.

    \note \c currentIndex is an index into the the view model. In this model, all
    tree nodes that are visible in the view are flattened into a list that can be
    shown in a table column.
    This also means that currentIndex.row is the table row relative to root node.
    To get the current index in the model, where the row is relative to parent
    node, use \l currentModelIndex or \l mapToModel():

    \code
    var indexInModel = currentModelIndex
    var indexInModelMapped = mapToModel(currentIndex)
    console.assert(indexInModel == indexInModelMapped)
    \endcode

    If no index has been selected as current, or if \l currentModelIndex is
    is not visible in the view, currentIndex.valid will be \c false.

    \sa currentModelIndex, currentItem, mapToModel()
*/

/*!
    \qmlproperty int QtQuick::TreeView::currentModelIndex

    This property holds the model index that has been selected as current. Selecting
    an index to be current is done by either using the arrow keys, clicking on a row
    (or a cell) with the mouse, or assign \l currentIndex a value explicitly.

    \note \c currentModelIndex is an index into the the model. This also means
    that currentModelIndex.row is the child number relative to the parent node.
    To get the current row in the view, which is relative to the
    root, use \l currentIndex or \l mapFromModel():

    \code
    var indexInView = currentViewIndex
    var indexInViewMapped = mapFromModel(currentModelIndex)
    console.assert(indexInView == indexInViewMapped)
    \endcode

    If no index has been selected as current, currentModelIndex.valid will be \c false.

    \sa currentIndex, currentItem, mapFromModel()
*/

/*!
    \qmlproperty int QtQuick::TreeView::currentItem
    \readonly

    This property holds the item that is selected as current. Selecting
    an item to be current is done by either using the arrow keys, clicking on a row
    (or a cell) with the mouse, or assign a value to \l currentIndex explicitly.

    \note Using \l currentItem is \e almost the same as calling
    \c {itemAtModelIndex(currentModelIndex)}. The difference is that the latter
    expression will only change when \l currentModelIndex changes. But \l currentItem
    can also change as a result of the view doing a relayout behind the
    scenes (which can force delegate items to be reloaded or recycled).
    Since the item can be recycled or destroyed at any point in time behind the
    applications back, you should never store this value.

    if \c currentIndex.valid is \c false, this property will be \c null.

    \sa itemAtCell(), currentIndex, currentModelIndex, itemAtModelIndex()
*/

/*!
    \qmlproperty int QtQuick::TreeView::navigationMode

    This property holds how you would like the user to be able
    navigate the tree. It can have the following values:

    \value TreeView.List The tree view acts like as a list view. This means that
        the user can only navigate up and down in the tree, but not sideways to
        the other columns. In this mode the left and right arrow keys will collapse
        and expand nodes in the tree (in addition to \c Qt.Key_Space).
    \value TreeView.Table The tree view acts like a table view. This means that
        the user can navigate up and down, but also left and right through
        all the columns in the tree. In this mode the left and right arrow keys will
        move currentIndex left and right, and only \c Qt.Key_Space will toggle
        nodes to be collapsed or expanded.

    \note the selected mode can also affect how the tree is styled by the delegate.
*/

/*!
    \qmlproperty int QtQuick::TreeView::styleHints

    This property holds a set of hints that can be used to tweak the
    appearance of the delegate without the need to create a custom one.
    The hints that can be set is the following:

    \value styleHints.indicator The color of the collapsed/expanded icon
    \value styleHints.indicatorCurrent The color of the current rows collapsed/expanded icon
    \value styleHints.indicatorHovered The color of the collapsed/expanded icon when hovered
    \value styleHints.overlay The color of the overlay used for \l currentIndex
    \value styleHints.overlayHovered The color of the overlay used for \l currentIndex when hovered
    \value styleHints.foregroundOdd The foreground color of odd rows
    \value styleHints.backgroundOdd The background color of odd rows
    \value styleHints.foregroundEven The foreground color of even rows
    \value styleHints.backgroundEven The background color of even rows
    \value styleHints.foregroundCurrent The foreground color of the current item (or
        the whole row, if \l navigationMode is \c TreeView.List)
    \value styleHints.backgroundCurrent The background color of the current item (or
        the whole row, if \l navigationMode is \c TreeView.List)
    \value styleHints.foregroundHovered The foreground color of the current item when hovered (or
        the whole row, if \l navigationMode is \c TreeView.List)
    \value styleHints.backgroundHovered The background color of the current item when hovered (or
        the whole row, if \l navigationMode is \c TreeView.List)
    \value styleHints.indent The horizontal space between a parent and a child node
    \value styleHints.columnPadding The padding between a cell and its contents
    \value styleHints.font The font used by text items in the delegate

    \note a delegate is free to ignore any hints specified by the application.
*/

/*!
    \qmlmethod bool QtQuick::TreeView::hasChildren(row)

    Returns if the given \a row in the view is shown as
    expanded.

    \note \a row should be the row in the view, not in the model.

    \sa viewIndex
*/

/*!
    \qmlmethod bool QtQuick::TreeView::hasSiblings(row)

    Returns if the given \a row in the view has
    siblings.

    \note \a row should be the row in the view, not in the model.

    \sa viewIndex
*/

/*!
    \qmlmethod int QtQuick::TreeView::depth(row)

    Returns the depth (the number of parents up to
    the root) of the given \a row.

    \note \a row should be the row in the view, not in the model.

    \sa viewIndex
*/

/*!
    \qmlmethod bool QtQuick::TreeView::isExpanded(row)

    Returns if the given \a row in the view is shown as
    expanded.

    \note \a row should be the row in the view, not in the model.

    \sa viewIndex
*/

/*!
    \qmlmethod QtQuick::TreeView::expand(row)

    Expands the tree node at the
    given \a row in the view.

    \note this function will not affect the model, only
    the visual representation in the view.

    \sa viewIndex, collapse(), expandModelIndex(), collapseModelIndex(), isExpanded()
*/

/*!
    \qmlmethod QtQuick::TreeView::collapse(row)

    Collapses the tree node at the
    given \a row in the view.

    \note this function will not affect the model, only
    the visual representation in the view.

    \sa viewIndex, expand(), expandModelIndex(), collapseModelIndex(), isExpanded()
*/

/*!
    \qmlmethod QtQuick::TreeView::toggleExpanded(row)

    Toggles if the tree node at the given \a row
    should be expanded. This is a convenience for doing:

    \code
    if (isExpanded(row))
        collapse(row)
    else
        expand(row)
    \endcode
*/

/*!
    \qmlmethod bool QtQuick::TreeView::isModelIndexExpanded(modelIndex)

    Returns \c true if the given \a modelIndex is shown as expanded
    in the view. This is a convenience for doing:

    \code
    isExpanded(mapFromModel(modelIndex).row)
    \endcode

    \sa viewIndex
*/

/*!
    \qmlmethod QtQuick::TreeView::collapseModelIndex(modelIndex)

    Collapses the tree node at the
    given \a modelIndex. This is a convenience for doing:

    \code
    collapse(mapFromModel(modelIndex).row)
    \endcode

    \sa viewIndex, collapse(), expand(), expandModelIndex(), isExpanded()
*/

/*!
    \qmlmethod QtQuick::TreeView::expandModelIndex(modelIndex)

    Expands the tree node at the
    given \a modelIndex. This is a convenience for doing:

    \code
    expand(mapFromModel(modelIndex).row)
    \endcode

    \sa viewIndex, collapse(), expand(), collapseModelIndex(), isExpanded()
*/

/*!
    \qmlmethod QtQuick::TreeView::toggleModelIndexExpanded(modelIndex)

    Toggles if the tree node at the given \a modelIndex
    should be expanded. This is a convenience for doing:

    \code
    if (isModelIndexExpanded(modelIndex))
        collapseModelIndex(modelIndex)
    else
        expandModelIndex(modelIndex)
    \endcode
*/

/*!
    \qmlmethod int QtQuick::TreeView::columnAtX(x, includeSpacing)

    Returns the column under \a x in view coordinates.

    If \a includeSpacing is set to \c false, and \a x is on top
    of the spacing between the columns, the return value will be -1.
    Otherwise, if \a x is on top of the spacing and \a includeSpacing
    is set to \c true, returns the column closest to the position.
*/

/*!
    \qmlmethod int QtQuick::TreeView::rowAtY(y, includeSpacing)

    Returns the row under \a y in view coordinates.

    If \a includeSpacing is set to \c false, and \a y is on top
    of the spacing between the rows, returns -1.
    Otherwise, if \a y is on top of the spacing and \a includeSpacing
    is set to \c true, returns the row closest to the position.
*/

/*!
    \qmlmethod Item QtQuick::TreeView::itemAtCell(point cell)

    Returns the item inside the given table \a cell. If the cell
    is not visible in the view, returns \c null.

    \sa currentItem, itemAtIndex()
*/

/*!
    \qmlmethod Item QtQuick::TreeView::itemAtIndex(viewIndex)

    Returns the item at the given view index \a viewIndex. If
    \c viewIndex.valid is \c false, returns \c null.

    \sa currentItem, itemAtCell(), viewIndex(), itemAtModelIndex()
*/

/*!
    \qmlmethod Item QtQuick::TreeView::itemAtModelIndex(modelIndex)

    Returns the item at the given model index \a modelIndex. If the
    item that represents the model index is not visible in the view,
    returns \c null. Convenience for doing:

    \code
    itemAtIndex(mapFromModel(modelIndex))
    \endcode

    \sa currentItem, itemAtCell(), viewIndex(), itemAtIndex()
*/

/*!
    \qmlmethod QModelIndex QtQuick::TreeView::viewIndex(column, row)

    Creates a model index into the view model from the given
    \a row and \a column.

    \note this index is only valid for use with the view. To create
    a model index to use with the model, refer to the documentation for
    \l QAbstractItemModel.
*/

/*!
    \qmlmethod QModelIndex QtQuick::TreeView::mapToModel(viewIndex)

    Maps \a viewIndex, an index pointing to an item in the view (the
    view model), to the corresponding model index in the model.

    TreeView uses a view model internally to convert a tree model into
    a model suited to be shown in a \l TableView. This view model will
    flatten the parts of the tree model that at any point in time is visible
    inside view, to a list. Depending on what you need to do, it's sometimes
    easier to work with model indexes rather than view indexes, and vice-versa.

    \sa mapFromModel()
*/

/*!
    \qmlmethod QModelIndex QtQuick::TreeView::mapFromModel(modelIndex)

    Maps an index, \a modelIndex, pointing to an item in the model to the
    corresponding model index in the view (the view model).

    TreeView uses a view model internally to convert a tree model into
    a model suited to be shown in a \l TableView. This view model will
    flatten the parts of the tree model that at any point in time is
    visible inside view, to a list. Depending on what you need to
    do, it's sometimes easier to work with model indexes rather than
    view indexes, and vice-versa.

    \sa mapToModel()
*/

/*!
    \qmlattachedproperty TreeView TreeView::view

    This attached property holds the view that manages the delegate instance.
    It is attached to each instance of the delegate.
*/

/*!
    \qmlattachedproperty bool TreeView::hasChildren

    This attached property holds if the delegate instance is to be shown as
    having children (the model index represented by the instance has children
    in the model). It is attached to each instance of the delegate.
*/

/*!
    \qmlattachedproperty bool TreeView::isExpanded

    This attached property holds if the delegate instance is to be shown as
    expanded (the view index represented by the instance is expanded in the view model).
    It is attached to each instance of the delegate.
*/

/*!
    \qmlattachedproperty int TreeView::depth

    This attached property holds the tree depth (the number of parents up to the root)
    of the delegate instance. It is attached to each instance of the delegate.
*/

QT_BEGIN_NAMESPACE

QQuickTreeViewPrivate::QQuickTreeViewPrivate()
    : QQuickTableViewPrivate()
{
}

QQuickTreeViewPrivate::~QQuickTreeViewPrivate()
{
}

QVariant QQuickTreeViewPrivate::modelImpl() const
{
    return m_assignedModel;
}

void QQuickTreeViewPrivate::setModelImpl(const QVariant &newModel)
{
    Q_Q(QQuickTreeView);

    if (newModel == m_assignedModel)
        return;

    m_assignedModel = newModel;
    QVariant effectiveModel = m_assignedModel;
    if (effectiveModel.userType() == qMetaTypeId<QJSValue>())
        effectiveModel = effectiveModel.value<QJSValue>().toVariant();

    if (effectiveModel.isNull())
        m_proxyModel.setModel(nullptr);
    else if (const auto qaim = qvariant_cast<QAbstractItemModel*>(effectiveModel))
        m_proxyModel.setModel(qaim);
    else
        qmlWarning(q) << "treeView only accept models of type QAbstractItemModel";


    scheduleRebuildTable(QQuickTableViewPrivate::RebuildOption::All);
    emit q->modelChanged();
}

void QQuickTreeViewPrivate::syncModel()
{
    if (model) {
        QObjectPrivate::disconnect(model, &QQmlInstanceModel::initItem, this, &QQuickTreeViewPrivate::initItemCallback);
        QObjectPrivate::disconnect(model, &QQmlInstanceModel::itemReused, this, &QQuickTreeViewPrivate::itemReusedCallback);
    }

    QQuickTableViewPrivate::syncModel();

    if (model) {
        QObjectPrivate::connect(model, &QQmlInstanceModel::initItem, this, &QQuickTreeViewPrivate::initItemCallback);
        QObjectPrivate::connect(model, &QQmlInstanceModel::itemReused, this, &QQuickTreeViewPrivate::itemReusedCallback);
    }
}

QQuickTreeViewAttached *QQuickTreeViewPrivate::getAttachedObject(const QObject *object) const
{
    QObject *attachedObject = qmlAttachedPropertiesObject<QQuickTreeView>(object);
    return static_cast<QQuickTreeViewAttached *>(attachedObject);
}

void QQuickTreeViewPrivate::initItemCallback(int modelIndex, QObject *object)
{
    Q_UNUSED(modelIndex);
    Q_Q(QQuickTreeView);

    if (auto attached = getAttachedObject(object)) {
        const auto context = qmlContext(object);
        const int row = context->contextProperty("row").toInt();
        attached->setView(q);
        attached->setIsExpanded(q->isExpanded(row));
        attached->setHasChildren(q->hasChildren(row));
        attached->setDepth(m_proxyModel.depthAtRow(row));
    }
}

void QQuickTreeViewPrivate::itemReusedCallback(int modelIndex, QObject *object)
{
    Q_UNUSED(modelIndex);
    Q_Q(QQuickTreeView);

    if (auto attached = getAttachedObject(object)) {
        const auto context = qmlContext(object);
        const int row = context->contextProperty("row").toInt();
        attached->setIsExpanded(q->isExpanded(row));
        attached->setHasChildren(q->hasChildren(row));
        attached->setDepth(m_proxyModel.depthAtRow(row));
    }
}

void QQuickTreeViewPrivate::updatePolish()
{
    QQuickTableViewPrivate::updatePolish();
    if (loadRequest.isActive())
        return;

    checkForPropertyChanges();
}

void QQuickTreeViewPrivate::checkForPropertyChanges()
{
    Q_Q(QQuickTreeView);

    if (m_currentModelIndexEmitted != m_currentModelIndex) {
        // m_currentIndex is a QPersistentModelIndex which will update automatically, so
        // we need this extra detour to check if is has changed after a model change.
        m_currentModelIndexEmitted = m_currentModelIndex;
        emit q->currentIndexChanged();
        emit q->currentModelIndexChanged();
    }

    QQuickItem *currentItem = q->currentItem();
    if (m_currentItemEmitted != currentItem) {
        // Because QQuickTableView shuffles and reuse items, we need to check each time
        // after a rebuild if currentItem has changed, and not trust that this only changes
        // when m_currentViewIndex changes.
        m_currentItemEmitted = currentItem;
        emit q->currentItemChanged();
    }
}

QQuickItem *QQuickTreeViewPrivate::itemAtCell(const QPoint &cell) const
{
    // (copy from qquicktableview in Qt6)
    const int modelIndex = modelIndexAtCell(cell);
    if (!loadedItems.contains(modelIndex))
        return nullptr;
    return loadedItems.value(modelIndex)->item;
}

qreal QQuickTreeViewPrivate::effectiveRowHeight(int row) const
{
    // (copy from qquicktableview in Qt6)
    return loadedTableItem(QPoint(leftColumn(), row))->geometry().height();
}

qreal QQuickTreeViewPrivate::effectiveColumnWidth(int column) const
{
    // (copy from qquicktableview in Qt6)
    return loadedTableItem(QPoint(column, topRow()))->geometry().width();
}

void QQuickTreeViewPrivate::moveCurrentViewIndex(int directionX, int directionY)
{
    Q_Q(QQuickTreeView);
    const QModelIndex oldViewIndex = q->mapFromModel(m_currentModelIndex);
    const int row = qBound(0, oldViewIndex.row() + directionY, q->rows() - 1);
    const int column = qBound(0, oldViewIndex.column() + directionX, q->columns() - 1);
    const QModelIndex newViewIndex = q->viewIndex(column, row);
    q->setCurrentModelIndex(q->mapToModel(newViewIndex));

    // Move the current item into the viewport so that it loads
    if (column < leftColumn())
        q->setContentX(q->contentX() - cellSpacing.width() - 1);
    else if (column > rightColumn())
        q->setContentX(q->contentX() + cellSpacing.width() + 1);

    if (row < topRow())
        q->setContentY(q->contentY() - cellSpacing.height() - 1);
    else if (row > bottomRow())
        q->setContentY(q->contentY() + cellSpacing.height() + 1);

    if (QQuickItem *item = q->currentItem()) {
        // Ensure that the whole item is visible
        const QRectF itemRect = QRectF(q->mapFromItem(q->contentItem(), item->position()), item->size());

        if (itemRect.x() <= 0)
            q->setContentX(item->x());
        else if (itemRect.right() > q->size().width())
            q->setContentX(item->x() + item->width() - q->width());

        if (itemRect.y() <= 0)
            q->setContentY(item->y());
        else if (itemRect.bottom() > q->size().height())
            q->setContentY(item->y() + item->height() - q->height());
    }
}

QQuickTreeView::QQuickTreeView(QQuickItem *parent)
    : QQuickTableView(*(new QQuickTreeViewPrivate), parent)
{
    Q_D(QQuickTreeView);

    // QQuickTableView will only ever see the proxy model
    const auto proxy = QVariant::fromValue(std::addressof(d->m_proxyModel));
    d->QQuickTableViewPrivate::setModelImpl(proxy);
}

QQuickTreeView::~QQuickTreeView()
{
}

bool QQuickTreeView::isExpanded(int row) const
{
    if (row < 0 || row >= rows())
        return false;

    return d_func()->m_proxyModel.isExpanded(row);
}

bool QQuickTreeView::hasChildren(int row) const
{
    if (row < 0 || row >= rows())
        return false;

    return d_func()->m_proxyModel.hasChildren(row);
}

bool QQuickTreeView::hasSiblings(int row) const
{
    if (row < 0 || row >= rows())
        return false;

    return d_func()->m_proxyModel.hasSiblings(row);
}

int QQuickTreeView::depth(int row) const
{
    if (row < 0 || row >= rows())
        return -1;

    return d_func()->m_proxyModel.depthAtRow(row);
}

void QQuickTreeView::expand(int row)
{
    Q_D(QQuickTreeView);
    if (row < 0 || row >= rows())
        return;

    if (d->m_proxyModel.isExpanded(row))
        return;

    d->m_proxyModel.expandRow(row);

    if (const auto delegateItem = d->itemAtCell(QPoint(0, row))) {
        if (auto attached = d->getAttachedObject(delegateItem))
            attached->setIsExpanded(true);
    }

    emit expanded(row);
}

void QQuickTreeView::expandModelIndex(const QModelIndex &modelIndex)
{
    expand(mapFromModel(modelIndex).row());
}

void QQuickTreeView::collapseModelIndex(const QModelIndex &modelIndex)
{
    collapse(mapFromModel(modelIndex).row());
}

void QQuickTreeView::toggleModelIndexExpanded(const QModelIndex &modelIndex)
{
    toggleExpanded(mapFromModel(modelIndex).row());
}

void QQuickTreeView::collapse(int row)
{
    Q_D(QQuickTreeView);
    if (row < 0 || row >= rows())
        return;

    if (!d->m_proxyModel.isExpanded(row))
        return;

    d_func()->m_proxyModel.collapseRow(row);

    if (const auto delegateItem = d->itemAtCell(QPoint(0, row))) {
        if (auto attached = d->getAttachedObject(delegateItem))
            attached->setIsExpanded(false);
    }

    emit collapsed(row);
}

void QQuickTreeView::toggleExpanded(int row)
{
    if (isExpanded(row))
        collapse(row);
    else
        expand(row);
}

bool QQuickTreeView::isModelIndexExpanded(const QModelIndex &modelIndex) const
{
    return isExpanded(mapFromModel(modelIndex).row());
}

int QQuickTreeView::rowAtY(int y, bool includeSpacing)
{
    // (copy from qquicktableview in Qt6)
    Q_D(const QQuickTreeView);

    if (!boundingRect().contains(QPointF(x(), y)))
        return -1;

    const qreal vSpace = d->cellSpacing.height();
    qreal currentRowEnd = d->loadedTableOuterRect.y() - contentY();
    int foundRow = -1;

    for (auto const row : qAsConst(d->loadedRows).keys()) {
        currentRowEnd += d->effectiveRowHeight(row);
        if (y < currentRowEnd) {
            foundRow = row;
            break;
        }
        currentRowEnd += vSpace;
        if (!includeSpacing && y < currentRowEnd) {
            // Hit spacing
            return -1;
        }
        if (includeSpacing && y < currentRowEnd - (vSpace / 2)) {
            foundRow = row;
            break;
        }
    }

    return foundRow;
}

int QQuickTreeView::columnAtX(int x, bool includeSpacing)
{
    // (copy from qquicktableview in Qt6)
    Q_D(const QQuickTreeView);

    if (!boundingRect().contains(QPointF(x, y())))
        return -1;

    const qreal hSpace = d->cellSpacing.width();
    qreal currentColumnEnd = d->loadedTableOuterRect.x() - contentX();
    int foundColumn = -1;

    for (auto const column : qAsConst(d->loadedColumns).keys()) {
        currentColumnEnd += d->effectiveColumnWidth(column);
        if (x < currentColumnEnd) {
            foundColumn = column;
            break;
        }
        currentColumnEnd += hSpace;
        if (!includeSpacing && x < currentColumnEnd) {
            // Hit spacing
            return -1;
        } else if (includeSpacing && x < currentColumnEnd - (hSpace / 2)) {
            foundColumn = column;
            break;
        }
    }

    return foundColumn;
}

QModelIndex QQuickTreeView::viewIndex(int column, int row)
{
    return d_func()->m_proxyModel.index(row, column);
}

QModelIndex QQuickTreeView::mapToModel(const QModelIndex &viewIndex) const
{
    return d_func()->m_proxyModel.mapToModel(viewIndex);
}

QModelIndex QQuickTreeView::mapFromModel(const QModelIndex &modelIndex) const
{
    return d_func()->m_proxyModel.mapFromModel(modelIndex);
}

QModelIndex QQuickTreeView::currentIndex() const
{
    return mapFromModel(currentModelIndex());
}

void QQuickTreeView::setCurrentIndex(const QModelIndex &index)
{
    setCurrentModelIndex(mapToModel(index));
}

QModelIndex QQuickTreeView::currentModelIndex() const
{
    return d_func()->m_currentModelIndex;
}

void QQuickTreeView::setCurrentModelIndex(const QModelIndex &modelIndex)
{
    Q_D(QQuickTreeView);
    if (d->m_currentModelIndex == modelIndex)
        return;

    d->m_currentModelIndex = modelIndex;
    d->m_currentModelIndexEmitted = modelIndex;
    d->m_currentItemEmitted = currentItem();

    emit currentIndexChanged();
    emit currentModelIndexChanged();
    emit currentItemChanged();
}

QQuickItem *QQuickTreeView::currentItem() const
{
    return itemAtModelIndex(currentModelIndex());
}

QQuickItem *QQuickTreeView::itemAtCell(const QPoint &cell) const
{
    Q_D(const QQuickTreeView);
    const int modelIndex = d->modelIndexAtCell(cell);
    if (!d->loadedItems.contains(modelIndex))
        return nullptr;
    return d->loadedItems.value(modelIndex)->item;
}

QQuickItem *QQuickTreeView::itemAtIndex(const QModelIndex &viewIndex) const
{
    return itemAtCell(QPoint(viewIndex.column(), viewIndex.row()));
}

QQuickItem *QQuickTreeView::itemAtModelIndex(const QModelIndex &modelIndex) const
{
    return itemAtIndex(mapFromModel(modelIndex));
}

void QQuickTreeView::keyPressEvent(QKeyEvent *e)
{
    Q_D(QQuickTreeView);

    switch (e->key()) {
    case Qt::Key_Up:
        d->moveCurrentViewIndex(0, -1);
        break;
    case Qt::Key_Down:
        d->moveCurrentViewIndex(0, 1);
        break;
    case Qt::Key_Left:
        if (d->m_navigationMode == QQuickTreeView::Table)
            d->moveCurrentViewIndex(-1, 0);
        else
            collapseModelIndex(d->m_currentModelIndex);
        break;
    case Qt::Key_Right:
        if (d->m_navigationMode == QQuickTreeView::Table)
            d->moveCurrentViewIndex(1, 0);
        else
            expandModelIndex(d->m_currentModelIndex);
        break;
    case Qt::Key_Space:
        toggleModelIndexExpanded(d->m_currentModelIndex);
        break;
    default:
        break;
    }
}

void QQuickTreeView::mousePressEvent(QMouseEvent *e)
{
    QQuickTableView::mousePressEvent(e);

    d_func()->m_contentItemPosAtMousePress = contentItem()->position();
}

void QQuickTreeView::mouseReleaseEvent(QMouseEvent *e)
{
    QQuickTableView::mouseReleaseEvent(e);

    // Clicking on a tree transfers focus to it
    setFocus(true);

    if (contentItem()->position() != d_func()->m_contentItemPosAtMousePress) {
        // content item was flicked, which should cancel setting current index
        return;
    }

    const int column = columnAtX(e->pos().x(), true);
    const int row = rowAtY(e->pos().y(), true);
    if (column == -1 || row == -1)
        return;

    if (d_func()->m_navigationMode == Table)
        setCurrentModelIndex(mapToModel(viewIndex(column, row)));
    else
        setCurrentModelIndex(mapToModel(viewIndex(0, row)));
}

void QQuickTreeView::mouseDoubleClickEvent(QMouseEvent *e)
{
    QQuickTableView::mouseDoubleClickEvent(e);

    const int row = rowAtY(e->pos().y(), true);
    if (row == -1)
        return;

    toggleExpanded(row);
}

void QQuickTreeView::viewportMoved(Qt::Orientations orientation)
{
    QQuickTableView::viewportMoved(orientation);
    d_func()->checkForPropertyChanges();
}

QQuickTreeViewAttached *QQuickTreeView::qmlAttachedProperties(QObject *obj)
{
    return new QQuickTreeViewAttached(obj);
}

QQuickTreeView::NavigateMode QQuickTreeView::navigationMode() const
{
    return d_func()->m_navigationMode;
}

void QQuickTreeView::setNavigationMode(QQuickTreeView::NavigateMode navigateMode)
{
    Q_D(QQuickTreeView);
    if (d->m_navigationMode == navigateMode)
        return;

    d->m_navigationMode = navigateMode;
    emit navigationModeChanged();
}

QQuickTreeViewStyleHints *QQuickTreeView::styleHints()
{
    return &d_func()->m_styleHints;
}

bool QQuickTreeViewAttached::hasChildren() const
{
    return m_hasChildren;
}

void QQuickTreeViewAttached::setHasChildren(bool hasChildren)
{
    if (m_hasChildren == hasChildren)
        return;

    m_hasChildren = hasChildren;
    emit hasChildrenChanged();
}

bool QQuickTreeViewAttached::isExpanded() const
{
    return m_isExpanded;
}

void QQuickTreeViewAttached::setIsExpanded(bool isExpanded)
{
    if (m_isExpanded == isExpanded)
        return;

    m_isExpanded = isExpanded;
    emit isExpandedChanged();
}

int QQuickTreeViewAttached::depth()
{
    return m_depth;
}

void QQuickTreeViewAttached::setDepth(int depth)
{
    if (m_depth == depth)
        return;

    m_depth = depth;
    emit depthChanged();
}

QT_END_NAMESPACE