diff options
author | Grace Karanja <gracie.karanja89@gmail.com> | 2015-06-09 22:20:44 +0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-06-20 14:26:56 -0700 |
commit | b7e4b35cde1f1a6be01818eac94732186a1b99ab (patch) | |
tree | 4dfa16a09d9e0ec8526d05ce1b4107b8c122d81e /qt-models | |
parent | bf882416f92fef648f8951a55b4b06dacf56bc3b (diff) | |
download | subsurface-b7e4b35cde1f1a6be01818eac94732186a1b99ab.tar.gz |
Add DiveListModel
This model will be used to show the dives in QML. This commit adds
the model, and the means to link it to QML.
Signed-off-by: Grace Karanja <gracie.karanja89@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models')
-rw-r--r-- | qt-models/divelistmodel.cpp | 225 | ||||
-rw-r--r-- | qt-models/divelistmodel.h | 100 |
2 files changed, 325 insertions, 0 deletions
diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp new file mode 100644 index 000000000..1786c85e1 --- /dev/null +++ b/qt-models/divelistmodel.cpp @@ -0,0 +1,225 @@ +#include "divelistmodel.h" +#include "helpers.h" + +Dive::Dive(dive *d) +{ + m_thisDive = d; + setDiveNumber(QString::number(d->number)); + setDate(get_dive_date_string(d->when)); + setDepth(get_depth_string(d->maxdepth)); + setDuration(get_dive_duration_string(d->duration.seconds, "h:","min")); + + if (!d->watertemp.mkelvin) + m_depth = ""; + + if (get_units()->temperature == units::CELSIUS) + m_depth = QString::number(mkelvin_to_C(d->watertemp.mkelvin), 'f', 1); + else + m_depth = QString::number(mkelvin_to_F(d->watertemp.mkelvin), 'f', 1); + + weight_t tw = { total_weight(d) }; + setWeight(weight_string(tw.grams)); + + setSuit(QString(d->suit)); + setCylinder(QString(d->cylinder[0].type.description)); + setSac(QString::number(d->sac)); + setLocation(get_dive_location(d)); +} + +QString Dive::date() const +{ + return m_date; +} + +void Dive::setDate(const QString &date) +{ + m_date = date; +} +QString Dive::location() const +{ + return m_location; +} + +void Dive::setLocation(const QString &location) +{ + m_location = location; +} +QString Dive::sac() const +{ + return m_sac; +} + +void Dive::setSac(const QString &sac) +{ + m_sac = sac; +} +QString Dive::gas() const +{ + return m_gas; +} + +void Dive::setGas(const QString &gas) +{ + m_gas = gas; +} +QString Dive::cylinder() const +{ + return m_cylinder; +} + +void Dive::setCylinder(const QString &cylinder) +{ + m_cylinder = cylinder; +} +QString Dive::suit() const +{ + return m_suit; +} + +void Dive::setSuit(const QString &suit) +{ + m_suit = suit; +} +QString Dive::weight() const +{ + return m_weight; +} + +void Dive::setWeight(const QString &weight) +{ + m_weight = weight; +} +QString Dive::temp() const +{ + return m_temp; +} + +void Dive::setTemp(const QString &temp) +{ + m_temp = temp; +} +QString Dive::duration() const +{ + return m_duration; +} + +void Dive::setDuration(const QString &duration) +{ + m_duration = duration; +} +QString Dive::depth() const +{ + return m_depth; +} + +void Dive::setDepth(const QString &depth) +{ + m_depth = depth; +} +QString Dive::rating() const +{ + return m_rating; +} + +void Dive::setRating(const QString &rating) +{ + m_rating = rating; +} +dive *Dive::thisDive() const +{ + return m_thisDive; +} + +void Dive::setThisDive(dive *thisDive) +{ + m_thisDive = thisDive; +} +QString Dive::diveNumber() const +{ + return m_diveNumber; +} + +void Dive::setDiveNumber(const QString &diveNumber) +{ + m_diveNumber = diveNumber; +} + + +DiveListModel *DiveListModel::m_instance = NULL; + +DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent) +{ + m_instance = this; +} + +void DiveListModel::addDive(dive *d) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + m_dives.append(Dive(d)); + endInsertRows(); +} + +int DiveListModel::rowCount(const QModelIndex &) const +{ + return m_dives.count(); +} + +QVariant DiveListModel::data(const QModelIndex &index, int role) const +{ + if(index.row() < 0 || index.row() > m_dives.count()) + return QVariant(); + + const Dive &dive = m_dives[index.row()]; + + if (role == DiveNumberRole) + return dive.diveNumber(); + else if (role == DiveDateRole) + return dive.date(); + else if (role == DiveRatingRole) + return dive.rating(); + else if (role == DiveDepthRole) + return dive.depth(); + else if (role == DiveDurationRole) + return dive.duration(); + else if (role == DiveTemperatureRole) + return dive.temp(); + else if (role == DiveWeightRole) + return dive.weight(); + else if (role == DiveSuitRole) + return dive.suit(); + else if (role == DiveCylinderRole) + return dive.cylinder(); + else if (role == DiveGasRole) + return dive.gas(); + else if (role == DiveSacRole) + return dive.sac(); + else if (role == DiveLocationRole) + return dive.location(); + return QVariant(); + + +} + +QHash<int, QByteArray> DiveListModel::roleNames() const +{ + QHash<int, QByteArray> roles; + roles[DiveNumberRole] = "diveNumber"; + roles[DiveDateRole] = "date"; + roles[DiveRatingRole] = "rating"; + roles[DiveDepthRole] = "depth"; + roles[DiveDurationRole] = "duration"; + roles[DiveTemperatureRole] = "temp"; + roles[DiveWeightRole] = "weight"; + roles[DiveSuitRole] = "suit"; + roles[DiveCylinderRole] = "cylinder"; + roles[DiveGasRole] = "gas"; + roles[DiveSacRole] = "sac"; + roles[DiveLocationRole] = "location"; + + return roles; +} + +DiveListModel *DiveListModel::instance() +{ + return m_instance; +} diff --git a/qt-models/divelistmodel.h b/qt-models/divelistmodel.h new file mode 100644 index 000000000..96f16912a --- /dev/null +++ b/qt-models/divelistmodel.h @@ -0,0 +1,100 @@ +#ifndef DIVELISTMODEL_H +#define DIVELISTMODEL_H + +#include <QAbstractListModel> +#include "dive.h" + +class Dive { +public: + Dive(dive* d); + + QString date() const; + void setDate(const QString &date); + + QString location() const; + void setLocation(const QString &location); + + QString sac() const; + void setSac(const QString &sac); + + QString gas() const; + void setGas(const QString &gas); + + QString cylinder() const; + void setCylinder(const QString &cylinder); + + QString suit() const; + void setSuit(const QString &suit); + + QString weight() const; + void setWeight(const QString &weight); + + QString temp() const; + void setTemp(const QString &temp); + + QString duration() const; + void setDuration(const QString &duration); + + QString depth() const; + void setDepth(const QString &depth); + + QString rating() const; + void setRating(const QString &rating); + + dive *thisDive() const; + void setThisDive(dive *thisDive); + + QString diveNumber() const; + void setDiveNumber(const QString &diveNumber); + +private: + QString m_diveNumber; + QString m_date; + QString m_rating; + QString m_depth; + QString m_duration; + QString m_temp; + QString m_weight; + QString m_suit; + QString m_cylinder; + QString m_gas; + QString m_sac; + QString m_location; + + + dive *m_thisDive; +}; + +class DiveListModel : public QAbstractListModel +{ + Q_OBJECT +public: + + enum DiveListRoles { + DiveNumberRole = Qt::UserRole + 1, + DiveDateRole, + DiveRatingRole, + DiveDepthRole, + DiveDurationRole, + DiveTemperatureRole, + DiveWeightRole, + DiveSuitRole, + DiveCylinderRole, + DiveGasRole, + DiveSacRole, + DiveLocationRole + }; + + static DiveListModel *instance(); + DiveListModel(QObject *parent = 0); + void addDive(dive *d); + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + +private: + QList<Dive> m_dives; + static DiveListModel *m_instance; +}; + +#endif // DIVELISTMODEL_H |