Theme download link fixed in web page.
[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 DEFAULT_BACKGROUND_COLOR = "#000";
34     QString const DEFAULT_COLOR = "#fff";
35     int const FONT_SIZE = 20;
36     int const START_HEIGHT = 80;
37     int const ITEM_WIDTH = 130;
38
39 }
40
41 DetailScreen::DetailScreen(QWidget* parent): ThemeScreen(parent)
42 {
43     QString unit = Odometer::getUnit();
44     QString speedUnit = Odometer::getSpeedUnit();
45
46     getScene()->setBackgroundBrush(QBrush(QColor(DEFAULT_BACKGROUND_COLOR)));
47
48     tripLabel_ = createItem(tr("Trip"));
49     trip_ = createItem(roundDouble(Odometer::instance().getTrip()));
50     tripUnit_ = createItem(unit);
51
52     totalLabel_ = createItem(tr("Total"));
53     total_ = createItem(roundDouble(Odometer::instance().getTotal()));
54     totalUnit_ = createItem(unit);
55
56     speedLabel_ = createItem(tr("Speed"));
57     speed_ = createItem(roundDouble(0.0));
58     speedUnit_ = createItem(speedUnit);
59
60     avgSpeedLabel_ = createItem(tr("Average speed"));
61     avgSpeed_ = createItem(roundDouble(Odometer::instance().getAverageSpeed()));
62     avgSpeedUnit_ = createItem(speedUnit);
63
64     maxSpeedLabel_ = createItem(tr("Max speed"));
65     maxSpeed_ = createItem(roundDouble(Odometer::instance().getMaxSpeed()));
66     maxSpeedUnit_ = createItem(speedUnit);
67
68     tripTimeLabel_ = createItem(tr("Trip time"));
69     tripTime_ = createItem(formatTime(Odometer::instance().getTripTime()));
70
71     totalTimeLabel_ = createItem(tr("Total time"));
72     totalTime_ = createItem(formatTime(Odometer::instance().getTotalTime()));
73
74     line1_ = createLine();
75     line2_ = createLine();
76
77     strength_ = new QGraphicsPixmapItem;
78     currentStrength_ = getStrength();
79     strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(currentStrength_) + ".png"));
80     strength_->setZValue(999);
81     getScene()->addItem(strength_);
82
83     connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
84     connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
85
86     reArrange();
87 }
88
89 void DetailScreen::update()
90 {
91     ThemeScreen::update();
92
93     speed_->setPlainText(roundDouble(Odometer::instance().getLatestFix().speed));
94     trip_->setPlainText(roundDouble(Odometer::instance().getTrip()));
95     total_->setPlainText(roundDouble(Odometer::instance().getTotal()));
96     avgSpeed_->setPlainText(roundDouble(Odometer::instance().getAverageSpeed()));
97     maxSpeed_->setPlainText(roundDouble(Odometer::instance().getMaxSpeed()));
98
99     int strength = getStrength();
100
101     if(strength != currentStrength_)
102     {
103         currentStrength_ = strength;
104         strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(strength) + ".png"));
105     }
106 }
107
108 void DetailScreen::updateTime()
109 {
110     tripTime_->setPlainText(formatTime(Odometer::instance().getTripTime()));
111     totalTime_->setPlainText(formatTime(Odometer::instance().getTotalTime()));
112 }
113
114 void DetailScreen::updateUnits()
115 {
116     QString unit = Odometer::getUnit();
117     QString speedUnit = Odometer::getSpeedUnit();
118
119     tripUnit_->setPlainText(unit);
120     totalUnit_->setPlainText(unit);
121     speedUnit_->setPlainText(speedUnit);
122     avgSpeedUnit_->setPlainText(speedUnit);
123     maxSpeedUnit_->setPlainText(speedUnit);
124
125     update();
126 }
127
128 void DetailScreen::reArrange()
129 {
130     ThemeScreen::reArrange();
131
132     int width = getScene()->width();
133     int height = getScene()->height();
134
135     int lineHeight = height / 9;
136
137     int padding = width / 22;
138     int area1 = (width / 3) + ITEM_WIDTH / 2 + 20;
139     int area2 = area1 + ITEM_WIDTH;
140
141     tripLabel_->setPos(padding, START_HEIGHT);
142     trip_->setPos(area1, START_HEIGHT);
143     tripUnit_->setPos(area2, START_HEIGHT);
144
145     totalLabel_->setPos(padding, START_HEIGHT + lineHeight);
146     total_->setPos(area1, START_HEIGHT + lineHeight);
147     totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
148
149     speedLabel_->setPos(padding, START_HEIGHT + 2 * lineHeight);
150     speed_->setPos(area1, START_HEIGHT + 2 * lineHeight);
151     speedUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
152
153     avgSpeedLabel_->setPos(padding, START_HEIGHT + 3 * lineHeight);
154     avgSpeed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
155     avgSpeedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
156
157     maxSpeedLabel_->setPos(padding, START_HEIGHT + 4 * lineHeight);
158     maxSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
159     maxSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
160
161     tripTimeLabel_->setPos(padding, START_HEIGHT + 5 * lineHeight);
162     tripTime_->setPos(area1, START_HEIGHT + 5 * lineHeight);
163
164     totalTimeLabel_->setPos(padding, START_HEIGHT + 6 * lineHeight);
165     totalTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
166
167     int y1 = START_HEIGHT + 2 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
168     int y2 = START_HEIGHT + 5 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
169     int x = width - padding;
170     line1_->setLine(padding + 2, y1, x, y1);
171     line2_->setLine(padding + 2, y2, x, y2);
172
173     strength_->setPos(width - padding - 64, height - padding - 41);
174 }
175
176 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
177 {
178     QGraphicsTextItem* item = new QGraphicsTextItem(text);
179     QFont font(FONT, FONT_SIZE);
180     font.setBold(true);
181     item->setFont(font);
182     item->setDefaultTextColor(QColor(DEFAULT_COLOR));
183     item->setZValue(999);
184     getScene()->addItem(item);
185     return item;
186 }
187
188 QGraphicsLineItem* DetailScreen::createLine()
189 {
190     QGraphicsLineItem* item = new QGraphicsLineItem;
191     QColor pColor(DEFAULT_COLOR);
192     QPen p(pColor);
193     p.setWidth(1);
194     item->setPen(p);
195     item->setZValue(999);
196     getScene()->addItem(item);
197     return item;
198 }
199
200 QString DetailScreen::roundDouble(double number)
201 {
202     QString result;
203     result.sprintf("%.2lf", number);
204     return result;
205 }
206
207 QString DetailScreen::formatTime(qulonglong time)
208 {
209     qulonglong secs = time % 60;
210     qulonglong mins_tmp = time / 60;
211     qulonglong mins = mins_tmp % 60;
212     qulonglong hours = mins_tmp / 60;
213
214     static char format[] = "%02d";
215
216     return QString().sprintf(format, hours) + ":" +
217            QString().sprintf(format, mins) + ":" +
218            QString().sprintf(format, secs);
219 }
220
221 int DetailScreen::getStrength()
222 {
223     double strength = round(Odometer::instance().getSignalStrength() / 25.0);
224     return static_cast<int>(strength);
225 }
226
227 void DetailScreen::setColor(QString const& color)
228 {
229     QColor c(color);
230
231     tripLabel_->setDefaultTextColor(c);
232     trip_->setDefaultTextColor(c);
233     tripUnit_->setDefaultTextColor(c);
234     totalLabel_->setDefaultTextColor(c);
235     total_->setDefaultTextColor(c);
236     totalUnit_->setDefaultTextColor(c);
237     speedLabel_->setDefaultTextColor(c);
238     speed_->setDefaultTextColor(c);
239     speedUnit_->setDefaultTextColor(c);
240     avgSpeedLabel_->setDefaultTextColor(c);
241     avgSpeed_->setDefaultTextColor(c);
242     avgSpeedUnit_->setDefaultTextColor(c);
243     maxSpeedLabel_->setDefaultTextColor(c);
244     maxSpeed_->setDefaultTextColor(c);
245     maxSpeedUnit_->setDefaultTextColor(c);
246     tripTimeLabel_->setDefaultTextColor(c);
247     tripTime_->setDefaultTextColor(c);
248     totalTimeLabel_->setDefaultTextColor(c);
249     totalTime_->setDefaultTextColor(c);
250
251     QPen p(c);
252     p.setWidth(1);
253
254     line1_->setPen(p);
255     line2_->setPen(p);
256 }