+// TODO: hide all controls when we are focusing
+// TODO: hide all controls when we are dragging
+
+MouseArea {
+ id: mouse
+ x: renderArea.x
+ y: renderArea.y
+ width: renderArea.width
+ height: renderArea.height
+ drag.minimumX: 0
+ drag.minimumY: 0
+ drag.maximumX: width - reticle.width
+ drag.maximumY: height - reticle.height
+
+ property variant videoResolution
+ property variant renderArea
+
+ property bool locked: false
+
+ property int cafStatus: AutoFocus.None
+ property int status: AutoFocus.None
+ property Camera cam
+ property bool touchMode
+
+ property variant touchPoint: Qt.point(mouse.width / 2, mouse.height / 2)
+
+ // A 100x100 central "rectangle"
+ property variant centerRect: Qt.rect((mouse.width / 2 - 50), (mouse.height / 2) - 50, 100, 100)
+
+ // ROI:
+ property variant primaryRoiRect: Qt.rect(0, 0, 0, 0)
+ property variant roiRects
+ property variant allRoiRects
+ property bool roiMode: allRoiRects != null && allRoiRects.length > 0 && !touchMode && !pressed
+
+ property variant __initialPos
+ onPressed: {
+
+ if (mouse.x >= reticle.x &&
+ mouse.y >= reticle.y &&
+ mouse.x <= reticle.x + reticle.width &&
+ mouse.y <= reticle.y + reticle.height) {
+ locked = true
+ }
+
+ __initialPos = touchPoint
+ calculateTouchPoint(mouse.x, mouse.y)
+ }
+
+ onReleased: {
+ calculateTouchPoint(mouse.x, mouse.y)
+ locked = false
+ }
+
+ onPositionChanged: calculateTouchPoint(mouse.x, mouse.y)
+ onCanceled: {
+ calculateTouchPoint(__initialPos.x, __initialPos.y)
+ locked = false
+ }
+
+ function resetReticle() {
+ calculateTouchPoint(centerRect.x, centerRect.y)
+ }
+
+ function setRegionOfInterest() {
+ if (!cam) {
+ // console.log("Cannot set ROI without camera object")
+ return
+ } else if (mouse.pressed) {
+ // console.log("Will not set ROI while pressed")
+ return
+ } else if (!touchMode && !roiMode) {
+ // console.log("resetting ROI")
+ if (cam.roi) {
+ cam.roi.resetRegionOfInterest()
+ }
+
+ return
+ }
+
+ // TODO: rework this and move to unnormalized coordinates
+ // in terms of video resolution:
+ var rx = (videoResolution.width * reticle.x) / mouse.width
+ var rwidth = (videoResolution.width * reticle.width) / mouse.width
+ var ry = (videoResolution.height * reticle.y) / mouse.height
+ var rheight = (videoResolution.height * reticle.height) / mouse.height
+
+ // Translate to normalized coordinates (1x1 square) as expected by our C++ backend
+ rx = rx / videoResolution.width
+ rwidth = rwidth / videoResolution.width
+ ry = ry / videoResolution.height
+ rheight = rheight / videoResolution.height
+
+ // console.log("Setting ROI to: " + rx + "x" + ry)
+ cam.roi.setRegionOfInterest(Qt.rect(rx, ry, rwidth, rheight))
+ }
+
+ function calculateTouchPoint(x, y) {
+ if (x >= centerRect.x && y >= centerRect.y &&
+ x <= centerRect.x + centerRect.width &&
+ y <= centerRect.y + centerRect.height) {
+ touchMode = false
+ touchPoint = Qt.point(mouse.width / 2, mouse.height / 2)
+ return
+ }
+
+ touchMode = true
+ touchPoint = Qt.point(x, y)
+ }
+
+ function predictColor(caf, status) {
+ if (status == AutoFocus.Success) {
+ return "steelblue"
+ } else if (status == AutoFocus.Fail) {
+ return "red"
+ } else if (status == AutoFocus.Running) {
+ return "white"
+ } else if (caf == AutoFocus.Success) {
+ return "steelblue"
+ } else {
+ return "white"
+ }
+ }
+
+ Repeater {