2020-11-30 17:03:52 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import "."
|
2021-09-28 15:04:06 +00:00
|
|
|
|
|
|
|
import utils 1.0
|
2020-11-30 17:03:52 +00:00
|
|
|
|
|
|
|
Item {
|
2021-03-29 12:28:41 +00:00
|
|
|
id: root
|
|
|
|
property Image image
|
2020-11-30 17:03:52 +00:00
|
|
|
property alias selectorRectangle: selectorRectangle
|
2021-03-05 16:19:40 +00:00
|
|
|
property string ratio: ""
|
|
|
|
property var splitRatio: !!ratio ? ratio.split(":") : null
|
|
|
|
property int widthRatio: !!ratio ? parseInt(splitRatio[0]) : -1
|
|
|
|
property int heightRatio: !!ratio ? parseInt(splitRatio[1]) : -1
|
|
|
|
property bool settingCorners: false
|
|
|
|
property int draggedCorner: 0
|
2021-03-29 12:28:41 +00:00
|
|
|
property bool ready: false
|
2021-03-05 16:19:40 +00:00
|
|
|
|
|
|
|
readonly property int topLeft: 0
|
|
|
|
readonly property int topRight: 1
|
|
|
|
readonly property int bottomLeft: 2
|
|
|
|
readonly property int bottomRight: 3
|
2020-11-30 17:03:52 +00:00
|
|
|
|
|
|
|
width: image.width
|
|
|
|
height: image.height
|
|
|
|
|
|
|
|
Rectangle {
|
|
|
|
id: selectorRectangle
|
|
|
|
visible: false
|
|
|
|
x: 0
|
|
|
|
y: 0
|
|
|
|
border.width: 2
|
|
|
|
border.color: Style.current.orange
|
|
|
|
color: Style.current.transparent
|
|
|
|
|
2021-03-05 16:19:40 +00:00
|
|
|
function fitRatio(makeBigger) {
|
|
|
|
if (!!ratio) {
|
|
|
|
if ((makeBigger && selectorRectangle.width < selectorRectangle.height) || (!makeBigger && selectorRectangle.width > selectorRectangle.height)) {
|
|
|
|
selectorRectangle.width = (selectorRectangle.height/heightRatio) * widthRatio
|
|
|
|
} else {
|
|
|
|
selectorRectangle.height = (selectorRectangle.width/widthRatio) * heightRatio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:03:52 +00:00
|
|
|
function initialSetup() {
|
2021-03-05 16:19:40 +00:00
|
|
|
selectorRectangle.width = image.width
|
|
|
|
selectorRectangle.height = image.height
|
|
|
|
|
|
|
|
fitRatio()
|
2020-11-30 17:03:52 +00:00
|
|
|
topLeftCorner.x = 0
|
|
|
|
topLeftCorner.y = 0
|
2021-03-05 16:19:40 +00:00
|
|
|
topRightCorner.x = selectorRectangle.width - topRightCorner.width
|
2020-11-30 17:03:52 +00:00
|
|
|
topRightCorner.y = 0
|
|
|
|
bottomLeftCorner.x = 0
|
2021-03-05 16:19:40 +00:00
|
|
|
bottomLeftCorner.y = selectorRectangle.height - topRightCorner.height
|
|
|
|
bottomRightCorner.x = selectorRectangle.width - topRightCorner.width
|
|
|
|
bottomRightCorner.y = selectorRectangle.height - topRightCorner.height
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:19:40 +00:00
|
|
|
|
2020-11-30 17:03:52 +00:00
|
|
|
function adjustRectangleSize() {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (!selectorRectangle.visible) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:03:52 +00:00
|
|
|
selectorRectangle.width = bottomRightCorner.x + bottomRightCorner.width - topLeftCorner.x
|
|
|
|
selectorRectangle.height = bottomRightCorner.y + bottomRightCorner.height - topLeftCorner.y
|
|
|
|
selectorRectangle.x = topLeftCorner.x
|
|
|
|
selectorRectangle.y = topLeftCorner.y
|
2021-03-05 16:19:40 +00:00
|
|
|
|
|
|
|
if (!!ratio) {
|
|
|
|
// FIXME with a ratio that is not 1:1, the rectangle can go out of bounds
|
|
|
|
fitRatio()
|
|
|
|
|
|
|
|
switch(draggedCorner) {
|
|
|
|
case topLeft:
|
|
|
|
selectorRectangle.x = topLeftCorner.x
|
|
|
|
selectorRectangle.y = topLeftCorner.y
|
|
|
|
break
|
|
|
|
case topRight:
|
|
|
|
selectorRectangle.x = topRightCorner.x - selectorRectangle.width + topRightCorner.width
|
|
|
|
selectorRectangle.y = topRightCorner.y
|
|
|
|
break
|
|
|
|
case bottomLeft:
|
|
|
|
selectorRectangle.x = bottomLeftCorner.x
|
|
|
|
selectorRectangle.y = bottomLeftCorner.y - selectorRectangle.height + bottomLeftCorner.height
|
|
|
|
break
|
|
|
|
case bottomRight:
|
|
|
|
selectorRectangle.x = bottomRightCorner.x - selectorRectangle.width + bottomRightCorner.width
|
|
|
|
selectorRectangle.y = bottomRightCorner.y - selectorRectangle.height + bottomRightCorner.height
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Connections {
|
|
|
|
target: image
|
|
|
|
onStatusChanged: {
|
|
|
|
if (image.status === Image.Ready) {
|
|
|
|
selectorRectangle.initialSetup()
|
|
|
|
selectorRectangle.visible = true
|
2021-03-29 12:28:41 +00:00
|
|
|
root.ready = true
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 16:19:40 +00:00
|
|
|
function putCorners() {
|
|
|
|
settingCorners = true
|
|
|
|
|
|
|
|
topLeftCorner.x = selectorRectangle.x
|
|
|
|
topLeftCorner.y = selectorRectangle.y
|
|
|
|
topRightCorner.x = selectorRectangle.x + selectorRectangle.width - topRightCorner.width
|
|
|
|
topRightCorner.y = selectorRectangle.y
|
|
|
|
bottomLeftCorner.x = selectorRectangle.x
|
|
|
|
bottomLeftCorner.y = selectorRectangle.y + selectorRectangle.height - topRightCorner.height
|
|
|
|
bottomRightCorner.x = selectorRectangle.x + selectorRectangle.width - topRightCorner.width
|
|
|
|
bottomRightCorner.y = selectorRectangle.y + selectorRectangle.height - topRightCorner.height
|
|
|
|
|
|
|
|
settingCorners = false
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:03:52 +00:00
|
|
|
|
|
|
|
// Size calculations are only done on top-left and bottom-right, because the other two corners follow them
|
|
|
|
CropCornerRectangle {
|
|
|
|
id: topLeftCorner
|
|
|
|
onXChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
if (x < 0) x = 0
|
|
|
|
if (x > topRightCorner.x - width) x = topRightCorner.x - width
|
|
|
|
|
|
|
|
bottomLeftCorner.x = x
|
|
|
|
selectorRectangle.adjustRectangleSize()
|
|
|
|
}
|
|
|
|
onYChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
if (y < 0) y = 0
|
|
|
|
if (y > bottomRightCorner.y - height) y = bottomRightCorner.y - height
|
|
|
|
|
|
|
|
topRightCorner.y = y
|
|
|
|
selectorRectangle.adjustRectangleSize()
|
|
|
|
}
|
2021-03-05 16:19:40 +00:00
|
|
|
onPressed: {
|
|
|
|
draggedCorner = topLeft
|
|
|
|
}
|
|
|
|
|
|
|
|
onReleased: {
|
|
|
|
putCorners()
|
|
|
|
}
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CropCornerRectangle {
|
|
|
|
id: topRightCorner
|
|
|
|
onXChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
bottomRightCorner.x = x
|
|
|
|
}
|
|
|
|
onYChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
topLeftCorner.y = y
|
|
|
|
}
|
2021-03-05 16:19:40 +00:00
|
|
|
onPressed: {
|
|
|
|
draggedCorner = topRight
|
|
|
|
}
|
|
|
|
onReleased: {
|
|
|
|
putCorners()
|
|
|
|
}
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CropCornerRectangle {
|
|
|
|
id: bottomLeftCorner
|
|
|
|
onXChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
topLeftCorner.x = x
|
|
|
|
}
|
|
|
|
onYChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
bottomRightCorner.y = y
|
|
|
|
}
|
2021-03-05 16:19:40 +00:00
|
|
|
onPressed: {
|
|
|
|
draggedCorner = bottomLeft
|
|
|
|
}
|
|
|
|
onReleased: {
|
|
|
|
putCorners()
|
|
|
|
}
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CropCornerRectangle {
|
|
|
|
id: bottomRightCorner
|
|
|
|
onXChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
if (x < topLeftCorner.x + topLeftCorner.width) x = topLeftCorner.x + topLeftCorner.width
|
|
|
|
if (x > image.width - width) x = image.width - width
|
|
|
|
topRightCorner.x = x
|
2021-03-05 16:19:40 +00:00
|
|
|
|
2020-11-30 17:03:52 +00:00
|
|
|
selectorRectangle.adjustRectangleSize()
|
|
|
|
}
|
|
|
|
onYChanged: {
|
2021-03-05 16:19:40 +00:00
|
|
|
if (settingCorners) return
|
2020-11-30 17:03:52 +00:00
|
|
|
if (y < topRightCorner.y + topRightCorner.height) y = topRightCorner.y + topRightCorner.height
|
|
|
|
if (y > image.height - height) y = image.height - height
|
|
|
|
bottomLeftCorner.y = y
|
2021-03-05 16:19:40 +00:00
|
|
|
|
2020-11-30 17:03:52 +00:00
|
|
|
selectorRectangle.adjustRectangleSize()
|
|
|
|
}
|
2021-03-05 16:19:40 +00:00
|
|
|
onPressed: {
|
|
|
|
draggedCorner = bottomRight
|
|
|
|
}
|
|
|
|
onReleased: {
|
|
|
|
putCorners()
|
|
|
|
}
|
2020-11-30 17:03:52 +00:00
|
|
|
}
|
|
|
|
}
|