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