2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Kaj Wallin - kaj.wallin@ixonos.com
6 Katri Kaikkonen - katri.kaikkonen@ixonos.com
7 Sami Rämö - sami.ramo@ixonos.com
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.
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.
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,
30 #include "panelcommon.h"
32 #include "indicatorbutton.h"
34 enum State {OFF, ON}; ///< Enumerator for led state
36 IndicatorButton::IndicatorButton(QWidget *parent)
37 : QToolButton(parent),
38 m_drawTriangle(false),
41 qDebug() << __PRETTY_FUNCTION__;
43 const qreal OPACITY = 0.50;
44 const int BUTTON_WIDTH = 66;
45 const int BUTTON_HEIGHT = 66;
47 m_indicatorLeds[OFF].load(":res/images/gps_position.png");
48 m_indicatorLeds[ON].load(":res/images/gps_position_s.png");
50 setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
53 m_normalColor = new QColor(Qt::black);
54 m_normalColor->setAlpha(floor(OPACITY * 255));
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));
64 connect(this, SIGNAL(clicked(bool)),
65 this, SIGNAL(autoCenteringTriggered(bool)));
70 IndicatorButton::~IndicatorButton()
72 qDebug() << __PRETTY_FUNCTION__;
75 delete m_selectedGradient;
78 const QPoint& IndicatorButton::eventPosition()
80 qDebug() << __PRETTY_FUNCTION__;
82 return m_eventPosition;
85 void IndicatorButton::mouseMoveEvent(QMouseEvent *event)
87 qDebug() << __PRETTY_FUNCTION__;
89 QToolButton::mouseMoveEvent(event);
94 void IndicatorButton::mousePressEvent(QMouseEvent *event)
96 qDebug() << __PRETTY_FUNCTION__;
98 QToolButton::mousePressEvent(event);
103 void IndicatorButton::mouseReleaseEvent(QMouseEvent *event)
105 qDebug() << __PRETTY_FUNCTION__;
107 QToolButton::mouseReleaseEvent(event);
112 void IndicatorButton::paintEvent(QPaintEvent *event)
114 qDebug() << __PRETTY_FUNCTION__;
116 const int ROUNDING_RADIUS = 9;
120 QPainterPath backgroundPath;
121 backgroundPath.addRoundedRect(this->rect(), ROUNDING_RADIUS, ROUNDING_RADIUS);
123 QPainter painter(this);
124 painter.setRenderHint(QPainter::Antialiasing);
127 painter.fillPath(backgroundPath, QBrush(*m_selectedGradient));
129 painter.fillPath(backgroundPath, QBrush(*m_normalColor));
131 const QPointF CENTER = QPointF(this->width(), this->height()) / 2;
134 const QPointF offset = QPointF(m_indicatorLeds[ON].width(),
135 m_indicatorLeds[ON].height()) / 2;
137 painter.drawPixmap(CENTER - offset, m_indicatorLeds[ON]);
139 const QPointF offset = QPointF(m_indicatorLeds[OFF].width(),
140 m_indicatorLeds[OFF].height()) / 2;
142 painter.drawPixmap(CENTER - offset, m_indicatorLeds[OFF]);
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;
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)
159 // base triangle is facing up, and needs to be rotated to the required direction
160 QTransform rotationTransform;
161 rotationTransform.rotate(m_direction);
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());
168 painter.setTransform(rotationTransform * translateTransform);
170 // setting the look of the triangle
171 painter.setBrush(Qt::red);
172 painter.setPen(Qt::red);
174 painter.drawPolygon(points, POINTS);
178 void IndicatorButton::setDirection(qreal direction, bool draw)
180 qDebug() << __PRETTY_FUNCTION__;
182 m_direction = direction;
183 m_drawTriangle = draw;