Warning, /plasma/plasma-systemmonitor/src/page/MoveButton.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
0008 import QtQuick.Controls
0009 import QtQuick.Layouts
0010 
0011 import org.kde.kirigami as Kirigami
0012 import org.kde.ksysguard.page
0013 
0014 ToolButton {
0015     id: root
0016 
0017     property int axis: Qt.YAxis
0018     property Item target
0019     property Page page
0020 
0021     signal moved(int from, int to)
0022 
0023     text: i18nc("@action", "Move")
0024     icon.name: "transform-move"
0025     flat: false
0026 
0027     DragHandler {
0028         id: drag
0029 
0030         target: root.target.mainControl
0031 
0032         xAxis.enabled: root.axis == Qt.XAxis
0033         xAxis.minimum: root.target.parent.Kirigami.ScenePosition.x
0034         xAxis.maximum: root.target.parent.Kirigami.ScenePosition.x + root.target.parent.width - root.target.width
0035 
0036         yAxis.enabled: root.axis == Qt.YAxis
0037         yAxis.minimum: root.target.parent.Kirigami.ScenePosition.y
0038         yAxis.maximum: root.target.parent.Kirigami.ScenePosition.y + root.target.parent.height - root.target.height
0039 
0040         property Item justMovedLeft
0041         property Item justMovedRight
0042         property real lastX: -1
0043         property real lastY: -1
0044 
0045         onTranslationChanged: {
0046             var pressPositionInTarget = parent.mapToItem(root.target.mainControl, centroid.pressPosition.x, centroid.pressPosition.y)
0047 
0048             var left = centroid.scenePosition.x - pressPositionInTarget.x
0049             var top = centroid.scenePosition.y - pressPositionInTarget.y
0050 
0051             var rectInParent = root.target.parent.mapFromItem(null, left, top, root.target.width, root.target.height)
0052 
0053             var x = 0
0054             var y = 0
0055             var child = null
0056             var beforeChild = null
0057             var afterChild = null
0058 
0059             if (root.axis == Qt.XAxis) {
0060                 x = rectInParent.left
0061                 y = root.target.y + root.target.height / 2
0062 
0063                 beforeChild = root.target.parent.childAt(rectInParent.left - 2, y);
0064                 afterChild = root.target.parent.childAt(rectInParent.right + 2, y);
0065 
0066                 if (beforeChild === afterChild) {
0067                     if (lastX > x) {
0068                         afterChild = null;
0069                     } else {
0070                         beforeChild = null;
0071                     }
0072                 }
0073 
0074                 var mappedLeft = beforeChild ? beforeChild.mapFromItem(root.target.parent, rectInParent.left - 2, y).x : -1;
0075                 var mappedRight = afterChild ? afterChild.mapFromItem(root.target.parent, rectInParent.right + 2, y).x : -1;
0076 
0077                 if (beforeChild && mappedLeft < beforeChild.width / 2 && mappedLeft > 0) {
0078                     child = beforeChild;
0079                 } else if (afterChild && mappedRight > afterChild.width / 2 && mappedRight < afterChild.width) {
0080                     child = afterChild;
0081                 }
0082 
0083             } else {
0084                 x = root.target.x + root.target.width / 2
0085                 y = rectInParent.top
0086 
0087                 beforeChild = root.target.parent.childAt(x, rectInParent.top - 2);
0088                 afterChild = root.target.parent.childAt(x, rectInParent.bottom + 2);
0089 
0090                 if (beforeChild === afterChild) {
0091                     if (lastY > y) {
0092                         afterChild = null;
0093                     } else {
0094                         beforeChild = null;
0095                     }
0096                 }
0097 
0098                 var mappedLeft = beforeChild ? beforeChild.mapFromItem(root.target.parent, x, rectInParent.top - 2).y : -1;
0099                 var mappedRight = afterChild ? afterChild.mapFromItem(root.target.parent, x, rectInParent.bottom + 2).y : -1;
0100 
0101                 if (beforeChild && mappedLeft < beforeChild.height / 2 && mappedLeft > 0) {
0102                     child = beforeChild;
0103                 } else if (afterChild && mappedRight > afterChild.height / 2 && mappedRight < afterChild.width) {
0104                     child = afterChild;
0105                 }
0106             }
0107 
0108             if (root.page) {
0109                 var sceneRect = root.target.mainControl.mapToItem(null, Qt.rect(0, 0, root.target.mainControl.width, root.target.mainControl.height))
0110                 root.page.scrollContents(sceneRect)
0111             }
0112 
0113             if (child && child != root.target &&
0114                 ((root.axis == Qt.XAxis &&
0115                     (lastX < x || child != justMovedLeft) &&
0116                     (lastX > x || child != justMovedRight))
0117                  || (root.axis == Qt.YAxis &&
0118                     (lastY < y || child != justMovedLeft) &&
0119                     (lastY > y || child != justMovedRight)))) {
0120 
0121                 if (child == beforeChild) {
0122                     justMovedLeft = child;
0123                     justMovedRight = null;
0124                 } else {
0125                     justMovedLeft = null;
0126                     justMovedRight = child;
0127                 }
0128 
0129                 root.moved(root.target.index, child.index);
0130             }
0131 
0132             lastX = x
0133             lastY = y
0134         }
0135     }
0136     PointHandler {
0137         id: point
0138 
0139         onActiveChanged: {
0140             if (active) {
0141                 let pos = root.target.Overlay.overlay.mapFromItem(root.target.mainControl, 0, 0);
0142                 root.target.mainControl.parent = root.target.Overlay.overlay;
0143                 root.target.mainControl.x = pos.x;
0144                 root.target.mainControl.y = pos.y;
0145                 root.target.raised = true;
0146 
0147             } else {
0148                 let pos = root.target.mapFromItem(root.target.mainControl, 0, 0);
0149                 root.target.mainControl.x = pos.x
0150                 root.target.mainControl.y = pos.y
0151                 root.target.mainControl.parent = root.target
0152 
0153                 dropAnimation.start()
0154 
0155                 drag.justMovedLeft = null
0156                 drag.justMovedRight = null
0157             }
0158         }
0159     }
0160 
0161     MouseArea {
0162         anchors.fill: parent
0163         acceptedButtons: Qt.NoButton
0164         cursorShape: root.target.raised ? Qt.ClosedHandCursor : Qt.OpenHandCursor
0165     }
0166 
0167     SequentialAnimation {
0168         id: dropAnimation
0169         NumberAnimation {
0170             id: dropNumberAnimation
0171             target: root.target.mainControl
0172             property: root.axis == Qt.YAxis ? "y" : "x"
0173             to: 0
0174             duration: Kirigami.Units.longDuration
0175             easing.type: Easing.InOutQuad
0176         }
0177         PropertyAction {
0178             target: root.target
0179             property: "raised"
0180             value: false
0181         }
0182     }
0183 }