From 5bec72247421946a302b40961c6483c7d377f372 Mon Sep 17 00:00:00 2001 From: Max Lapan Date: Thu, 25 Mar 2010 20:12:23 +0300 Subject: [PATCH] Simplify traffic info class. We don't need to store information we are not going to display :). So, I removed TrafficInfo class completely. ExtendedTrafficInfo renamed to CityTrafficInfo. --- light.cpp | 10 ++-- light.hpp | 6 +-- mainwidget.cpp | 8 +-- tests/traffic/mainwindow.hpp | 2 +- tests/traffic/traffic.pro | 1 + traffic.cpp | 113 ++++++++++++++---------------------------- traffic.hpp | 47 ++++++------------ 7 files changed, 66 insertions(+), 121 deletions(-) diff --git a/light.cpp b/light.cpp index 61613af..a918196 100644 --- a/light.cpp +++ b/light.cpp @@ -9,7 +9,7 @@ TrafficLight::TrafficLight (QWidget *parent) : QWidget (parent) { - _color = ExtendedTrafficInfo::Unknown; + _color = CityTrafficInfo::Unknown; setFixedSize (40, 40); } @@ -21,16 +21,16 @@ void TrafficLight::paintEvent (QPaintEvent *) int side = qMin (width (), height ()); switch (_color) { - case ExtendedTrafficInfo::Unknown: + case CityTrafficInfo::Unknown: color = Qt::gray; break; - case ExtendedTrafficInfo::Green: + case CityTrafficInfo::Green: color = Qt::green; break; - case ExtendedTrafficInfo::Yellow: + case CityTrafficInfo::Yellow: color = Qt::yellow; break; - case ExtendedTrafficInfo::Red: + case CityTrafficInfo::Red: color = Qt::red; break; } diff --git a/light.hpp b/light.hpp index 8f00870..4b5ddef 100644 --- a/light.hpp +++ b/light.hpp @@ -10,7 +10,7 @@ class TrafficLight : public QWidget Q_OBJECT private: - ExtendedTrafficInfo::light_color _color; + CityTrafficInfo::light_color _color; protected: void paintEvent (QPaintEvent *event); @@ -18,10 +18,10 @@ protected: public: TrafficLight (QWidget *parent); - ExtendedTrafficInfo::light_color color () const + CityTrafficInfo::light_color color () const { return _color; }; - void setColor (ExtendedTrafficInfo::light_color color) + void setColor (CityTrafficInfo::light_color color) { _color = color; update (); diff --git a/mainwidget.cpp b/mainwidget.cpp index 545292a..f42b87b 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -20,7 +20,7 @@ MainWidget::MainWidget () setAttribute(Qt::WA_TranslucentBackground); #endif _light = new TrafficLight (this); - _label = new QLabel (this); + _label = new QLabel (tr ("No data"), this); _timer = new QTimer (this); _label->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter); @@ -69,7 +69,7 @@ void MainWidget::paintEvent(QPaintEvent *event) void MainWidget::trafficUpdated () { - ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ()); + CityTrafficInfo info = _traffic->lookup_ext (_settings->regionID ()); if (info.valid ()) { QString data; @@ -86,7 +86,7 @@ void MainWidget::trafficUpdated () if (_settings->check (Settings::C_ShowTime)) { if (!first) data.append (", "); - data.append (info.localtime ()); + data.append (info.ts ().toString ("h:mm")); first = false; } @@ -100,7 +100,7 @@ void MainWidget::trafficUpdated () } else { Log::instance ()->add ("trafficUpdated, but info not valid"); - _light->setColor (ExtendedTrafficInfo::Unknown); + _light->setColor (CityTrafficInfo::Unknown); _label->setText (tr ("No data")); } } diff --git a/tests/traffic/mainwindow.hpp b/tests/traffic/mainwindow.hpp index b45ffca..a074d00 100644 --- a/tests/traffic/mainwindow.hpp +++ b/tests/traffic/mainwindow.hpp @@ -21,7 +21,7 @@ protected slots: void trafficUpdated () { - ExtendedTrafficInfo info; + CityTrafficInfo info; printf ("Traffic data updated:\n"); printf ("ts: %s\n", _traffic.ts ().toString ().toUtf8 ().data ()); diff --git a/tests/traffic/traffic.pro b/tests/traffic/traffic.pro index a1aa5f0..420899c 100644 --- a/tests/traffic/traffic.pro +++ b/tests/traffic/traffic.pro @@ -4,3 +4,4 @@ HEADERS += mainwindow.hpp SOURCES += main.cpp include (../../yandex-traffic-core.pri) + diff --git a/traffic.cpp b/traffic.cpp index 05d5adf..05b1f38 100644 --- a/traffic.cpp +++ b/traffic.cpp @@ -6,22 +6,40 @@ // -------------------------------------------------- -// TrafficInfo +// CityTrafficInfo // -------------------------------------------------- -TrafficInfo::TrafficInfo (const QDomElement& elem) throw (const QString&) +CityTrafficInfo::CityTrafficInfo (const QDomElement& elem) throw (const QString&) + : TrafficInfo () { - _valid = false; - _len1 = getFloatNode (elem, "length_1", 0); - _len2 = getFloatNode (elem, "length_2", 0); - _len = getFloatNode (elem, "length", 0); + QString color; + + setValid (false); _ts = getTSNode (elem, "timestamp"); - _isotime = getStringNode (elem, "isotime"); - _localtime = getStringNode (elem, "localtime"); - _valid = true; + _level = getIntNode (elem, "level", 1); + _tend = getIntNode (elem, "tend", 0); + _hint = getStringNode (elem, "hint"); + + color = getStringNode (elem, "icon"); + if (color == "green") + _color = Green; + else if (color == "yellow") + _color = Yellow; + else if (color == "red") + _color = Red; + else + throw "Color is unknown"; + + setValid (true); +} + + +void CityTrafficInfo::dump () +{ + Log::instance ()->add (QString ("CityTrafficInfo: level = %1, hint = %2").arg (_level).arg (_hint)); } -float TrafficInfo::getFloatNode (const QDomElement& elem, const char* node, float def) +float CityTrafficInfo::getFloatNode (const QDomElement& elem, const char* node, float def) { QDomElement e; bool ok; @@ -37,7 +55,7 @@ float TrafficInfo::getFloatNode (const QDomElement& elem, const char* node, floa } -int TrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def) +int CityTrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def) { QDomElement e; bool ok; @@ -53,7 +71,7 @@ int TrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def) } -QString TrafficInfo::getStringNode (const QDomElement& elem, const char* node) throw (const QString&) +QString CityTrafficInfo::getStringNode (const QDomElement& elem, const char* node) throw (const QString&) { QDomElement e; QString val; @@ -65,7 +83,7 @@ QString TrafficInfo::getStringNode (const QDomElement& elem, const char* node) t } -QDateTime TrafficInfo::getTSNode (const QDomElement& elem, const char* node) throw (const QString&) +QDateTime CityTrafficInfo::getTSNode (const QDomElement& elem, const char* node) throw (const QString&) { QDomElement e; bool ok; @@ -83,47 +101,6 @@ QDateTime TrafficInfo::getTSNode (const QDomElement& elem, const char* node) thr } -void TrafficInfo::dump () -{ - Log::instance ()->add (QString ("TrafficInfo (%1): time = %2").arg (_valid ? "valid" : "not valid").arg (_localtime)); -} - - -// -------------------------------------------------- -// ExtendedTrafficInfo -// -------------------------------------------------- -ExtendedTrafficInfo::ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&) - : TrafficInfo (elem) -{ - QString color; - - setValid (false); - _level_raw = getFloatNode (elem, "level_raw", 0); - _level = getIntNode (elem, "level", 1); - _tend = getIntNode (elem, "tend", 0); - _hint = getStringNode (elem, "hint"); - - color = getStringNode (elem, "icon"); - if (color == "green") - _color = Green; - else if (color == "yellow") - _color = Yellow; - else if (color == "red") - _color = Red; - else - throw "Color is unknown"; - - setValid (true); -} - - -void ExtendedTrafficInfo::dump () -{ - TrafficInfo::dump (); - Log::instance ()->add (QString ("ExtTrafficInfo: level = %1, hint = %2").arg (_level).arg (_hint)); -} - - // -------------------------------------------------- // Traffic // -------------------------------------------------- @@ -165,8 +142,7 @@ bool Traffic::parse_traffic_data (const QString& xml) QDomNode n; bool ok; QString s; - QMap new_info; - QMap new_ext_info; + QMap new_ext_info; if (!doc.setContent (xml)) return false; @@ -193,15 +169,10 @@ bool Traffic::parse_traffic_data (const QString& xml) try { // Check that it is an extended traffic info if (!e.firstChildElement ("level").isNull ()) { - ExtendedTrafficInfo info (e); + CityTrafficInfo info (e); if (info.valid ()) new_ext_info[s] = info; } - else { - TrafficInfo info (e); - if (info.valid ()) - new_info[s] = info; - } } catch (const QString& msg) { } @@ -210,30 +181,18 @@ bool Traffic::parse_traffic_data (const QString& xml) } _ts = new_ts; - _info = new_info; _ext_info = new_ext_info; return true; } -TrafficInfo Traffic::lookup (const QString &id) const -{ - QMap::const_iterator it = _info.find (id); - - if (it == _info.end ()) - return TrafficInfo (); - else - return it.value (); -} - - -ExtendedTrafficInfo Traffic::lookup_ext (const QString &id) const +CityTrafficInfo Traffic::lookup_ext (const QString &id) const { - QMap::const_iterator it = _ext_info.find (id); + QMap::const_iterator it = _ext_info.find (id); if (it == _ext_info.end ()) - return ExtendedTrafficInfo (); + return CityTrafficInfo (); else return it.value (); } diff --git a/traffic.hpp b/traffic.hpp index 03283b8..546b2d8 100644 --- a/traffic.hpp +++ b/traffic.hpp @@ -12,19 +12,9 @@ class TrafficInfo { private: - float _len1, _len2, _len; - QDateTime _ts; - QString _isotime; - QString _localtime; - bool _valid; protected: - float getFloatNode (const QDomElement& elem, const char* node, float def); - int getIntNode (const QDomElement& elem, const char* node, int def); - QString getStringNode (const QDomElement& elem, const char* node) throw (const QString&); - QDateTime getTSNode (const QDomElement& elem, const char* node) throw (const QString&); - void setValid (bool new_val) { _valid = new_val; }; @@ -33,22 +23,12 @@ public: : _valid (false) {}; - TrafficInfo (const QDomElement& elem) throw (const QString&); - bool valid () const { return _valid; }; - - QString localtime () const - { return _localtime; }; - - QDateTime ts () const - { return _ts; }; - - virtual void dump (); }; -class ExtendedTrafficInfo : public TrafficInfo +class CityTrafficInfo : public TrafficInfo { public: enum light_color { @@ -59,18 +39,25 @@ public: }; private: - float _level_raw; - int _level; + QDateTime _ts; + int _level, _tend; light_color _color; - int _tend; QString _hint; + float getFloatNode (const QDomElement& elem, const char* node, float def); + int getIntNode (const QDomElement& elem, const char* node, int def); + QString getStringNode (const QDomElement& elem, const char* node) throw (const QString&); + QDateTime getTSNode (const QDomElement& elem, const char* node) throw (const QString&); + public: - ExtendedTrafficInfo () + CityTrafficInfo () : TrafficInfo () {}; - ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&); + CityTrafficInfo (const QDomElement& elem) throw (const QString&); + + QDateTime ts () const + { return _ts; }; int level () const { return _level; }; @@ -81,7 +68,7 @@ public: QString hint () const { return _hint; }; - ExtendedTrafficInfo::light_color color () const + CityTrafficInfo::light_color color () const { return _color; }; virtual void dump (); @@ -95,8 +82,7 @@ class Traffic : public QObject private: QDateTime _ts; - QMap _info; - QMap _ext_info; + QMap _ext_info; HttpFetcher _fetcher; @@ -116,8 +102,7 @@ public: QDateTime ts () const { return _ts; }; - TrafficInfo lookup (const QString &id) const; - ExtendedTrafficInfo lookup_ext (const QString &id) const; + CityTrafficInfo lookup_ext (const QString &id) const; }; -- 1.7.9.5