Added a true list model to load the station list from.
--- /dev/null
+#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();
+ }
+ }
+}
--- /dev/null
+#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
SOURCES += \
- tst_app.cpp
+ tst_app.cpp ../application/stationlistmodel.cpp
+
+HEADERS += ../application/stationlistmodel.h
+
DEFINES += SRCDIR=\\\"$$PWD/\\\"
unix:!symbian {
}
INSTALLS += target
}
+
+OTHER_FILES += \
+ testfiles/empty.qpl \
+ testfiles/malformed1.qpl \
+ testfiles/emptystation.qpl \
+ testfiles/teststation.qpl
--- /dev/null
+<stations>
+</stations>
--- /dev/null
+<stations>
+<station>
+</station>
+</stations>
--- /dev/null
+<stations>
+</>
--- /dev/null
+<stations>
+<station>
+<name>Topolinia</name>
+<pos>0.0,0.0</pos>
+</station>
+</stations>
#include <QtTest/QtTest>
#include <QtCore/QCoreApplication>
+#include "../application/stationlistmodel.h"
+
class AppTest : public QObject
{
Q_OBJECT
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()
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);