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 = 80;
37 int const ITEM_WIDTH = 130;
41 DetailScreen::DetailScreen(QWidget* parent): ThemeScreen(parent)
43 QString unit = Odometer::getUnit();
44 QString speedUnit = Odometer::getSpeedUnit();
46 getScene()->setBackgroundBrush(QBrush(QColor(DEFAULT_BACKGROUND_COLOR)));
48 tripLabel_ = createItem(tr("Trip"));
49 trip_ = createItem(roundDouble(Odometer::instance().getTrip()));
50 tripUnit_ = createItem(unit);
52 totalLabel_ = createItem(tr("Total"));
53 total_ = createItem(roundDouble(Odometer::instance().getTotal()));
54 totalUnit_ = createItem(unit);
56 speedLabel_ = createItem(tr("Speed"));
57 speed_ = createItem(roundDouble(0.0));
58 speedUnit_ = createItem(speedUnit);
60 avgSpeedLabel_ = createItem(tr("Average speed"));
61 avgSpeed_ = createItem(roundDouble(Odometer::instance().getAverageSpeed()));
62 avgSpeedUnit_ = createItem(speedUnit);
64 maxSpeedLabel_ = createItem(tr("Max speed"));
65 maxSpeed_ = createItem(roundDouble(Odometer::instance().getMaxSpeed()));
66 maxSpeedUnit_ = createItem(speedUnit);
68 tripTimeLabel_ = createItem(tr("Trip time"));
69 tripTime_ = createItem(formatTime(Odometer::instance().getTripTime()));
71 totalTimeLabel_ = createItem(tr("Total time"));
72 totalTime_ = createItem(formatTime(Odometer::instance().getTotalTime()));
74 line1_ = createLine();
75 line2_ = createLine();
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_);
83 connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
84 connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
89 void DetailScreen::update()
91 ThemeScreen::update();
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()));
99 int strength = getStrength();
101 if(strength != currentStrength_)
103 currentStrength_ = strength;
104 strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(strength) + ".png"));
108 void DetailScreen::updateTime()
110 tripTime_->setPlainText(formatTime(Odometer::instance().getTripTime()));
111 totalTime_->setPlainText(formatTime(Odometer::instance().getTotalTime()));
114 void DetailScreen::updateUnits()
116 QString unit = Odometer::getUnit();
117 QString speedUnit = Odometer::getSpeedUnit();
119 tripUnit_->setPlainText(unit);
120 totalUnit_->setPlainText(unit);
121 speedUnit_->setPlainText(speedUnit);
122 avgSpeedUnit_->setPlainText(speedUnit);
123 maxSpeedUnit_->setPlainText(speedUnit);
128 void DetailScreen::reArrange()
130 ThemeScreen::reArrange();
132 int width = getScene()->width();
133 int height = getScene()->height();
135 int lineHeight = height / 9;
137 int padding = width / 22;
138 int area1 = (width / 3) + ITEM_WIDTH / 2 + 20;
139 int area2 = area1 + ITEM_WIDTH;
141 tripLabel_->setPos(padding, START_HEIGHT);
142 trip_->setPos(area1, START_HEIGHT);
143 tripUnit_->setPos(area2, START_HEIGHT);
145 totalLabel_->setPos(padding, START_HEIGHT + lineHeight);
146 total_->setPos(area1, START_HEIGHT + lineHeight);
147 totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
149 speedLabel_->setPos(padding, START_HEIGHT + 2 * lineHeight);
150 speed_->setPos(area1, START_HEIGHT + 2 * lineHeight);
151 speedUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
153 avgSpeedLabel_->setPos(padding, START_HEIGHT + 3 * lineHeight);
154 avgSpeed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
155 avgSpeedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
157 maxSpeedLabel_->setPos(padding, START_HEIGHT + 4 * lineHeight);
158 maxSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
159 maxSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
161 tripTimeLabel_->setPos(padding, START_HEIGHT + 5 * lineHeight);
162 tripTime_->setPos(area1, START_HEIGHT + 5 * lineHeight);
164 totalTimeLabel_->setPos(padding, START_HEIGHT + 6 * lineHeight);
165 totalTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
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);
173 strength_->setPos(width - padding - 64, height - padding - 41);
176 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
178 QGraphicsTextItem* item = new QGraphicsTextItem(text);
179 QFont font(FONT, FONT_SIZE);
182 item->setDefaultTextColor(QColor(DEFAULT_COLOR));
183 item->setZValue(999);
184 getScene()->addItem(item);
188 QGraphicsLineItem* DetailScreen::createLine()
190 QGraphicsLineItem* item = new QGraphicsLineItem;
191 QColor pColor(DEFAULT_COLOR);
195 item->setZValue(999);
196 getScene()->addItem(item);
200 QString DetailScreen::roundDouble(double number)
203 result.sprintf("%.2lf", number);
207 QString DetailScreen::formatTime(qulonglong time)
209 qulonglong secs = time % 60;
210 qulonglong mins_tmp = time / 60;
211 qulonglong mins = mins_tmp % 60;
212 qulonglong hours = mins_tmp / 60;
214 static char format[] = "%02d";
216 return QString().sprintf(format, hours) + ":" +
217 QString().sprintf(format, mins) + ":" +
218 QString().sprintf(format, secs);
221 int DetailScreen::getStrength()
223 double strength = round(Odometer::instance().getSignalStrength() / 25.0);
224 return static_cast<int>(strength);
227 void DetailScreen::setColor(QString const& color)
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);