Set media.name to '*'.
[harmattan/cameraplus] / lib / qtcamzoom.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "qtcamzoom.h"
22 #include "qtcamcapability_p.h"
23 #include "qtcamdevice_p.h"
24
25 class QtCamZoomPrivate : public QtCamCapabilityPrivate {
26 public:
27   QtCamZoomPrivate(QtCamDevice *d, QtCamZoom *q) :
28     QtCamCapabilityPrivate(d, QtCamCapability::Zoom), q_ptr(q), binHandler(0) {
29
30   }
31
32   ~QtCamZoomPrivate() {
33     if (binHandler) {
34       g_signal_handler_disconnect(bin, binHandler);
35     }
36   }
37
38   void init() {
39     if (bin) {
40       binHandler = g_signal_connect(bin, "notify", G_CALLBACK(camera_bin_notify), this);
41     }
42   }
43
44   static void camera_bin_notify(GObject *gobject, GParamSpec *pspec, QtCamZoomPrivate *d) {
45     Q_UNUSED(gobject);
46
47     QLatin1String name(pspec->name);
48     if (name == QLatin1String("max-zoom")) {
49       QMetaObject::invokeMethod(d->q_ptr, "maximumValueChanged", Qt::QueuedConnection);
50     }
51     else if (name == QLatin1String("zoom")) {
52       QMetaObject::invokeMethod(d->q_ptr, "valueChanged", Qt::QueuedConnection);
53     }
54   }
55
56   qreal zoom() {
57     if (!bin) {
58       return 1.0;
59     }
60
61     gfloat v = 1.0;
62
63     g_object_get(bin, "zoom", &v, NULL);
64
65     return v;
66   }
67
68   qreal maxZoom() {
69     if (!bin) {
70       return 1.0;
71     }
72
73     gfloat v = 1.0;
74
75     g_object_get(bin, "max-zoom", &v, NULL);
76
77     return v;
78   }
79
80   bool setZoom(qreal zoom) {
81     if (!bin) {
82       return false;
83     }
84
85     if (qFuzzyCompare(QtCamZoomPrivate::zoom(), zoom)) {
86         return false;
87       }
88
89     g_object_set(bin, "zoom", zoom, NULL);
90
91     return true;
92   }
93
94   QtCamZoom *q_ptr;
95   gulong binHandler;
96 };
97
98 QtCamZoom::QtCamZoom(QtCamDevice *dev, QObject *parent) :
99   QtCamCapability(new QtCamZoomPrivate(dev, this), parent) {
100
101   dynamic_cast<QtCamZoomPrivate *>(d_ptr)->init();
102 }
103
104 QtCamZoom::~QtCamZoom() {
105
106 }
107
108 qreal QtCamZoom::value() {
109   return dynamic_cast<QtCamZoomPrivate *>(d_ptr)->zoom();
110 }
111
112 bool QtCamZoom::setValue(qreal zoom) {
113   if (dynamic_cast<QtCamZoomPrivate *>(d_ptr)->setZoom(zoom)) {
114     emit valueChanged();
115     return true;
116   }
117
118   return false;
119 }
120
121 qreal QtCamZoom::minimumValue() {
122   if (!d_ptr->bin) {
123     return 1.0;
124   }
125
126   GParamSpec *pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(d_ptr->bin), "max-zoom");
127   if (pspec && G_IS_PARAM_SPEC_FLOAT(pspec)) {
128     return G_PARAM_SPEC_FLOAT(pspec)->minimum;
129   }
130
131   return 1.0;
132 }
133
134 qreal QtCamZoom::maximumValue() {
135   return dynamic_cast<QtCamZoomPrivate *>(d_ptr)->maxZoom();
136 }