AppletConfiguration.qml 11.4 KB
Newer Older
1
/*
2
3
4
5
6
7
    SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
    SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de>
    SPDX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
8

9
import QtQuick 2.15
10
import QtQuick.Dialogs 1.1
11
import QtQuick.Controls 2.3 as QtControls
12
import QtQuick.Layouts 1.1
13
14
import QtQuick.Window 2.2

15
import org.kde.kirigami 2.14 as Kirigami
16
import org.kde.plasma.core 2.1 as PlasmaCore
17
18
19
20
import org.kde.plasma.configuration 2.0

Rectangle {
    id: root
21
22

    implicitWidth: PlasmaCore.Units.gridUnit * 40
23
    implicitHeight: PlasmaCore.Units.gridUnit * 30
24

25
26
    Layout.minimumWidth: PlasmaCore.Units.gridUnit * 30
    Layout.minimumHeight: PlasmaCore.Units.gridUnit * 21
27

28
29
30
    LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
    LayoutMirroring.childrenInherit: true

31
    color: Kirigami.Theme.backgroundColor
32
33

    property bool isContainment: false
34
35
36
37
38
39

    property ConfigModel globalConfigModel:  globalAppletConfigModel

    ConfigModel {
        id: globalAppletConfigModel
        ConfigCategory {
40
            name: i18nd("plasma_shell_org.kde.plasma.desktop", "Keyboard Shortcuts")
41
42
43
44
            icon: "preferences-desktop-keyboard"
            source: "ConfigurationShortcuts.qml"
        }
    }
45
46
47
48
49
50
51

    PlasmaCore.SortFilterModel {
        id: configDialogFilterModel
        sourceModel: configDialog.configModel
        filterRole: "visible"
        filterCallback: function(source_row, value) { return value; }
    }
52

53
54
55
    function settingValueChanged() {
        applyButton.enabled = true;
    }
56

57
58
    function pushReplace(item, config) {
        let page;
Marco Martin's avatar
Marco Martin committed
59
        if (app.pageStack.depth === 0) {
60
61
62
63
64
65
66
67
68
69
70
            page = app.pageStack.push(item, config);
        } else {
            page = app.pageStack.replace(item, config);
        }
        app.currentConfigPage = page;
    }
    Component {
        id: configurationKcmPageComponent
        ConfigurationKcmPage {
        }
    }
71

72
73
    function open(item) {
        app.isAboutPage = false;
74
        if (item.source) {
75
            app.isAboutPage = item.source === "AboutPlugin.qml";
76
            if (item.source === "ConfigurationContainmentAppearance.qml") {
77
                pushReplace(Qt.resolvedUrl(item.source), {title: item.name});
78
            } else {
79
                pushReplace(Qt.resolvedUrl("ConfigurationAppletPage.qml"), {configItem: item, title: item.name});
80
            }
81
        } else if (item.kcm) {
82
            pushReplace(configurationKcmPageComponent, {kcm: item.kcm, internalPage: item.kcm.mainUi});
83
        } else {
84
            app.pageStack.pop();
85
86
87
88
89
90
        }

        applyButton.enabled = false
    }

    Connections {
91
        target: app.currentConfigPage
92
93

        function onSettingValueChanged() {
94
            applyButton.enabled = true;
95
96
97
98
99
100
101
102
103
104
        }
    }

    Component.onCompleted: {
        // if we are a containment then the first item will be ConfigurationContainmentAppearance
        // if the applet does not have own configs then the first item will be Shortcuts
        if (isContainment || !configDialog.configModel || configDialog.configModel.count === 0) {
            open(root.globalConfigModel.get(0))
        } else {
            open(configDialog.configModel.get(0))
105
106
107
        }
    }

108
109
    function applicationWindow() {
        return app;
110
111
112
    }


Marco Martin's avatar
Marco Martin committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    QtControls.ScrollView {
        id: categoriesScroll
        anchors {
            left: parent.left
            top: parent.top
            bottom: parent.bottom
        }
        width: Kirigami.Units.gridUnit * 7
        Kirigami.Theme.colorSet: Kirigami.Theme.View
        leftPadding: 0
        rightPadding: 0
        topPadding: 0
        bottomPadding: 0
        focus: true
        Accessible.role: Accessible.MenuBar
        background: Rectangle {
            color: Kirigami.Theme.backgroundColor
        }
131

Marco Martin's avatar
Marco Martin committed
132
133
        ColumnLayout {
            id: categories
134
135
136
137
138

            spacing: 0
            width: categoriesScroll.width
            focus: true

Marco Martin's avatar
Marco Martin committed
139
140
141
142
143
144
145
146
147
148
            Keys.onUpPressed: {
                const buttons = categories.children

                let foundPrevious = false
                for (let i = buttons.length - 1; i >= 0; --i) {
                    const button = buttons[i];
                    if (!button.hasOwnProperty("highlighted")) {
                        // not a ConfigCategoryDelegate
                        continue;
                    }
149

Marco Martin's avatar
Marco Martin committed
150
151
152
153
154
155
156
157
                    if (foundPrevious) {
                        categories.openCategory(button.item)
                        return
                    } else if (button.highlighted) {
                        foundPrevious = true
                    }
                }
            }
158

Marco Martin's avatar
Marco Martin committed
159
160
161
162
163
164
165
166
            Keys.onDownPressed: {
                const buttons = categories.children

                let foundNext = false
                for (let i = 0, length = buttons.length; i < length; ++i) {
                    const button = buttons[i];
                    if (!button.hasOwnProperty("highlighted")) {
                        continue;
167
168
                    }

Marco Martin's avatar
Marco Martin committed
169
170
171
172
173
                    if (foundNext) {
                        categories.openCategory(button.item)
                        return
                    } else if (button.highlighted) {
                        foundNext = true
174
                    }
Marco Martin's avatar
Marco Martin committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
                }
            }

            function openCategory(item) {
                if (applyButton.enabled) {
                    messageDialog.item = item;
                    messageDialog.open();
                    return;
                }
                open(item)
            }

            Component {
                id: categoryDelegate
                ConfigCategoryDelegate {
                    id: delegate
                    onActivated: categories.openCategory(model);
                    highlighted: {
193
194
195
196
197
198
199
200
                        if ( app.pageStack.currentItem ){
                            if (model.kcm && app.pageStack.currentItem.kcm) {
                                return model.kcm == app.pageStack.currentItem.kcm
                            } else if (app.pageStack.currentItem.configItem) {
                                return model.source == app.pageStack.currentItem.configItem.source
                            } else {
                                return app.pageStack.currentItem.source == Qt.resolvedUrl(model.source)
                            }
201
                        }
202
                        return false
203
                    }
Marco Martin's avatar
Marco Martin committed
204
205
206
                    item: model
                }
            }
207

Marco Martin's avatar
Marco Martin committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
            Repeater {
                Layout.fillWidth: true
                model: root.isContainment ? globalConfigModel : undefined
                delegate: categoryDelegate
            }
            Repeater {
                Layout.fillWidth: true
                model: configDialogFilterModel
                delegate: categoryDelegate
            }
            Repeater {
                Layout.fillWidth: true
                model: !root.isContainment ? globalConfigModel : undefined
                delegate: categoryDelegate
            }
            Repeater {
                Layout.fillWidth: true
                model: ConfigModel {
                    ConfigCategory{
                        name: i18nd("plasma_shell_org.kde.plasma.desktop", "About")
                        icon: "help-about"
                        source: "AboutPlugin.qml"
230
231
                    }
                }
Marco Martin's avatar
Marco Martin committed
232
                delegate: categoryDelegate
233
            }
234
        }
Marco Martin's avatar
Marco Martin committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    }

    Kirigami.Separator {
        anchors {
            left: parent.left
            right: parent.right
            top: parent.top
        }
        z: 1
    }
    Kirigami.Separator {
        anchors {
            right: categoriesScroll.right
            top: parent.top
            bottom: parent.bottom
        }
        z: 1
    }

    Kirigami.ApplicationItem {
        id: app
        anchors {
            left: categoriesScroll.right
            top: parent.top
            right: parent.right
            bottom: parent.bottom
        }

        pageStack.globalToolBar.style: Kirigami.ApplicationHeaderStyle.Breadcrumb
        wideScreen: true
        pageStack.globalToolBar.separatorVisible: bottomSeparator.visible
        pageStack.globalToolBar.colorSet: Kirigami.Theme.Window

        property var currentConfigPage: null
        property bool isAboutPage: false
270

271
272
273
274
275
276
277
278
279
280
        MessageDialog {
            id: messageDialog
            icon: StandardIcon.Warning
            property var item
            title: i18nd("plasma_shell_org.kde.plasma.desktop", "Apply Settings")
            text: i18nd("plasma_shell_org.kde.plasma.desktop", "The settings of the current module have changed. Do you want to apply the changes or discard them?")
            standardButtons: StandardButton.Apply | StandardButton.Discard | StandardButton.Cancel
            onApply: {
                applyAction.trigger()
                root.open(item)
281
            }
282
283
            onDiscard: {
                root.open(item)
284
            }
285
        }
286

Marco Martin's avatar
Marco Martin committed
287
        footer: QtControls.Pane {
288
289
290

            padding: Kirigami.Units.largeSpacing

291
292
293
            contentItem: RowLayout {
                id: buttonsRow
                spacing: Kirigami.Units.smallSpacing
294

295
296
                Item {
                    Layout.fillWidth: true
297
                }
298

299
300
301
302
                QtControls.Button {
                    icon.name: "dialog-ok"
                    text: i18nd("plasma_shell_org.kde.plasma.desktop", "OK")
                    onClicked: acceptAction.trigger()
303
                    KeyNavigation.tab: categories
304
305
306
307
308
309
                }
                QtControls.Button {
                    id: applyButton
                    enabled: false
                    icon.name: "dialog-ok-apply"
                    text: i18nd("plasma_shell_org.kde.plasma.desktop", "Apply")
310
                    visible: !app.isAboutPage && app.pageStack.currentItem && (!app.pageStack.currentItem.kcm || app.pageStack.currentItem.kcm.buttons & 4) // 4 = Apply button
311
312
313
314
315
316
                    onClicked: applyAction.trigger()
                }
                QtControls.Button {
                    icon.name: "dialog-cancel"
                    text: i18nd("plasma_shell_org.kde.plasma.desktop", "Cancel")
                    onClicked: cancelAction.trigger()
317
                    visible: !app.isAboutPage
318
                }
319
            }
Marco Martin's avatar
Marco Martin committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
            background: Item {
                Kirigami.Separator {
                    id: bottomSeparator
                    visible: app.pageStack.currentItem
                        && app.pageStack.currentItem.flickable
                        && !(app.pageStack.currentItem.flickable.atYBeginning
                        && app.pageStack.currentItem.flickable.atYEnd)
                    anchors {
                        left: parent.left
                        right: parent.right
                        top: parent.top
                    }
                }
            }
334
        }
335
336
337
338
339
340
341
342
343
344
345
346
347

        QtControls.Action {
            id: acceptAction
            onTriggered: {
                applyAction.trigger();
                configDialog.close();
            }
            shortcut: "Return"
        }

        QtControls.Action {
            id: applyAction
            onTriggered: {
348
                app.pageStack.get(0).saveConfig()
349
350
351
352
353
354
355
356
357
358

                applyButton.enabled = false;
            }
        }

        QtControls.Action {
            id: cancelAction
            onTriggered: configDialog.close();
            shortcut: "Escape"
        }
359
360
    }
}