1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qdeclarativecamera_p.h"
41#include "qdeclarativecamerafocus_p.h"
42
43QT_BEGIN_NAMESPACE
44
45/*!
46 \qmltype CameraFocus
47 \instantiates QDeclarativeCameraFocus
48 \inqmlmodule QtMultimedia
49 \brief An interface for focus related camera settings.
50 \ingroup multimedia_qml
51 \ingroup camera_qml
52
53 This type allows control over manual and automatic
54 focus settings, including information about any parts of the
55 camera frame that are selected for autofocusing.
56
57 It should not be constructed separately, instead the
58 \c focus property of a \l Camera should be used.
59
60 \qml
61
62 Item {
63 width: 640
64 height: 360
65
66 Camera {
67 id: camera
68
69 focus {
70 focusMode: Camera.FocusMacro
71 focusPointMode: Camera.FocusPointCustom
72 customFocusPoint: Qt.point(0.2, 0.2) // Focus relative to top-left corner
73 }
74 }
75
76 VideoOutput {
77 source: camera
78 anchors.fill: parent
79 }
80 }
81
82 \endqml
83*/
84
85/*!
86 \class QDeclarativeCameraFocus
87 \internal
88 \brief An interface for focus related camera settings.
89*/
90
91/*!
92 Construct a declarative camera focus object using \a parent object.
93 */
94
95QDeclarativeCameraFocus::QDeclarativeCameraFocus(QCamera *camera, QObject *parent) :
96 QObject(parent)
97{
98 m_focus = camera->focus();
99 m_focusZones = new FocusZonesModel(this);
100
101 updateFocusZones();
102
103 connect(asender: m_focus, SIGNAL(focusZonesChanged()), SLOT(updateFocusZones()));
104 connect(sender: camera, signal: &QCamera::statusChanged, slot: [this](QCamera::Status status) {
105 if (status != QCamera::UnloadedStatus && status != QCamera::LoadedStatus
106 && status != QCamera::ActiveStatus) {
107 return;
108 }
109
110 emit supportedFocusModesChanged();
111 emit supportedFocusPointModesChanged();
112 });
113}
114
115QDeclarativeCameraFocus::~QDeclarativeCameraFocus()
116{
117}
118/*!
119 \property QDeclarativeCameraFocus::focusMode
120
121 This property holds the current camera focus mode.
122
123 It's possible to combine multiple QCameraFocus::FocusMode enum values,
124 for example QCameraFocus::MacroFocus + QCameraFocus::ContinuousFocus.
125
126 In automatic focusing modes, the \l focusPointMode
127 and \l focusZones properties provide information and control
128 over how automatic focusing is performed.
129*/
130
131/*!
132 \qmlproperty enumeration CameraFocus::focusMode
133
134 This property holds the current camera focus mode, which can be one of the following values:
135
136 \table
137 \header
138 \li Value
139 \li Description
140 \row
141 \li FocusManual
142 \li Manual or fixed focus mode.
143 \row
144 \li FocusHyperfocal
145 \li Focus to hyperfocal distance, with the maximum depth of field achieved. All objects at distances from half of this distance out to infinity will be acceptably sharp.
146 \row
147 \li FocusInfinity
148 \li Focus strictly to infinity.
149 \row
150 \li FocusAuto
151 \li One-shot auto focus mode.
152 \row
153 \li FocusContinuous
154 \li Continuous auto focus mode.
155 \row
156 \li FocusMacro
157 \li One shot auto focus to objects close to camera.
158 \endtable
159
160 It's possible to combine multiple Camera::FocusMode values,
161 for example Camera.FocusMacro + Camera.FocusContinuous.
162
163 In automatic focusing modes, the \l focusPointMode property
164 and \l focusZones property provide information and control
165 over how automatic focusing is performed.
166*/
167QDeclarativeCameraFocus::FocusMode QDeclarativeCameraFocus::focusMode() const
168{
169 return QDeclarativeCameraFocus::FocusMode(int(m_focus->focusMode()));
170}
171
172/*!
173 \qmlproperty list<FocusMode> CameraFocus::supportedFocusModes
174
175 This property holds the supported focus modes of the camera.
176
177 \since 5.11
178 \sa focusMode
179*/
180QVariantList QDeclarativeCameraFocus::supportedFocusModes() const
181{
182 QVariantList supportedModes;
183
184 for (int i = int(FocusManual); i <= int(FocusMacro); ++i) {
185 if (m_focus->isFocusModeSupported(mode: (QCameraFocus::FocusMode) i))
186 supportedModes.append(t: i);
187 }
188
189 return supportedModes;
190}
191
192#if QT_DEPRECATED_SINCE(5, 11)
193/*!
194 \qmlmethod bool QtMultimedia::CameraFocus::isFocusModeSupported(mode) const
195 \obsolete
196
197 Returns true if the supplied \a mode is a supported focus mode, and
198 false otherwise.
199*/
200bool QDeclarativeCameraFocus::isFocusModeSupported(QDeclarativeCameraFocus::FocusMode mode) const
201{
202 return m_focus->isFocusModeSupported(mode: QCameraFocus::FocusModes(int(mode)));
203}
204#endif
205
206void QDeclarativeCameraFocus::setFocusMode(QDeclarativeCameraFocus::FocusMode mode)
207{
208 if (mode != focusMode()) {
209 m_focus->setFocusMode(QCameraFocus::FocusModes(int(mode)));
210 emit focusModeChanged(focusMode());
211 }
212}
213/*!
214 \property QDeclarativeCameraFocus::focusPointMode
215
216 This property holds the current camera focus point mode. It is used in
217 automatic focusing modes to determine what to focus on.
218
219 If the current focus point mode is \l QCameraFocus::FocusPointCustom, the
220 \l customFocusPoint property allows you to specify which part of
221 the frame to focus on.
222*/
223/*!
224 \qmlproperty enumeration CameraFocus::focusPointMode
225
226 This property holds the current camera focus point mode. It is used in automatic
227 focusing modes to determine what to focus on. If the current
228 focus point mode is \c Camera.FocusPointCustom, the \l customFocusPoint
229 property allows you to specify which part of the frame to focus on.
230
231 The property can take one of the following values:
232 \table
233 \header
234 \li Value
235 \li Description
236 \row
237 \li FocusPointAuto
238 \li Automatically select one or multiple focus points.
239 \row
240 \li FocusPointCenter
241 \li Focus to the frame center.
242 \row
243 \li FocusPointFaceDetection
244 \li Focus on faces in the frame.
245 \row
246 \li FocusPointCustom
247 \li Focus to the custom point, defined by the customFocusPoint property.
248 \endtable
249*/
250QDeclarativeCameraFocus::FocusPointMode QDeclarativeCameraFocus::focusPointMode() const
251{
252 return QDeclarativeCameraFocus::FocusPointMode(m_focus->focusPointMode());
253}
254
255/*!
256 \qmlproperty list<enumeration> CameraFocus::supportedFocusPointModes
257
258 This property holds the supported focus point modes of the camera.
259
260 \since 5.10
261 \sa focusPointMode
262*/
263QVariantList QDeclarativeCameraFocus::supportedFocusPointModes() const
264{
265 QVariantList supportedModes;
266
267 for (int i = int(FocusPointAuto); i <= int(FocusPointCustom); i++) {
268 if (m_focus->isFocusPointModeSupported(QCameraFocus::FocusPointMode(i)))
269 supportedModes.append(t: i);
270 }
271
272 return supportedModes;
273}
274
275void QDeclarativeCameraFocus::setFocusPointMode(QDeclarativeCameraFocus::FocusPointMode mode)
276{
277 if (mode != focusPointMode()) {
278 m_focus->setFocusPointMode(QCameraFocus::FocusPointMode(mode));
279 emit focusPointModeChanged(focusPointMode());
280 }
281}
282
283#if QT_DEPRECATED_SINCE(5, 10)
284/*!
285 \qmlmethod bool QtMultimedia::CameraFocus::isFocusPointModeSupported(mode) const
286 \obsolete
287
288 Returns true if the supplied \a mode is a supported focus point mode, and
289 false otherwise.
290*/
291bool QDeclarativeCameraFocus::isFocusPointModeSupported(QDeclarativeCameraFocus::FocusPointMode mode) const
292{
293 return m_focus->isFocusPointModeSupported(QCameraFocus::FocusPointMode(mode));
294}
295#endif
296
297/*!
298 \property QDeclarativeCameraFocus::customFocusPoint
299
300 This property holds the position of the custom focus point in relative
301 frame coordinates. For example, QPointF(0,0) pointing to the left top corner of the frame, and QPointF(0.5,0.5)
302 pointing to the center of the frame.
303
304 Custom focus point is used only in QCameraFocus::FocusPointCustom focus mode.
305*/
306
307/*!
308 \qmlproperty point QtMultimedia::CameraFocus::customFocusPoint
309
310 This property holds the position of custom focus point, in relative frame coordinates:
311 QPointF(0,0) points to the left top frame point, QPointF(0.5,0.5)
312 points to the frame center.
313
314 Custom focus point is used only in FocusPointCustom focus mode.
315*/
316
317QPointF QDeclarativeCameraFocus::customFocusPoint() const
318{
319 return m_focus->customFocusPoint();
320}
321
322void QDeclarativeCameraFocus::setCustomFocusPoint(const QPointF &point)
323{
324 if (point != customFocusPoint()) {
325 m_focus->setCustomFocusPoint(point);
326 emit customFocusPointChanged(customFocusPoint());
327 }
328}
329/*!
330 \property QDeclarativeCameraFocus::focusZones
331
332 This property holds the list of current camera focus zones,
333 each including \c area specified in the same coordinates as \l customFocusPoint, and zone \c status as one of the following values:
334 \table
335 \header \li Value \li Description
336 \row \li QCameraFocusZone::Unused \li This focus point area is currently unused in autofocusing.
337 \row \li QCameraFocusZone::Selected \li This focus point area is used in autofocusing, but is not in focus.
338 \row \li QCameraFocusZone::Focused \li This focus point is used in autofocusing, and is in focus.
339 \endtable
340*/
341/*!
342 \qmlproperty list<focusZone> QtMultimedia::CameraFocus::focusZones
343
344 This property holds the list of current camera focus zones,
345 each including \c area specified in the same coordinates as \l customFocusPoint,
346 and zone \c status as one of the following values:
347
348 \table
349 \header \li Value \li Description
350 \row \li Camera.FocusAreaUnused \li This focus point area is currently unused in autofocusing.
351 \row \li Camera.FocusAreaSelected \li This focus point area is used in autofocusing, but is not in focus.
352 \row \li Camera.FocusAreaFocused \li This focus point is used in autofocusing, and is in focus.
353 \endtable
354
355 \qml
356
357 VideoOutput {
358 id: viewfinder
359 source: camera
360
361 //display focus areas on camera viewfinder:
362 Repeater {
363 model: camera.focus.focusZones
364
365 Rectangle {
366 border {
367 width: 2
368 color: status == Camera.FocusAreaFocused ? "green" : "white"
369 }
370 color: "transparent"
371
372 // Map from the relative, normalized frame coordinates
373 property variant mappedRect: viewfinder.mapNormalizedRectToItem(area);
374
375 x: mappedRect.x
376 y: mappedRect.y
377 width: mappedRect.width
378 height: mappedRect.height
379 }
380 }
381 }
382 \endqml
383*/
384
385QAbstractListModel *QDeclarativeCameraFocus::focusZones() const
386{
387 return m_focusZones;
388}
389
390/*! \internal */
391void QDeclarativeCameraFocus::updateFocusZones()
392{
393 m_focusZones->setFocusZones(m_focus->focusZones());
394}
395
396
397FocusZonesModel::FocusZonesModel(QObject *parent)
398 :QAbstractListModel(parent)
399{
400}
401
402int FocusZonesModel::rowCount(const QModelIndex &parent) const
403{
404 if (parent == QModelIndex())
405 return m_focusZones.count();
406
407 return 0;
408}
409
410QVariant FocusZonesModel::data(const QModelIndex &index, int role) const
411{
412 if (index.row() < 0 || index.row() > m_focusZones.count())
413 return QVariant();
414
415 QCameraFocusZone zone = m_focusZones.value(i: index.row());
416
417 if (role == StatusRole)
418 return zone.status();
419
420 if (role == AreaRole)
421 return zone.area();
422
423 return QVariant();
424}
425
426QHash<int,QByteArray> FocusZonesModel::roleNames() const
427{
428 return {{StatusRole, QByteArrayLiteral("status")},
429 {AreaRole, QByteArrayLiteral("area")}};
430}
431
432void FocusZonesModel::setFocusZones(const QCameraFocusZoneList &zones)
433{
434 beginResetModel();
435 m_focusZones = zones;
436 endResetModel();
437}
438
439QT_END_NAMESPACE
440
441#include "moc_qdeclarativecamerafocus_p.cpp"
442

source code of qtmultimedia/src/imports/multimedia/qdeclarativecamerafocus.cpp