RulesList.qml 7.63 KB
Newer Older
Ismael Asensio's avatar
Ismael Asensio committed
1
/*
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
2 3 4 5
    SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
Ismael Asensio's avatar
Ismael Asensio committed
6 7 8 9 10 11

import QtQuick 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14 as QQC2
import QtQml.Models 2.14
import org.kde.kcm 1.2
12
import org.kde.kirigami 2.12 as Kirigami
Ismael Asensio's avatar
Ismael Asensio committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

ScrollViewKCM {
    id: rulesListKCM

    // FIXME: ScrollViewKCM.qml:73:13: QML Control: Binding loop detected for property "implicitHeight"
    implicitWidth: Kirigami.Units.gridUnit * 35
    implicitHeight: Kirigami.Units.gridUnit * 25

    ConfigModule.columnWidth: Kirigami.Units.gridUnit * 23
    ConfigModule.buttons: ConfigModule.Apply

    property var selectedIndexes: []

    // Manage KCM pages
    Connections {
        target: kcm
29
        function onEditIndexChanged() {
Ismael Asensio's avatar
Ismael Asensio committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            if (kcm.editIndex < 0) {
                // If no rule is being edited, hide RulesEdidor page
                kcm.pop();
            } else if (kcm.depth < 2) {
                // Add the RulesEditor page if it wasn't already
                kcm.push("RulesEditor.qml");
            }
        }
    }

    view: ListView {
        id: ruleBookView
        clip: true

        model: kcm.ruleBookModel
        currentIndex: kcm.editIndex
        delegate: Kirigami.DelegateRecycler {
            width: ruleBookView.width
            sourceComponent: ruleBookDelegate
        }

        displaced: Transition {
            NumberAnimation { properties: "y"; duration: Kirigami.Units.longDuration }
        }

55
        Kirigami.PlaceholderMessage {
56
            visible: ruleBookView.count === 0
57 58
            anchors.centerIn: parent
            width: parent.width - (units.largeSpacing * 4)
Ismael Asensio's avatar
Ismael Asensio committed
59 60 61 62 63 64 65 66 67 68
            text: i18n("No rules for specific windows are currently set");
        }
    }

    header: Kirigami.InlineMessage {
        id: exportInfo
        icon.source: "document-export"
        showCloseButton: true
        text: i18n("Select the rules to export")
        actions: [
69 70 71 72
            Kirigami.Action {
                iconName: "object-select-symbolic"
                text: checked ? i18n("Unselect All") : i18n("Select All")
                checkable: true
73
                checked: selectedIndexes.length === ruleBookView.count
74 75 76 77 78 79 80 81 82
                onToggled: {
                    if (checked) {
                        selectedIndexes = [...Array(ruleBookView.count).keys()]
                    } else {
                        selectedIndexes = [];
                    }
                }
            }
            ,
Ismael Asensio's avatar
Ismael Asensio committed
83 84 85
            Kirigami.Action {
                iconName: "document-save"
                text: i18n("Save Rules")
86
                enabled: selectedIndexes.length > 0
Ismael Asensio's avatar
Ismael Asensio committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                onTriggered: {
                    exportDialog.active = true;
                }
            }
        ]
    }

    footer: RowLayout {
        QQC2.Button {
            text: i18n("Add New...")
            icon.name: "list-add-symbolic"
            enabled: !exportInfo.visible
            onClicked: {
                kcm.createRule();
            }
        }
        Item {
            Layout.fillWidth: true
        }
        QQC2.Button {
            text: i18n("Import...")
            icon.name: "document-import"
            enabled: !exportInfo.visible
            onClicked: {
                importDialog.active = true;
            }
        }
        QQC2.Button {
115 116
            text: checked ? i18n("Cancel Export") : i18n("Export...")
            icon.name: exportInfo.visible ? "dialog-cancel" : "document-export"
Ismael Asensio's avatar
Ismael Asensio committed
117 118 119 120 121 122 123 124 125 126 127 128
            enabled: ruleBookView.count > 0
            checkable: true
            checked: exportInfo.visible
            onToggled: {
                selectedIndexes = []
                exportInfo.visible = checked;
            }
        }
    }

    Component {
        id: ruleBookDelegate
129
        Kirigami.SwipeListItem {
Ismael Asensio's avatar
Ismael Asensio committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            id: ruleBookItem

            RowLayout {
                //FIXME: If not used within DelegateRecycler, item goes on top of the first item when clicked
                Kirigami.ListItemDragHandle {
                    visible: !exportInfo.visible
                    listItem: ruleBookItem
                    listView: ruleBookView
                    onMoveRequested: {
                        kcm.moveRule(oldIndex, newIndex);
                    }
                }

                QQC2.TextField {
                    id: descriptionField
                    Layout.minimumWidth: Kirigami.Units.gridUnit * 2
                    Layout.fillWidth: true
                    background: Item {}
                    horizontalAlignment: Text.AlignLeft
149
                    text: model && model.display
Ismael Asensio's avatar
Ismael Asensio committed
150 151 152 153 154 155 156
                    onEditingFinished: {
                        kcm.setRuleDescription(index, text);
                    }
                    Keys.onPressed: {
                        switch (event.key) {
                        case Qt.Key_Escape:
                            // On <Esc> key reset to model data before losing focus
157
                            text = model.display;
Ismael Asensio's avatar
Ismael Asensio committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                        case Qt.Key_Enter:
                        case Qt.Key_Return:
                        case Qt.Key_Tab:
                            ruleBookItem.forceActiveFocus();
                            event.accepted = true;
                            break;
                        }
                    }

                    MouseArea {
                        anchors.fill: parent
                        enabled: exportInfo.visible
                        cursorShape: enabled ? Qt.PointingHandCursor : Qt.IBeamCursor
                        onClicked: {
                            itemSelectionCheck.toggle();
                            itemSelectionCheck.toggled();
                        }
                    }
                }

                QQC2.CheckBox {
                    id: itemSelectionCheck
                    visible: exportInfo.visible
                    checked: selectedIndexes.includes(index)
                    onToggled: {
                        var position = selectedIndexes.indexOf(index);
                        if (checked) {
                            if (position < 0) { selectedIndexes.push(index); }
                        } else {
                            if (position >= 0) { selectedIndexes.splice(position, 1); }
                        }
189
                        selectedIndexesChanged();
Ismael Asensio's avatar
Ismael Asensio committed
190 191 192
                    }
                }
            }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

            actions: [
                Kirigami.Action {
                    text: i18n("Edit")
                    iconName: "edit-entry"
                    visible: !exportInfo.visible
                    onTriggered: {
                        kcm.editRule(index);
                    }
                }
                ,
                Kirigami.Action {
                    text: i18n("Delete")
                    iconName: "entry-delete"
                    visible: !exportInfo.visible
                    onTriggered: {
                        kcm.removeRule(index);
                    }
                }
            ]
Ismael Asensio's avatar
Ismael Asensio committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        }
    }

    FileDialogLoader {
        id: importDialog
        title: i18n("Import Rules")
        isSaveDialog: false
        onLastFolderChanged: {
            exportDialog.lastFolder = lastFolder;
        }
        onFileSelected: {
            kcm.importFromFile(path);
        }
    }

    FileDialogLoader {
        id: exportDialog
        title: i18n("Export Rules")
        isSaveDialog: true
        onLastFolderChanged: {
            importDialog.lastFolder = lastFolder;
        }
        onFileSelected: {
            selectedIndexes.sort();
            kcm.exportToFile(path, selectedIndexes);
            exportInfo.visible = false;
        }
    }
}