Warning, /plasma/kwin/src/kcms/rules/ui/ValueEditor.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 import QtQuick
0008 import QtQuick.Layouts
0009 import QtQuick.Controls as QQC2
0010 
0011 import org.kde.kirigami 2.10 as Kirigami
0012 import org.kde.kquickcontrols as KQC
0013 import org.kde.kcms.kwinrules
0014 
0015 
0016 Loader {
0017     id: valueEditor
0018     focus: true
0019 
0020     property var ruleValue
0021     property var ruleOptions
0022     property int controlType
0023 
0024     signal valueEdited(var value)
0025 
0026     sourceComponent: {
0027         switch (controlType) {
0028             case RuleItem.Boolean: return booleanEditor
0029             case RuleItem.String: return stringEditor
0030             case RuleItem.Integer: return integerEditor
0031             case RuleItem.Option: return optionEditor
0032             case RuleItem.NetTypes: return netTypesEditor
0033             case RuleItem.Percentage: return percentageEditor
0034             case RuleItem.Point: return coordinateEditor
0035             case RuleItem.Size: return coordinateEditor
0036             case RuleItem.Shortcut: return shortcutEditor
0037             case RuleItem.OptionList: return optionListEditor
0038             default: return emptyEditor
0039         }
0040     }
0041 
0042     Component {
0043         id: emptyEditor
0044         Item {}
0045     }
0046 
0047     Component {
0048         id: booleanEditor
0049         RowLayout {
0050             Item {
0051                 Layout.fillWidth: true
0052             }
0053             QQC2.RadioButton {
0054                 text: i18n("Yes")
0055                 checked: ruleValue
0056                 Layout.margins: Kirigami.Units.smallSpacing
0057                 onToggled: valueEditor.valueEdited(checked)
0058             }
0059             QQC2.RadioButton {
0060                 text: i18n("No")
0061                 checked: !ruleValue
0062                 Layout.margins: Kirigami.Units.smallSpacing
0063                 onToggled: valueEditor.valueEdited(!checked)
0064             }
0065         }
0066     }
0067 
0068     Component {
0069         id: stringEditor
0070         QQC2.TextField {
0071             id: stringTextField
0072             property bool isTextEdited: false
0073             horizontalAlignment: Text.AlignLeft
0074             onTextEdited: { valueEditor.valueEdited(text); }
0075             Connections {
0076                 target: valueEditor
0077                 function onRuleValueChanged() {
0078                     if (!stringTextField.activeFocus) {  // Protects from self-updating when editing
0079                         stringTextField.text = valueEditor.ruleValue
0080                     }
0081                 }
0082             }
0083             Component.onCompleted: { this.text = valueEditor.ruleValue }
0084         }
0085     }
0086 
0087     Component {
0088         id: integerEditor
0089         QQC2.SpinBox {
0090             editable: true
0091             value: ruleValue
0092             onValueModified: valueEditor.valueEdited(value)
0093         }
0094     }
0095 
0096     Component {
0097         id: optionEditor
0098         OptionsComboBox {
0099             model: ruleOptions
0100             onActivated: (index) => {
0101                 valueEditor.valueEdited(currentValue);
0102             }
0103         }
0104     }
0105 
0106     Component {
0107         id: netTypesEditor
0108         OptionsComboBox {
0109             model: ruleOptions
0110             multipleChoice: true
0111             // Filter the provided value with the options mask
0112             selectionMask: ruleValue & model.allOptionsMask
0113             onActivated: {
0114                 valueEditor.valueEdited(selectionMask);
0115             }
0116         }
0117     }
0118 
0119     Component {
0120         id: optionListEditor
0121         OptionsComboBox {
0122             id: optionListCombo
0123             model: ruleOptions
0124             multipleChoice: true
0125 
0126             onActivated: {
0127                 let selectionList = []
0128                 for (let i = 0; i < count; i++) {
0129                     if (selectionMask & (1 << i)) {
0130                         selectionList.push(model.data(model.index(i,0), Qt.UserRole))
0131                     }
0132                 }
0133                 valueEditor.valueEdited(selectionList);
0134             }
0135 
0136             function updateSelectionMask() {
0137                 selectionMask = 0
0138                 for (let i = 0; i < count; i++) {
0139                     if (ruleValue.includes(model.data(model.index(i,0), Qt.UserRole))) {
0140                         selectionMask += 1 << i
0141                     }
0142                 }
0143             }
0144 
0145             onModelChanged: updateSelectionMask()
0146             Component.onCompleted: updateSelectionMask()
0147             Connections {
0148                 target: valueEditor
0149                 function onRuleValueChanged() {
0150                     optionListCombo.updateSelectionMask()
0151                 }
0152             }
0153         }
0154     }
0155 
0156     Component {
0157         id: percentageEditor
0158         RowLayout {
0159             QQC2.Slider {
0160                 id: slider
0161                 Layout.fillWidth: true
0162                 from: 0
0163                 to: 100
0164                 value: ruleValue
0165                 onMoved: valueEditor.valueEdited(Math.round(slider.value))
0166             }
0167             QQC2.Label {
0168                 text: i18n("%1 %", Math.round(slider.value))
0169                 horizontalAlignment: Qt.AlignRight
0170                 Layout.minimumWidth: maxPercentage.width + Kirigami.Units.smallSpacing
0171                 Layout.margins: Kirigami.Units.smallSpacing
0172             }
0173             TextMetrics {
0174                 id: maxPercentage
0175                 text: i18n("%1 %", 100)
0176             }
0177         }
0178     }
0179 
0180     Component {
0181         id: coordinateEditor
0182         RowLayout {
0183             id: coordItem
0184             spacing: Kirigami.Units.smallSpacing
0185 
0186             readonly property bool isSize: controlType == RuleItem.Size
0187             readonly property var coord: (isSize) ? Qt.size(coordX.value, coordY.value)
0188                                                   : Qt.point(coordX.value, coordY.value)
0189 
0190             QQC2.SpinBox {
0191                 id: coordX
0192                 editable: true
0193                 Layout.preferredWidth: 50   // 50%
0194                 Layout.fillWidth: true
0195                 from: (isSize) ? 0 : -32767
0196                 to: 32767
0197                 value: (isSize) ? ruleValue.width : ruleValue.x
0198                 onValueModified: valueEditor.valueEdited(coord)
0199             }
0200             QQC2.Label {
0201                 id: coordSeparator
0202                 Layout.preferredWidth: implicitWidth
0203                 text: i18nc("(x, y) coordinates separator in size/position","x")
0204                 horizontalAlignment: Text.AlignHCenter
0205             }
0206             QQC2.SpinBox {
0207                 id: coordY
0208                 editable: true
0209                 from: coordX.from
0210                 to: coordX.to
0211                 Layout.preferredWidth: 50   // 50%
0212                 Layout.fillWidth: true
0213                 value: (isSize) ? ruleValue.height : ruleValue.y
0214                 onValueModified: valueEditor.valueEdited(coord)
0215             }
0216         }
0217     }
0218 
0219     Component {
0220         id: shortcutEditor
0221         RowLayout {
0222             Item {
0223                 Layout.fillWidth: true
0224             }
0225             KQC.KeySequenceItem {
0226                 keySequence: ruleValue
0227                 onCaptureFinished: valueEditor.valueEdited(keySequence)
0228             }
0229         }
0230     }
0231 }