2 * This file is part of jSpeed.
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.
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.
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/>.
19 #include <QtGui/QGraphicsTextItem>
20 #include <QtGui/QGraphicsLineItem>
21 #include <QtGui/QGraphicsPixmapItem>
22 #include <QtGui/QPixmap>
23 #include <QtCore/QString>
24 #include <QtCore/QDebug>
26 #include "detailscreen.h"
28 #include "graphicsscene.h"
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 = 61;
37 int const ITEM_WIDTH = 130;
41 DetailScreen::DetailScreen(QWidget* parent): ThemeScreen(parent)
43 QString unit = Odometer::getUnit();
44 QString speedUnit = Odometer::getSpeedUnit();
45 QString meterUnit = Odometer::getMeterUnit();
47 getScene()->setBackgroundBrush(QBrush(QColor(DEFAULT_BACKGROUND_COLOR)));
49 tripLabel_ = createItem(tr("Trip"));
50 trip_ = createItem(roundDouble(Odometer::instance().getTrip()));
51 tripUnit_ = createItem(unit);
53 totalLabel_ = createItem(tr("Total"));
54 total_ = createItem(roundDouble(Odometer::instance().getTotal()));
55 totalUnit_ = createItem(unit);
57 altitudeLabel_ = createItem(tr("Altitude"));
58 altitude_ = createItem(roundDouble(Odometer::instance().getLatestFix().altitude));
59 altitudeUnit_ = createItem(meterUnit);
61 speedLabel_ = createItem(tr("Speed"));
62 speed_ = createItem(roundDouble(0.0));
63 speedUnit_ = createItem(speedUnit);
65 avgSpeedLabel_ = createItem(tr("Average speed"));
66 avgSpeed_ = createItem(roundDouble(Odometer::instance().getAverageSpeed()));
67 avgSpeedUnit_ = createItem(speedUnit);
69 maxSpeedLabel_ = createItem(tr("Max speed"));
70 maxSpeed_ = createItem(roundDouble(Odometer::instance().getMaxSpeed()));
71 maxSpeedUnit_ = createItem(speedUnit);
73 tripTimeLabel_ = createItem(tr("Trip time"));
74 tripTime_ = createItem(formatTime(Odometer::instance().getTripTime()));
76 totalTimeLabel_ = createItem(tr("Total time"));
77 totalTime_ = createItem(formatTime(Odometer::instance().getTotalTime()));
79 line1_ = createLine();
80 line2_ = createLine();
82 strength_ = new QGraphicsPixmapItem;
83 currentStrength_ = getStrength();
84 strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(currentStrength_) + ".png"));
85 strength_->setZValue(999);
86 getScene()->addItem(strength_);
88 connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
89 connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
94 void DetailScreen::update()
96 ThemeScreen::update();
98 speed_->setPlainText(roundDouble(Odometer::instance().getLatestFix().speed));
99 trip_->setPlainText(roundDouble(Odometer::instance().getTrip()));
100 altitude_->setPlainText(roundDouble(Odometer::instance().getLatestFix().altitude));
101 total_->setPlainText(roundDouble(Odometer::instance().getTotal()));
102 avgSpeed_->setPlainText(roundDouble(Odometer::instance().getAverageSpeed()));
103 maxSpeed_->setPlainText(roundDouble(Odometer::instance().getMaxSpeed()));
105 int strength = getStrength();
107 if(strength != currentStrength_)
109 currentStrength_ = strength;
110 strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(strength) + ".png"));
114 void DetailScreen::updateTime()
116 tripTime_->setPlainText(formatTime(Odometer::instance().getTripTime()));
117 totalTime_->setPlainText(formatTime(Odometer::instance().getTotalTime()));
120 void DetailScreen::updateUnits()
122 QString unit = Odometer::getUnit();
123 QString speedUnit = Odometer::getSpeedUnit();
124 QString meterUnit = Odometer::getMeterUnit();
126 tripUnit_->setPlainText(unit);
127 totalUnit_->setPlainText(unit);
128 altitudeUnit_->setPlainText(meterUnit);
129 speedUnit_->setPlainText(speedUnit);
130 avgSpeedUnit_->setPlainText(speedUnit);
131 maxSpeedUnit_->setPlainText(speedUnit);
136 void DetailScreen::reArrange()
138 ThemeScreen::reArrange();
140 int width = getScene()->width();
141 int height = getScene()->height();
143 int lineHeight = (height - START_HEIGHT) / 8;
145 int padding = width / 22;
146 int area1 = (width / 3) + ITEM_WIDTH / 2 + 20;
147 int area2 = area1 + ITEM_WIDTH;
149 tripLabel_->setPos(padding, START_HEIGHT);
150 trip_->setPos(area1, START_HEIGHT);
151 tripUnit_->setPos(area2, START_HEIGHT);
153 totalLabel_->setPos(padding, START_HEIGHT + lineHeight);
154 total_->setPos(area1, START_HEIGHT + lineHeight);
155 totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
157 altitudeLabel_->setPos(padding, START_HEIGHT + 2 * lineHeight);
158 altitude_->setPos(area1, START_HEIGHT + 2 * lineHeight);
159 altitudeUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
161 speedLabel_->setPos(padding, START_HEIGHT + 3 * lineHeight);
162 speed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
163 speedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
165 avgSpeedLabel_->setPos(padding, START_HEIGHT + 4 * lineHeight);
166 avgSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
167 avgSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
169 maxSpeedLabel_->setPos(padding, START_HEIGHT + 5 * lineHeight);
170 maxSpeed_->setPos(area1, START_HEIGHT + 5 * lineHeight);
171 maxSpeedUnit_->setPos(area2, START_HEIGHT + 5 * lineHeight);
173 tripTimeLabel_->setPos(padding, START_HEIGHT + 6 * lineHeight);
174 tripTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
176 totalTimeLabel_->setPos(padding, START_HEIGHT + 7 * lineHeight);
177 totalTime_->setPos(area1, START_HEIGHT + 7 * lineHeight);
179 int y1 = START_HEIGHT + 3 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
180 int y2 = START_HEIGHT + 6 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
181 int x = width - padding;
182 line1_->setLine(padding + 2, y1, x, y1);
183 line2_->setLine(padding + 2, y2, x, y2);
185 strength_->setPos(width - padding - 64, height - padding - 41);
188 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
190 QGraphicsTextItem* item = new QGraphicsTextItem(text);
191 QFont font(FONT, FONT_SIZE);
194 item->setDefaultTextColor(QColor(DEFAULT_COLOR));
195 item->setZValue(999);
196 getScene()->addItem(item);
200 QGraphicsLineItem* DetailScreen::createLine()
202 QGraphicsLineItem* item = new QGraphicsLineItem;
203 QColor pColor(DEFAULT_COLOR);
207 item->setZValue(999);
208 getScene()->addItem(item);
212 QString DetailScreen::roundDouble(double number)
215 result.sprintf("%.2lf", number);
219 QString DetailScreen::formatTime(qulonglong time)
221 qulonglong secs = time % 60;
222 qulonglong mins_tmp = time / 60;
223 qulonglong mins = mins_tmp % 60;
224 qulonglong hours = mins_tmp / 60;
226 static char format[] = "%02d";
228 return QString().sprintf(format, hours) + ":" +
229 QString().sprintf(format, mins) + ":" +
230 QString().sprintf(format, secs);
233 int DetailScreen::getStrength()
235 double strength = round(Odometer::instance().getSignalStrength() / 25.0);
236 return static_cast<int>(strength);
239 void DetailScreen::setColor(QString const& color)
243 tripLabel_->setDefaultTextColor(c);
244 trip_->setDefaultTextColor(c);
245 tripUnit_->setDefaultTextColor(c);
246 totalLabel_->setDefaultTextColor(c);
247 total_->setDefaultTextColor(c);
248 totalUnit_->setDefaultTextColor(c);
249 speedLabel_->setDefaultTextColor(c);
250 speed_->setDefaultTextColor(c);
251 speedUnit_->setDefaultTextColor(c);
252 avgSpeedLabel_->setDefaultTextColor(c);
253 avgSpeed_->setDefaultTextColor(c);
254 avgSpeedUnit_->setDefaultTextColor(c);
255 maxSpeedLabel_->setDefaultTextColor(c);
256 maxSpeed_->setDefaultTextColor(c);
257 maxSpeedUnit_->setDefaultTextColor(c);
258 tripTimeLabel_->setDefaultTextColor(c);
259 tripTime_->setDefaultTextColor(c);
260 totalTimeLabel_->setDefaultTextColor(c);
261 totalTime_->setDefaultTextColor(c);