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 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 <QDebug>
41#include <QMediaService>
42
43#include "qdeclarativetorch_p.h"
44
45QT_BEGIN_NAMESPACE
46
47/*!
48 \qmltype Torch
49 \instantiates QDeclarativeTorch
50 \inqmlmodule QtMultimedia
51 \brief Simple control over torch functionality.
52
53 \ingroup multimedia_qml
54
55 In many cases the torch hardware is shared with camera flash functionality,
56 and might be automatically controlled by the device. You have control over
57 the power level (of course, higher power levels are brighter but reduce
58 battery life significantly).
59
60 \qml
61
62 Torch {
63 power: 75 // 75% of full power
64 enabled: true // On
65 }
66
67 \endqml
68*/
69QDeclarativeTorch::QDeclarativeTorch(QObject *parent)
70 : QObject(parent)
71{
72 m_camera = new QCamera(this);
73 QMediaService *service = m_camera->service();
74
75 m_exposure = service ? service->requestControl<QCameraExposureControl*>() : 0;
76 m_flash = service ? service->requestControl<QCameraFlashControl*>() : 0;
77
78 if (m_exposure)
79 connect(asender: m_exposure, SIGNAL(actualValueChanged(int)), SLOT(parameterChanged(int)));
80
81 // XXX There's no signal for flash mode changed
82}
83
84QDeclarativeTorch::~QDeclarativeTorch()
85{
86}
87
88/*!
89 \qmlproperty bool QtMultimedia::Torch::enabled
90
91 This property indicates whether the torch is enabled. If the torch functionality is shared
92 with the camera flash hardware, the camera will take priority
93 over torch settings and the torch is disabled.
94*/
95/*!
96 \property QDeclarativeTorch::enabled
97
98 This property indicates whether the torch is enabled. If the torch functionality
99 is shared with the camera flash hardware, the camera will take
100 priority and the torch is disabled.
101 */
102bool QDeclarativeTorch::enabled() const
103{
104 if (!m_flash)
105 return false;
106
107 return m_flash->flashMode() & QCameraExposure::FlashTorch;
108}
109
110
111/*!
112 If \a on is true, attempt to turn on the torch.
113 If the torch hardware is already in use by the camera, this will
114 be ignored.
115*/
116void QDeclarativeTorch::setEnabled(bool on)
117{
118 if (!m_flash)
119 return;
120
121 QCameraExposure::FlashModes mode = m_flash->flashMode();
122
123 if (mode & QCameraExposure::FlashTorch) {
124 if (!on) {
125 m_flash->setFlashMode(mode & ~QCameraExposure::FlashTorch);
126 emit enabledChanged();
127 }
128 } else {
129 if (on) {
130 m_flash->setFlashMode(mode | QCameraExposure::FlashTorch);
131 emit enabledChanged();
132 }
133 }
134}
135
136/*!
137 \qmlproperty int QtMultimedia::Torch::power
138
139 This property holds the current torch power setting, as a percentage of full power.
140
141 In some cases this setting may change automatically as a result
142 of temperature or battery conditions.
143*/
144/*!
145 \property QDeclarativeTorch::power
146
147 This property holds the current torch power settings, as a percentage of full power.
148*/
149int QDeclarativeTorch::power() const
150{
151 if (!m_exposure)
152 return 0;
153
154 return m_exposure->requestedValue(parameter: QCameraExposureControl::TorchPower).toInt();
155}
156
157/*!
158 Sets the current torch power setting to \a power, as a percentage of
159 full power.
160*/
161void QDeclarativeTorch::setPower(int power)
162{
163 if (!m_exposure)
164 return;
165
166 power = qBound(min: 0, val: power, max: 100);
167 if (this->power() != power)
168 m_exposure->setValue(parameter: QCameraExposureControl::TorchPower, value: power);
169}
170
171/* Check for changes in flash power */
172void QDeclarativeTorch::parameterChanged(int parameter)
173{
174 if (parameter == QCameraExposureControl::FlashPower) {
175 emit powerChanged();
176 }
177}
178
179QT_END_NAMESPACE
180
181

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