Hide focus reticle if we cannot focus while a particular scene mode is active
[harmattan/cameraplus] / lib / qtcamautofocus_p.h
1 // -*- c++ -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef QT_CAM_AUTO_FOCUS_P_H
24 #define QT_CAM_AUTO_FOCUS_P_H
25
26 #include "qtcamautofocus.h"
27 #include "qtcamgstreamermessagehandler.h"
28 #include "qtcamgstreamermessagelistener.h"
29 #include "qtcamdevice.h"
30 #include "qtcamdevice_p.h"
31
32 #include <QDebug>
33
34 #ifndef GST_USE_UNSTABLE_API
35 #define GST_USE_UNSTABLE_API
36 #endif /* GST_USE_UNSTABLE_API */
37 #include <gst/interfaces/photography.h>
38
39 class QtCamAutoFocusPrivate : public QObject {
40   Q_OBJECT
41
42 public:
43   QtCamAutoFocusPrivate(QtCamDevice *device, QtCamAutoFocus *q, QObject *parent = 0) :
44     QObject(parent),
45     dev(device),
46     q_ptr(q),
47     status(QtCamAutoFocus::None),
48     cafStatus(QtCamAutoFocus::None) {
49
50     handler = new QtCamGStreamerMessageHandler(GST_PHOTOGRAPHY_AUTOFOCUS_DONE, this);
51
52     QObject::connect(handler, SIGNAL(messageSent(GstMessage *)),
53                      this, SLOT(handleMessage(GstMessage *)));
54
55     dev->listener()->addHandler(handler);
56
57     cafHandler = new QtCamGStreamerMessageHandler("caf-update", this);
58
59     QObject::connect(cafHandler, SIGNAL(messageSent(GstMessage *)),
60                      this, SLOT(handleCafMessage(GstMessage *)));
61
62     dev->listener()->addHandler(cafHandler);
63   }
64
65   ~QtCamAutoFocusPrivate() {
66     dev->listener()->removeHandler(handler);
67     delete handler; handler = 0;
68     dev = 0;
69     q_ptr = 0;
70   }
71
72   bool setEnabled(bool enabled) {
73     if (!dev->d_ptr->videoSource) {
74       return false;
75     }
76
77     if (!GST_IS_PHOTOGRAPHY(dev->d_ptr->videoSource)) {
78       return false;
79     }
80
81     GstPhotography *photo = GST_PHOTOGRAPHY(dev->d_ptr->videoSource);
82     if (!photo) {
83       return false;
84     }
85
86     gst_photography_set_autofocus(photo, enabled ? TRUE : FALSE);
87
88     if (enabled) {
89       status = QtCamAutoFocus::Running;
90       emit q_ptr->statusChanged();
91     }
92     else {
93       status = QtCamAutoFocus::None;
94       emit q_ptr->statusChanged();
95     }
96
97     return true;
98   }
99
100   bool setStatus(QtCamAutoFocus::Status *status, GstMessage *message) {
101     const GstStructure *s = gst_message_get_structure(message);
102     int st = GST_PHOTOGRAPHY_FOCUS_STATUS_NONE;
103
104     if (gst_structure_get_int(s, "status", &st)) {
105       if (*status != st) {
106         *status = (QtCamAutoFocus::Status) st;
107         return true;
108       }
109     }
110
111     return false;
112   }
113
114 public slots:
115   void handleMessage(GstMessage *message) {
116     if (setStatus(&status, message)) {
117       // TODO: focus-window-rows, focus-window-columns, focus-windows and the rest
118       emit q_ptr->statusChanged();
119     }
120   }
121
122   void handleCafMessage(GstMessage *message) {
123     if (setStatus(&cafStatus, message)) {
124       emit q_ptr->cafStatusChanged();
125     }
126   }
127
128 public:
129   QtCamDevice *dev;
130   QtCamAutoFocus *q_ptr;
131
132   QtCamAutoFocus::Status status;
133   QtCamAutoFocus::Status cafStatus;
134
135   QtCamGStreamerMessageHandler *handler;
136   QtCamGStreamerMessageHandler *cafHandler;
137 };
138
139 #endif /* QT_CAM_AUTO_FOCUS_P_H */