From 126bc8cfa3fa60707f87f0920043c44db0d2c513 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Tue, 21 May 2013 09:33:55 -0300 Subject: [PATCH] Added the code to show the cylinders from a dive. i Added the code to show the cylinders from a dive, this code also already permits additions from the interface, so the user can click 'add' and insert what he wants there. Signed-off-by: Tomaz Canabrava --- dive.h | 8 +++---- qt-ui/maintab.cpp | 3 ++- qt-ui/models.cpp | 53 ++++++++++++++++++++++++++++++----------------- qt-ui/models.h | 8 +++---- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/dive.h b/dive.h index 50e0dc4d4..7c65c39fe 100644 --- a/dive.h +++ b/dive.h @@ -155,10 +155,10 @@ typedef struct { const char *description; /* "integrated", "belt", "ankle" */ } weightsystem_t; -extern gboolean cylinder_nodata(cylinder_t *cyl); -extern gboolean cylinder_none(void *_data); -extern gboolean no_weightsystems(weightsystem_t *ws); -extern gboolean weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2); +extern bool cylinder_nodata(cylinder_t *cyl); +extern bool cylinder_none(void *_data); +extern bool no_weightsystems(weightsystem_t *ws); +extern bool weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2); extern int get_pressure_units(unsigned int mb, const char **units); extern double get_depth_units(unsigned int mm, int *frac, const char **units); diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index e9a8cd1c4..bb5169e52 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -202,6 +202,7 @@ void MainTab::updateDiveInfo(int dive) ui->averageTimeAllText->setText(get_time_string(seconds, 0)); ui->longestAllText->setText(get_time_string(stats_selection.longest_time.seconds, 0)); ui->shortestAllText->setText(get_time_string(stats_selection.shortest_time.seconds, 0)); + cylindersModel->setDive(d); } else { /* make the fields read-only */ ui->location->setReadOnly(true); @@ -226,6 +227,7 @@ void MainTab::updateDiveInfo(int dive) ui->airTemperatureText->clear(); ui->gasUsedText->clear(); ui->airPressureText->clear(); + cylindersModel->clear(); } /* statisticsTab*/ /* we can access the stats_selection struct, but how do we ensure the relevant dives are selected @@ -292,7 +294,6 @@ void MainTab::on_delWeight_clicked() void MainTab::reload() { - cylindersModel->update(); } void MainTab::on_editAccept_clicked(bool edit) diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 90c826331..d20bf2323 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -13,7 +13,7 @@ extern struct tank_info tank_info[100]; -CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent) +CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent), current(0), rows(0) { } @@ -62,7 +62,7 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const if (!index.isValid() || index.row() >= MAX_CYLINDERS) return ret; - cylinder_t& cyl = current_dive->cylinder[index.row()]; + cylinder_t& cyl = current->cylinder[index.row()]; if (role == Qt::DisplayRole) { switch(index.column()) { @@ -94,49 +94,64 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const int CylindersModel::rowCount(const QModelIndex& parent) const { - return usedRows[current_dive]; + return rows; } void CylindersModel::add(cylinder_t* cyl) { - if (usedRows[current_dive] >= MAX_CYLINDERS) { - free(cyl); + if (rows >= MAX_CYLINDERS) { return; } - int row = usedRows[current_dive]; + int row = rows; - cylinder_t& cylinder = current_dive->cylinder[row]; + cylinder_t& cylinder = current->cylinder[row]; cylinder.end.mbar = cyl->end.mbar; cylinder.start.mbar = cyl->start.mbar; + cylinder.type.description = strdup(cyl->type.description); + cylinder.type.size = cyl->type.size; + cylinder.type.workingpressure = cyl->type.workingpressure; beginInsertRows(QModelIndex(), row, row); - usedRows[current_dive]++; + rows++; endInsertRows(); } void CylindersModel::update() { - if (usedRows[current_dive] > 0) { - beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); - endRemoveRows(); - } - if (usedRows[current_dive] > 0) { - beginInsertRows(QModelIndex(), 0, usedRows[current_dive]-1); - endInsertRows(); - } + setDive(current); } void CylindersModel::clear() { - if (usedRows[current_dive] > 0) { - beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); - usedRows[current_dive] = 0; + if (rows > 0) { + beginRemoveRows(QModelIndex(), 0, rows-1); endRemoveRows(); } } +void CylindersModel::setDive(dive* d) +{ + if (current) + clear(); + + int amount = 0; + for(int i = 0; i < MAX_CYLINDERS; i++){ + cylinder_t& cylinder = current_dive->cylinder[i]; + qDebug() << QString(cylinder.type.description); + if (!cylinder.type.description){ + amount = i; + break; + } + } + + beginInsertRows(QModelIndex(), 0, amount-1); + rows = amount; + current = d; + endInsertRows(); +} + void WeightModel::clear() { if (usedRows[current_dive] > 0) { diff --git a/qt-ui/models.h b/qt-ui/models.h index ac533fd71..41bd4f892 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -49,11 +49,11 @@ public: void add(cylinder_t *cyl); void clear(); void update(); + void setDive(struct dive *d); + private: - /* Since the dive doesn't stores the number of cylinders that - * it has (max 8) and since I don't want to make a - * model-for-each-dive, let's hack this here instead. */ - QMap usedRows; + struct dive *current; + int rows; }; /* Encapsulation of the Weight Model, that represents