Implemented table of regions.
authorMax Lapan <max.lapan@gmail.com>
Sun, 7 Mar 2010 17:47:26 +0000 (20:47 +0300)
committerMax Lapan <max.lapan@gmail.com>
Sun, 7 Mar 2010 17:50:48 +0000 (20:50 +0300)
It is not complete so far (it lacks save/load and refresh code), but it already has tests.

main.cpp
regions.cpp [new file with mode: 0644]
regions.hpp [new file with mode: 0644]
tests/regions/main.cpp [new file with mode: 0644]
tests/regions/regions.pro [new file with mode: 0644]
tests/tests.pro [new file with mode: 0644]
yandex-traffic-core.pri [new file with mode: 0644]
yandex-traffic-widget.pro

index 075f0f7..c3e31ae 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -10,7 +10,7 @@ int main(int argc, char *argv[])
     QMaemo5HomescreenAdaptor *adaptor = new QMaemo5HomescreenAdaptor (&w);
 
     adaptor->setSettingsAvailable (true);
-    
+
     w.show ();
 
     return app.exec ();
diff --git a/regions.cpp b/regions.cpp
new file mode 100644 (file)
index 0000000..9a2c1bc
--- /dev/null
@@ -0,0 +1,31 @@
+#include <QtCore>
+
+#include "regions.hpp"
+
+
+RegionsTable::RegionsTable ()
+{
+    // Load saved table, if failed, initialize default table of regions
+    makeDefaultTable ();
+}
+
+
+void RegionsTable::makeDefaultTable ()
+{
+    _map.clear ();
+    _map.insert ("1", RegionInfo ("1", "Moscow"));
+    _map.insert ("10174", RegionInfo ("10174", "St. Petersburg"));
+    _map.insert ("20544", RegionInfo ("20544", "Kiev"));
+    _map.insert ("11162", RegionInfo ("11162", "Ekaterinburg"));
+}
+
+
+const RegionInfo *RegionsTable::lookup (const QString &id) const
+{
+    QMap<QString, RegionInfo>::const_iterator it = _map.find (id);
+
+    if (it == _map.end ())
+        return NULL;
+    else
+        return &*it;
+}
diff --git a/regions.hpp b/regions.hpp
new file mode 100644 (file)
index 0000000..0c3c6b5
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef __REGIONS_H__
+#define __REGIONS_H__
+
+#include <QtCore>
+
+
+class RegionInfo
+{
+private:
+    QString _id;
+    QString _name;
+
+public:
+    RegionInfo (const QString &id, const QString &name)
+        : _id (id),
+          _name (name)
+    {};
+
+    QString id () const
+    { return _id; };
+
+    QString name () const
+    { return _name; };
+};
+
+
+// Map between region ID and it's information
+class RegionsTable
+{
+private:
+    QMap<QString, RegionInfo> _map;
+
+    void makeDefaultTable ();
+
+public:
+    RegionsTable ();
+
+    const RegionInfo *lookup (const QString &id) const;
+};
+
+#endif // __REGIONS_H__
diff --git a/tests/regions/main.cpp b/tests/regions/main.cpp
new file mode 100644 (file)
index 0000000..a2f6fdc
--- /dev/null
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <QtCore>
+
+#include <regions.hpp>
+
+
+int main()
+{
+    RegionsTable table;
+    const RegionInfo *ri;
+
+    ri = table.lookup ("1");
+    if (ri)
+        printf ("Region 1 is %s\n", ri->name ().toUtf8 ().constData ());
+    else
+        printf ("Region 1 is not found\n");
+
+    return 0;
+}
+
diff --git a/tests/regions/regions.pro b/tests/regions/regions.pro
new file mode 100644 (file)
index 0000000..104a01c
--- /dev/null
@@ -0,0 +1,5 @@
+TEMPLATE = app
+
+SOURCES += main.cpp
+
+include (../../yandex-traffic-core.pri)
diff --git a/tests/tests.pro b/tests/tests.pro
new file mode 100644 (file)
index 0000000..cfcadce
--- /dev/null
@@ -0,0 +1,2 @@
+TEMPLATE = subdirs
+SUBDIRS = regions
diff --git a/yandex-traffic-core.pri b/yandex-traffic-core.pri
new file mode 100644 (file)
index 0000000..d7d6021
--- /dev/null
@@ -0,0 +1,4 @@
+HEADERS += $$PWD/regions.hpp
+SOURCES += $$PWD/regions.cpp
+
+INCLUDEPATH += $$PWD
\ No newline at end of file
index 9a239ea..1ac06e4 100644 (file)
@@ -6,6 +6,8 @@ HEADERS += qmaemo5homescreenadaptor.h
 SOURCES += main.cpp
 HEADERS += mainwidget.hpp
 
+include (ynadex-traffic-core.pri)
+
 desktop.path = /usr/share/applications/hildon-home
 desktop.files = yandex-traffic-widget.desktop