Improved special field handling in text element.
[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/QGraphicsView>
22 #include <QtGui/QApplication>
23 #include <QtGui/QDesktopWidget>
24 #include <QtCore/QString>
25 #include <QtCore/QDebug>
26 #include <math.h>
27 #include "detailscreen.h"
28 #include "odometer.h"
29 #include "graphicsscene.h"
30
31 namespace
32 {
33     QString const FONT = "Tahoma";
34     QString const BACKGROUND = ":/resources/themes/default/background.png";
35     int const FONT_SIZE = 20;
36     int const PADDING_LEFT = 40;
37     int const START_HEIGHT = 80;
38     int const ITEM_WIDTH = 130;
39 }
40
41 DetailScreen::DetailScreen(QWidget* parent): GraphicsScreen(parent)
42 {
43     QString unit = Odometer::getUnit();
44     QString speedUnit = Odometer::getSpeedUnit();
45
46     QGraphicsPixmapItem* background = new QGraphicsPixmapItem(QPixmap(BACKGROUND));
47     getScene()->addItem(background);
48
49     tripLabel_ = createItem(tr("Trip"));
50     trip_ = createItem(roundDouble(Odometer::instance().getTrip()));
51     tripUnit_ = createItem(unit);
52
53     totalLabel_ = createItem(tr("Total"));
54     total_ = createItem(roundDouble(Odometer::instance().getTotal()));
55     totalUnit_ = createItem(unit);
56
57     speedLabel_ = createItem(tr("Speed"));
58     speed_ = createItem(roundDouble(0.0));
59     speedUnit_ = createItem(speedUnit);
60
61     avgSpeedLabel_ = createItem(tr("Average speed"));
62     avgSpeed_ = createItem(roundDouble(Odometer::instance().getAverageSpeed()));
63     avgSpeedUnit_ = createItem(speedUnit);
64
65     maxSpeedLabel_ = createItem(tr("Max speed"));
66     maxSpeed_ = createItem(roundDouble(Odometer::instance().getMaxSpeed()));
67     maxSpeedUnit_ = createItem(speedUnit);
68
69     tripTimeLabel_ = createItem(tr("Trip time"));
70     tripTime_ = createItem(formatTime(Odometer::instance().getTripTime()));
71
72     totalTimeLabel_ = createItem(tr("Total time"));
73     totalTime_ = createItem(formatTime(Odometer::instance().getTotalTime()));
74
75     line1_ = createLine();
76     line2_ = createLine();
77
78     connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
79     connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
80
81     reArrange();
82 }
83
84 void DetailScreen::update()
85 {
86     speed_->setPlainText(roundDouble(Odometer::instance().getLatestFix().speed));
87     trip_->setPlainText(roundDouble(Odometer::instance().getTrip()));
88     total_->setPlainText(roundDouble(Odometer::instance().getTotal()));
89     avgSpeed_->setPlainText(roundDouble(Odometer::instance().getAverageSpeed()));
90     maxSpeed_->setPlainText(roundDouble(Odometer::instance().getMaxSpeed()));
91 }
92
93 void DetailScreen::updateTime()
94 {
95     tripTime_->setPlainText(formatTime(Odometer::instance().getTripTime()));
96     totalTime_->setPlainText(formatTime(Odometer::instance().getTotalTime()));
97 }
98
99 void DetailScreen::updateUnits()
100 {
101     QString unit = Odometer::getUnit();
102     QString speedUnit = Odometer::getSpeedUnit();
103
104     tripUnit_->setPlainText(unit);
105     totalUnit_->setPlainText(unit);
106     speedUnit_->setPlainText(speedUnit);
107     avgSpeedUnit_->setPlainText(speedUnit);
108     maxSpeedUnit_->setPlainText(speedUnit);
109
110     update();
111 }
112
113 void DetailScreen::reArrange()
114 {
115     GraphicsScreen::reArrange();
116
117     int lineHeight = getScene()->height() / 9;
118
119     int area1 = (getScene()->width() / 3) + ITEM_WIDTH / 2 + 20;
120     int area2 = area1 + ITEM_WIDTH;
121
122     tripLabel_->setPos(PADDING_LEFT, START_HEIGHT);
123     trip_->setPos(area1, START_HEIGHT);
124     tripUnit_->setPos(area2, START_HEIGHT);
125
126     totalLabel_->setPos(PADDING_LEFT, START_HEIGHT + lineHeight);
127     total_->setPos(area1, START_HEIGHT + lineHeight);
128     totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
129
130     speedLabel_->setPos(PADDING_LEFT, START_HEIGHT + 2 * lineHeight);
131     speed_->setPos(area1, START_HEIGHT + 2 * lineHeight);
132     speedUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
133
134     avgSpeedLabel_->setPos(PADDING_LEFT, START_HEIGHT + 3 * lineHeight);
135     avgSpeed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
136     avgSpeedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
137
138     maxSpeedLabel_->setPos(PADDING_LEFT, START_HEIGHT + 4 * lineHeight);
139     maxSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
140     maxSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
141
142     tripTimeLabel_->setPos(PADDING_LEFT, START_HEIGHT + 5 * lineHeight);
143     tripTime_->setPos(area1, START_HEIGHT + 5 * lineHeight);
144
145     totalTimeLabel_->setPos(PADDING_LEFT, START_HEIGHT + 6 * lineHeight);
146     totalTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
147
148     int y1 = START_HEIGHT + 2 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
149     int y2 = START_HEIGHT + 5 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
150     int x = getScene()->width() - PADDING_LEFT;
151     line1_->setLine(PADDING_LEFT + 2, y1, x, y1);
152     line2_->setLine(PADDING_LEFT + 2, y2, x, y2);
153 }
154
155 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
156 {
157     QGraphicsTextItem* item = new QGraphicsTextItem(text);
158     QFont font(FONT, FONT_SIZE);
159     font.setBold(true);
160     item->setFont(font);
161     item->setDefaultTextColor(QColor("#fff"));
162     getScene()->addItem(item);
163     return item;
164 }
165
166 QGraphicsLineItem* DetailScreen::createLine()
167 {
168     QGraphicsLineItem* item = new QGraphicsLineItem;
169     QPen pen(QColor("#ddd"));
170     pen.setWidth(1);
171     item->setPen(pen);
172     getScene()->addItem(item);
173     return item;
174 }
175
176 QString DetailScreen::roundDouble(double number)
177 {
178     QString result;
179     result.sprintf("%.2lf", number);
180     return result;
181 }
182
183 QString DetailScreen::formatTime(qulonglong time)
184 {
185     qulonglong secs = time % 60;
186     qulonglong mins_tmp = time / 60;
187     qulonglong mins = mins_tmp % 60;
188     qulonglong hours = mins_tmp / 60;
189
190     static char format[] = "%02d";
191
192     return QString().sprintf(format, hours) + ":" +
193            QString().sprintf(format, mins) + ":" +
194            QString().sprintf(format, secs);
195 }