Debian Codegraph @

src/qml/Profiles.qml

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 import QtQuick 2.15
5 import QtQuick.Controls 2.15
6 import QtQuick.Layouts 1.15
7 import CoreCtrl.UIComponents 1.0
8 import "Style.js" as Style
9
10 Page {
11 hoverEnabled: Style.g_hover
12
13 PROFILE_MANAGER {
14 id: profileManager
15 objectName: "PROFILE_MANAGER"
16
17 onInitProfiles: profiles => p.initProfileButtons(profiles)
18 onProfileAdded: (name, exe, icon, hasCustomIcon, isActive) => p.createProfileButton(name, exe, icon, hasCustomIcon, isActive)
19 onProfileRemoved: name => p.removeProfileButton(name)
20 onProfileSaved: name => p.updateToolBarButtonsOnProfileSaved()
21 onProfileInfoChanged: (oldName, newName, exe, icon, hasCustomIcon, isActive) => p.updateProfileButton(oldName, newName, exe, icon, hasCustomIcon, isActive)
22 onProfileChanged: name => p.updateProfileUIState(name)
23 onProfileActiveChanged: (name, active) => p.updateProfileButtonActiveState(name, active);
24 onManualProfileToggled: (name, active) => p.updateProfileButtonManualToggle(name, active);
25 }
26
27 QtObject { // private stuff
28 id: p
29
30 property bool restoringProfile: false
31 property bool unappliedSettings: false
32 property bool unsavedSettings: false
33 property var editedProfileBtn: undefined
34
35 function initProfileButtons(profiles) {
36 for (var i = 0; i < profiles.length; i+=5) {
37
38 var name = profiles[i]
39 var exe = profiles[i+1]
40 var icon = profiles[i+2]
41 var isActive = profiles[i+3]
42 var hasCustomIcon = profiles[i+4]
43 var isGlobal = i === 0
44
45 profileModel.append({ "_label": isGlobal ? qsTranslate("ProfileManagerUI", name) : name,
46 "_name": name,
47 "_exe": exe,
48 "_icon": icon,
49 "_isGlobal": isGlobal,
50 "_hasCustomIcon": hasCustomIcon,
51 "_active": isActive,
52 "_toggledManual": false })
53 }
54 }
55
56 function createProfileButton(name, exe, icon, hasCustomIcon, isActive) {
57 profileModel.append({ "_label": name,
58 "_name": name,
59 "_exe": exe,
60 "_icon": icon,
61 "_isGlobal": false,
62 "_hasCustomIcon": hasCustomIcon,
63 "_active": isActive,
64 "_toggledManual": false })
65
66 // move model element to the correct position
67 for (var i = 1; i < profileModel.count; ++i) {
68 if (name < profileModel.get(i)._name) {
69 profileModel.move(profileModel.count - 1, i, 1)
70 break
71 }
72 }
73 }
74
75 function removeProfileButton(name) {
76 for (var i = 0; i < profileModel.count; ++i) {
77 if (profileModel.get(i)._name === name) {
78 profileModel.remove(i)
79 break
80 }
81 }
82 }
83
84 function updateProfileButton(oldName, newName, exe, icon, hasCustomIcon, isActive) {
85 // get model element old and new positions
86 var from = -1
87 var to = profileModel.count
88 for (var i = 1; i < profileModel.count; ++i) {
89 var itemName = profileModel.get(i)._name
90 if (from < 0 && oldName === itemName) {
91 from = i
92 if (to < profileModel.count)
93 break
94 }
95 if (to === profileModel.count && newName < itemName) {
96 to = i
97 if (from > 0)
98 break
99 }
100 }
101
102 if (from > 0) { // model element found
103 to = from < to ? to - 1 : to
104 profileModel.move(from, to, 1)
105
106 // update model element
107 var toggledManual = profileModel.get(to)._toggledManual
108 profileModel.set(to, {"_label": newName,
109 "_name": newName,
110 "_exe": exe,
111 "_icon": icon,
112 "_hasCustomIcon": hasCustomIcon,
113 "_active": isActive,
114 "_toggledManual": exe.length > 0 ? false : toggledManual })
115
116 refreshProfileButtonsIcon(icon)
117 }
118 }
119
120 function updateProfileButtonActiveState(name, active) {
121 for (var i = 1; i < profileModel.count; ++i) {
122 if (profileModel.get(i)._name === name &&
123 profileModel.get(i)._active !== active) {
124 profileModel.set(i, {"_active": active})
125 break
126 }
127 }
128 }
129
130 function updateProfileButtonManualToggle(name, active) {
131 for (var i = 1; i < profileModel.count; ++i) {
132 if (profileModel.get(i)._name === name) {
133 profileModel.set(i, {"_toggledManual": active})
134 break
135 }
136 }
137 }
138
139 function refreshProfileButtonsIcon(icon) {
140 for (var i = 1; i < profileModel.count; ++i) {
141 if (profileModel.get(i)._icon === icon) {
142 profileModel.set(i, {"_icon": ""}) // force icon update
143 profileModel.set(i, {"_icon": icon})
144 }
145 }
146 }
147
148 function updateToolBarProfileInfo(name, icon, isActive) {
149 tbLabel.text = isActive ? name : name + " (" + qsTr("Disabled") + ")"
150 tbLabel.color = isActive ? Style.ToolBar.text_color : Style.ToolBar.text_color_alt
151 tbIcon.source = icon
152 }
153
154 function updateToolBarButtonsOnSettingsChanged() {
155 unappliedSettings = true
156 unsavedSettings = false
157 }
158
159 function updateToolBarButtonsOnProfileChanged() {
160 unappliedSettings = false
161 unsavedSettings = true
162 }
163
164 function updateToolBarButtonsOnProfileSaved() {
165 unsavedSettings = false
166 }
167
168 function updateProfileUIState(name) {
169 if (editedProfileBtn !== undefined) {
170 var active = profileManager.isProfileActive(name)
171 updateToolBarProfileInfo(editedProfileBtn.label, tbIcon.source, active)
172
173 if (restoringProfile) {
174 restoringProfile = false
175 updateToolBarButtonsOnProfileSaved()
176 }
177 else {
178 updateToolBarButtonsOnProfileChanged()
179 }
180 }
181 }
182
183 function goToProfileView() {
184 if (unappliedSettings)
185 applyProfileSettingsDlg.open()
186 else
187 exitProfileSettings()
188 }
189
190 function exitProfileSettings() {
191 stack.pop()
192 sysModelView.visible = false
193
194 unappliedSettings = false
195 unsavedSettings = false
196 editedProfileBtn = undefined
197 }
198
199 function goToProfileSettingsView() {
200 sysModelView.visible = true
201 stack.push(sysModelView)
202 }
203 }
204
205 ListModel {
206 id: profileModel
207 }
208
209 Component {
210 id: profileModelDelegate
211
212 ProfileButton {
213 id: pBtn
214
215 label: _label
216 name: _name
217 icon: _icon
218 exe: _exe
219 isGlobal: _isGlobal
220 profileActivated: _active
221 toggledManual: _toggledManual
222
223 onClicked: {
224 p.editedProfileBtn = pBtn
225 p.updateToolBarProfileInfo(label, icon, profileActivated)
226 profileManager.loadSettings(name)
227 p.goToProfileSettingsView()
228
229 if (profileManager.isProfileUnsaved(name))
230 p.unsavedSettings = true
231 }
232
233 onEnableProfile: enable => profileManager.activate(name, enable)
234
235 onEdit: {
236 p.editedProfileBtn = pBtn
237
238 infoDlg.title = qsTr("Profile properties")
239 infoDlg.name = name
240 infoDlg.exe = exe
241 infoDlg.icon = icon
242 infoDlg.hasCustomIcon = _hasCustomIcon
243 infoDlg.newInfoAction = function(name, exe, icon) {
244 p.editedProfileBtn = undefined
245 if (pBtn.name !== name || pBtn.exe !== exe || pBtn.icon !== icon)
246 profileManager.updateInfo(pBtn.name, name, exe, icon)
247 }
248 infoDlg.open()
249 }
250
251 onClone: {
252 infoDlg.title = qsTr("New profile properties")
253 infoDlg.name = ""
254 infoDlg.exe = ""
255 infoDlg.icon = isGlobal ? infoDlg.defaultIcon : icon
256 infoDlg.hasCustomIcon = _hasCustomIcon
257 infoDlg.forceAutomaticActivation = exe.length > 0
258 infoDlg.newInfoAction = function(name, exe, icon) {
259 profileManager.add(name, exe, icon, pBtn.name)
260 }
261 infoDlg.open()
262 }
263
264 onExportTo: {
265 exportToDialog.profileName = name
266 exportToDialog.open()
267 }
268
269 onRemove: {
270 removeWarningDlg.profileBtn = pBtn
271 removeWarningDlg.profileName = pBtn.name
272 removeWarningDlg.open()
273 }
274
275 onToggleManualProfile: name => profileManager.toggleManualProfile(name)
276 }
277 }
278
279 NativeFileDialog {
280 id: exportToDialog
281 title: qsTr("Export profile to...")
282 nameFilters: [qsTr("CoreCtrl profile") + " (ccpro)(*.ccpro)"]
283 defaultSuffix: "ccpro"
284 saveDlg: true
285
286 property string profileName: ""
287
288 onAccepted: {
289 if(!profileManager.exportProfile(profileName, file)) {
290 errorDlg.textDlg = qsTr("Cannot export profile.\nCheck the permissions of the destination file and directory.")
291 errorDlg.open()
292 }
293 }
294 }
295
296 ProfileInfoDialog {
297 id: infoDlg
298 defaultIcon: profileManager.defaultIcon()
299
300 function openNewProfileDialog() {
301 title = qsTr("New profile properties")
302 name = ""
303 exe = ""
304 icon = defaultIcon
305 hasCustomIcon = false
306 newInfoAction = function(name, exe, icon) {
307 profileManager.add(name, exe, icon, "defaultProfile")
308 }
309 open()
310 }
311
312 onUpdateProfileNameUsed: profileName => {
313 if (p.editedProfileBtn !== undefined && profileName === p.editedProfileBtn.name)
314 profileNameUsed = false
315 else
316 profileNameUsed = profileManager.isProfileNameInUse(profileName)
317 }
318 onUpdateExecutableNameUsed: executableName => {
319 if (p.editedProfileBtn !== undefined && executableName === p.editedProfileBtn.exe)
320 executableNameUsed = false
321 else
322 executableNameUsed = profileManager.isExecutableNameInUse(executableName)
323 }
324 }
325
326 Dialog {
327 id: errorDlg
328
329 property alias textDlg: errorDlgLbl.text
330
331 title: qsTr("Error")
332
333 focus: true
334 modal: true
335 standardButtons: Dialog.Ok
336 closePolicy: Popup.CloseOnEscape
337
338 // center in parent
339 x: (parent.width - width) / 2
340 y: (parent.height - height) / 2
341
342 Label { id: errorDlgLbl }
343
344 Component.onCompleted: {
345 footer.standardButton(Dialog.Ok).hoverEnabled = Style.g_hover
346 }
347 }
348
349 Dialog {
350 id: removeWarningDlg
351
352 title: qsTr("Warning")
353
354 property var profileName
355 property var profileBtn
356
357 focus: true
358 modal: true
359 standardButtons: Dialog.Yes | Dialog.No
360 closePolicy: Popup.CloseOnEscape
361
362 // center in parent
363 x: (parent.width - width) / 2
364 y: (parent.height - height) / 2
365
366 Label {
367 text: qsTr("This action is permantent.\nDo you really want to remove %1?")
368 .arg(removeWarningDlg.profileName)
369 }
370
371 onAccepted: profileManager.remove(profileName)
372
373 Component.onCompleted: {
374 footer.standardButton(Dialog.Yes).hoverEnabled = Style.g_hover
375 footer.standardButton(Dialog.No).hoverEnabled = Style.g_hover
376 }
377 }
378
379 Dialog {
380 id: applyProfileSettingsDlg
381
382 title: qsTr("Warning")
383
384 focus: true
385 modal: true
386 standardButtons: Dialog.Ok | Dialog.Cancel
387 closePolicy: Popup.CloseOnEscape
388
389 // center in parent
390 x: (parent.width - width) / 2
391 y: (parent.height - height) / 2
392
393 Label {
394 text: qsTr("Unapplied settings will be lost.\nDo you want to apply them now?")
395 }
396
397 onAccepted: {
398 profileManager.applySettings(p.editedProfileBtn.name)
399 p.exitProfileSettings()
400 close()
401 }
402
403 onRejected: {
404 p.exitProfileSettings()
405 close()
406 }
407
408 Component.onCompleted: {
409 footer.standardButton(Dialog.Ok).hoverEnabled = Style.g_hover
410 footer.standardButton(Dialog.Cancel).hoverEnabled = Style.g_hover
411 }
412 }
413
414 Dialog {
415 id: resetProfileSettingsDlg
416
417 title: qsTr("Warning")
418
419 focus: true
420 modal: true
421 standardButtons: Dialog.Yes | Dialog.No
422 closePolicy: Popup.CloseOnEscape
423
424 // center in parent
425 x: (parent.width - width) / 2
426 y: (parent.height - height) / 2
427
428 Label {
429 text: (p.unsavedSettings ? qsTr("Unsaved settings will be lost.\n") : "") +
430 qsTr("Do you want to load the default settings?")
431 }
432
433 onAccepted: {
434 profileManager.resetSettings(p.editedProfileBtn.name)
435 }
436
437 Component.onCompleted: {
438 footer.standardButton(Dialog.Yes).hoverEnabled = Style.g_hover
439 footer.standardButton(Dialog.No).hoverEnabled = Style.g_hover
440 }
441 }
442
443 Dialog {
444 id: restoreProfileSettingsDlg
445
446 title: qsTr("Warning")
447
448 focus: true
449 modal: true
450 standardButtons: Dialog.Yes | Dialog.No
451 closePolicy: Popup.CloseOnEscape
452
453 // center in parent
454 x: (parent.width - width) / 2
455 y: (parent.height - height) / 2
456
457 Label {
458 text: (p.unsavedSettings ? qsTr("Current settings will be discarded.\n") : "") +
459 qsTr("Do you want to load the saved settings?")
460 }
461
462 onAccepted: {
463 p.restoringProfile = true
464 profileManager.restoreSettings(p.editedProfileBtn.name)
465 }
466
467 Component.onCompleted: {
468 footer.standardButton(Dialog.Yes).hoverEnabled = Style.g_hover
469 footer.standardButton(Dialog.No).hoverEnabled = Style.g_hover
470 }
471 }
472
473 header: ToolBar {
474 RowLayout {
475 anchors.fill: parent
476
477 Label {
478 text: qsTr("Manage profiles for your applications...")
479 leftPadding: 10
480 color: Style.ToolBar.text_color_msg
481 visible: stack.depth === 1
482 }
483
484 ToolButton {
485 text: Style.g_icon.BACK
486 font.pointSize: Style.g_text.icon_size_tabbar
487 implicitWidth: 48
488 visible: stack.depth > 1
489 onClicked: p.goToProfileView()
490 }
491
492 Item {
493 implicitWidth: tbIcon.width
494 implicitHeight: tbIcon.height
495
496 Image {
497 id: tbIcon
498 anchors.fill: parent
499 fillMode: Image.PreserveAspectFit
500 width: Style.g_icon.size
501 height: Style.g_icon.size
502 sourceSize.width: Style.g_icon.source_size
503 visible: stack.depth > 1
504 }
505 }
506
507 Label {
508 id: tbLabel
509 leftPadding: 5
510 font.pointSize: Style.g_text.size + 2
511 elide: Text.ElideRight
512 Layout.maximumWidth: 300
513 visible: stack.depth > 1
514 }
515
516 Rectangle {
517 Layout.fillWidth: true
518 }
519
520 ToolButton {
521 text: Style.g_icon.ADD
522 font.pointSize: Style.g_text.icon_size_tabbar
523 implicitWidth: 48
524 visible: stack.depth === 1
525 onClicked: infoDlg.openNewProfileDialog()
526 }
527
528 ToolButton {
529 text: qsTr("Save")
530 visible: stack.depth > 1 && p.unsavedSettings
531 onClicked: profileManager.saveSettings(p.editedProfileBtn.name)
532 }
533
534 ToolButton {
535 text: qsTr("Apply")
536 visible: stack.depth > 1 && p.unappliedSettings
537 onClicked: profileManager.applySettings(p.editedProfileBtn.name)
538 }
539
540 ToolButton {
541 text: qsTr("Restore")
542 visible: stack.depth > 1 && p.unsavedSettings
543 onClicked: restoreProfileSettingsDlg.open()
544 }
545
546 Label {
547 text: Style.g_icon.SPLIT
548 font.pointSize: Style.g_text.icon_size
549 visible: stack.depth > 1
550 }
551
552 ToolButton {
553 text: qsTr("Load from...")
554 visible: stack.depth > 1
555
556 NativeFileDialog {
557 id: importFDialog
558 title: qsTr("Load settings from...")
559 nameFilters: [qsTr("CoreCtrl profile") + " (ccpro)(*.ccpro)"]
560 defaultSuffix: "ccpro"
561
562 onAccepted: {
563 if (!profileManager.loadSettings(p.editedProfileBtn.name, file)) {
564 errorDlg.textDlg = qsTr("Cannot load profile.\nInvalid or corrupted file.")
565 errorDlg.open()
566 }
567 }
568 }
569 onClicked: importFDialog.open()
570 }
571
572 ToolButton {
573 text: qsTr("Reset")
574 visible: stack.depth > 1
575 onClicked: resetProfileSettingsDlg.open()
576 }
577 }
578 }
579
580 StackView {
581 id: stack
582 initialItem: profilesView
583 anchors.fill: parent
584 clip: true
585
586 Item {
587 id: sysModelView
588 objectName: "PROFILE_SYS_MODEL_Plug"
589 visible: false
590
591 onChildrenChanged: {
592 for (var i = 0; i < children.length; ++i) {
593 children[i].settingsChanged.disconnect(p.updateToolBarButtonsOnSettingsChanged)
594 children[i].settingsChanged.connect(p.updateToolBarButtonsOnSettingsChanged)
595 }
596 }
597 }
598
599 Pane {
600 id: profilesView
601 padding: 2
602
603 ScrollView {
604 id: scrollview
605 anchors.fill: parent
606 clip: true
607 ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
608 ScrollBar.vertical.policy: ScrollBar.AlwaysOn
609 ScrollBar.horizontal.visible: ScrollBar.horizontal.size < 1
610 ScrollBar.vertical.visible: ScrollBar.vertical.size < 1
611
612 Flow {
613 width: scrollview.availableWidth
614 height: scrollview.availableHeight
615 spacing: Style.RectItemList.spacing
616
617 Repeater {
618 model: profileModel
619 delegate: profileModelDelegate
620 }
621
622 move: Transition {
623 NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad }
624 }
625 }
626 }
627 }
628
629 pushEnter: Transition {
630 PropertyAnimation {
631 property: "opacity"
632 from: 0
633 to: 1
634 duration: Style.Profiles.opacity_anim_duration
635 }
636 }
637 pushExit: Transition {
638 PropertyAnimation {
639 property: "opacity"
640 from: 1
641 to: 0
642 duration: Style.Profiles.opacity_anim_duration
643 }
644 }
645 popEnter: Transition {
646 PropertyAnimation {
647 property: "opacity"
648 from: 0
649 to: 1
650 duration: Style.Profiles.opacity_anim_duration
651 }
652 }
653 popExit: Transition {
654 PropertyAnimation {
655 property: "opacity"
656 from: 1
657 to: 0
658 duration: Style.Profiles.opacity_anim_duration
659 }
660 }
661 }
662 }