Kirigami-addons

HeaderDelegate.qml
1/*
2 * Copyright 2023 Evgeny Chesnokov <echesnokov@astralinux.ru>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6import QtQuick
7import QtQuick.Controls as QQC2
8import QtQuick.Layouts
9
10import org.kde.kirigami as Kirigami
11
12import org.kde.kirigamiaddons.tableview as Tables
13
14QQC2.Control {
15 id: delegate
16
17 Accessible.role: Accessible.ColumnHeader
18 padding: Kirigami.Units.smallSpacing
19
20 z: Drag.active ? 2 : 0
21 Drag.active: dragHandler.active && delegate.headerComponent.draggable
22 Drag.hotSpot.x: width / 2
23 Drag.hotSpot.y: height / 2
24
25 required property int index
26 required property var headerComponent
27
28 property bool sortEnabled
29 property int sortOrder
30
31 signal clicked()
32 signal doubleClicked()
33
34 background: Rectangle {
35 readonly property bool containsDrag: delegate.enabled &&
36 delegate.headerComponent.draggable &&
37 dropArea.containsDrag &&
38 dropArea.drag.source.index !== delegate.index
39
40 border.color: containsDrag ? Kirigami.Theme.highlightColor : "Transparent"
41 color: {
42 if (!delegate.enabled) {
43 return "Transparent"
44 }
45
46 if (delegate.hovered && !Kirigami.Settings.isMobile) {
47 return Qt.alpha(Kirigami.Theme.hoverColor, 0.3)
48 }
49
50 return Kirigami.Theme.backgroundColor
51 }
52
53 Kirigami.Separator {
54 height: parent.height
55 anchors.right: parent.right
56 visible: !parent.containsDrag && !delegate.Drag.active
57 }
58
59 Kirigami.Separator {
60 width: parent.width
61 anchors.bottom: parent.bottom
62 visible: !parent.containsDrag && !delegate.Drag.active
63 }
64 }
65
66 contentItem: DropArea {
67 id: dropArea
68
69 TapHandler {
70 onTapped: delegate.clicked()
71 onDoubleTapped: delegate.doubleClicked()
72 }
73
74 DragHandler {
75 id: dragHandler
76 target: delegate
77 enabled: delegate.headerComponent.draggable
78 yAxis.enabled: false
79 cursorShape: Qt.DragMoveCursor
80
81 // For restore initial position after drop
82 property real startX: -1
83
84 onActiveChanged: {
85 if (!active) {
86 delegate.Drag.drop();
87 delegate.x = startX;
88 startX = -1;
89 } else {
90 startX = target.x;
91 }
92 }
93 }
94
95 RowLayout {
96 anchors.fill: parent
97 spacing: delegate.spacing
98
99 Loader {
100 id: leadingLoader
101 Layout.alignment: Qt.AlignVCenter
102 visible: delegate.headerComponent.leading
103 sourceComponent: delegate.headerComponent.leading
104 }
105
106 Loader {
107 id: contentLoader
108 Layout.alignment: Qt.AlignVCenter
109 Layout.fillWidth: true
110 sourceComponent: delegate.headerComponent.headerDelegate
111 readonly property string modelData: delegate.headerComponent.title
112 readonly property int index: delegate.index
113 }
114
115 Loader {
116 id: sortIndicatorLoader
117 Layout.alignment: Qt.AlignVCenter
118 visible: delegate.sortEnabled
119 sourceComponent: delegate.sortEnabled ? delegate.sortIndicator : null
120 }
121 }
122
123 onDropped: function(drop) {
124 const currentItemIndex = delegate.index
125 const dropItemIndex = drop.source.index
126
127 if (!delegate.headerComponent.draggable)
128 return
129
130 if (currentItemIndex === -1 || dropItemIndex === -1)
131 return
132
133 if (currentItemIndex === dropItemIndex)
134 return
135
136 __columnModel.move(dropItemIndex, currentItemIndex, 1)
137 }
138 }
139
140 property Component sortIndicator: Kirigami.Icon {
141 id: sortIndicator
142
143 source: "arrow-up-symbolic"
144
145 implicitWidth: Kirigami.Units.iconSizes.small
146 implicitHeight: Kirigami.Units.iconSizes.small
147
148 states: State {
149 when: delegate.sortEnabled && delegate.sortOrder === Qt.DescendingOrder
150 PropertyChanges { target: sortIndicator; rotation: 180 }
151 }
152
153 transitions: Transition {
154 RotationAnimation {
155 duration: Kirigami.Units.longDuration
156 direction: RotationAnimation.Counterclockwise
157 }
158 }
159 }
160}
KIOWIDGETS_EXPORT DropJob * drop(const QDropEvent *dropEvent, const QUrl &destUrl, DropJobFlags dropjobFlags, JobFlags flags=DefaultFlags)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:05:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.