Added new StationListModel class
authorLuciano Montanaro <mileima@cirulla.net>
Sun, 29 May 2011 08:59:06 +0000 (10:59 +0200)
committerLuciano Montanaro <mileima@cirulla.net>
Sun, 29 May 2011 08:59:06 +0000 (10:59 +0200)
Added a true list model to load the station list from.

application/stationlistmodel.cpp [new file with mode: 0644]
application/stationlistmodel.h [new file with mode: 0644]
test/test.pro
test/testfiles/empty.qpl [new file with mode: 0644]
test/testfiles/emptystation.qpl [new file with mode: 0644]
test/testfiles/malformed1.qpl [new file with mode: 0644]
test/testfiles/teststation.qpl [new file with mode: 0644]
test/tst_app.cpp

diff --git a/application/stationlistmodel.cpp b/application/stationlistmodel.cpp
new file mode 100644 (file)
index 0000000..4dd4f8e
--- /dev/null
@@ -0,0 +1,130 @@
+#include "stationlistmodel.h"
+
+#include <QFile>
+#include <QDebug>
+#include <QStandardItem>
+
+StationListModel::StationListModel(QObject *parent) :
+    QStandardItemModel(parent)
+
+{
+    setRowCount(0);
+}
+
+bool StationListModel::load(const QString &filename)
+{
+    QFile file(filename);
+
+    qDebug() << "loading filename:" << filename;
+
+    if (!file.open(QFile::ReadOnly | QFile::Text)) {
+        qDebug() << "cannot open file:" << filename;
+        return false;
+    }
+    reader.setDevice(&file);
+    reader.readNext();
+    while (!reader.atEnd()) {
+        if (reader.isStartElement()) {
+            if(reader.name() == "stations") {
+                readStationsElement();
+            } else {
+                reader.raiseError(tr("Not a qpl file"));
+            }
+        } else {
+            reader.readNext();
+        }
+    }
+    file.close();
+    if (reader.hasError()) {
+        qDebug() << "parser error for:" << filename;
+        return false;
+    } else if (file.error() != QFile::NoError) {
+        qDebug() << "file error for:" << filename;
+        return false;
+    }
+    return true;
+}
+
+void StationListModel::readStationsElement()
+{
+    qDebug() << "reading stations element";
+
+    reader.readNext();
+    while (!reader.atEnd()) {
+        if (reader.isEndElement()) {
+            reader.readNext();
+            break;
+        } else if (reader.isStartElement()) {
+            if (reader.name() == "station") {
+                readStationElement();
+            } else {
+                skipUnknownElement();
+            }
+        } else {
+            reader.readNext();
+        }
+    }
+}
+
+void StationListModel::readStationElement()
+{
+    qDebug() << "reading station element";
+
+    QStandardItem *item = new QStandardItem;
+    reader.readNext();
+    while (!reader.atEnd()) {
+        if (reader.isEndElement()) {
+            this->appendRow(item);
+            reader.readNext();
+            break;
+        } else if (reader.isStartElement()) {
+            if (reader.name() == "pos") {
+                readPosElement(item);
+            } else  if (reader.name() == "name") {
+                readNameElement(item);
+            } else {
+                skipUnknownElement();
+            }
+        } else {
+            reader.readNext();
+        }
+    }
+}
+
+void StationListModel::readPosElement(QStandardItem *item)
+{
+    qDebug() << "reading pos element";
+
+    reader.readElementText();
+    if (reader.isEndElement()) {
+        reader.readNext();
+    }
+}
+
+void StationListModel::readNameElement(QStandardItem *item)
+{
+    qDebug() << "reading name element";
+
+    item->setText(reader.readElementText());
+    qDebug() << "name:" << item->text();
+    if (reader.isEndElement()) {
+        reader.readNext();
+    }
+}
+
+void StationListModel::skipUnknownElement()
+{
+    qDebug() << "skipping unknown element";
+
+    reader.readNext();
+    while (!reader.atEnd()) {
+        if (reader.isEndElement()) {
+            reader.readNext();
+            break;
+        } else if (!reader.isStartElement()) {
+            skipUnknownElement();
+        } else {
+            reader.readNext();
+        }
+    }
+}
diff --git a/application/stationlistmodel.h b/application/stationlistmodel.h
new file mode 100644 (file)
index 0000000..b2e8bfa
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef STATIONLISTMODEL_H
+#define STATIONLISTMODEL_H
+
+#include <QStandardItemModel>
+#include <QXmlStreamReader>
+
+class QStandardItem;
+
+class StationListModel : public QStandardItemModel
+{
+    Q_OBJECT
+
+    enum StationListRoles {
+        PositionRole = Qt::UserRole
+    };
+public:
+    explicit StationListModel(QObject *parent = 0);
+
+    bool load(const QString &filename);
+
+signals:
+
+public slots:
+
+private:
+    void readStationsElement();
+    void readStationElement();
+    void readPosElement(QStandardItem *item);
+    void readNameElement(QStandardItem *item);
+    void skipUnknownElement();
+
+    QXmlStreamReader reader;
+};
+
+#endif // STATIONLISTMODEL_H
index 2e02a2e..90c15e5 100644 (file)
@@ -14,7 +14,10 @@ TEMPLATE = app
 
 
 SOURCES += \
-    tst_app.cpp
+    tst_app.cpp ../application/stationlistmodel.cpp
+
+HEADERS += ../application/stationlistmodel.h
+
 DEFINES += SRCDIR=\\\"$$PWD/\\\"
 
 unix:!symbian {
@@ -25,3 +28,9 @@ unix:!symbian {
     }
     INSTALLS += target
 }
+
+OTHER_FILES += \
+    testfiles/empty.qpl \
+    testfiles/malformed1.qpl \
+    testfiles/emptystation.qpl \
+    testfiles/teststation.qpl
diff --git a/test/testfiles/empty.qpl b/test/testfiles/empty.qpl
new file mode 100644 (file)
index 0000000..5ebc19e
--- /dev/null
@@ -0,0 +1,2 @@
+<stations>
+</stations>
diff --git a/test/testfiles/emptystation.qpl b/test/testfiles/emptystation.qpl
new file mode 100644 (file)
index 0000000..329be56
--- /dev/null
@@ -0,0 +1,4 @@
+<stations>
+<station>
+</station>
+</stations>
diff --git a/test/testfiles/malformed1.qpl b/test/testfiles/malformed1.qpl
new file mode 100644 (file)
index 0000000..3cb0f29
--- /dev/null
@@ -0,0 +1,2 @@
+<stations>
+</>
diff --git a/test/testfiles/teststation.qpl b/test/testfiles/teststation.qpl
new file mode 100644 (file)
index 0000000..d686aec
--- /dev/null
@@ -0,0 +1,6 @@
+<stations>
+<station>
+<name>Topolinia</name>
+<pos>0.0,0.0</pos>
+</station>
+</stations>
index 7185c74..a86ce4b 100644 (file)
@@ -23,6 +23,8 @@ Boston, MA 02110-1301, USA.
 #include <QtTest/QtTest>
 #include <QtCore/QCoreApplication>
 
+#include "../application/stationlistmodel.h"
+
 class AppTest : public QObject
 {
     Q_OBJECT
@@ -34,11 +36,20 @@ private Q_SLOTS:
     void initTestCase();
     void cleanupTestCase();
     void testCase1();
-    void testCase1_data();
+    void testCase2();
+    void testCase3();
+    void testCase4();
+    void testCase5();
+
+private:
+    StationListModel *model;
+    QString path;
 };
 
 AppTest::AppTest()
 {
+        model = new StationListModel(this);
+        path = QCoreApplication::applicationDirPath();
 }
 
 void AppTest::initTestCase()
@@ -51,14 +62,32 @@ void AppTest::cleanupTestCase()
 
 void AppTest::testCase1()
 {
-    QFETCH(QString, data);
-    QVERIFY2(true, "Failure");
+    QVERIFY2(false == model->load(path + "/testfiles/missing.qpl"), "File does not exist, should not return ok");
+}
+
+void AppTest::testCase2()
+{
+    QVERIFY2(true == model->load(path + "/testfiles/empty.qpl"), "File cannot be loaeded");
+    QVERIFY2(0 == model->rowCount(), "Should be empty");
+}
+
+void AppTest::testCase3()
+{
+    QVERIFY2(false == model->load(path + "/testfiles/malformed1.qpl"), "Tags are wrong, should not return ok");
+}
+
+void AppTest::testCase4()
+{
+    QVERIFY2(true == model->load(path + "/testfiles/emptystation.qpl"), "cannot open file");
 }
 
-void AppTest::testCase1_data()
+void AppTest::testCase5()
 {
-    QTest::addColumn<QString>("data");
-    QTest::newRow("0") << QString();
+    qDebug() << model->rowCount();
+    QVERIFY2(true == model->load(path + "/testfiles/teststation.qpl"), "cannot open file");
+    qDebug() << model->rowCount();
+    QVERIFY(model->rowCount() == 1);
+    QVERIFY(model->item(0)->text() == "Topolinia");
 }
 
 QTEST_MAIN(AppTest);