Updated tests cases matching the new tabs
[situare] / src / map / gpslocationitem.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare 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
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QDebug>
23 #include <QPainter>
24
25 #include "gps/gpscommon.h"
26 #include "map/mapcommon.h"
27
28 #include "gpslocationitem.h"
29
30 const qreal RING_OUTLINE_PEN_WIDTH = 1.0;
31
32 GPSLocationItem::GPSLocationItem()
33     : m_showOnNextUpdate(true)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36
37     setPos(QPoint(UNDEFINED, UNDEFINED));
38     setZValue(GPSLocationItemZValue);
39
40     // create a child item for the actual red position spot
41     m_pixmapItem = new QGraphicsPixmapItem(QPixmap(":/res/images/gps_position.png"));
42     m_pixmapItem->setParentItem(this);
43     m_pixmapItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
44     m_pixmapItem->setOffset(-m_pixmapItem->pixmap().width() / 2,
45                             -m_pixmapItem->pixmap().height() / 2);
46
47     setEnabled(false);
48 }
49
50 QRectF GPSLocationItem::boundingRect() const
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     return m_boundingRect;
55 }
56
57 void GPSLocationItem::paint(QPainter *painter,
58                             const QStyleOptionGraphicsItem *option,
59                             QWidget *widget)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     Q_UNUSED(option);
64     Q_UNUSED(widget);
65
66     QColor color = QColor(Qt::red);
67
68     const int RING_OUTLINE_ALPHA = 64;
69     const int RING_FILL_ALPHA = 32;
70
71     // set outline ring
72     color.setAlpha(RING_OUTLINE_ALPHA);
73     QPen pen = QPen(color);
74     pen.setWidthF(RING_OUTLINE_PEN_WIDTH);
75     pen.setCosmetic(true);
76     painter->setPen(pen);
77
78     // set ring fill
79     color.setAlpha(RING_FILL_ALPHA);
80     painter->setBrush(color);
81
82     painter->setRenderHint(QPainter::Antialiasing);
83
84     QPointF center = QPointF();
85     painter->drawEllipse(center, m_radius, m_radius);
86 }
87
88 void GPSLocationItem::setEnabled(bool enable)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     if (enable) {
93         m_showOnNextUpdate = true;
94     }
95     else {
96         m_showOnNextUpdate = false;
97         hide();
98     }
99 }
100
101 void GPSLocationItem::setAccuracyRingRadius(qreal accuracy, qreal sceneResolution)
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     qreal newRadius = accuracy / sceneResolution;
106
107     if (m_radius != newRadius) {
108         m_radius = newRadius;
109         setBoundingRect(newRadius);
110     }
111 }
112
113 void GPSLocationItem::setBoundingRect(qreal radius)
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     prepareGeometryChange();
118
119     m_boundingRect = QRectF(-radius - RING_OUTLINE_PEN_WIDTH / 2,
120                             -radius - RING_OUTLINE_PEN_WIDTH / 2,
121                             2 * radius + RING_OUTLINE_PEN_WIDTH,
122                             2 * radius + RING_OUTLINE_PEN_WIDTH);
123 }
124
125 void GPSLocationItem::updateItem(QPointF scenePosition, qreal accuracy, qreal sceneResolution)
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     setPos(scenePosition);
130     setAccuracyRingRadius(accuracy, sceneResolution);
131
132     if (m_showOnNextUpdate) {
133         show();
134         m_showOnNextUpdate = false;
135     }
136
137     update();
138 }