Kstars

KSListView.qml
1// SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4import QtQuick 2.6
5import QtQuick.Layouts 1.1
6import QtQuick.Controls 2.0
7import "../constants/" 1.0
8
9ListView {
10 id: listView
11 clip: true
12 property bool checkCurrent: false
13 property bool checkable: false
14 property bool onClickCheck: true //true if the item should be be mapped with a blue rectangle when clicked
15
16 property int maxWidth: 0
17 property bool modelIsChanged: false
18 implicitWidth: maxWidth
19 //To skip the case when contentItem.height equals to 99000+
20 implicitHeight: contentItem.height >= window.height ? window.height : contentItem.height
21
22 //For some reason we can't use num constant inside states so we wrap sysPalette as property
23 property SystemPalette sysPalette: Num.sysPalette
24
25 onCountChanged: {
26 for(var child in listView.contentItem.children) {
27 var childWidth = listView.contentItem.children[child].textWidth
28 if(childWidth == undefined) childWidth = 0
29 maxWidth = maxWidth > childWidth ? maxWidth : childWidth
30 }
31 }
32
33 ScrollIndicator.vertical: ScrollIndicator { }
34 property string textRole: ""
35
36 signal clicked(var text, var index, var checked)
37 property bool modelIsArray: false
38
39 onModelChanged: {
40 modelIsArray = !model ? model.constructor === Array : false
41 }
42
43 delegate: Rectangle {
44 id: delegateRect
45 width: parent.width
46 height: objName.contentHeight + 30
47 property int textWidth: objRow.width + objRow.anchors.leftMargin*2
48 property bool checked: false
49 property string visibleText: objName.text
50 color: sysPalette.base
51
52 border {
53 color: Num.sysPalette.light//"#becad5"
54 width: 1
55 }
56
57 Behavior on color {
58 ColorAnimation { duration: 200 }
59 }
60
61 states: [
62 State {
63 name: "hovered"
64 PropertyChanges {
65 target: delegateRect
66 color: sysPalette.highlight //"#d0e8fa"
67 }
68 PropertyChanges {
69 target: objName
70 color: sysPalette.highlightedText //"#31363b"
71 }
72 },
73 State {
74 name: "selected"
75 PropertyChanges {
76 target: delegateRect
77 color: sysPalette.button//"#2196F3"
78 }
79 PropertyChanges {
80 target: objName
81 color: sysPalette.buttonText//"#eff0fa"
82 }
83 },
84 State {
85 name: "default"
86 PropertyChanges {
87 target: delegateRect
88 color: sysPalette.base//"white"
89 }
90 PropertyChanges {
91 target: objName
92 color: sysPalette.text//"black"
93 }
94 }
95 ]
96
97 MouseArea {
98 id: delMouseArea
99 anchors.fill: parent
100 hoverEnabled: true
101
102 function hoveredColor() {
103 if(pressed) {
104 delegateRect.state = "selected"
105 } else {
106 if(containsMouse) {
107 delegateRect.state = "hovered"
108 } else {
109 delegateRect.state = "default"
110 }
111 }
112 }
113
114 onContainsMouseChanged: {
115 hoveredColor()
116 }
117
118 onPressedChanged: {
119 hoveredColor()
120 }
121
122 onClicked: {
123 if(onClickCheck) listView.currentIndex = model.index
124 if(checkable) delegateRect.checked = !delegateRect.checked
125 listView.clicked(objName.text, model.index, delegateRect.checked)
126 }
127 }
128
129 RowLayout {
130 id: objRow
131
132 anchors {
133 verticalCenter: parent.verticalCenter
134 left: parent.left
135 leftMargin: 20
136 }
137
138 Rectangle {
139 visible: (checkCurrent && listView.currentIndex == model.index) || (checkable && delegateRect.checked)
140 color: objName.color //"#2173f3"
141 width: height
142 height: objName.font.pixelSize/2
143 radius: width * 0.5
144 }
145
146 KSText {
147 id: objName
148
149 text: textRole == "" ? modelData : listView.modelIsArray ? modelData[textRole] : model[textRole]
150 wrapMode: Text.WrapAnywhere
151
152 Behavior on color {
153 ColorAnimation { duration: 200 }
154 }
155 }
156 }
157 }
158}
KGUIADDONS_EXPORT QWindow * window(QObject *job)
QString name(StandardAction id)
QTextStream & left(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.