Convert TextSwitch to a platform specific component and rename it to CameraTextSwitch
[harmattan/cameraplus] / lib / qtcamroi.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 "qtcamroi.h"
22 #include "qtcamroi_p.h"
23
24 #ifndef G_VALUE_INIT
25 #define G_VALUE_INIT  { 0, { { 0 } } }
26 #endif /* G_VALUE_INIT */
27
28 QtCamRoi::QtCamRoi(QtCamDevice *dev, QObject *parent) :
29   QObject(parent),
30   d_ptr(new QtCamRoiPrivate(dev, this, this)) {
31
32   d_ptr->init();
33   d_ptr->installHandler();
34 }
35
36 QtCamRoi::~QtCamRoi() {
37   delete d_ptr; d_ptr = 0;
38 }
39
40 void QtCamRoi::setRegionOfInterest(const QRectF& roi) {
41   if (!d_ptr->dev || !d_ptr->dev->viewfinder()) {
42     return;
43   }
44
45   QSizeF vf = d_ptr->dev->viewfinder()->videoResolution();
46   if (vf.isEmpty()) {
47     return;
48   }
49
50   int frameWidth = vf.width();
51   int frameHeight = vf.height();
52   int x = roi.x() * frameWidth;
53   int y = roi.y() * frameHeight;
54   int width = roi.width() * frameWidth;
55   int height = roi.height() * frameHeight;
56
57   // if we have an empty roi then we reset:
58   int priority = roi.isEmpty() ? 0 : 1;
59
60   GstStructure *region = gst_structure_new("region0",
61                                            "region-x", G_TYPE_UINT, x,
62                                            "region-y", G_TYPE_UINT, y,
63                                            "region-w", G_TYPE_UINT, width,
64                                            "region-h", G_TYPE_UINT, height,
65                                            "region-priority", G_TYPE_UINT, priority,
66                                            "region-id", G_TYPE_UINT, 0,
67                                            NULL);
68
69   GValue regionValue = G_VALUE_INIT;
70   GValue regionList = G_VALUE_INIT;
71
72   g_value_init(&regionValue, GST_TYPE_STRUCTURE);
73   g_value_init(&regionList, GST_TYPE_LIST);
74
75   gst_value_set_structure(&regionValue, region);
76   gst_value_list_append_value(&regionList, &regionValue);
77
78   GstStructure *s = gst_structure_new("regions-of-interest",
79                                       "frame-width", G_TYPE_UINT, frameWidth,
80                                       "frame-height", G_TYPE_UINT, frameHeight,
81                                       NULL);
82   gst_structure_set_value(s, "regions", &regionList);
83
84   GstEvent *event = gst_event_new_custom(GST_EVENT_CUSTOM_UPSTREAM, s);
85   gst_structure_free(region);
86   g_value_unset(&regionValue);
87   g_value_unset(&regionList);
88
89   if (!d_ptr->sendEventToSource(event)) {
90     qWarning() << "Failed to send ROI event";
91   }
92 }
93
94 void QtCamRoi::resetRegionOfInterest() {
95   setRegionOfInterest(QRectF());
96 }
97
98 void QtCamRoi::setEnabled(bool enabled) {
99   if (enabled == isEnabled()) {
100     return;
101   }
102
103   if (!d_ptr->roi) {
104     return;
105   }
106
107   gboolean val = enabled ? TRUE : FALSE;
108
109   g_object_set(d_ptr->roi, d_ptr->dev->config()->roiEnableProperty().toUtf8().constData(),
110                val, NULL);
111 }
112
113 bool QtCamRoi::isEnabled() {
114   if (!d_ptr->roi) {
115     return false;
116   }
117
118   gboolean val = FALSE;
119
120   g_object_get(d_ptr->roi, d_ptr->dev->config()->roiEnableProperty().toUtf8().constData(),
121                &val, NULL);
122
123   return val == TRUE;
124 }
125
126 QtCamDevice *QtCamRoi::device() {
127   return d_ptr->dev;
128 }