Use region identifier from settings. Update region IDs.
[yandex-traffic] / mainwidget.cpp
index a9c1277..1477f9c 100644 (file)
@@ -1,7 +1,7 @@
 #include <QtGui>
 
 #include "mainwidget.hpp"
-
+#include "settingsDialog.hpp"
 
 
 // --------------------------------------------------
@@ -11,63 +11,137 @@ MainWidget::MainWidget ()
     : QWidget ()
 {
 #ifdef Q_WS_MAEMO_5
-        setAttribute(Qt::WA_TranslucentBackground);
+    setAttribute(Qt::WA_TranslucentBackground);
 #endif
-        _light = new TrafficLight (this);
-        _label = new QLabel (this);
+    _light = new TrafficLight (this);
+    _label = new QLabel (this);
+
+    _label->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter);
+
+    _traffic = new Traffic;
+    _regions = new RegionsTable;
+    _settings = new Settings;
+
+    QHBoxLayout *layout = new QHBoxLayout;
+    layout->addWidget (_light);
+    layout->addWidget (_label);
+    setLayout (layout);
 
-        QHBoxLayout *layout = new QHBoxLayout;
-        layout->addWidget (_light);
-        layout->addWidget (_label);
-        setLayout (layout);
+    _light->setVisible (_settings->check (Settings::C_ShowLight));
 
-        connect (&_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
+    updateSize ();
 
-        // every 5 minutes (TODO, make option)
-        startTimer (5*60*1000);
+    connect (_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
 
-        // perform update just after creation
-        _traffic.update ();
+    // every 5 minutes (TODO, make option)
+    startTimer (5*60*1000);
+
+    updateData ();
 }
 
 
-QSize MainWidget::sizeHint() const
+MainWidget::~MainWidget ()
 {
-    return _label->sizeHint () + _light->sizeHint () + QSize (20, 0);
+    delete _traffic;
+    delete _regions;
+    delete _settings;
+
+    delete _light;
+    delete _label;
 }
 
 
 void MainWidget::paintEvent(QPaintEvent *event)
 {
     QPainter p(this);
-    p.setBrush(QColor(0, 0, 0, 128));
-    p.setPen(Qt::NoPen);
-    p.drawRoundedRect(rect(), 10, 10);
-    p.end();
+    p.setBrush (QColor(0, 0, 0, 128));
+    p.setPen (Qt::NoPen);
+    p.drawRoundedRect (rect(), 10, 10);
+    p.end ();
 
-    QWidget::paintEvent(event);
+    QWidget::paintEvent (event);
 }
 
 
 void MainWidget::timerEvent (QTimerEvent *)
 {
-    // Perform traffic information refresh
-    // TODO: only if internet connection is available
-    _traffic.update ();
+    updateData ();
 }
 
 
+
 void MainWidget::trafficUpdated ()
 {
-    ExtendedTrafficInfo info = _traffic.lookup_ext ("1");
+    ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ());
 
     if (info.valid ()) {
+        QString data;
+        bool first = true;
         _light->setColor (info.color ());
-        _label->setText (QString ("%1, %2\n%3")
-                         .arg (QString::number (info.level ()))
-                         .arg (info.localtime ())
-                         .arg (info.hint ()));
+
+        if (_settings->check (Settings::C_ShowRank)) {
+            data.append (QString::number (info.level ()));
+            data.append (info.level () > 1 ? tr (" points") : tr (" point"));
+            first = false;
+        }
+
+        if (_settings->check (Settings::C_ShowTime)) {
+            if (!first)
+                data.append (", ");
+            data.append (info.localtime ());
+            first = false;
+        }
+
+        if (_settings->check (Settings::C_ShowHint)) {
+            if (!first)
+                data.append ("\n");
+            data.append (info.hint ());
+        }
+
+        _label->setText (data);
     }
-    else
+    else {
         _light->setColor (ExtendedTrafficInfo::Unknown);
+        _label->setText (tr ("No data"));
+    }
+}
+
+
+void MainWidget::updateData ()
+{
+    // Here we need to check for internet connection
+    _traffic->update ();
+}
+
+
+void MainWidget::settingsDialog ()
+{
+    SettingsDialog dlg (_settings);
+
+    dlg.exec ();
+
+    // Handle settings
+    _light->setVisible (_settings->check (Settings::C_ShowLight));
+
+    updateSize ();
+    trafficUpdated ();
+}
+
+
+void MainWidget::updateSize ()
+{
+    QSize minSize (0, 80);
+
+    if (_settings->check (Settings::C_ShowLight))
+        minSize += QSize (80, 0);
+    if (_settings->check (Settings::C_ShowHint))
+        minSize += QSize (270, 0);
+    else {
+        if (_settings->check (Settings::C_ShowTime))
+            minSize += QSize (75, 0);
+        if (_settings->check (Settings::C_ShowRank))
+            minSize += QSize (75, 0);
+    }
+
+    setFixedSize (minSize);
 }