Commit a3d0c8b8 authored by Volker Krause's avatar Volker Krause

Make the timeline model element type explicit

This will allow us to add more elements that have no reservation attached
to them, like country information or weather information.
parent a4982d74
...@@ -43,9 +43,7 @@ static bool needsSplitting(const QVariant &res) ...@@ -43,9 +43,7 @@ static bool needsSplitting(const QVariant &res)
static QDateTime relevantDateTime(const QVariant &res, TimelineModel::RangeType range) static QDateTime relevantDateTime(const QVariant &res, TimelineModel::RangeType range)
{ {
if (res.isNull()) { // today marker if (res.userType() == qMetaTypeId<FlightReservation>()) {
return QDateTime(QDate::currentDate(), QTime(0, 0));
} else if (res.userType() == qMetaTypeId<FlightReservation>()) {
const auto flight = res.value<FlightReservation>().reservationFor().value<Flight>(); const auto flight = res.value<FlightReservation>().reservationFor().value<Flight>();
if (flight.boardingTime().isValid()) { if (flight.boardingTime().isValid()) {
return flight.boardingTime(); return flight.boardingTime();
...@@ -82,6 +80,16 @@ static QString passId(const QVariant &res) ...@@ -82,6 +80,16 @@ static QString passId(const QVariant &res)
return passTypeId + QLatin1Char('/') + QString::fromUtf8(serialNum.toUtf8().toBase64(QByteArray::Base64UrlEncoding)); return passTypeId + QLatin1Char('/') + QString::fromUtf8(serialNum.toUtf8().toBase64(QByteArray::Base64UrlEncoding));
} }
static TimelineModel::ElementType elementType(const QVariant &res)
{
if (JsonLd::isA<FlightReservation>(res)) { return TimelineModel::Flight; }
if (JsonLd::isA<LodgingReservation>(res)) { return TimelineModel::Hotel; }
if (JsonLd::isA<TrainReservation>(res)) { return TimelineModel::TrainTrip; }
if (JsonLd::isA<BusReservation>(res)) { return TimelineModel::BusTrip; }
if (JsonLd::isA<FoodEstablishmentReservation>(res)) { return TimelineModel::Restaurant; }
return {};
}
TimelineModel::TimelineModel(QObject *parent) TimelineModel::TimelineModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
...@@ -101,14 +109,14 @@ void TimelineModel::setReservationManager(ReservationManager* mgr) ...@@ -101,14 +109,14 @@ void TimelineModel::setReservationManager(ReservationManager* mgr)
for (const auto &resId : mgr->reservations()) { for (const auto &resId : mgr->reservations()) {
const auto res = m_resMgr->reservation(resId); const auto res = m_resMgr->reservation(resId);
if (needsSplitting(res)) { if (needsSplitting(res)) {
m_elements.push_back(Element{resId, relevantDateTime(mgr->reservation(resId), RangeBegin), RangeBegin}); m_elements.push_back(Element{resId, relevantDateTime(res, RangeBegin), elementType(res), RangeBegin});
m_elements.push_back(Element{resId, relevantDateTime(mgr->reservation(resId), RangeEnd), RangeEnd}); m_elements.push_back(Element{resId, relevantDateTime(res, RangeEnd), elementType(res), RangeEnd});
} else { } else {
m_elements.push_back(Element{resId, relevantDateTime(mgr->reservation(resId), SelfContained), SelfContained}); m_elements.push_back(Element{resId, relevantDateTime(res, SelfContained), elementType(res), SelfContained});
} }
} }
m_elements.push_back(Element{{}, relevantDateTime({}, SelfContained), SelfContained}); // today marker m_elements.push_back(Element{{}, QDateTime(QDate::currentDate(), QTime(0, 0)), TodayMarker, SelfContained});
std::sort(m_elements.begin(), m_elements.end(), [this](const Element &lhs, const Element &rhs) { std::sort(m_elements.begin(), m_elements.end(), [](const Element &lhs, const Element &rhs) {
return lhs.dt < rhs.dt; return lhs.dt < rhs.dt;
}); });
connect(mgr, &ReservationManager::reservationAdded, this, &TimelineModel::reservationAdded); connect(mgr, &ReservationManager::reservationAdded, this, &TimelineModel::reservationAdded);
...@@ -150,21 +158,9 @@ QVariant TimelineModel::data(const QModelIndex& index, int role) const ...@@ -150,21 +158,9 @@ QVariant TimelineModel::data(const QModelIndex& index, int role) const
case ReservationIdRole: case ReservationIdRole:
return elem.id; return elem.id;
case ElementTypeRole: case ElementTypeRole:
if (res.isNull()) return elem.elementType;
return TodayMarker;
else if (res.userType() == qMetaTypeId<FlightReservation>())
return Flight;
else if (res.userType() == qMetaTypeId<LodgingReservation>())
return Hotel;
else if (res.userType() == qMetaTypeId<TrainReservation>())
return TrainTrip;
else if (res.userType() == qMetaTypeId<BusReservation>())
return BusTrip;
else if (res.userType() == qMetaTypeId<FoodEstablishmentReservation>())
return Restaurant;
return {};
case TodayEmptyRole: case TodayEmptyRole:
if (res.isNull()) { if (elem.elementType == TodayMarker) {
return index.row() == (int)(m_elements.size() - 1) || m_elements.at(index.row() + 1).dt.date() > QDate::currentDate(); return index.row() == (int)(m_elements.size() - 1) || m_elements.at(index.row() + 1).dt.date() > QDate::currentDate();
} }
return {}; return {};
...@@ -201,10 +197,10 @@ void TimelineModel::reservationAdded(const QString &resId) ...@@ -201,10 +197,10 @@ void TimelineModel::reservationAdded(const QString &resId)
{ {
const auto res = m_resMgr->reservation(resId); const auto res = m_resMgr->reservation(resId);
if (needsSplitting(res)) { if (needsSplitting(res)) {
insertElement(Element{resId, relevantDateTime(res, RangeBegin), RangeBegin}); insertElement(Element{resId, relevantDateTime(res, RangeBegin), elementType(res), RangeBegin});
insertElement(Element{resId, relevantDateTime(res, RangeEnd), RangeEnd}); insertElement(Element{resId, relevantDateTime(res, RangeEnd), elementType(res), RangeEnd});
} else { } else {
insertElement(Element{resId, relevantDateTime(res, SelfContained), SelfContained}); insertElement(Element{resId, relevantDateTime(res, SelfContained), elementType(res), SelfContained});
} }
emit todayRowChanged(); emit todayRowChanged();
...@@ -246,7 +242,7 @@ void TimelineModel::updateElement(const QString &resId, const QVariant &res, Tim ...@@ -246,7 +242,7 @@ void TimelineModel::updateElement(const QString &resId, const QVariant &res, Tim
beginRemoveRows({}, row, row); beginRemoveRows({}, row, row);
m_elements.erase(it); m_elements.erase(it);
endRemoveRows(); endRemoveRows();
insertElement(Element{resId, newDt, rangeType}); insertElement(Element{resId, newDt, elementType(res), rangeType});
} else { } else {
emit dataChanged(index(row, 0), index(row, 0)); emit dataChanged(index(row, 0), index(row, 0));
} }
......
...@@ -43,12 +43,14 @@ public: ...@@ -43,12 +43,14 @@ public:
}; };
enum ElementType { enum ElementType {
Undefined,
Flight, Flight,
TrainTrip, TrainTrip,
BusTrip, BusTrip,
Hotel, Hotel,
Restaurant, Restaurant,
TodayMarker TodayMarker,
CountryInfo
}; };
Q_ENUM(ElementType) Q_ENUM(ElementType)
...@@ -79,6 +81,7 @@ private: ...@@ -79,6 +81,7 @@ private:
struct Element { struct Element {
QString id; // reservation id QString id; // reservation id
QDateTime dt; // relevant date/time QDateTime dt; // relevant date/time
ElementType elementType;
RangeType rangeType; RangeType rangeType;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment