Added signal strength indicator to detail screen. Changed speed treshold to change...
[jspeed] / src / detailscreen.cpp
index ba54052..660ba27 100644 (file)
 
 #include <QtGui/QGraphicsTextItem>
 #include <QtGui/QGraphicsLineItem>
+#include <QtGui/QGraphicsPixmapItem>
+#include <QtGui/QPixmap>
 #include <QtCore/QString>
 #include <QtCore/QDebug>
+#include <math.h>
 #include "detailscreen.h"
 #include "odometer.h"
 #include "graphicsscene.h"
@@ -29,7 +32,6 @@ namespace
     QString const FONT = "Tahoma";
     QString const BACKGROUND = ":/resources/themes/default/background.png";
     int const FONT_SIZE = 20;
-    int const PADDING_LEFT = 40;
     int const START_HEIGHT = 80;
     int const ITEM_WIDTH = 130;
 }
@@ -71,6 +73,11 @@ DetailScreen::DetailScreen(QWidget* parent): GraphicsScreen(parent)
     line1_ = createLine();
     line2_ = createLine();
 
+    strength_ = new QGraphicsPixmapItem;
+    currentStrength_ = getStrength();
+    strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(currentStrength_) + ".png"));
+    getScene()->addItem(strength_);
+
     connect(&(Odometer::instance()), SIGNAL(timeUpdated()), this, SLOT(updateTime()));
     connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(updateUnits()));
 
@@ -84,6 +91,14 @@ void DetailScreen::update()
     total_->setPlainText(roundDouble(Odometer::instance().getTotal()));
     avgSpeed_->setPlainText(roundDouble(Odometer::instance().getAverageSpeed()));
     maxSpeed_->setPlainText(roundDouble(Odometer::instance().getMaxSpeed()));
+
+    int strength = getStrength();
+
+    if(strength != currentStrength_)
+    {
+        currentStrength_ = strength;
+        strength_->setPixmap(QPixmap(":/resources/signal_" + QString::number(strength) + ".png"));
+    }
 }
 
 void DetailScreen::updateTime()
@@ -110,42 +125,48 @@ void DetailScreen::reArrange()
 {
     GraphicsScreen::reArrange();
 
-    int lineHeight = getScene()->height() / 9;
+    int width = getScene()->width();
+    int height = getScene()->height();
+
+    int lineHeight = height / 9;
 
-    int area1 = (getScene()->width() / 3) + ITEM_WIDTH / 2 + 20;
+    int padding = width / 22;
+    int area1 = (width / 3) + ITEM_WIDTH / 2 + 20;
     int area2 = area1 + ITEM_WIDTH;
 
-    tripLabel_->setPos(PADDING_LEFT, START_HEIGHT);
+    tripLabel_->setPos(padding, START_HEIGHT);
     trip_->setPos(area1, START_HEIGHT);
     tripUnit_->setPos(area2, START_HEIGHT);
 
-    totalLabel_->setPos(PADDING_LEFT, START_HEIGHT + lineHeight);
+    totalLabel_->setPos(padding, START_HEIGHT + lineHeight);
     total_->setPos(area1, START_HEIGHT + lineHeight);
     totalUnit_->setPos(area2, START_HEIGHT + lineHeight);
 
-    speedLabel_->setPos(PADDING_LEFT, START_HEIGHT + 2 * lineHeight);
+    speedLabel_->setPos(padding, START_HEIGHT + 2 * lineHeight);
     speed_->setPos(area1, START_HEIGHT + 2 * lineHeight);
     speedUnit_->setPos(area2, START_HEIGHT + 2 * lineHeight);
 
-    avgSpeedLabel_->setPos(PADDING_LEFT, START_HEIGHT + 3 * lineHeight);
+    avgSpeedLabel_->setPos(padding, START_HEIGHT + 3 * lineHeight);
     avgSpeed_->setPos(area1, START_HEIGHT + 3 * lineHeight);
     avgSpeedUnit_->setPos(area2, START_HEIGHT + 3 * lineHeight);
 
-    maxSpeedLabel_->setPos(PADDING_LEFT, START_HEIGHT + 4 * lineHeight);
+    maxSpeedLabel_->setPos(padding, START_HEIGHT + 4 * lineHeight);
     maxSpeed_->setPos(area1, START_HEIGHT + 4 * lineHeight);
     maxSpeedUnit_->setPos(area2, START_HEIGHT + 4 * lineHeight);
 
-    tripTimeLabel_->setPos(PADDING_LEFT, START_HEIGHT + 5 * lineHeight);
+    tripTimeLabel_->setPos(padding, START_HEIGHT + 5 * lineHeight);
     tripTime_->setPos(area1, START_HEIGHT + 5 * lineHeight);
 
-    totalTimeLabel_->setPos(PADDING_LEFT, START_HEIGHT + 6 * lineHeight);
+    totalTimeLabel_->setPos(padding, START_HEIGHT + 6 * lineHeight);
     totalTime_->setPos(area1, START_HEIGHT + 6 * lineHeight);
 
     int y1 = START_HEIGHT + 2 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
     int y2 = START_HEIGHT + 5 * lineHeight - lineHeight / 2 + FONT_SIZE + 2;
-    int x = getScene()->width() - PADDING_LEFT;
-    line1_->setLine(PADDING_LEFT + 2, y1, x, y1);
-    line2_->setLine(PADDING_LEFT + 2, y2, x, y2);
+    int x = width - padding;
+    line1_->setLine(padding + 2, y1, x, y1);
+    line2_->setLine(padding + 2, y2, x, y2);
+
+    strength_->setPos(width - padding - 64, height - padding - 41);
 }
 
 QGraphicsTextItem* DetailScreen::createItem(QString const& text)
@@ -189,3 +210,9 @@ QString DetailScreen::formatTime(qulonglong time)
            QString().sprintf(format, mins) + ":" +
            QString().sprintf(format, secs);
 }
+
+int DetailScreen::getStrength()
+{
+    double strength = round(Odometer::instance().getSignalStrength() / 25.0);
+    return static_cast<int>(strength);
+}