4 * This file is part of CameraPlus.
6 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 // TODO: hide all controls when we are focusing
28 // TODO: hide all controls when we are dragging
34 width: renderArea.width
35 height: renderArea.height
38 drag.maximumX: width - reticle.width
39 drag.maximumY: height - reticle.height
41 property variant videoResolution
42 property variant renderArea
44 property bool locked: false
46 property int cafStatus: AutoFocus.None
47 property int status: AutoFocus.None
49 property bool touchMode
51 property variant touchPoint: Qt.point(mouse.width / 2, mouse.height / 2)
53 // A 100x100 central "rectangle"
54 property variant centerRect: Qt.rect((mouse.width / 2 - 50), (mouse.height / 2) - 50, 100, 100)
57 property variant primaryRoiRect: Qt.rect(0, 0, 0, 0)
58 property variant roiRects
59 property variant allRoiRects
60 property bool roiMode: allRoiRects != null && allRoiRects.length > 0 && !touchMode && !pressed
62 property variant __initialPos
65 if (mouse.x >= reticle.x &&
66 mouse.y >= reticle.y &&
67 mouse.x <= reticle.x + reticle.width &&
68 mouse.y <= reticle.y + reticle.height) {
72 __initialPos = touchPoint
73 calculateTouchPoint(mouse.x, mouse.y)
77 calculateTouchPoint(mouse.x, mouse.y)
81 onPositionChanged: calculateTouchPoint(mouse.x, mouse.y)
83 calculateTouchPoint(__initialPos.x, __initialPos.y)
87 function resetReticle() {
88 calculateTouchPoint(centerRect.x, centerRect.y)
91 function setRegionOfInterest() {
93 // console.log("Cannot set ROI without camera object")
95 } else if (mouse.pressed) {
96 // console.log("Will not set ROI while pressed")
98 } else if (!touchMode && !roiMode) {
99 // console.log("resetting ROI")
101 cam.roi.resetRegionOfInterest()
107 // TODO: rework this and move to unnormalized coordinates
108 // in terms of video resolution:
109 var rx = (videoResolution.width * reticle.x) / mouse.width
110 var rwidth = (videoResolution.width * reticle.width) / mouse.width
111 var ry = (videoResolution.height * reticle.y) / mouse.height
112 var rheight = (videoResolution.height * reticle.height) / mouse.height
114 // Translate to normalized coordinates (1x1 square) as expected by our C++ backend
115 rx = rx / videoResolution.width
116 rwidth = rwidth / videoResolution.width
117 ry = ry / videoResolution.height
118 rheight = rheight / videoResolution.height
120 // console.log("Setting ROI to: " + rx + "x" + ry)
121 cam.roi.setRegionOfInterest(Qt.rect(rx, ry, rwidth, rheight))
124 function calculateTouchPoint(x, y) {
125 if (x >= centerRect.x && y >= centerRect.y &&
126 x <= centerRect.x + centerRect.width &&
127 y <= centerRect.y + centerRect.height) {
129 touchPoint = Qt.point(mouse.width / 2, mouse.height / 2)
134 touchPoint = Qt.point(x, y)
137 function predictColor(caf, status) {
138 if (status == AutoFocus.Success) {
140 } else if (status == AutoFocus.Fail) {
142 } else if (status == AutoFocus.Running) {
144 } else if (caf == AutoFocus.Success) {
153 model: roiMode ? roiRects : 0
155 delegate: Rectangle {
158 width: modelData.width
159 height: modelData.height
168 width: mouse.pressed ? 150 : mouse.touchMode ? 200 : roiMode ? primaryRoiRect.width : 250
169 height: mouse.pressed ? 90 : mouse.touchMode ? 120 : roiMode ? primaryRoiRect.height : 150
170 x: Math.min(Math.max(mouse.touchPoint.x - (width / 2), drag.minimumX), drag.maximumX)
171 y: Math.min(Math.max(mouse.touchPoint.y - (height / 2), drag.minimumY), drag.maximumY)
172 color: predictColor(cafStatus, status)
174 onXChanged: setRegionOfInterest()
175 onYChanged: setRegionOfInterest()
178 PropertyAnimation { duration: 100 }
179 enabled: !mouse.pressed
183 PropertyAnimation { duration: 100 }
184 enabled: !mouse.pressed
188 PropertyAnimation { duration: 100 }
192 PropertyAnimation { duration: 100 }
199 // Changing mode (which implies changing pages) will not reset ROI
200 // thus we do it here
201 onModeChanged: resetReticle()
206 onRunningChanged: resetReticle()
207 onVideoResolutionChanged: resetReticle()
213 allRoiRects = regions
214 primaryRoiRect = primary
217 if (regions.length == 0) {
222 touchPoint = Qt.point(primary.x + (reticle.width / 2),
223 primary.y + (reticle.height / 2))
228 // This is for debugging
240 width: centerRect.width
241 height: centerRect.height
246 running: status == AutoFocus.Running
247 triggeredOnStart: true
249 onTriggered: reticle.visible = !reticle.visible
252 reticle.visible = true