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 BACKGROUND = ":/resources/themes/default/background.png";
34 int const FONT_SIZE = 20;
35 int const START_HEIGHT = 80;
36 int const ITEM_WIDTH = 130;
39 DetailScreen::DetailScreen(QWidget* parent): GraphicsScreen(parent)
41 QString unit = Odometer::getUnit();
42 QString speedUnit = Odometer::getSpeedUnit();
44 QGraphicsPixmapItem* background = new QGraphicsPixmapItem(QPixmap(BACKGROUND));
45 getScene()->addItem(background);
47 tripLabel_ = createItem(tr("Trip"));
48 trip_ = createItem(roundDouble(Odometer::instance().getTrip()));
49 tripUnit_ = createItem(unit);
51 totalLabel_ = createItem(tr("Total"));
52 total_ = createItem(roundDouble(Odometer::instance().getTotal()));
53 totalUnit_ = createItem(unit);
55 speedLabel_ = createItem(tr("Speed"));
56 speed_ = createItem(roundDouble(0.0));
57 speedUnit_ = createItem(speedUnit);
59 avgSpeedLabel_ = createItem(tr("Average speed"));
60 avgSpeed_ = createItem(roundDouble(Odometer::instance().getAverageSpeed()));
61 avgSpeedUnit_ = createItem(speedUnit);
63 maxSpeedLabel_ = createItem(tr("Max speed"));
64 maxSpeed_ = createItem(roundDouble(Odometer::instance().getMaxSpeed()));
65 maxSpeedUnit_ = createItem(speedUnit);
67 tripTimeLabel_ = createItem(tr("Trip time"));
68 tripTime_ = createItem(formatTime(Odometer::instance().getTripTime()));
70 totalTimeLabel_ = createItem(tr("Total time"));
71 totalTime_ = createItem(formatTime(Odometer::instance().getTotalTime()));
73 line1_ = createLine();
74 line2_ = createLine();
76 strength_ = new QGraphicsPixmapItem;
77 currentStrength_ = getStrength();
78 strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(currentStrength_) + ".png"));
79 getScene()->addItem(strength_);
81 connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
82 connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
87 void DetailScreen::update()
89 speed_->setPlainText(roundDouble(Odometer::instance().getLatestFix().speed));
90 trip_->setPlainText(roundDouble(Odometer::instance().getTrip()));
91 total_->setPlainText(roundDouble(Odometer::instance().getTotal()));
92 avgSpeed_->setPlainText(roundDouble(Odometer::instance().getAverageSpeed()));
93 maxSpeed_->setPlainText(roundDouble(Odometer::instance().getMaxSpeed()));
95 int strength = getStrength();
97 if(strength != currentStrength_)
99 currentStrength_ = strength;
100 strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(strength) + ".png"));
104 void DetailScreen::updateTime()
106 tripTime_->setPlainText(formatTime(Odometer::instance().getTripTime()));
107 totalTime_->setPlainText(formatTime(Odometer::instance().getTotalTime()));
110 void DetailScreen::updateUnits()
112 QString unit = Odometer::getUnit();
113 QString speedUnit = Odometer::getSpeedUnit();
115 tripUnit_->setPlainText(unit);
116 totalUnit_->setPlainText(unit);
117 speedUnit_->setPlainText(speedUnit);
118 avgSpeedUnit_->setPlainText(speedUnit);
119 maxSpeedUnit_->setPlainText(speedUnit);
124 void DetailScreen::reArrange()
126 GraphicsScreen::reArrange();
128 int width = getScene()->width();
129 int height = getScene()->height();
131 int lineHeight = height / 9;
133 int padding = width / 22;
134 int area1 = (width / 3) + ITEM_WIDTH / 2 + 20;
135 int area2 = area1 + ITEM_WIDTH;
137 tripLabel_->setPos(padding, START_HEIGHT);
138 trip_->setPos(area1, START_HEIGHT);
139 tripUnit_->setPos(area2, START_HEIGHT);
141 totalLabel_->setPos(padding, START_HEIGHT + lineHeight);
142 total_->setPos(area1, START_HEIGHT + lineHeight);
143 totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
145 speedLabel_->setPos(padding, START_HEIGHT + 2 * lineHeight);
146 speed_->setPos(area1, START_HEIGHT + 2 * lineHeight);
147 speedUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
149 avgSpeedLabel_->setPos(padding, START_HEIGHT + 3 * lineHeight);
150 avgSpeed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
151 avgSpeedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
153 maxSpeedLabel_->setPos(padding, START_HEIGHT + 4 * lineHeight);
154 maxSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
155 maxSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
157 tripTimeLabel_->setPos(padding, START_HEIGHT + 5 * lineHeight);
158 tripTime_->setPos(area1, START_HEIGHT + 5 * lineHeight);
160 totalTimeLabel_->setPos(padding, START_HEIGHT + 6 * lineHeight);
161 totalTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
163 int y1 = START_HEIGHT + 2 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
164 int y2 = START_HEIGHT + 5 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
165 int x = width - padding;
166 line1_->setLine(padding + 2, y1, x, y1);
167 line2_->setLine(padding + 2, y2, x, y2);
169 strength_->setPos(width - padding - 64, height - padding - 41);
172 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
174 QGraphicsTextItem* item = new QGraphicsTextItem(text);
175 QFont font(FONT, FONT_SIZE);
178 item->setDefaultTextColor(QColor("#fff"));
179 getScene()->addItem(item);
183 QGraphicsLineItem* DetailScreen::createLine()
185 QGraphicsLineItem* item = new QGraphicsLineItem;
186 QPen pen(QColor("#ddd"));
189 getScene()->addItem(item);
193 QString DetailScreen::roundDouble(double number)
196 result.sprintf("%.2lf", number);
200 QString DetailScreen::formatTime(qulonglong time)
202 qulonglong secs = time % 60;
203 qulonglong mins_tmp = time / 60;
204 qulonglong mins = mins_tmp % 60;
205 qulonglong hours = mins_tmp / 60;
207 static char format[] = "%02d";
209 return QString().sprintf(format, hours) + ":" +
210 QString().sprintf(format, mins) + ":" +
211 QString().sprintf(format, secs);
214 int DetailScreen::getStrength()
216 double strength = round(Odometer::instance().getSignalStrength() / 25.0);
217 return static_cast<int>(strength);