Merge branch 'master' into new_panels
[situare] / src / ui / indicatorbutton.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6         Katri Kaikkonen - katri.kaikkonen@ixonos.com
7         Sami Rämö - sami.ramo@ixonos.com
8
9     Situare is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License
11     version 2 as published by the Free Software Foundation.
12
13     Situare 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
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with Situare; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21     USA.
22 */
23
24 #include <QDebug>
25 #include <QPainter>
26 #include <QSettings>
27
28 #include "common.h"
29 #include "math.h"
30 #include "panelcommon.h"
31
32 #include "indicatorbutton.h"
33
34 enum State {OFF, ON};                       ///< Enumerator for led state
35
36 IndicatorButton::IndicatorButton(QWidget *parent)
37     : QToolButton(parent),
38       m_drawTriangle(false),
39       m_direction(0)
40 {
41     qDebug() << __PRETTY_FUNCTION__;
42
43     const qreal OPACITY = 0.50;
44     const int BUTTON_WIDTH = 66;
45     const int BUTTON_HEIGHT = 66;
46
47     m_indicatorLeds[OFF].load(":res/images/gps_position.png");
48     m_indicatorLeds[ON].load(":res/images/gps_position_s.png");
49
50     setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
51
52     // Normal background
53     m_normalColor = new QColor(Qt::black);
54     m_normalColor->setAlpha(floor(OPACITY * 255));
55
56     // Selected background
57     m_selectedGradient = new QLinearGradient(0, 0, 0, this->height());
58     m_selectedGradient->setColorAt(0.02, QColor(0, 113, 181));
59     m_selectedGradient->setColorAt(0.25, QColor(24, 142, 214));
60     m_selectedGradient->setColorAt(0.5, QColor(41, 162, 239));
61     m_selectedGradient->setColorAt(0.75, QColor(82, 195, 255));
62     m_selectedGradient->setColorAt(0.98, QColor(115, 215, 255));
63
64     connect(this, SIGNAL(clicked(bool)),
65             this, SIGNAL(autoCenteringTriggered(bool)));
66
67     setCheckable(true);
68 }
69
70 IndicatorButton::~IndicatorButton()
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73
74     delete m_normalColor;
75     delete m_selectedGradient;
76 }
77
78 const QPoint& IndicatorButton::eventPosition()
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     return m_eventPosition;
83 }
84
85 void IndicatorButton::mouseMoveEvent(QMouseEvent *event)
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     QToolButton::mouseMoveEvent(event);
90
91     event->ignore();
92 }
93
94 void IndicatorButton::mousePressEvent(QMouseEvent *event)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     QToolButton::mousePressEvent(event);
99
100     event->ignore();
101 }
102
103 void IndicatorButton::mouseReleaseEvent(QMouseEvent *event)
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106
107     QToolButton::mouseReleaseEvent(event);
108
109     event->ignore();
110 }
111
112 void IndicatorButton::paintEvent(QPaintEvent *event)
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     const int ROUNDING_RADIUS = 9;
117
118     Q_UNUSED(event);
119
120     QPainterPath backgroundPath;
121     backgroundPath.addRoundedRect(this->rect(), ROUNDING_RADIUS, ROUNDING_RADIUS);
122
123     QPainter painter(this);
124     painter.setRenderHint(QPainter::Antialiasing);
125
126     if (isDown())
127         painter.fillPath(backgroundPath, QBrush(*m_selectedGradient));
128     else
129         painter.fillPath(backgroundPath, QBrush(*m_normalColor));
130
131     const QPointF CENTER = QPointF(this->width(), this->height()) / 2;
132
133     if (isChecked()) {
134         const QPointF offset = QPointF(m_indicatorLeds[ON].width(),
135                                        m_indicatorLeds[ON].height()) / 2;
136
137         painter.drawPixmap(CENTER - offset, m_indicatorLeds[ON]);
138     } else {
139         const QPointF offset = QPointF(m_indicatorLeds[OFF].width(),
140                                        m_indicatorLeds[OFF].height()) / 2;
141
142         painter.drawPixmap(CENTER - offset, m_indicatorLeds[OFF]);
143     }
144
145     // draw the direction indicator triangle only when autocentering is disabled and MapEngine
146     // doesn't deny drawing (because GPS location item is visible)
147     if (!isChecked() && m_drawTriangle) {
148         const int TRIANGLE_WIDTH = 10;
149         const int TRIANGLE_HEIGHT = 10;
150         const int TRIANGLE_DISTANCE_FROM_CENTER = 15;
151
152         const int POINTS = 3;
153         const QPointF points[POINTS] = {
154             QPointF(-TRIANGLE_WIDTH / 2, -TRIANGLE_DISTANCE_FROM_CENTER),
155             QPointF(0, -(TRIANGLE_DISTANCE_FROM_CENTER + TRIANGLE_HEIGHT)),
156             QPointF(TRIANGLE_WIDTH / 2, -TRIANGLE_DISTANCE_FROM_CENTER)
157         };
158
159         // base triangle is facing up, and needs to be rotated to the required direction
160         QTransform rotationTransform;
161         rotationTransform.rotate(m_direction);
162
163         // origin is in the top left corner of the button, and needs to be translated to the
164         // center of the button
165         QTransform translateTransform;
166         translateTransform.translate(CENTER.x(), CENTER.y());
167
168         painter.setTransform(rotationTransform * translateTransform);
169
170         // setting the look of the triangle
171         painter.setBrush(Qt::red);
172         painter.setPen(Qt::red);
173
174         painter.drawPolygon(points, POINTS);
175     }
176 }
177
178 void IndicatorButton::setDirection(qreal direction, bool draw)
179 {
180     qDebug() << __PRETTY_FUNCTION__;
181
182     m_direction = direction;
183     m_drawTriangle = draw;
184
185     update();
186 }
187