5bf0d8a9c553223c17ea55fd4d18791d1a81b3c2
[harmattan/cameraplus] / lib / qtcamzoom.cpp
1 #include "qtcamzoom.h"
2 #include "qtcamcapability_p.h"
3 #include "qtcamdevice_p.h"
4
5 class QtCamZoomPrivate : public QtCamCapabilityPrivate {
6 public:
7   QtCamZoomPrivate(QtCamDevice *d, QtCamZoom *q) :
8     QtCamCapabilityPrivate(d, QtCamCapability::Zoom), q_ptr(q), binHandler(0) {
9
10   }
11
12   ~QtCamZoomPrivate() {
13     if (binHandler) {
14       g_signal_handler_disconnect(bin, binHandler);
15     }
16   }
17
18   void init() {
19     if (bin) {
20       binHandler = g_signal_connect(bin, "notify", G_CALLBACK(camera_bin_notify), this);
21     }
22   }
23
24   static void camera_bin_notify(GObject *gobject, GParamSpec *pspec, QtCamZoomPrivate *d) {
25     Q_UNUSED(gobject);
26
27     QLatin1String name(pspec->name);
28     if (name == QLatin1String("max-zoom")) {
29       QMetaObject::invokeMethod(d->q_ptr, "maximumValueChanged", Qt::QueuedConnection);
30     }
31     else if (name == QLatin1String("zoom")) {
32       QMetaObject::invokeMethod(d->q_ptr, "valueChanged", Qt::QueuedConnection);
33     }
34   }
35
36   qreal zoom() {
37     if (!bin) {
38       return 1.0;
39     }
40
41     gfloat v = 1.0;
42
43     g_object_get(bin, "zoom", &v, NULL);
44
45     return v;
46   }
47
48   qreal maxZoom() {
49     if (!bin) {
50       return 1.0;
51     }
52
53     gfloat v = 1.0;
54
55     g_object_get(bin, "max-zoom", &v, NULL);
56
57     return v;
58   }
59
60   bool setZoom(qreal zoom) {
61     if (!bin) {
62       return false;
63     }
64
65     if (qFuzzyCompare(QtCamZoomPrivate::zoom(), zoom)) {
66         return false;
67       }
68
69     g_object_set(bin, "zoom", zoom, NULL);
70
71     return true;
72   }
73
74   QtCamZoom *q_ptr;
75   gulong binHandler;
76 };
77
78 QtCamZoom::QtCamZoom(QtCamDevice *dev, QObject *parent) :
79   QtCamCapability(new QtCamZoomPrivate(dev, this), parent) {
80
81   ((QtCamZoomPrivate *) d_ptr)->init();
82 }
83
84 QtCamZoom::~QtCamZoom() {
85
86 }
87
88 qreal QtCamZoom::value() {
89   return ((QtCamZoomPrivate *) d_ptr)->zoom();
90 }
91
92 bool QtCamZoom::setValue(qreal zoom) {
93   if (((QtCamZoomPrivate *) d_ptr)->setZoom(zoom)) {
94     emit valueChanged();
95     return true;
96   }
97
98   return false;
99 }
100
101 qreal QtCamZoom::minimumValue() {
102   return 1.0;
103 }
104
105 qreal QtCamZoom::maximumValue() {
106   return ((QtCamZoomPrivate *) d_ptr)->maxZoom();
107 }