Added signal strength indicator to detail screen. Changed speed treshold to change...
[jspeed] / src / detailscreen.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtGui/QGraphicsTextItem>
20 #include <QtGui/QGraphicsLineItem>
21 #include <QtGui/QGraphicsPixmapItem>
22 #include <QtGui/QPixmap>
23 #include <QtCore/QString>
24 #include <QtCore/QDebug>
25 #include <math.h>
26 #include "detailscreen.h"
27 #include "odometer.h"
28 #include "graphicsscene.h"
29
30 namespace
31 {
32     QString const FONT = "Tahoma";
33     QString const BACKGROUND = ":/resources/themes/default/background.png";
34     int const FONT_SIZE = 20;
35     int const START_HEIGHT = 80;
36     int const ITEM_WIDTH = 130;
37 }
38
39 DetailScreen::DetailScreen(QWidget* parent): GraphicsScreen(parent)
40 {
41     QString unit = Odometer::getUnit();
42     QString speedUnit = Odometer::getSpeedUnit();
43
44     QGraphicsPixmapItem* background = new QGraphicsPixmapItem(QPixmap(BACKGROUND));
45     getScene()->addItem(background);
46
47     tripLabel_ = createItem(tr("Trip"));
48     trip_ = createItem(roundDouble(Odometer::instance().getTrip()));
49     tripUnit_ = createItem(unit);
50
51     totalLabel_ = createItem(tr("Total"));
52     total_ = createItem(roundDouble(Odometer::instance().getTotal()));
53     totalUnit_ = createItem(unit);
54
55     speedLabel_ = createItem(tr("Speed"));
56     speed_ = createItem(roundDouble(0.0));
57     speedUnit_ = createItem(speedUnit);
58
59     avgSpeedLabel_ = createItem(tr("Average speed"));
60     avgSpeed_ = createItem(roundDouble(Odometer::instance().getAverageSpeed()));
61     avgSpeedUnit_ = createItem(speedUnit);
62
63     maxSpeedLabel_ = createItem(tr("Max speed"));
64     maxSpeed_ = createItem(roundDouble(Odometer::instance().getMaxSpeed()));
65     maxSpeedUnit_ = createItem(speedUnit);
66
67     tripTimeLabel_ = createItem(tr("Trip time"));
68     tripTime_ = createItem(formatTime(Odometer::instance().getTripTime()));
69
70     totalTimeLabel_ = createItem(tr("Total time"));
71     totalTime_ = createItem(formatTime(Odometer::instance().getTotalTime()));
72
73     line1_ = createLine();
74     line2_ = createLine();
75
76     strength_ = new QGraphicsPixmapItem;
77     currentStrength_ = getStrength();
78     strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(currentStrength_) + ".png"));
79     getScene()->addItem(strength_);
80
81     connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
82     connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
83
84     reArrange();
85 }
86
87 void DetailScreen::update()
88 {
89     speed_->setPlainText(roundDouble(Odometer::instance().getLatestFix().speed));
90     trip_->setPlainText(roundDouble(Odometer::instance().getTrip()));
91     total_->setPlainText(roundDouble(Odometer::instance().getTotal()));
92     avgSpeed_->setPlainText(roundDouble(Odometer::instance().getAverageSpeed()));
93     maxSpeed_->setPlainText(roundDouble(Odometer::instance().getMaxSpeed()));
94
95     int strength = getStrength();
96
97     if(strength != currentStrength_)
98     {
99         currentStrength_ = strength;
100         strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(strength) + ".png"));
101     }
102 }
103
104 void DetailScreen::updateTime()
105 {
106     tripTime_->setPlainText(formatTime(Odometer::instance().getTripTime()));
107     totalTime_->setPlainText(formatTime(Odometer::instance().getTotalTime()));
108 }
109
110 void DetailScreen::updateUnits()
111 {
112     QString unit = Odometer::getUnit();
113     QString speedUnit = Odometer::getSpeedUnit();
114
115     tripUnit_->setPlainText(unit);
116     totalUnit_->setPlainText(unit);
117     speedUnit_->setPlainText(speedUnit);
118     avgSpeedUnit_->setPlainText(speedUnit);
119     maxSpeedUnit_->setPlainText(speedUnit);
120
121     update();
122 }
123
124 void DetailScreen::reArrange()
125 {
126     GraphicsScreen::reArrange();
127
128     int width = getScene()->width();
129     int height = getScene()->height();
130
131     int lineHeight = height / 9;
132
133     int padding = width / 22;
134     int area1 = (width / 3) + ITEM_WIDTH / 2 + 20;
135     int area2 = area1 + ITEM_WIDTH;
136
137     tripLabel_->setPos(padding, START_HEIGHT);
138     trip_->setPos(area1, START_HEIGHT);
139     tripUnit_->setPos(area2, START_HEIGHT);
140
141     totalLabel_->setPos(padding, START_HEIGHT + lineHeight);
142     total_->setPos(area1, START_HEIGHT + lineHeight);
143     totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
144
145     speedLabel_->setPos(padding, START_HEIGHT + 2 * lineHeight);
146     speed_->setPos(area1, START_HEIGHT + 2 * lineHeight);
147     speedUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
148
149     avgSpeedLabel_->setPos(padding, START_HEIGHT + 3 * lineHeight);
150     avgSpeed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
151     avgSpeedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
152
153     maxSpeedLabel_->setPos(padding, START_HEIGHT + 4 * lineHeight);
154     maxSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
155     maxSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
156
157     tripTimeLabel_->setPos(padding, START_HEIGHT + 5 * lineHeight);
158     tripTime_->setPos(area1, START_HEIGHT + 5 * lineHeight);
159
160     totalTimeLabel_->setPos(padding, START_HEIGHT + 6 * lineHeight);
161     totalTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
162
163     int y1 = START_HEIGHT + 2 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
164     int y2 = START_HEIGHT + 5 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
165     int x = width - padding;
166     line1_->setLine(padding + 2, y1, x, y1);
167     line2_->setLine(padding + 2, y2, x, y2);
168
169     strength_->setPos(width - padding - 64, height - padding - 41);
170 }
171
172 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
173 {
174     QGraphicsTextItem* item = new QGraphicsTextItem(text);
175     QFont font(FONT, FONT_SIZE);
176     font.setBold(true);
177     item->setFont(font);
178     item->setDefaultTextColor(QColor("#fff"));
179     getScene()->addItem(item);
180     return item;
181 }
182
183 QGraphicsLineItem* DetailScreen::createLine()
184 {
185     QGraphicsLineItem* item = new QGraphicsLineItem;
186     QPen pen(QColor("#ddd"));
187     pen.setWidth(1);
188     item->setPen(pen);
189     getScene()->addItem(item);
190     return item;
191 }
192
193 QString DetailScreen::roundDouble(double number)
194 {
195     QString result;
196     result.sprintf("%.2lf", number);
197     return result;
198 }
199
200 QString DetailScreen::formatTime(qulonglong time)
201 {
202     qulonglong secs = time % 60;
203     qulonglong mins_tmp = time / 60;
204     qulonglong mins = mins_tmp % 60;
205     qulonglong hours = mins_tmp / 60;
206
207     static char format[] = "%02d";
208
209     return QString().sprintf(format, hours) + ":" +
210            QString().sprintf(format, mins) + ":" +
211            QString().sprintf(format, secs);
212 }
213
214 int DetailScreen::getStrength()
215 {
216     double strength = round(Odometer::instance().getSignalStrength() / 25.0);
217     return static_cast<int>(strength);
218 }