From: Luciano Montanaro Date: Sun, 11 Dec 2011 20:52:47 +0000 (+0100) Subject: Added type property to StationScheduleModel X-Git-Tag: tags/0.4.81~24 X-Git-Url: https://vcs.maemo.org/git/?a=commitdiff_plain;h=d009477ebb32060d3b2890e361c685103c3d7030;p=quandoparte Added type property to StationScheduleModel --- diff --git a/application/stationschedulemodel.cpp b/application/stationschedulemodel.cpp index e648760..27151ab 100644 --- a/application/stationschedulemodel.cpp +++ b/application/stationschedulemodel.cpp @@ -22,11 +22,13 @@ Boston, MA 02110-1301, USA. #include "stationschedulemodel.h" #include "dataprovider.h" +#include "settings.h" #include #include #include #include + StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) : QAbstractListModel(parent), m_name(name) @@ -38,8 +40,8 @@ StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) roles[DepartureStationRole] = "departureStation"; roles[DepartureTimeRole] = "departureTime"; roles[ArrivalStationRole] = "arrivalStation"; - roles[ArrivalTimeRole] = "ArrivalTime"; - roles[DetailsUrlRole] = "DetailsUrl"; + roles[ArrivalTimeRole] = "arrivalTime"; + roles[DetailsUrlRole] = "detailsUrl"; roles[DelayRole] = "delay"; roles[DelayClassRole] = "delayClassRole"; setRoleNames(roles); @@ -61,6 +63,25 @@ void StationScheduleModel::setName(const QString &name) } } +StationScheduleModel::ScheduleType StationScheduleModel::type() +{ + return m_scheduleType; +} + +void StationScheduleModel::setType(StationScheduleModel::ScheduleType type) +{ + if (type != m_scheduleType) { + emit layoutAboutToBeChanged(); + beginResetModel(); + m_scheduleType = type; + emit typeChanged(); + endResetModel(); + emit layoutChanged(); + Settings *settings = Settings::instance(); + settings->setShowArrivalsPreferred(m_scheduleType == ArrivalSchedule ? true : false); + } +} + static void parseDelayClass(const QWebElement &element, StationScheduleItem &item) { if (!element.isNull()) { @@ -149,7 +170,7 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr qDebug() << "--- end of query result ----- cut here ------"; emit layoutAboutToBeChanged(); - beginInsertRows(QModelIndex(), 0, 0); + beginResetModel(); QWebPage page; page.mainFrame()->setContent(htmlReply, "text/html", baseUrl); QWebElement doc = page.mainFrame()->documentElement(); @@ -198,7 +219,7 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr if (current.isNull()) break; } - endInsertRows(); + endResetModel(); emit layoutChanged(); } @@ -212,8 +233,14 @@ void StationScheduleModel::fetch(const QString &name) int StationScheduleModel::rowCount(const QModelIndex &parent) const { - qDebug() << "schedule.count" << m_departureSchedules.count(); - return m_departureSchedules.count(); + Q_UNUSED(parent); + if (m_scheduleType == DepartureSchedule) { + qDebug() << "schedule.count" << m_departureSchedules.count(); + return m_departureSchedules.count(); + } else { + qDebug() << "schedule.count" << m_arrivalSchedules.count(); + return m_arrivalSchedules.count(); + } } QVariant StationScheduleModel::data(const QModelIndex &index, int role) const @@ -222,10 +249,12 @@ QVariant StationScheduleModel::data(const QModelIndex &index, int role) const if (!index.isValid()) { return QVariant(); } - if (index.row() >= m_departureSchedules.count()) { + const QList &schedules = + (m_scheduleType == DepartureSchedule) ? m_departureSchedules : m_arrivalSchedules; + if (index.row() < 0 || index.row() >= schedules.count()) { return QVariant(); } - StationScheduleItem item = m_departureSchedules[index.row()]; + StationScheduleItem item = schedules[index.row()]; switch (role) { case Qt::DisplayRole: case TrainRole: diff --git a/application/stationschedulemodel.h b/application/stationschedulemodel.h index ffded53..bead128 100644 --- a/application/stationschedulemodel.h +++ b/application/stationschedulemodel.h @@ -32,6 +32,8 @@ class StationScheduleModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(ScheduleType type READ type WRITE setType NOTIFY typeChanged) + Q_ENUMS(ScheduleType) enum StationRoles { TrainRole = Qt::UserRole +1, @@ -45,16 +47,25 @@ class StationScheduleModel : public QAbstractListModel }; public: + enum ScheduleType { + DepartureSchedule, + ArrivalSchedule + }; + explicit StationScheduleModel(const QString &name = "", QObject *parent = 0); QString &name(); void setName(const QString &name); + ScheduleType type(); + void setType(ScheduleType type); + int rowCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; signals: void nameChanged(); + void typeChanged(); public slots: void fetch(const QString &name); @@ -66,6 +77,7 @@ private: QString m_name; QList m_departureSchedules; QList m_arrivalSchedules; + ScheduleType m_scheduleType; }; #endif // STATIONSCHEDULEMODEL_H