Warning, /plasma/plasma-systemmonitor/src/page/PageSortDialog.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 import QtQuick 2.12
0008 import QtQuick.Controls 2.12
0009 import QtQuick.Layouts 1.12
0010 
0011 import org.kde.kirigami 2.12 as Kirigami
0012 import org.kde.kitemmodels 1.0 as KItemModels
0013 
0014 
0015 import org.kde.ksysguard.page 1.0 as Page
0016 
0017 Dialog {
0018     property alias model : sortModel.sourceModel
0019     property string startPage
0020 
0021     modal: true
0022     focus: true
0023     standardButtons: Dialog.Ok | Dialog.Cancel
0024 
0025     width: parent ? parent.width * 0.75 : 0
0026     height: parent ? parent.height * 0.75 : 0
0027 
0028     x: parent ? Math.round(parent.width / 2 - width / 2) : 0
0029     y: ApplicationWindow.window ? ApplicationWindow.window.pageStack.globalToolBar.height - Kirigami.Units.smallSpacing : 0
0030 
0031     leftPadding: 1
0032     rightPadding: 1
0033     bottomPadding: Kirigami.Units.smallSpacing
0034     topPadding: Kirigami.Units.smallSpacing
0035     bottomInset: -Kirigami.Units.smallSpacing
0036 
0037     onAboutToShow: {
0038         sortModel.sourceModel = model
0039         startPageBox.currentIndex = startPageBox.indexOfValue(startPage)
0040     }
0041 
0042     onAccepted: {
0043         sortModel.applyChangesToSourceModel()
0044         startPage = startPageBox.currentValue
0045     }
0046 
0047     Kirigami.Theme.colorSet: Kirigami.Theme.View
0048 
0049     Rectangle {
0050         anchors.fill: parent
0051         Kirigami.Theme.colorSet: Kirigami.Theme.View
0052         color: Kirigami.Theme.backgroundColor
0053     }
0054 
0055     ColumnLayout {
0056         anchors.fill: parent
0057         spacing: 0
0058 
0059         Kirigami.Separator { Layout.fillWidth: true; }
0060 
0061         ScrollView {
0062             Layout.fillWidth: true
0063             Layout.fillHeight: true
0064             ListView {
0065                 id: pageList
0066                 model: Page.PageSortModel {
0067                     id: sortModel
0068                 }
0069                 delegate: Kirigami.DelegateRecycler {
0070                     sourceComponent: delegateComponent
0071                 }
0072                 Component {
0073                     id: delegateComponent
0074                     Kirigami.AbstractListItem {
0075                         id: listItem
0076                         readonly property string title: model ? model.title : ""
0077                         readonly property string icon: model ? model.icon : ""
0078                         //Using directly model.hidden below doesn't work for some reason
0079                         readonly property bool hidden: model ? model.hidden : false
0080                         readonly property var filesWritable: model ? model.filesWriteable : false
0081                         readonly property bool shouldRemoveFiles: model ? model.shouldRemoveFiles : false
0082 
0083                         width: pageList.width
0084 
0085                         activeFocusOnTab: false
0086 
0087                         contentItem: RowLayout {
0088                             Kirigami.ListItemDragHandle {
0089                                 id: handle
0090                                 Layout.fillHeight: true
0091                                 Layout.rowSpan: 2
0092                                 listItem: listItem
0093                                 listView: pageList
0094                                 onMoveRequested: {
0095                                     sortModel.move(oldIndex, newIndex)
0096                                 }
0097                             }
0098                             CheckBox {
0099                                 checked: !listItem.hidden
0100                                 onToggled: pageList.model.setData(pageList.model.index(index, 0), !listItem.hidden, Page.PagesModel.HiddenRole)
0101                                 ToolTip.text: listItem.hidden ? i18nc("@info:tooltip", "Show Page")
0102                                                               : i18nc("@info:tooltip", "Hide Page")
0103                                 ToolTip.delay: Kirigami.Units.toolTipDelay
0104                                 ToolTip.visible: hovered
0105                             }
0106                             Kirigami.Icon {
0107                                 Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
0108                                 Layout.preferredHeight: Layout.preferredWidth
0109                                 source: listItem.icon
0110                                 opacity: listItem.hidden ? 0.3 : 1
0111                             }
0112                             ColumnLayout {
0113                                 Layout.alignment: Qt.AlignVCenter
0114                                 spacing: 0
0115                                 Label {
0116                                     Layout.fillWidth: true
0117                                     text: listItem.title
0118                                     opacity: listItem.hidden ? 0.3 : 1
0119                                 }
0120                                 Label {
0121                                     id: subtitle
0122                                     Layout.fillWidth: true
0123                                     font: Kirigami.Theme.smallFont
0124                                     opacity: 0.7
0125                                     visible: text.length > 0
0126                                 }
0127                             }
0128                             Button {
0129                                 id: removeButton
0130                                 onClicked: pageList.model.setData(pageList.model.index(index, 0), !listItem.shouldRemoveFiles, Page.PageSortModel.ShouldRemoveFilesRole)
0131                                 ToolTip.delay: Kirigami.Units.toolTipDelay
0132                                 ToolTip.visible: hovered
0133                                 states: [
0134                                     State {
0135                                         name: "noChanges"
0136                                         extend: "localChanges"
0137                                         when: listItem.filesWritable == Page.PagesModel.NotWriteable
0138                                         PropertyChanges {
0139                                             target: removeButton;
0140                                             enabled: false;
0141                                         }
0142                                     },
0143                                     State {
0144                                         name: "removeLocalChanges"
0145                                         when:  listItem.filesWritable == Page.PagesModel.LocalChanges && listItem.shouldRemoveFiles
0146                                         PropertyChanges {
0147                                             target: removeButton
0148                                             icon.name: "edit-redo"
0149                                             ToolTip.text: i18nc("@info:tooltip", "Do not reset the page")
0150                                         }
0151                                         PropertyChanges {
0152                                             target: subtitle
0153                                             text: i18nc("@item:intable", "The page will be reset to its default state")
0154                                         }
0155                                     },
0156                                     State {
0157                                         name: "localChanges"
0158                                         when: listItem.filesWritable == Page.PagesModel.LocalChanges
0159                                         PropertyChanges {
0160                                             target: removeButton
0161                                             icon.name: "edit-reset"
0162                                             ToolTip.text: i18nc("@info:tooltip", "Reset the page to its default state")
0163                                         }
0164                                     },
0165                                     State {
0166                                         name: "remove"
0167                                         when: listItem.filesWritable == Page.PagesModel.AllWriteable && listItem.shouldRemoveFiles
0168                                         PropertyChanges {
0169                                             target: removeButton
0170                                             icon.name: "edit-undo"
0171                                             ToolTip.text: i18nc("@info:tooltip", "Do not remove this page")
0172                                         }
0173                                         PropertyChanges {
0174                                             target: subtitle
0175                                             text: i18nc("@item:intable", "The page will be removed")
0176                                         }
0177                                         PropertyChanges {
0178                                             target: listItem
0179                                             backgroundColor: Kirigami.Theme.negativeBackgroundColor
0180                                         }
0181                                     },
0182                                     State {
0183                                         name: "removeable"
0184                                         when: listItem.filesWritable == Page.PagesModel.AllWriteable
0185                                         PropertyChanges {
0186                                             target: removeButton
0187                                             icon.name: "edit-delete"
0188                                             ToolTip.text: i18nc("@info:tooltip", "Remove this page")
0189                                         }
0190                                     }
0191                                 ]
0192                             }
0193                         }
0194                     }
0195                 }
0196             }
0197         }
0198         Kirigami.Separator { Layout.fillWidth: true; }
0199     }
0200     footer: DialogButtonBox {
0201         // Use an AbstractButton so the button box includes it
0202         AbstractButton {
0203             implicitWidth: childrenRect.width
0204             DialogButtonBox.buttonRole: DialogButtonBox.ActionRole
0205             RowLayout {
0206                 Label {
0207                     text: i18nc("@label:listbox", "Start with:")
0208                 }
0209                 ComboBox {
0210                     id: startPageBox
0211                     textRole: "title"
0212                     valueRole: "fileName"
0213                     model: createModel()
0214 
0215                     function createModel() {
0216                         var result = [{fileName: "", title: i18n("Last Visited Page"), icon: "clock"}];
0217                         for (var i = 0; i < sortModel.rowCount(); ++i) {
0218                             const index = sortModel.index(i, 0)
0219                             if (sortModel.data(index, Page.PagesModel.HiddenRole)) {
0220                                 continue
0221                             }
0222                             if (sortModel.data(index, Page.PageSortModel.ShouldRemoveFilesRole)
0223                                 && sortModel.data(index, Page.PagesModel.FilesWriteableRole) == Page.PagesModel.AllWriteable) {
0224                                 continue
0225                             }
0226                             result.push({fileName: sortModel.data(index, Page.PagesModel.FileNameRole),
0227                                          title: sortModel.data(index, Page.PagesModel.TitleRole),
0228                                          icon: sortModel.data(index, Page.PagesModel.IconRole)})
0229                         }
0230                         return result
0231                     }
0232 
0233                     Connections {
0234                         target: sortModel
0235                         function onDataChanged() { refreshModel() }
0236                         function onRowsMoved() { refreshModel() }
0237                         function refreshModel() {
0238                             const value = startPageBox.currentValue
0239                             startPageBox.model = startPageBox.createModel()
0240                             startPageBox.currentIndex = startPageBox.indexOfValue(value)
0241                         }
0242                     }
0243 
0244                     delegate: ItemDelegate {
0245                         width: parent.width
0246                         highlighted: startPageBox.highlightedIndex == index
0247                         contentItem: RowLayout {
0248                             Kirigami.Icon {
0249                                 color: highlighted ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
0250                                 source: modelData.icon
0251                                 Layout.preferredHeight: Kirigami.Units.iconSizes.small
0252                                 Layout.preferredWidth: Kirigami.Units.iconSizes.small
0253                             }
0254                             Label {
0255                                 text: modelData.title
0256                                 color: highlighted ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
0257                                 Layout.fillWidth: true
0258                             }
0259                         }
0260                     }
0261                 }
0262             }
0263         }
0264     }
0265 }