Decrease the size of the corners a bit
[harmattan/cameraplus] / src / focusrectangle.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 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 "focusrectangle.h"
22 #include <QPainter>
23 #include <QDebug>
24
25 #define OUTER_WIDTH  4
26 #define INNER_WIDTH  2
27 #define OUTER_LENGTH 30
28 #define INNER_LENGTH 29
29
30 FocusRectangle::FocusRectangle(QDeclarativeItem *parent) :
31   QDeclarativeItem(parent),
32   m_color(Qt::white) {
33
34   setFlag(QGraphicsItem::ItemHasNoContents, false);
35 }
36
37 FocusRectangle::~FocusRectangle() {
38
39 }
40
41 QColor FocusRectangle::color() const {
42   return m_color;
43 }
44
45 void FocusRectangle::setColor(const QColor& color) {
46   if (color != m_color) {
47     m_color = color;
48     emit colorChanged();
49
50     update();
51   }
52 }
53
54 void FocusRectangle::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
55                            QWidget* widget) {
56
57   QDeclarativeItem::paint(painter, option, widget);
58
59   painter->save();
60
61   painter->setPen(QPen(QBrush(Qt::black), OUTER_WIDTH));
62   painter->drawPath(m_outer);
63
64   painter->setPen(QPen(QBrush(m_color), INNER_WIDTH));
65   painter->drawPath(m_inner);
66
67   painter->restore();
68 }
69
70 void FocusRectangle::geometryChanged( const QRectF& newGeometry, const QRectF& oldGeometry) {
71   QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
72
73   qreal w = width();
74   qreal h = height();
75
76   // Outer:
77   // top left:
78   m_outer = QPainterPath(QPointF(0, OUTER_LENGTH));
79   m_outer.lineTo(0, 0);
80   m_outer.lineTo(OUTER_LENGTH, 0);
81
82   // top right:
83   m_outer.moveTo(w - OUTER_LENGTH, 0);
84   m_outer.lineTo(w, 0);
85   m_outer.lineTo(w, OUTER_LENGTH);
86
87   // bottom right:
88   m_outer.moveTo(w, h - OUTER_LENGTH);
89   m_outer.lineTo(w, h);
90   m_outer.lineTo(w - OUTER_LENGTH, h);
91
92   // bottom left:
93   m_outer.moveTo(OUTER_LENGTH, h);
94   m_outer.lineTo(0, h);
95   m_outer.lineTo(0, h - OUTER_LENGTH);
96
97   // Inner:
98   // top left:
99   m_inner = QPainterPath(QPointF(0, INNER_LENGTH));
100   m_inner.lineTo(0, 0);
101   m_inner.lineTo(INNER_LENGTH, 0);
102
103   // top right:
104   m_inner.moveTo(w - INNER_LENGTH, 0);
105   m_inner.lineTo(w, 0);
106   m_inner.lineTo(w, INNER_LENGTH);
107
108   // bottom right:
109   m_inner.moveTo(w, h - INNER_LENGTH);
110   m_inner.lineTo(w, h);
111   m_inner.lineTo(w - INNER_LENGTH, h);
112
113   // bottom left:
114   m_inner.moveTo(INNER_LENGTH, h);
115   m_inner.lineTo(0, h);
116   m_inner.lineTo(0, h - INNER_LENGTH);
117 }