Changeset View
Changeset View
Standalone View
Standalone View
plasmoid/package/contents/ui/PrinterItem.qml
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2012-2013 Daniel Nicoletti <dantti12@gmail.com> | 2 | * Copyright 2012-2013 Daniel Nicoletti <dantti12@gmail.com> | ||
3 | * Copyright 2014-2015 Jan Grulich <jgrulich@redhat.com> | 3 | * Copyright 2014-2015 Jan Grulich <jgrulich@redhat.com> | ||
4 | * Copyright 2020 Nate Graham <nate@kde.org> | ||||
4 | * | 5 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU Library General Public License as | 7 | * it under the terms of the GNU Library General Public License as | ||
7 | * published by the Free Software Foundation; either version 2 or | 8 | * published by the Free Software Foundation; either version 2 or | ||
8 | * (at your option) any later version. | 9 | * (at your option) any later version. | ||
9 | * | 10 | * | ||
10 | * This program is distributed in the hope that it will be useful, | 11 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details | 14 | * GNU General Public License for more details | ||
14 | * | 15 | * | ||
15 | * You should have received a copy of the GNU Library General Public | 16 | * You should have received a copy of the GNU Library General Public | ||
16 | * License along with this program; if not, write to the | 17 | * License along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | 18 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | */ | 20 | */ | ||
20 | 21 | | |||
21 | import QtQuick 2.2 | 22 | import QtQuick 2.2 | ||
22 | import org.kde.kquickcontrolsaddons 2.0 | 23 | import QtQuick.Controls 2.9 | ||
23 | import org.kde.plasma.core 2.0 as PlasmaCore | 24 | | ||
24 | import org.kde.plasma.extras 2.0 as PlasmaExtras | 25 | import org.kde.plasma.extras 2.0 as PlasmaExtras | ||
25 | import org.kde.plasma.components 2.0 as PlasmaComponents | | |||
26 | import org.kde.kquickcontrolsaddons 2.0 as KQuickControlsAddons | | |||
27 | import org.kde.plasma.printmanager 0.2 as PrintManager | 26 | import org.kde.plasma.printmanager 0.2 as PrintManager | ||
28 | 27 | | |||
29 | Item { | 28 | PlasmaExtras.ExpandableListItem { | ||
30 | id: printerItem | 29 | readonly property bool isPaused: model.printerState === 5 | ||
31 | | ||||
32 | readonly property bool isPaused: printerState === 5 | | |||
33 | readonly property bool expanded: ListView.view.currentExpanded == index | | |||
34 | | ||||
35 | height: container.childrenRect.height + Math.round(units.gridUnit / 2) | | |||
36 | width: parent.width - Math.round(units.gridUnit / 2) | | |||
37 | | ||||
38 | MouseArea { | | |||
39 | id: container | | |||
40 | anchors.fill: parent | | |||
41 | | ||||
42 | hoverEnabled: true | | |||
43 | onEntered: { | | |||
44 | printerItem.ListView.view.currentIndex = index; | | |||
45 | | ||||
46 | //this is done to hide the highlight if the mouse moves out of the list view | | |||
47 | //and we are not mouseoverring anything | | |||
48 | if (printerItem.ListView.view.highlightItem) { | | |||
49 | printerItem.ListView.view.highlightItem.opacity = 1 | | |||
50 | } | | |||
51 | } | | |||
52 | onExited: { | | |||
53 | if (printerItem.ListView.view.highlightItem) { | | |||
54 | printerItem.ListView.view.highlightItem.opacity = 0 | | |||
55 | } | | |||
56 | } | | |||
57 | onClicked: { | | |||
58 | // the order is important, don't change since the last | | |||
59 | // line resets the value of expanded and makes the code hard to read | | |||
60 | // if it's not the last one | | |||
61 | jobsFilterModel.filteredPrinters = expanded ? "" : printerName | | |||
62 | printerItem.ListView.view.currentExpanded = expanded ? -1 : index; | | |||
63 | } | | |||
64 | | ||||
65 | KQuickControlsAddons.QIconItem { | | |||
66 | id: printerIcon | | |||
67 | | ||||
68 | anchors { | | |||
69 | left: parent.left | | |||
70 | leftMargin: units.smallSpacing | | |||
71 | verticalCenter: labelsColumn.verticalCenter | | |||
72 | } | | |||
73 | | ||||
74 | height: units.iconSizes.medium | | |||
75 | width: height | | |||
76 | icon: iconName | | |||
77 | Behavior on opacity { PropertyAnimation {} } | | |||
78 | } | | |||
79 | | ||||
80 | Column { | | |||
81 | id: labelsColumn | | |||
82 | | ||||
83 | anchors { | | |||
84 | top: parent.top | | |||
85 | left: printerIcon.right | | |||
86 | right: stateChangeButton.left | | |||
87 | topMargin: units.smallSpacing | | |||
88 | leftMargin: units.smallSpacing | | |||
89 | rightMargin: units.smallSpacing | | |||
90 | } | | |||
91 | | ||||
92 | PlasmaComponents.Label { | | |||
93 | id: printerNameLabel | | |||
94 | | ||||
95 | anchors { | | |||
96 | left: parent.left | | |||
97 | right: parent.right | | |||
98 | } | | |||
99 | | ||||
100 | height: paintedHeight | | |||
101 | elide: Text.ElideRight | | |||
102 | font.weight: isDefault ? Font.DemiBold : Font.Normal | | |||
103 | text: printerName | | |||
104 | } | | |||
105 | | ||||
106 | PlasmaComponents.Label { | | |||
107 | id: printerStatusLabel | | |||
108 | | ||||
109 | anchors { | | |||
110 | left: parent.left | | |||
111 | right: parent.right | | |||
112 | } | | |||
113 | | ||||
114 | height: paintedHeight | | |||
115 | elide: Text.ElideRight | | |||
116 | font.pointSize: theme.smallestFont.pointSize | | |||
117 | opacity: 0.6 | | |||
118 | text: stateMessage | | |||
119 | } | | |||
120 | } | | |||
121 | | ||||
122 | PlasmaComponents.ToolButton { | | |||
123 | id: stateChangeButton | | |||
124 | | ||||
125 | anchors { | | |||
126 | right: parent.right | | |||
127 | rightMargin: Math.round(units.gridUnit / 2) | | |||
128 | verticalCenter: printerIcon.verticalCenter | | |||
129 | } | | |||
130 | | ||||
131 | iconSource: isPaused ? "media-playback-start" : "media-playback-pause" | | |||
132 | tooltip: isPaused ? i18n("Resume printing") : i18n("Pause printing") | | |||
133 | opacity: container.containsMouse ? 1 : 0 | | |||
134 | visible: opacity != 0 | | |||
135 | | ||||
136 | onClicked: toggleSelection() | | |||
137 | } | | |||
138 | | ||||
139 | ListView { | | |||
140 | id: actionsList | | |||
141 | | ||||
142 | anchors { | | |||
143 | top: labelsColumn.bottom | | |||
144 | left: printerIcon.right | | |||
145 | right: stateChangeButton.left | | |||
146 | leftMargin: Math.round(units.gridUnit / 2) | | |||
147 | rightMargin: Math.round(units.gridUnit / 2) | | |||
148 | } | | |||
149 | interactive: false | | |||
150 | model: ListModel { | | |||
151 | Component.onCompleted: { | | |||
152 | append({"name":i18n("Configure printer"), "icon":"configure", "actionType":1}) | | |||
153 | append({"name":i18n("Open print queue"), "icon":"view-list-details", "actionType":2}) | | |||
154 | } | | |||
155 | } | | |||
156 | property int actionIconHeight: units.iconSizes.medium * 0.8 | | |||
157 | height: expanded ? ((actionIconHeight + Math.round(units.gridUnit / 2)) * 2) + Math.round(units.gridUnit / 4) : 0 | | |||
158 | opacity: expanded ? 1 : 0 | | |||
159 | highlight: PlasmaComponents.Highlight{} | | |||
160 | delegate: actionItem | | |||
161 | | ||||
162 | | ||||
163 | Behavior on opacity { NumberAnimation { duration: units.shortDuration * 3 } } | | |||
164 | | ||||
165 | Component.onCompleted: currentIndex = -1 | | |||
166 | 30 | | |||
167 | Component { | 31 | icon: model.iconName | ||
168 | id: actionItem | 32 | iconEmblem: isPaused ? "emblem-pause" : "" | ||
169 | 33 | title: model.printerName | |||
170 | Item { | 34 | subtitle: { | ||
171 | height: Math.max(actionIcon.height, actionText.height + jobsCountText.height) + Math.round(units.gridUnit / 3) | | |||
172 | width: actionsList.width | | |||
173 | | ||||
174 | PlasmaCore.IconItem { | | |||
175 | id: actionIcon | | |||
176 | | ||||
177 | source: icon | | |||
178 | height: actionsList.actionIconHeight | | |||
179 | width: actionsList.actionIconHeight | | |||
180 | | ||||
181 | anchors { | | |||
182 | left: parent.left | | |||
183 | leftMargin: 3 | | |||
184 | verticalCenter: actionLabels.verticalCenter | | |||
185 | } | | |||
186 | } | | |||
187 | | ||||
188 | Column { | | |||
189 | id: actionLabels | | |||
190 | | ||||
191 | anchors { | | |||
192 | left: actionIcon.right | | |||
193 | leftMargin: 5 | | |||
194 | right: parent.right | | |||
195 | rightMargin: 3 | | |||
196 | verticalCenter: parent.verticalCenter | | |||
197 | } | | |||
198 | | ||||
199 | PlasmaComponents.Label { | | |||
200 | id: actionText | | |||
201 | | ||||
202 | anchors { | | |||
203 | left: parent.left | | |||
204 | right: parent.right | | |||
205 | } | | |||
206 | | ||||
207 | height: paintedHeight | | |||
208 | verticalAlignment: Text.AlignVCenter | | |||
209 | wrapMode: Text.WordWrap | | |||
210 | text: name | | |||
211 | } | | |||
212 | | ||||
213 | PlasmaComponents.Label { | | |||
214 | id: jobsCountText | | |||
215 | | ||||
216 | anchors { | | |||
217 | left: parent.left | | |||
218 | right: parent.right | | |||
219 | } | | |||
220 | | ||||
221 | height: actionType == 2 ? paintedHeight : 0 | | |||
222 | elide: Text.ElideRight | | |||
223 | font.pointSize: theme.smallestFont.pointSize | | |||
224 | opacity: 0.6 | | |||
225 | text: getJobsLabel() | | |||
226 | } | | |||
227 | } | | |||
228 | | ||||
229 | MouseArea { | | |||
230 | anchors.fill: parent | | |||
231 | hoverEnabled: true | | |||
232 | | ||||
233 | onEntered: { | | |||
234 | actionsList.currentIndex = index; | | |||
235 | actionsList.highlightItem.opacity = 1; | | |||
236 | } | | |||
237 | onExited: { | | |||
238 | actionsList.highlightItem.opacity = 0; | | |||
239 | } | | |||
240 | onClicked: { | | |||
241 | // Configure printer | | |||
242 | if (actionType == 1) { | | |||
243 | processRunner.configurePrinter(printerName) | | |||
244 | // Open print queue | | |||
245 | } else { | | |||
246 | processRunner.openPrintQueue(printerName) | | |||
247 | } | | |||
248 | } | | |||
249 | } | | |||
250 | } | | |||
251 | } | | |||
252 | } | | |||
253 | } | | |||
254 | | ||||
255 | states: [ | | |||
256 | State { | | |||
257 | name: "NORMAL" | | |||
258 | when: !isPaused | | |||
259 | }, | | |||
260 | | ||||
261 | State { | | |||
262 | name: "PAUSED" | | |||
263 | when: isPaused | | |||
264 | PropertyChanges { target: printerNameLabel; opacity: 0.6 } | | |||
265 | PropertyChanges { target: printerIcon; opacity: 0.6 } | | |||
266 | } | | |||
267 | ] | | |||
268 | | ||||
269 | function getJobsLabel() { | | |||
270 | if (printmanager.jobsFilter == PrintManager.JobModel.WhichActive) { | 35 | if (printmanager.jobsFilter == PrintManager.JobModel.WhichActive) { | ||
271 | if (jobsFilterModel.count == 0) { | 36 | if (jobsFilterModel.count == 0) { | ||
272 | return i18n("No active jobs"); | 37 | return i18n("%1, no active jobs", model.stateMessage); | ||
273 | } else { | 38 | } else { | ||
274 | return i18np("One active job", "%1 active jobs", jobsFilterModel.count); | 39 | return i18np("%1, %2 active job", "%1, %2 active jobs", model.stateMessage, jobsFilterModel.count); | ||
275 | } | 40 | } | ||
276 | } else { | 41 | } else { | ||
277 | if (jobsFilterModel.count == 0) { | 42 | if (jobsFilterModel.count == 0) { | ||
278 | return i18n("No jobs"); | 43 | return i18n("%1, no jobs", model.stateMessage); | ||
279 | } else { | 44 | } else { | ||
280 | return i18np("One job", "%1 jobs", jobsFilterModel.count); | 45 | return i18np("%1, %2 job", "%1, %2 jobs", model.stateMessage, jobsFilterModel.count); | ||
281 | } | 46 | } | ||
282 | } | 47 | } | ||
283 | } | 48 | } | ||
284 | 49 | isDefault: model.isDefault | |||
285 | function toggleSelection() { | 50 | defaultActionButtonAction: Action { | ||
51 | icon.name: isPaused ? "media-playback-start" : "media-playback-pause" | ||||
52 | text: isPaused ? i18n("Resume printing") : i18n("Pause printing") | ||||
53 | onTriggered: { | ||||
286 | if (isPaused) { | 54 | if (isPaused) { | ||
287 | printersModel.resumePrinter(printerName) | 55 | printersModel.resumePrinter(model.printerName); | ||
288 | } else { | 56 | } else { | ||
289 | printersModel.pausePrinter(printerName) | 57 | printersModel.pausePrinter(model.printerName); | ||
58 | } | ||||
290 | } | 59 | } | ||
291 | } | 60 | } | ||
61 | contextualActionsModel: [ | ||||
62 | Action { | ||||
63 | icon.name: "configure" | ||||
64 | text: i18n("Configure printer...") | ||||
65 | onTriggered: processRunner.configurePrinter(model.printerName); | ||||
66 | }, | ||||
67 | Action { | ||||
68 | icon.name: "view-list-details" | ||||
69 | text: i18n("View print queue") | ||||
70 | onTriggered: processRunner.openPrintQueue(printerName); | ||||
71 | } | ||||
72 | ] | ||||
292 | } | 73 | } |