Differential D28487 Diff 79120 applets/systemmonitor/systemmonitor/package/contents/ui/config/UsedSensorsView.qml
Changeset View
Changeset View
Standalone View
Standalone View
applets/systemmonitor/systemmonitor/package/contents/ui/config/UsedSensorsView.qml
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 Marco Martin <mart@kde.org> | ||||
3 | * Copyright 2019 David Edmundson <davidedmundson@kde.org> | ||||
4 | * Copyright 2019 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
5 | * | ||||
6 | * This program is free software; you can redistribute it and/or modify | ||||
7 | * it under the terms of the GNU Library General Public License as | ||||
8 | * published by the Free Software Foundation; either version 2, or | ||||
9 | * (at your option) any later version. | ||||
10 | * | ||||
11 | * This program is distributed in the hope that it will be useful, | ||||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
14 | * GNU General Public License for more details | ||||
15 | * | ||||
16 | * You should have received a copy of the GNU Library General Public | ||||
17 | * License along with this program; if not, write to the | ||||
18 | * Free Software Foundation, Inc., | ||||
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
20 | */ | ||||
21 | | ||||
22 | import QtQuick 2.9 | ||||
23 | import QtQuick.Layouts 1.2 | ||||
24 | import QtQuick.Controls 2.2 as Controls | ||||
25 | import QtQml.Models 2.12 | ||||
26 | | ||||
27 | import org.kde.kirigami 2.5 as Kirigami | ||||
28 | import org.kde.kquickcontrols 2.0 | ||||
29 | | ||||
30 | import org.kde.ksysguard.sensors 1.0 as Sensors | ||||
31 | | ||||
32 | DropArea { | ||||
33 | id: root | ||||
34 | Layout.fillWidth: true | ||||
35 | Layout.fillHeight: true | ||||
36 | Layout.preferredWidth: usedSensorsScroll.implicitWidth | ||||
37 | | ||||
38 | property alias count: usedSensorsView.count | ||||
39 | property alias delegateComponent: delegateComponent | ||||
40 | property var sensorIds: [] | ||||
41 | property var sensorColors: [] | ||||
42 | property bool showColor: true | ||||
43 | | ||||
44 | function appendSensor(sensorId) { | ||||
45 | insertSensor(usedSensorsModel.count, sensorId); | ||||
46 | } | ||||
47 | | ||||
48 | function insertSensor(index, sensorId) { | ||||
49 | var color = Kirigami.Theme.highlightColor; | ||||
50 | color.hsvHue = Math.random(); | ||||
51 | usedSensorsModel.insert(index, {"sensorId": sensorId, "color": color.toString()}) | ||||
52 | usedSensorsModel.save() | ||||
53 | } | ||||
54 | | ||||
55 | function positionViewAtIndex(index, mode) { | ||||
56 | usedSensorsView.positionViewAtIndex(index, mode); | ||||
57 | } | ||||
58 | | ||||
59 | function load() { | ||||
60 | for (var i in sensorIds) { | ||||
61 | usedSensorsModel.append({"sensorId": sensorIds[i], "color": (sensorColors[i] || "").toString()}) | ||||
62 | } | ||||
63 | } | ||||
64 | Component { | ||||
65 | id: delegateComponent | ||||
66 | Kirigami.SwipeListItem { | ||||
67 | id: listItem | ||||
68 | actions: Kirigami.Action { | ||||
69 | icon.name: "list-remove" | ||||
70 | text: i18n("Remove") | ||||
71 | onTriggered: { | ||||
72 | usedSensorsModel.remove(index, 1); | ||||
73 | usedSensorsModel.save(); | ||||
74 | } | ||||
75 | } | ||||
76 | contentItem: RowLayout { | ||||
77 | Kirigami.ListItemDragHandle { | ||||
78 | listItem: listItem | ||||
79 | listView: usedSensorsView | ||||
80 | onMoveRequested: { | ||||
81 | usedSensorsModel.move(oldIndex, newIndex, 1) | ||||
82 | usedSensorsModel.save(); | ||||
83 | } | ||||
84 | } | ||||
85 | ColorButton { | ||||
86 | id: textColorButton | ||||
87 | color: model.color | ||||
88 | visible: root.showColor | ||||
89 | showAlphaChannel: true | ||||
90 | onAccepted: { | ||||
91 | usedSensorsModel.setProperty(index, "color", color.toString()); | ||||
92 | usedSensorsModel.save(); | ||||
93 | } | ||||
94 | } | ||||
95 | Controls.Label { | ||||
96 | Layout.fillWidth: true | ||||
97 | text: sensor.name | ||||
98 | Sensors.Sensor { | ||||
99 | id: sensor | ||||
100 | sensorId: model.sensorId | ||||
101 | } | ||||
102 | } | ||||
103 | } | ||||
104 | } | ||||
105 | } | ||||
106 | | ||||
107 | onEntered: { | ||||
108 | if (drag.formats.indexOf("application/x-ksysguard") == -1) { | ||||
109 | drag.accepted = false; | ||||
110 | return; | ||||
111 | } | ||||
112 | } | ||||
113 | function dropIndex() { | ||||
114 | if (!containsDrag) { | ||||
115 | return -1; | ||||
116 | } else if (usedSensorsView.count == 0) { | ||||
117 | return 0; | ||||
118 | } | ||||
119 | | ||||
120 | var itemHeight = usedSensorsView.contentItem.children[0].height; | ||||
121 | var contentY = usedSensorsView.contentY + drag.y; | ||||
122 | | ||||
123 | return Math.max(0, Math.min(usedSensorsView.count, Math.round(contentY / itemHeight))); | ||||
124 | } | ||||
125 | | ||||
126 | onDropped: insertSensor(dropIndex(), drop.getDataAsString("application/x-ksysguard")) | ||||
127 | | ||||
128 | Controls.Label { | ||||
129 | anchors.centerIn: parent | ||||
130 | z: 2 | ||||
131 | visible: usedSensorsView.count == 0 | ||||
132 | text: i18n("Drop Sensors Here") | ||||
133 | } | ||||
134 | Rectangle { | ||||
135 | anchors { | ||||
136 | left: parent.left | ||||
137 | right: parent.right | ||||
138 | } | ||||
139 | height: Kirigami.Units.smallSpacing | ||||
140 | color: Kirigami.Theme.highlightColor | ||||
141 | visible: parent.containsDrag | ||||
142 | y: { | ||||
143 | if (usedSensorsView.count == 0) { | ||||
144 | return 0; | ||||
145 | } | ||||
146 | var itemHeight = usedSensorsView.contentItem.children[0].height; | ||||
147 | return itemHeight * parent.dropIndex() - usedSensorsView.contentY; | ||||
148 | } | ||||
149 | opacity: 0.6 | ||||
150 | z: 2 | ||||
151 | } | ||||
152 | Controls.ScrollView { | ||||
153 | id: usedSensorsScroll | ||||
154 | anchors.fill: parent | ||||
155 | | ||||
156 | ListView { | ||||
157 | id: usedSensorsView | ||||
158 | | ||||
159 | model: ListModel { | ||||
160 | id: usedSensorsModel | ||||
161 | function save() { | ||||
162 | var ids = []; | ||||
163 | var colors = []; | ||||
164 | for (var i = 0; i < count; ++i) { | ||||
165 | ids.push(get(i).sensorId); | ||||
166 | colors.push(get(i).color); | ||||
167 | } | ||||
168 | root.sensorIds = ids; | ||||
169 | root.sensorColors = colors; | ||||
170 | } | ||||
171 | } | ||||
172 | //NOTE: this row is necessary to make the drag handle work | ||||
173 | delegate: Kirigami.DelegateRecycler { | ||||
174 | width: usedSensorsView.width | ||||
175 | sourceComponent: delegateComponent | ||||
176 | } | ||||
177 | } | ||||
178 | Component.onCompleted: background.visible = true; | ||||
179 | Controls.ScrollBar.horizontal.visible: false | ||||
180 | } | ||||
181 | } | ||||
182 | |