PageSortDialog.qml 12.4 KB
Newer Older
1
2
3
4
5
6
/*
 * SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

7
8
9
10
11
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

import org.kde.kirigami 2.12 as Kirigami
12
13
import org.kde.kitemmodels 1.0 as KItemModels

14
15
16
17
18

import org.kde.ksysguard.page 1.0 as Page

Dialog {
    property alias model : sortModel.sourceModel
19
    property string startPage
20
21

    modal: true
22
    focus: true
23
24
25
26
27
    standardButtons: Dialog.Ok | Dialog.Cancel

    width: parent ? parent.width * 0.75 : 0
    height: parent ? parent.height * 0.75 : 0

28
    x: parent ? Math.round(parent.width / 2 - width / 2) : 0
David Redondo's avatar
David Redondo committed
29
30
    y: ApplicationWindow.window ? ApplicationWindow.window.pageStack.globalToolBar.height - Kirigami.Units.smallSpacing : 0

31
32
33
34
35
36
37
38
    leftPadding: Kirigami.Units.devicePixelRatio
    rightPadding: Kirigami.Units.devicePixelRatio
    bottomPadding: Kirigami.Units.smallSpacing
    topPadding: Kirigami.Units.smallSpacing
    bottomInset: -Kirigami.Units.smallSpacing

    onAboutToShow: {
        sortModel.sourceModel = model
39
        startPageBox.currentIndex = startPageBox.indexOfValue(startPage)
40
41
42
43
    }

    onAccepted: {
        sortModel.applyChangesToSourceModel()
44
        startPage = startPageBox.currentValue
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    }

    Kirigami.Theme.colorSet: Kirigami.Theme.View

    Rectangle {
        anchors.fill: parent
        Kirigami.Theme.colorSet: Kirigami.Theme.View
        color: Kirigami.Theme.backgroundColor
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 0

        Kirigami.Separator { Layout.fillWidth: true; }

        ScrollView {
            Layout.fillWidth: true
            Layout.fillHeight: true
            ListView {
                id: pageList
                model: Page.PageSortModel {
                    id: sortModel
                }
                delegate: Kirigami.DelegateRecycler {
                    sourceComponent: delegateComponent
                }
                Component {
                    id: delegateComponent
                    Kirigami.AbstractListItem {
                        id: listItem
76
77
                        readonly property string title: model ? model.title : ""
                        readonly property string icon: model ? model.icon : ""
David Redondo's avatar
David Redondo committed
78
                        //Using directly model.hidden below doesn't work for some reason
79
80
81
                        readonly property bool hidden: model ? model.hidden : false
                        readonly property var filesWritable: model ? model.filesWriteable : false
                        readonly property bool shouldRemoveFiles: model ? model.shouldRemoveFiles : false
82

83
84
                        width: pageList.width

85
86
                        activeFocusOnTab: false

87
88
89
90
91
92
93
94
95
96
97
                        contentItem: RowLayout {
                            Kirigami.ListItemDragHandle {
                                id: handle
                                Layout.fillHeight: true
                                Layout.rowSpan: 2
                                listItem: listItem
                                listView: pageList
                                onMoveRequested: {
                                    sortModel.move(oldIndex, newIndex)
                                }
                            }
David Redondo's avatar
David Redondo committed
98
                            CheckBox {
99
100
101
102
                                checked: !listItem.hidden
                                onToggled: pageList.model.setData(pageList.model.index(index, 0), !listItem.hidden, Page.PagesModel.HiddenRole)
                                ToolTip.text: listItem.hidden ? i18nc("@info:tooltip", "Show Page")
                                                              : i18nc("@info:tooltip", "Hide Page")
David Redondo's avatar
David Redondo committed
103
104
105
                                ToolTip.delay: Kirigami.Units.toolTipDelay
                                ToolTip.visible: hovered
                            }
106
107
108
                            Kirigami.Icon {
                                Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
                                Layout.preferredHeight: Layout.preferredWidth
109
110
                                source: listItem.icon
                                opacity: listItem.hidden ? 0.3 : 1
111
                            }
112
113
114
115
116
                            ColumnLayout {
                                Layout.alignment: Qt.AlignVCenter
                                spacing: 0
                                Label {
                                    Layout.fillWidth: true
117
118
                                    text: listItem.title
                                    opacity: listItem.hidden ? 0.3 : 1
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
                                }
                                Label {
                                    id: subtitle
                                    Layout.fillWidth: true
                                    font: Kirigami.Theme.smallFont
                                    opacity: 0.7
                                    visible: text.length > 0
                                }
                            }
                            Button {
                                id: removeButton
                                onClicked: pageList.model.setData(pageList.model.index(index, 0), !listItem.shouldRemoveFiles, Page.PageSortModel.ShouldRemoveFilesRole)
                                ToolTip.delay: Kirigami.Units.toolTipDelay
                                ToolTip.visible: hovered
                                states: [
                                    State {
                                        name: "noChanges"
                                        extend: "localChanges"
                                        when: listItem.filesWritable == Page.PagesModel.NotWriteable
                                        PropertyChanges {
                                            target: removeButton;
                                            enabled: false;
                                        }
                                    },
                                    State {
                                        name: "removeLocalChanges"
                                        when:  listItem.filesWritable == Page.PagesModel.LocalChanges && listItem.shouldRemoveFiles
                                        PropertyChanges {
                                            target: removeButton
                                            icon.name: "edit-redo"
149
                                            ToolTip.text: i18nc("@info:tooltip", "Do not reset the page")
150
151
152
                                        }
                                        PropertyChanges {
                                            target: subtitle
153
                                            text: i18nc("@item:intable", "The page will be reset to its default state")
154
155
156
157
158
159
160
161
                                        }
                                    },
                                    State {
                                        name: "localChanges"
                                        when: listItem.filesWritable == Page.PagesModel.LocalChanges
                                        PropertyChanges {
                                            target: removeButton
                                            icon.name: "edit-reset"
162
                                            ToolTip.text: i18nc("@info:tooltip", "Reset the page to its default state")
163
164
165
166
167
168
169
170
                                        }
                                    },
                                    State {
                                        name: "remove"
                                        when: listItem.filesWritable == Page.PagesModel.AllWriteable && listItem.shouldRemoveFiles
                                        PropertyChanges {
                                            target: removeButton
                                            icon.name: "edit-undo"
171
                                            ToolTip.text: i18nc("@info:tooltip", "Do not remove this page")
172
173
174
                                        }
                                        PropertyChanges {
                                            target: subtitle
175
                                            text: i18nc("@item:intable", "The page will be removed")
176
177
178
179
180
181
182
183
184
185
186
187
                                        }
                                        PropertyChanges {
                                            target: listItem
                                            backgroundColor: Kirigami.Theme.negativeBackgroundColor
                                        }
                                    },
                                    State {
                                        name: "removeable"
                                        when: listItem.filesWritable == Page.PagesModel.AllWriteable
                                        PropertyChanges {
                                            target: removeButton
                                            icon.name: "edit-delete"
188
                                            ToolTip.text: i18nc("@info:tooltip", "Remove this page")
189
190
191
                                        }
                                    }
                                ]
192
193
194
195
196
197
198
199
                            }
                        }
                    }
                }
            }
        }
        Kirigami.Separator { Layout.fillWidth: true; }
    }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    footer: DialogButtonBox {
        // Use an AbstractButton so the button box includes it
        AbstractButton {
            implicitWidth: childrenRect.width
            DialogButtonBox.buttonRole: DialogButtonBox.ActionRole
            RowLayout {
                Label {
                    text: i18nc("@label:listbox", "Start with:")
                }
                ComboBox {
                    id: startPageBox
                    textRole: "title"
                    valueRole: "fileName"
                    model: createModel()

                    function createModel() {
                        var result = [{fileName: "", title: i18n("Last Visited Page"), icon: "clock"}];
                        for (var i = 0; i < sortModel.rowCount(); ++i) {
                            const index = sortModel.index(i, 0)
                            if (sortModel.data(index, Page.PagesModel.HiddenRole)) {
                                continue
                            }
                            if (sortModel.data(index, Page.PageSortModel.ShouldRemoveFilesRole)
                                && sortModel.data(index, Page.PagesModel.FilesWriteableRole) == Page.PagesModel.AllWriteable) {
                                continue
                            }
                            result.push({fileName: sortModel.data(index, Page.PagesModel.FileNameRole),
                                         title: sortModel.data(index, Page.PagesModel.TitleRole),
                                         icon: sortModel.data(index, Page.PagesModel.IconRole)})
                        }
                        return result
                    }

                    Connections {
                        target: sortModel
235
236
237
238
239
240
241
                        function onDataChanged() { refreshModel() }
                        function onRowsMoved() { refreshModel() }
                        function refreshModel() {
                            const value = startPageBox.currentValue
                            startPageBox.model = startPageBox.createModel()
                            startPageBox.currentIndex = startPageBox.indexOfValue(value)
                        }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                    }

                    delegate: ItemDelegate {
                        width: parent.width
                        highlighted: startPageBox.highlightedIndex == index
                        contentItem: RowLayout {
                            Kirigami.Icon {
                                color: highlighted ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
                                source: modelData.icon
                                Layout.preferredHeight: Kirigami.Units.iconSizes.small
                                Layout.preferredWidth: Kirigami.Units.iconSizes.small
                            }
                            Label {
                                text: modelData.title
                                color: highlighted ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
                                Layout.fillWidth: true
                            }
                        }
                    }
                }
            }
        }
    }
265
}