diff --git a/qml/DragRect.qml b/qml/DragRect.qml new file mode 100644 index 0000000..9e5098e --- /dev/null +++ b/qml/DragRect.qml @@ -0,0 +1,40 @@ +import QtQuick 2.9 +import QtQuick.Controls 2.0 +Item { + id: root + property alias containsMouse: mouseArea.containsMouse + signal posChange(int xOffset, int yOffset) + implicitWidth: 12 //这里隐式的宽为12 + implicitHeight: 12 //这里隐式的高为12 + property int posType: Qt.ArrowCursor + //5.10之前, qml是不能定义枚举的,用只读的int属性代替一下。 + readonly property int posLeftTop: Qt.SizeFDiagCursor + readonly property int posLeftBottom: Qt.SizeBDiagCursor + readonly property int posRightTop: Qt.SizeBDiagCursor + readonly property int posRightBottom: Qt.SizeFDiagCursor + MouseArea { + id: mouseArea + anchors.fill: parent + hoverEnabled: true + property int lastX: 0 + property int lastY: 0 + onContainsMouseChanged: { + if (containsMouse) { + cursorShape = posType; + } else { + cursorShape = Qt.ArrowCursor; + } + } + onPressedChanged: { + if (containsPress) { + lastX = mouseX; + lastY = mouseY; + } + } + onPositionChanged: { + if (pressed) { + posChange(mouseX - lastX, mouseY - lastY) + } + } + } +} \ No newline at end of file diff --git a/qml/MoveArea.qml b/qml/MoveArea.qml new file mode 100644 index 0000000..186b1a9 --- /dev/null +++ b/qml/MoveArea.qml @@ -0,0 +1,29 @@ +import QtQuick 2.9 + +MouseArea { + id: root + + property real lastX: 0 + property real lastY: 0 + property bool mask: false //有时候外面需要屏蔽拖动,导出一个mask属性, 默认false。 + property var control: parent //导出一个control属性,指定要拖动的目标, 默认就用parent好了。注意目标要有x和y属性并且可修改 + + onPressed: { + lastX = mouseX; + lastY = mouseY; + } + onContainsMouseChanged: { //修改一下鼠标样式,以示区别 + if (containsMouse) { + cursorShape = Qt.SizeAllCursor; + } else { + cursorShape = Qt.ArrowCursor; + } + } + onPositionChanged: { + if (!mask && pressed && control) + { + control.x +=mouseX - lastX + control.y +=mouseY - lastY + } + } +} \ No newline at end of file diff --git a/qml/ResizeBorder.qml b/qml/ResizeBorder.qml new file mode 100644 index 0000000..dd18351 --- /dev/null +++ b/qml/ResizeBorder.qml @@ -0,0 +1,62 @@ +import QtQuick 2.7 +import FishUI 1.0 as FishUI + +Rectangle { + id: root + color: "transparent" + border.width: 2 + border.color: FishUI.Theme.highlightColor + width: parent.width + height: parent.height + property var control: parent + DragRect { + posType: posLeftTop + onPosChange: { + //不要简化这个判断条件,至少让以后维护的人能看懂。化简过后我自己都看不懂了。 + if (control.x + xOffset < control.x + control.width) + control.x += xOffset; + if (control.y + yOffset < control.y + control.height) + control.y += yOffset; + if (control.width - xOffset > 0) + control.width-= xOffset; + if (control.height -yOffset > 0) + control.height -= yOffset; + } + } + DragRect { + posType: posRightTop + x: parent.width - width + onPosChange: { + //向左拖动时,xOffset为负数 + if (control.width + xOffset > 0) + control.width += xOffset; + if (control.height - yOffset > 0) + control.height -= yOffset; + if (control.y + yOffset < control.y + control.height) + control.y += yOffset; + } + } + DragRect { + posType: posLeftBottom + y: parent.height - height + onPosChange: { + if (control.x + xOffset < control.x + control.width) + control.x += xOffset; + if (control.width - xOffset > 0) + control.width-= xOffset; + if (control.height + yOffset > 0) + control.height += yOffset; + } + } + DragRect { + posType: posRightBottom + x: parent.width - width + y: parent.height - height + onPosChange: { + if (control.width + xOffset > 0) + control.width += xOffset; + if (control.height + yOffset > 0) + control.height += yOffset; + } + } +} \ No newline at end of file diff --git a/qml/main.qml b/qml/main.qml index 28ad70c..d84a5dc 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -123,7 +123,7 @@ Item { property int newX: 0 property int newY: 0 - + property var control: parent z: 999 height: 0 width: 0 @@ -131,7 +131,6 @@ Item { y: 0 visible: false clip: true - function reset() { selectLayer.x = 0 selectLayer.y = 0 @@ -151,31 +150,14 @@ Item { y: -selectLayer.y } - Rectangle { + MoveArea { anchors.fill: parent - color: "transparent" - border.width: 2 - border.color: FishUI.Theme.highlightColor - } - - DragHandler { - target: selectLayer - - xAxis.enabled: true - xAxis.minimum: control.x - xAxis.maximum: control.width - selectLayer.width - - yAxis.enabled: true - yAxis.minimum: control.y - yAxis.maximum: control.height - selectLayer.height + control: parent } - MouseArea { - id: selectLayerMouseArea + ResizeBorder { + control: parent anchors.fill: parent - cursorShape: Qt.SizeAllCursor - acceptedButtons: Qt.LeftButton - onDoubleClicked: control.save() } } @@ -328,4 +310,4 @@ Item { } } } -} +} \ No newline at end of file