Changeset View
Standalone View
kcmkwin/kwinrules/package/contents/ui/OptionsComboBox.qml
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2020 Ismael Asensio <isma.af@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | import QtQuick 2.12 | ||||
22 | import QtQuick.Layouts 1.12 | ||||
23 | import QtQuick.Controls 2.12 as QQC2 | ||||
24 | | ||||
25 | import org.kde.kirigami 2.10 as Kirigami | ||||
26 | | ||||
27 | | ||||
28 | QQC2.ComboBox { | ||||
29 | id: optionsCombo | ||||
30 | | ||||
31 | textRole: "text" | ||||
32 | //FIXME: After Qt 5.14 this can be replaced by the new implemented properties: | ||||
33 | // valueRole: "value"` | ||||
34 | // currentValue: model.value | ||||
35 | property var currentValue | ||||
36 | property var values: [] | ||||
37 | | ||||
38 | property bool multipleChoice: false | ||||
39 | property int selectionMask: 0 | ||||
40 | property var itemText: [] | ||||
41 | | ||||
42 | currentIndex: model.selectedIndex | ||||
broulik: Kudos for using `onActivated` rather than `onCurrentIndexChanged`! ;)
However, you can… | |||||
That approach fails for me (QCoreApplication::postEvent: Unexpected null receiver), even with some protections iasensio: That approach fails for me (`QCoreApplication::postEvent: Unexpected null receiver`), even with… | |||||
43 | | ||||
44 | onActivated: (index) => { | ||||
45 | currentValue = values[index]; | ||||
46 | } | ||||
47 | | ||||
48 | onSelectionMaskChanged: { | ||||
49 | if (multipleChoice) { | ||||
50 | displayText = selectionText(); | ||||
51 | } | ||||
52 | } | ||||
53 | | ||||
54 | onCountChanged: { | ||||
55 | selectionMaskChanged(); | ||||
56 | } | ||||
57 | | ||||
58 | delegate: QQC2.ItemDelegate { | ||||
59 | readonly property bool reversed : Qt.application.layoutDirection === Qt.RightToLeft | ||||
60 | | ||||
61 | highlighted: optionsCombo.highlightedIndex == index | ||||
62 | width: parent.width | ||||
Use %1 instead of the hardcoded number 1 for the singular case (there are languages where e.g. the number 21 uses the singular form) ngraham: Use `%1` instead of the hardcoded number 1 for the singular case (there are languages where e.g. | |||||
63 | | ||||
64 | LayoutMirroring.enabled: reversed | ||||
65 | LayoutMirroring.childrenInherit: true | ||||
66 | | ||||
67 | contentItem: RowLayout { | ||||
68 | QQC2.CheckBox { | ||||
69 | id: itemSelection | ||||
Is this necessary to manually assign? I thought LayoutMirroring.enabled got set automatically? ngraham: Is this necessary to manually assign? I thought `LayoutMirroring.enabled` got set automatically? | |||||
You're right, it works automatically now! iasensio: You're right, it works automatically now!
It didn't previously for me, so maybe something was… | |||||
70 | visible: multipleChoice | ||||
ngraham: would be nice if clicking anywhere in the row activated that row's checkbox | |||||
Hmm, this is not fixed yet for me. Probably you need to put a MouseArea inside the ItemDelegate that toggles the checkbox if clicked. ngraham: Hmm, this is not fixed yet for me. Probably you need to put a MouseArea inside the ItemDelegate… | |||||
oh, silly me! I did that on the main list selection when exporting.. will repeat it here iasensio: oh, silly me! I did that on the main list selection when exporting.. will repeat it here | |||||
71 | checked: (selectionMask & (1 << value)) | ||||
72 | onToggled: { | ||||
73 | selectionMask = (selectionMask & ~(1 << value)) | (checked << value); | ||||
74 | activated(index); | ||||
75 | } | ||||
76 | } | ||||
77 | Kirigami.Icon { | ||||
78 | source: model.icon | ||||
79 | Layout.preferredHeight: Kirigami.Units.iconSizes.small | ||||
80 | Layout.preferredWidth: Kirigami.Units.iconSizes.small | ||||
81 | } | ||||
82 | QQC2.Label { | ||||
83 | text: model.text | ||||
84 | color: highlighted ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor | ||||
85 | Layout.fillWidth: true | ||||
86 | horizontalAlignment: Text.AlignLeft | ||||
87 | } | ||||
88 | } | ||||
89 | | ||||
90 | QQC2.ToolTip { | ||||
91 | text: model.description | ||||
92 | visible: hovered && (model.description != "") | ||||
93 | } | ||||
94 | | ||||
95 | Component.onCompleted: { | ||||
96 | values[index] = model.value; | ||||
97 | itemText[index] = model.text; | ||||
98 | optionsCombo.popup.width = Math.max(implicitWidth, optionsCombo.width, optionsCombo.popup.width); | ||||
99 | } | ||||
100 | } | ||||
101 | | ||||
102 | function selectionText() { | ||||
103 | var selectionCount = selectionMask.toString(2).replace(/0/g, '').length; | ||||
104 | switch (selectionCount) { | ||||
105 | case 0: return i18n("None selected"); | ||||
106 | case count: return i18n("All selected"); | ||||
107 | case 1: break; | ||||
108 | default: return i18np("1 selected", "%1 selected", selectionCount); | ||||
109 | } | ||||
Can you add a TODO or FIXME comment indicating that this works around https://bugs.kde.org/show_bug.cgi?id=403153? ngraham: Can you add a TODO or FIXME comment indicating that this works around https://bugs.kde. | |||||
110 | // Use the display text of the only selected item | ||||
111 | for (var i = 0; i < count; i++) { | ||||
112 | if (selectionMask & (1 << values[i])) return itemText[i]; | ||||
113 | } | ||||
114 | } | ||||
Add braces, either on the same line, or in the more conventional multi-line style ngraham: Add braces, either on the same line, or in the more conventional multi-line style | |||||
115 | } |
Kudos for using onActivated rather than onCurrentIndexChanged! ;)
However, you can probably make this a binding
or perhaps a string property, even