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.14 | ||||
22 | import QtQuick.Layouts 1.14 | ||||
23 | import QtQuick.Controls 2.14 as QQC2 | ||||
24 | | ||||
25 | import org.kde.kirigami 2.10 as Kirigami | ||||
26 | | ||||
27 | | ||||
28 | QQC2.ComboBox { | ||||
29 | id: optionsCombo | ||||
30 | | ||||
31 | textRole: "display" | ||||
32 | //TODO: After KF5 (qqc2-desktop-style) depends on Qt 5.15 this can be simplified using newer API | ||||
33 | // (https://bugs.kde.org/show_bug.cgi?id=419521) | ||||
34 | // valueRole: "value" | ||||
35 | property var currentValue | ||||
36 | | ||||
37 | onActivated: (index) => { | ||||
38 | var modelIndex = model.index(index, 0); | ||||
39 | currentValue = model.data(modelIndex, Qt.UserRole); | ||||
40 | } | ||||
41 | | ||||
42 | property bool multipleChoice: false | ||||
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 | property int selectionMask: 0 | ||||
44 | | ||||
45 | currentIndex: multipleChoice ? -1 : model.selectedIndex | ||||
46 | | ||||
47 | displayText: { | ||||
48 | if (!multipleChoice) { | ||||
49 | return currentText; | ||||
50 | } | ||||
51 | var selectionCount = selectionMask.toString(2).replace(/0/g, '').length; | ||||
52 | switch (selectionCount) { | ||||
53 | case 0: | ||||
54 | return i18n("None selected"); | ||||
55 | case 1: | ||||
56 | var selectedValue = selectionMask.toString(2).length - 1; | ||||
57 | return model.textOfValue(selectedValue); | ||||
58 | case count: | ||||
59 | return i18n("All selected"); | ||||
60 | } | ||||
61 | return i18np("1 selected", "%1 selected", selectionCount); | ||||
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. | |||||
62 | } | ||||
63 | | ||||
64 | delegate: QQC2.ItemDelegate { | ||||
65 | highlighted: optionsCombo.highlightedIndex == index | ||||
66 | width: parent.width | ||||
67 | | ||||
68 | LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft | ||||
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… | |||||
69 | LayoutMirroring.childrenInherit: true | ||||
70 | | ||||
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 | contentItem: RowLayout { | ||||
72 | QQC2.CheckBox { | ||||
73 | id: itemSelection | ||||
74 | visible: multipleChoice | ||||
75 | checked: (selectionMask & (1 << value)) | ||||
76 | onToggled: { | ||||
77 | selectionMask = (selectionMask & ~(1 << value)) | (checked << value); | ||||
78 | activated(index); | ||||
79 | } | ||||
80 | } | ||||
81 | Kirigami.Icon { | ||||
82 | source: model.decoration | ||||
83 | Layout.preferredHeight: Kirigami.Units.iconSizes.small | ||||
84 | Layout.preferredWidth: Kirigami.Units.iconSizes.small | ||||
85 | } | ||||
86 | QQC2.Label { | ||||
87 | text: model.display | ||||
88 | color: highlighted ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor | ||||
89 | Layout.fillWidth: true | ||||
90 | horizontalAlignment: Text.AlignLeft | ||||
91 | } | ||||
92 | } | ||||
93 | | ||||
94 | MouseArea { | ||||
95 | anchors.fill: contentItem | ||||
96 | enabled: multipleChoice | ||||
97 | onClicked: { | ||||
98 | itemSelection.toggle(); | ||||
99 | itemSelection.toggled(); | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | QQC2.ToolTip { | ||||
104 | text: model.tooltip | ||||
105 | visible: hovered && (model.tooltip != "") | ||||
106 | } | ||||
107 | | ||||
108 | Component.onCompleted: { | ||||
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. | |||||
109 | optionsCombo.popup.width = Math.max(implicitWidth, optionsCombo.width, optionsCombo.popup.width); | ||||
110 | } | ||||
111 | | ||||
112 | onFocusChanged: { | ||||
113 | if (!focus) popup.close(); | ||||
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 | |||||
114 | } | ||||
115 | } | ||||
116 | } |
Kudos for using onActivated rather than onCurrentIndexChanged! ;)
However, you can probably make this a binding
or perhaps a string property, even