Reworked postcapture model
[harmattan/cameraplus] / declarative / roi.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 "roi.h"
22 #include "qtcamroi.h"
23 #include "qtcamdevice.h"
24 #include "qtcamviewfinder.h"
25
26 class UnNormalizer {
27 public:
28   void init(const QRectF& renderArea, bool normalize) {
29     m_area = renderArea;
30     m_normalize = normalize;
31   }
32
33   QRectF unNormalize(const QRectF& rect) {
34     if (m_normalize) {
35       return rect;
36     }
37
38     return QRectF(rect.x() * m_area.width(),
39                   rect.y() * m_area.height(),
40                   rect.width() * m_area.width(),
41                   rect.height() * m_area.height());
42   }
43
44   QVariantList unNormalize(const QList<QRectF>& rects) {
45     QVariantList vars;
46
47     foreach (const QRectF& rect, rects) {
48       vars.append(QVariant::fromValue(unNormalize(rect)));
49     }
50
51     return vars;
52   }
53
54 private:
55   QRectF m_area;
56   bool m_normalize;
57 };
58
59 Roi::Roi(QtCamDevice *device, QObject *parent) :
60   QObject(parent),
61   m_roi(new QtCamRoi(device)),
62   m_normalize(true) {
63
64   QObject::connect(m_roi,
65                    SIGNAL(regionsOfInterestUpdated(const QList<QRectF>&, const QRectF&, const QList<QRectF>&)),
66                    this,
67                    SLOT(handleRegionsChanged(const QList<QRectF>&, const QRectF&, const QList<QRectF>&)));
68 }
69
70 Roi::~Roi() {
71   delete m_roi; m_roi = 0;
72 }
73
74
75 void Roi::setEnabled(bool enabled) {
76   if (Roi::isEnabled() != enabled) {
77     m_roi->setEnabled(enabled);
78     emit enabledChanged();
79   }
80 }
81
82 bool Roi::isEnabled() {
83   return m_roi->isEnabled();
84 }
85
86 void Roi::setRegionOfInterest(const QRectF& region) {
87   m_roi->setRegionOfInterest(region);
88 }
89
90 void Roi::resetRegionOfInterest() {
91   m_roi->resetRegionOfInterest();
92 }
93
94 void Roi::handleRegionsChanged(const QList<QRectF>& regions, const QRectF& primary,
95                                const QList<QRectF>& rest) {
96
97   UnNormalizer n;
98
99   n.init(m_roi->device()->viewfinder()->renderArea(), m_normalize);
100
101   QVariantList regionsList = n.unNormalize(regions);
102   QVariantList restList = n.unNormalize(rest);
103   QVariant primaryRect = QVariant::fromValue(n.unNormalize(primary));
104
105   emit regionsChanged(regionsList, primaryRect, restList);
106 }
107
108 bool Roi::normalize() const {
109   return m_normalize;
110 }
111
112 void Roi::setNormalize(bool normalize) {
113   if (normalize != m_normalize) {
114     m_normalize = normalize;
115     emit normalizeChanged();
116   }
117 }