summaryrefslogtreecommitdiffstats
path: root/qt-models/divelistmodel.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2016-01-07 16:01:24 -0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2016-01-08 08:04:44 -0800
commit25aa80846b84c0b3cd3e0b0256a046e1e92fb1db (patch)
treec9934f39f2e039e5877af7ee0d3b0d96c5a1debf /qt-models/divelistmodel.cpp
parentc022f5c4f71f3e7670ed458d714d96fd6e88c55b (diff)
downloadsubsurface-25aa80846b84c0b3cd3e0b0256a046e1e92fb1db.tar.gz
Move Dive class from qthelper.h/cpp to it's own file
and rename it to DiveObjectHelper, since it should be an QObject based class to make it easier on the QML, grantlee and widgets side to display the dive's internal data. each Q_PROPERTY defined in the DiveObjectHelper.h file can be acessed directly via it's name. So, if you are on a model that returns a dive, acess it's name by dive.name Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models/divelistmodel.cpp')
-rw-r--r--qt-models/divelistmodel.cpp81
1 files changed, 15 insertions, 66 deletions
diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp
index e812f3df7..e6514064f 100644
--- a/qt-models/divelistmodel.cpp
+++ b/qt-models/divelistmodel.cpp
@@ -12,15 +12,15 @@ DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
void DiveListModel::addDive(dive *d)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
- m_dives.append(Dive(d));
+ m_dives.append(new DiveObjectHelper(d));
endInsertRows();
}
void DiveListModel::updateDive(dive *d)
{
for (int i = 0; i < m_dives.count(); i++) {
- if (m_dives.at(i).id() == d->id) {
- Dive newDive(d);
+ if (m_dives.at(i)->id() == d->id) {
+ DiveObjectHelper *newDive = new DiveObjectHelper(d);
m_dives.replace(i, newDive);
break;
}
@@ -31,6 +31,7 @@ void DiveListModel::clear()
{
if (m_dives.count()) {
beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1);
+ qDeleteAll(m_dives);
m_dives.clear();
endRemoveRows();
}
@@ -46,76 +47,20 @@ 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.number();
- else if (role == DiveTripRole)
- return dive.trip();
- else if (role == DiveDateRole)
- return (qlonglong)dive.timestamp();
- else if (role == DiveDateStringRole)
- return dive.date() + " " + dive.time();
- else if (role == DiveRatingRole)
- return QString::number(dive.rating());
- else if (role == DiveDepthRole)
- return dive.depth();
- else if (role == DiveDurationRole)
- return dive.duration();
- else if (role == DiveAirTemperatureRole)
- return dive.airTemp();
- else if (role == DiveWaterTemperatureRole)
- return dive.waterTemp();
- else if (role == DiveWeightRole)
- return dive.weight(0);
- else if (role == DiveSuitRole)
- return dive.suit();
- else if (role == DiveCylinderRole)
- return dive.cylinder(0);
- else if (role == DiveGasRole)
- return dive.gas();
- else if (role == DiveSacRole)
- return dive.sac();
- else if (role == DiveLocationRole)
- return dive.location();
- else if (role == DiveGPSRole)
- return dive.gps();
- else if (role == DiveNotesRole)
- return dive.notes();
- else if (role == DiveBuddyRole)
- return dive.buddy();
- else if (role == DiveMasterRole)
- return dive.divemaster();
- else if (role == DiveIdRole)
- return QString::number(dive.id());
+ DiveObjectHelper *curr_dive = m_dives[index.row()];
+ switch(role) {
+ case DiveRole: return QVariant::fromValue<QObject*>(curr_dive);
+ case DiveDateRole: return (qlonglong)curr_dive->timestamp();
+ }
return QVariant();
-
}
QHash<int, QByteArray> DiveListModel::roleNames() const
{
QHash<int, QByteArray> roles;
- roles[DiveNumberRole] = "diveNumber";
- roles[DiveTripRole] = "trip";
- roles[DiveDateStringRole] = "date";
- roles[DiveRatingRole] = "rating";
- roles[DiveDepthRole] = "depth";
- roles[DiveDurationRole] = "duration";
- roles[DiveAirTemperatureRole] = "airtemp";
- roles[DiveWaterTemperatureRole] = "watertemp";
- roles[DiveWeightRole] = "weight";
- roles[DiveSuitRole] = "suit";
- roles[DiveCylinderRole] = "cylinder";
- roles[DiveGasRole] = "gas";
- roles[DiveSacRole] = "sac";
- roles[DiveLocationRole] = "location";
- roles[DiveGPSRole] = "gps";
- roles[DiveNotesRole] = "notes";
- roles[DiveBuddyRole] = "buddy";
- roles[DiveMasterRole] = "divemaster";
- roles[DiveIdRole] = "id";
-
+ roles[DiveRole] = "dive";
+ roles[DiveDateRole] = "date";
return roles;
}
@@ -140,3 +85,7 @@ DiveListModel *DiveListModel::instance()
{
return m_instance;
}
+
+DiveObjectHelper* DiveListModel::at(int i){
+ return m_dives.at(i);
+} \ No newline at end of file