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

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  * SPDX-FileCopyrightText: 2023 Nate Graham <nate@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 import QtQuick
0009 import QtQuick.Controls
0010 import QtQuick.Layouts
0011 
0012 import org.kde.kirigami as Kirigami
0013 
0014 import org.kde.ksysguard.formatter as Formatter
0015 import org.kde.ksysguard.table as Table
0016 
0017 Kirigami.Dialog {
0018     id: columnDialog
0019 
0020     property alias model: columnView.model
0021     property alias sourceModel: sortModel.sourceModel
0022     property var visibleColumns: ["name"]
0023     property var sortedColumns: []
0024     property var fixedColumns: []
0025     property var columnDisplay: {"name": "text"}
0026 
0027     title: i18ndc("plasma-systemmonitor", "@window:title", "Configure Columns")
0028 
0029     preferredWidth: parent ? parent.width * 0.75 : 0
0030     preferredHeight: parent ? parent.height * 0.75 : 0
0031 
0032     focus: true
0033 
0034     // We already have a cancel button in the footer
0035     showCloseButton: false
0036 
0037     standardButtons: Dialog.Ok | Dialog.Cancel
0038 
0039     function setColumnDisplay(display) {
0040         columnDisplay = display
0041         prepare()
0042         apply()
0043     }
0044 
0045     onAccepted: {
0046         apply()
0047     }
0048 
0049     onAboutToShow: {
0050         prepare()
0051     }
0052 
0053     function prepare() {
0054         sortModel.sortedColumns = sortedColumns.filter(id => fixedColumns.indexOf(id) == -1)
0055         let tempDisplay = columnDisplay
0056         fixedColumns.forEach(column => delete tempDisplay[column])
0057         displayModel.columnDisplay = tempDisplay
0058     }
0059 
0060     function apply() {
0061         sortedColumns = fixedColumns.concat(sortModel.sortedColumns)
0062         visibleColumns = fixedColumns.concat(displayModel.visibleColumnIds)
0063         columnDisplay = displayModel.columnDisplay
0064     }
0065 
0066     ListView {
0067         id: columnView
0068 
0069         Kirigami.Theme.colorSet: Kirigami.Theme.View
0070         Kirigami.Theme.inherit: false
0071 
0072         clip: true
0073 
0074         model: Table.ColumnDisplayModel {
0075             id: displayModel
0076 
0077             sourceModel: Table.ColumnSortModel {
0078                 id: sortModel
0079             }
0080         }
0081 
0082         delegate: Loader {
0083             width: columnView.width
0084             height: Kirigami.Units.gridUnit * 3
0085             property var modelData: model
0086             sourceComponent: delegateComponent
0087         }
0088 
0089         Component {
0090             id: delegateComponent
0091             Kirigami.SubtitleDelegate {
0092                 id: delegate
0093                 rightPadding: Kirigami.Units.smallSpacing
0094                 property int index: modelData ? modelData.row : -1
0095                 activeFocusOnTab: false
0096 
0097                 // We don't want visual interactivity for the background
0098                 highlighted: false
0099                 hoverEnabled: false
0100                 down: false
0101 
0102                 text: modelData?.name ?? ""
0103                 subtitle: modelData?.description ?? ""
0104 
0105                 Kirigami.Theme.useAlternateBackgroundColor: true
0106 
0107                 ToolTip.visible: hoverHandler.hovered && contentItem.truncated
0108 
0109                 contentItem: RowLayout {
0110                     spacing: Kirigami.Units.smallSpacing
0111 
0112                     property bool truncated: titleSubtitle.truncated
0113 
0114                     Kirigami.ListItemDragHandle {
0115                         id: handle
0116                         listItem: delegate
0117                         listView: columnView
0118                         onMoveRequested: (oldIndex, newIndex) => {
0119                             sortModel.move(oldIndex, newIndex);
0120                         }
0121                     }
0122 
0123                     Kirigami.TitleSubtitle {
0124                         id: titleSubtitle
0125                         Layout.fillWidth: true
0126                         title: delegate.text
0127                         subtitle: delegate.subtitle
0128                     }
0129 
0130                     ComboBox {
0131                         id: showCombo
0132                         textRole: "text"
0133                         Layout.rowSpan: 2
0134                         Layout.rightMargin: Kirigami.Units.smallSpacing
0135                         model: {
0136                             var result = [
0137                                 {text: i18ndc("plasma-systemmonitor", "@item:inlistbox", "Hidden"), value: "hidden"},
0138                                 {text: i18ndc("plasma-systemmonitor", "@item:inlistbox", "Text Only"), value: "text"},
0139                             ]
0140 
0141                             if (modelData && modelData.unit) {
0142                                 if (modelData.unit != Formatter.Units.UnitInvalid
0143                                     && modelData.unit != Formatter.Units.UnitNone) {
0144                                     result.push({text: i18ndc("plasma-systemmonitor", "@item:inlistbox", "Line Chart"), value: "line"})
0145                                 }
0146                                 if (modelData.unit == Formatter.Units.UnitPercent
0147                                     && modelData.maximum != 100) {
0148                                     result.push({text: i18ndc("plasma-systemmonitor", "@item:inlistbox", "Text Only (Scaled to 100%)"), value: "textScaled"})
0149                                     result.push({text: i18ndc("plasma-systemmonitor", "@item:inlistbox", "Line Chart (Scaled to 100%)"), value: "lineScaled"})
0150                                 }
0151                             }
0152                             return result
0153                         }
0154 
0155                         currentIndex: {
0156                             if (!modelData) {
0157                                 return -1;
0158                             }
0159 
0160                             for (var i = 0; i < model.length; ++i) {
0161                                 if (model[i].value == modelData.displayStyle) {
0162                                     return i;
0163                                 }
0164                             }
0165                             return -1;
0166                         }
0167 
0168                         onActivated: {
0169                             displayModel.setDisplay(delegate.index, model[index].value);
0170                         }
0171                     }
0172                 }
0173 
0174                 HoverHandler {
0175                     id: hoverHandler
0176                 }
0177             }
0178         }
0179     }
0180 }