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

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
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 import QtQml.Models 2.12
0011 
0012 import org.kde.kirigami 2.2 as Kirigami
0013 import org.kde.ksysguard.table 1.0 as Table
0014 
0015 Control
0016 {
0017     id: root
0018 
0019     property int row: model.row
0020     property int column: model.column
0021     property bool selected: false
0022 
0023     readonly property bool rowHovered: root.TableView.view.hoveredRow == row
0024 
0025     readonly property var __selection: TableView.view.selectionModel
0026 
0027     // We need to update:
0028     // if the selected indexes changes
0029     // if our delegate moves
0030     // if the model moves and the delegate stays in the same place
0031     function updateIsSelected() {
0032         selected = __selection.rowIntersectsSelection(row)
0033     }
0034 
0035     leftPadding: 0
0036     rightPadding: 0
0037     topPadding: 0
0038     bottomPadding: 0
0039 
0040     Connections {
0041         target: __selection
0042         function onSelectionChanged() {
0043             updateIsSelected();
0044         }
0045     }
0046 
0047     onRowChanged: updateIsSelected();
0048 
0049     Connections {
0050         target: root.TableView.view
0051         function onModelLayoutHasChanged() {
0052             updateIsSelected();
0053         }
0054     }
0055 
0056     Component.onCompleted: {
0057         updateIsSelected();
0058     }
0059 
0060     Kirigami.Theme.colorSet: selected ? Kirigami.Theme.Selection : Kirigami.Theme.View
0061     Kirigami.Theme.inherit: false
0062 
0063     background: Rectangle {
0064         color: (row % 2 == 0 || selected) ? Kirigami.Theme.backgroundColor : Kirigami.Theme.alternateBackgroundColor
0065 
0066         Rectangle {
0067             anchors.fill: parent
0068             Kirigami.Theme.inherit: false
0069             Kirigami.Theme.colorSet: Kirigami.Theme.Selection
0070             color: Kirigami.Theme.backgroundColor
0071             opacity: 0.3
0072             visible: root.rowHovered
0073         }
0074     }
0075 
0076     hoverEnabled: true
0077 
0078     MouseArea {
0079         anchors.fill: parent
0080         hoverEnabled: true
0081 
0082         acceptedButtons: Qt.LeftButton | Qt.RightButton
0083 
0084         onEntered: {
0085             root.TableView.view.hoveredRow = row
0086         }
0087         onExited: {
0088             root.TableView.view.hoveredRow = -1
0089         }
0090 
0091         onClicked: {
0092             var modelIndex = root.TableView.view.model.index(row, column);
0093             root.TableView.view.forceActiveFocus(Qt.ClickFocus)
0094 
0095             // latest clicks sets current index
0096             root.__selection.setCurrentIndex(modelIndex, ItemSelectionModel.Current | ItemSelectionModel.Rows)
0097 
0098             if (root.__selection.isSelected(modelIndex) && mouse.button == Qt.RightButton) {
0099                 root.TableView.view.contextMenuRequested(modelIndex, mapToGlobal(mouse.x, mouse.y))
0100                 return
0101             }
0102 
0103             if (mouse.modifiers & Qt.ShiftModifier) {
0104                 //TODO: Implement range selection
0105                 root.__selection.select(modelIndex, ItemSelectionModel.Toggle | ItemSelectionModel.Rows)
0106             } else if (mouse.modifiers & Qt.ControlModifier) {
0107                 root.__selection.select(modelIndex, ItemSelectionModel.Toggle | ItemSelectionModel.Rows)
0108             } else {
0109                 root.__selection.select(modelIndex, ItemSelectionModel.ClearAndSelect | ItemSelectionModel.Rows)
0110             }
0111 
0112             if (mouse.button == Qt.RightButton) {
0113                 root.TableView.view.contextMenuRequested(modelIndex, mapToGlobal(mouse.x, mouse.y))
0114             }
0115         }
0116     }
0117 }