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 Kirigami.Theme.colorSet: Kirigami.Theme.Header
36 Kirigami.Theme.inherit: false
37
38 readonly property bool containsDrag: delegate.enabled &&
39 delegate.headerComponent.draggable &&
40 dropArea.containsDrag &&
41 dropArea.drag.source.index !== delegate.index
42
43 border.color: containsDrag ? Kirigami.Theme.highlightColor : "Transparent"
44 color: {
45 if (!delegate.enabled) {
46 return "Transparent"
47 }
48
49 if (delegate.hovered && !Kirigami.Settings.isMobile) {
50 return Qt.alpha(Kirigami.Theme.hoverColor, 0.3)
51 }
52
53 return Kirigami.Theme.backgroundColor
54 }
55
56 Kirigami.Separator {
57 height: parent.height
58 anchors.right: parent.right
59 visible: !parent.containsDrag && !delegate.Drag.active
60 }
61
62 Kirigami.Separator {
63 width: parent.width
64 anchors.bottom: parent.bottom
65 visible: !parent.containsDrag && !delegate.Drag.active
66 }
67 }
68
69 contentItem: DropArea {
70 id: dropArea
71
72 TapHandler {
73 onTapped: delegate.clicked()
74 onDoubleTapped: delegate.doubleClicked()
75 }
76
77 DragHandler {
78 id: dragHandler
79 target: delegate
80 enabled: delegate.headerComponent.draggable
81 yAxis.enabled: false
82 cursorShape: Qt.DragMoveCursor
83
84 // For restore initial position after drop
85 property real startX: -1
86
87 onActiveChanged: {
88 if (!active) {
89 delegate.Drag.drop();
90 delegate.x = startX;
91 startX = -1;
92 } else {
93 startX = target.x;
94 }
95 }
96 }
97
98 RowLayout {
99 anchors.fill: parent
100 spacing: delegate.spacing
101
102 Loader {
103 id: leadingLoader
104 Layout.alignment: Qt.AlignVCenter
105 visible: delegate.headerComponent.leading
106 sourceComponent: delegate.headerComponent.leading
107 }
108
109 Loader {
110 id: contentLoader
111 Layout.alignment: Qt.AlignVCenter
112 Layout.fillWidth: true
113 sourceComponent: delegate.headerComponent.headerDelegate
114 readonly property string modelData: delegate.headerComponent.title
115 readonly property int index: delegate.index
116 }
117
118 Loader {
119 id: sortIndicatorLoader
120 Layout.alignment: Qt.AlignVCenter
121 visible: delegate.sortEnabled
122 sourceComponent: delegate.sortEnabled ? delegate.sortIndicator : null
123 }
124 }
125
126 onDropped: function(drop) {
127 const currentItemIndex = delegate.index
128 const dropItemIndex = drop.source.index
129
130 if (!delegate.headerComponent.draggable)
131 return
132
133 if (currentItemIndex === -1 || dropItemIndex === -1)
134 return
135
136 if (currentItemIndex === dropItemIndex)
137 return
138
139 __columnModel.move(dropItemIndex, currentItemIndex, 1)
140 }
141 }
142
143 property Component sortIndicator: Kirigami.Icon {
144 id: sortIndicator
145
146 source: "arrow-up-symbolic"
147
148 implicitWidth: Kirigami.Units.iconSizes.small
149 implicitHeight: Kirigami.Units.iconSizes.small
150
151 states: State {
152 when: delegate.sortEnabled && delegate.sortOrder === Qt.DescendingOrder
153 PropertyChanges { target: sortIndicator; rotation: 180 }
154 }
155
156 transitions: Transition {
157 RotationAnimation {
158 duration: Kirigami.Units.longDuration
159 direction: RotationAnimation.Counterclockwise
160 }
161 }
162 }
163}
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 May 10 2024 11:50:14 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.