Widget wich displays light.
authorMax Lapan <max.lapan@gmail.com>
Tue, 9 Mar 2010 17:02:09 +0000 (20:02 +0300)
committerMax Lapan <max.lapan@gmail.com>
Tue, 9 Mar 2010 17:02:09 +0000 (20:02 +0300)
light.cpp [new file with mode: 0644]
light.hpp [new file with mode: 0644]
mainwidget.cpp [new file with mode: 0644]
mainwidget.hpp
yandex-traffic-gui.pri

diff --git a/light.cpp b/light.cpp
new file mode 100644 (file)
index 0000000..dc29991
--- /dev/null
+++ b/light.cpp
@@ -0,0 +1,39 @@
+#include <QtGui>
+#include "light.hpp"
+#include "traffic.hpp"
+
+
+// --------------------------------------------------
+// TrafficLight
+// --------------------------------------------------
+TrafficLight::TrafficLight (QWidget *parent)
+    : QWidget (parent)
+{
+    _color = ExtendedTrafficInfo::Green;
+}
+
+
+void TrafficLight::paintEvent (QPaintEvent *)
+{
+    QPainter painter (this);
+    QColor color;
+    int side = qMin (width (), height ());
+
+    switch (_color) {
+        case ExtendedTrafficInfo::Green:
+            color = Qt::green;
+            break;
+        case ExtendedTrafficInfo::Yellow:
+            color = Qt::yellow;
+            break;
+        case ExtendedTrafficInfo::Red:
+            color = Qt::red;
+            break;
+    }
+
+    painter.setRenderHint(QPainter::Antialiasing);
+    painter.setPen (color);
+    painter.setBrush (color);
+    painter.drawEllipse ((width ()-side)/2, (height ()-side)/2, side, side);
+}
+
diff --git a/light.hpp b/light.hpp
new file mode 100644 (file)
index 0000000..8f00870
--- /dev/null
+++ b/light.hpp
@@ -0,0 +1,31 @@
+#ifndef __LIGHT_H__
+#define __LIGHT_H__
+
+#include <QtGui>
+
+#include "traffic.hpp"
+
+class TrafficLight : public QWidget
+{
+    Q_OBJECT
+
+private:
+    ExtendedTrafficInfo::light_color _color;
+
+protected:
+    void paintEvent (QPaintEvent *event);
+
+public:
+    TrafficLight (QWidget *parent);
+
+    ExtendedTrafficInfo::light_color color () const
+    { return _color; };
+
+    void setColor (ExtendedTrafficInfo::light_color color)
+    { 
+        _color = color;
+        update ();
+    }
+};
+
+#endif // __LIGHT_H__
diff --git a/mainwidget.cpp b/mainwidget.cpp
new file mode 100644 (file)
index 0000000..f6363a6
--- /dev/null
@@ -0,0 +1,31 @@
+#include <QtGui>
+
+#include "mainwidget.hpp"
+
+
+
+// --------------------------------------------------
+// MainWidget
+// --------------------------------------------------
+MainWidget::MainWidget ()
+    : QWidget ()
+{
+#ifdef Q_WS_MAEMO_5
+        setAttribute(Qt::WA_TranslucentBackground);
+#endif
+        _light = new TrafficLight (this);
+        _label = new QLabel (this);
+
+        QHBoxLayout *layout = new QHBoxLayout;
+        layout->addWidget (_light);
+        layout->addWidget (_label);
+        setLayout (layout);
+
+        _label->setText ("Bla-bla");
+}
+
+
+QSize MainWidget::sizeHint() const
+{
+    return QWidget::sizeHint () + QSize (20, 0);
+}
index e7c791f..4e29b66 100644 (file)
@@ -3,36 +3,25 @@
 
 #include <QtGui>
 
-#include <settings.hpp>
-#include <regions.hpp>
+#include "settings.hpp"
+#include "regions.hpp"
+#include "light.hpp"
 
 
-class MainWidget : public QLabel
+class MainWidget : public QWidget
 {
     Q_OBJECT
 private:
     RegionsTable _regions;
     Settings _settings;
 
-public:
-    MainWidget ()
-        : QLabel ()
-    {
-        const RegionInfo *ri;
-
-        setAlignment(Qt::AlignCenter);
-#ifdef Q_WS_MAEMO_5
-        setAttribute(Qt::WA_TranslucentBackground);
-#endif
-        ri = _regions.lookup (_settings.regions ()[0]);
-
-        setText (_settings.regions ().join (", ") + ", " + QString (ri ? ri->name () : ""));
-    }
+    // Widgets
+    TrafficLight *_light;
+    QLabel *_label;
 
-    QSize sizeHint() const
-    {
-        return QLabel::sizeHint() + QSize (20, 0);
-    }
+public:
+    MainWidget ();
+    QSize sizeHint () const;
 
 protected:
     void paintEvent(QPaintEvent *event)
@@ -43,7 +32,7 @@ protected:
         p.drawRoundedRect(rect(), 10, 10);
         p.end();
 
-        QLabel::paintEvent(event);
+        QWidget::paintEvent(event);
     }
 };
 
index c3d87e9..347ffa2 100644 (file)
@@ -1,5 +1,5 @@
-HEADERS += $$PWD/mainwidget.hpp
-SOURCES += 
+HEADERS += $$PWD/mainwidget.hpp $$PWD/light.hpp
+SOURCES += $$PWD/mainwidget.cpp $$PWD/light.cpp
 
 QT += network xml