summaryrefslogtreecommitdiffstats
path: root/qt-ui/models.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'qt-ui/models.cpp')
-rw-r--r--qt-ui/models.cpp147
1 files changed, 92 insertions, 55 deletions
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 360058dfd..eb5a9a5b4 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -6,6 +6,7 @@
*/
#include "models.h"
#include "../helpers.h"
+#include "../dive.h"
#include <QCoreApplication>
#include <QDebug>
#include <QColor>
@@ -33,8 +34,8 @@ QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, in
case SIZE:
ret = tr("Size");
break;
- case MAXPRESS:
- ret = tr("MaxPress");
+ case WORKINGPRESS:
+ ret = tr("WorkPress");
break;
case START:
ret = tr("Start");
@@ -66,7 +67,7 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
cylinder_t *cyl = &current->cylinder[index.row()];
- if (role == Qt::DisplayRole) {
+ if (role == Qt::DisplayRole || role==Qt::EditRole) {
switch(index.column()) {
case TYPE:
ret = QString(cyl->type.description);
@@ -76,14 +77,14 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
// sizes take working pressure into account...
if (cyl->type.size.mliter) {
if (prefs.units.volume == prefs.units.CUFT) {
- int cuft = ml_to_cuft(gas_volume(cyl, cyl->type.workingpressure));
+ int cuft = 0.5 + ml_to_cuft(gas_volume(cyl, cyl->type.workingpressure));
ret = QString("%1cuft").arg(cuft);
} else {
ret = QString("%1l").arg(cyl->type.size.mliter / 1000.0, 0, 'f', 1);
}
}
break;
- case MAXPRESS:
+ case WORKINGPRESS:
if (cyl->type.workingpressure.mbar)
ret = get_pressure_string(cyl->type.workingpressure, TRUE);
break;
@@ -113,45 +114,90 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
return ret;
}
+#define CHANGED(_t,_u1,_u2) value._t() != data(index, role).toString().replace(_u1,"").replace(_u2,"")._t()
+
bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
cylinder_t *cyl = &current->cylinder[index.row()];
- switch(index.column()){
- case TYPE:{
- QByteArray desc = value.toByteArray();
- cyl->type.description = strdup(desc.data());
- break;
+ switch(index.column()) {
+ case TYPE:
+ if (!value.isNull()) {
+ char *text = value.toByteArray().data();
+ if (!cyl->type.description || strcmp(cyl->type.description, text)) {
+ cyl->type.description = strdup(text);
+ mark_divelist_changed(TRUE);
+ }
}
- case SIZE:
- // we can't use get_volume_string because the idiotic imperial tank
- // sizes take working pressure into account...
- if (cyl->type.size.mliter) {
+ break;
+ case SIZE:
+ if (CHANGED(toDouble, "cuft", "l")) {
+ // if units are CUFT then this value is meaningless until we have working pressure
+ if (value.toDouble() != 0.0) {
if (prefs.units.volume == prefs.units.CUFT) {
- double liters = cuft_to_l(value.toDouble());
- cyl->type.size.mliter = liters * 1000.0;
+ if (cyl->type.workingpressure.mbar == 0)
+ // this is a hack as we can't store a wet size
+ // without working pressure in cuft mode
+ // so we assume it's an aluminum tank at 3000psi
+ cyl->type.workingpressure.mbar = psi_to_mbar(3000);
+ if (cyl->type.size.mliter != wet_volume(value.toDouble(), cyl->type.workingpressure)) {
+ mark_divelist_changed(TRUE);
+ cyl->type.size.mliter = wet_volume(value.toDouble(), cyl->type.workingpressure);
+ }
} else {
- cyl->type.size.mliter = value.toDouble() * 1000.0;
+ if (cyl->type.size.mliter != value.toDouble() * 1000.0) {
+ mark_divelist_changed(TRUE);
+ cyl->type.size.mliter = value.toDouble() * 1000.0;
+ }
}
}
- break;
- case MAXPRESS:
- cyl->type.workingpressure.mbar = value.toInt();
- break;
- case START:
- cyl->start.mbar = value.toInt();
- break;
- case END:
- cyl->end.mbar = value.toInt();
- break;
- case O2:
+ }
+ break;
+ case WORKINGPRESS:
+ if (CHANGED(toDouble, "psi", "bar")) {
+ if (value.toDouble() != 0.0) {
+ if (prefs.units.pressure == prefs.units.PSI)
+ cyl->type.workingpressure.mbar = psi_to_mbar(value.toDouble());
+ else
+ cyl->type.workingpressure.mbar = value.toDouble() * 1000;
+ mark_divelist_changed(TRUE);
+ }
+ }
+ break;
+ case START:
+ if (CHANGED(toDouble, "psi", "bar")) {
+ if (value.toDouble() != 0.0) {
+ if (prefs.units.pressure == prefs.units.PSI)
+ cyl->start.mbar = psi_to_mbar(value.toDouble());
+ else
+ cyl->start.mbar = value.toDouble() * 1000;
+ mark_divelist_changed(TRUE);
+ }
+ }
+ break;
+ case END:
+ if (CHANGED(toDouble, "psi", "bar")) {
+ if (value.toDouble() != 0.0) {
+ if (prefs.units.pressure == prefs.units.PSI)
+ cyl->end.mbar = psi_to_mbar(value.toDouble());
+ else
+ cyl->end.mbar = value.toDouble() * 1000;
+ }
+ }
+ break;
+ case O2:
+ if (CHANGED(toInt, "%", "%")) {
cyl->gasmix.o2.permille = value.toInt() * 10 - 5;
- break;
- case HE:
+ mark_divelist_changed(TRUE);
+ }
+ break;
+ case HE:
+ if (CHANGED(toInt, "%", "%")) {
cyl->gasmix.he.permille = value.toInt() * 10 - 5;
- break;
+ mark_divelist_changed(TRUE);
+ }
+ break;
}
-
- return QAbstractItemModel::setData(index, value, role);
+ return QAbstractItemModel::setData(index, value, role);
}
int CylindersModel::rowCount(const QModelIndex& parent) const
@@ -159,7 +205,7 @@ int CylindersModel::rowCount(const QModelIndex& parent) const
return rows;
}
-void CylindersModel::add(cylinder_t* cyl)
+void CylindersModel::add()
{
if (rows >= MAX_CYLINDERS) {
return;
@@ -167,14 +213,6 @@ void CylindersModel::add(cylinder_t* cyl)
int row = rows;
- 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);
rows++;
endInsertRows();
@@ -226,7 +264,9 @@ void CylindersModel::remove(const QModelIndex& index)
return;
}
beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly.
- // Remove code should be here.
+ rows--;
+ remove_cylinder(current, index.row());
+ mark_divelist_changed(TRUE);
endRemoveRows();
}
@@ -236,7 +276,9 @@ void WeightModel::remove(const QModelIndex& index)
return;
}
beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly.
- // Remove code should be here.
+ rows--;
+ remove_weightsystem(current, index.row());
+ mark_divelist_changed(TRUE);
endRemoveRows();
}
@@ -261,7 +303,7 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
weightsystem_t *ws = &current_dive->weightsystem[index.row()];
- if (role == Qt::DisplayRole) {
+ if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch(index.column()) {
case TYPE:
ret = QString(ws->description);
@@ -326,18 +368,12 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r
return ret;
}
-void WeightModel::add(weightsystem_t* weight)
+void WeightModel::add()
{
if (rows >= MAX_WEIGHTSYSTEMS)
return;
int row = rows;
-
- weightsystem_t *ws = &current->weightsystem[row];
-
- ws->description = weight->description;
- ws->weight.grams = weight->weight.grams;
-
beginInsertRows(QModelIndex(), row, row);
rows++;
endInsertRows();
@@ -395,9 +431,10 @@ QVariant TankInfoModel::data(const QModelIndex& index, int role) const
int bar = ((info->psi) ? psi_to_bar(info->psi) : info->bar) * 1000 + 0.5;
- if (info->cuft) {
- double airvolume = cuft_to_l(info->cuft) * 1000.0;
- ml = airvolume / bar_to_atm(bar) + 0.5;
+ if (info->cuft && info->psi) {
+ pressure_t p;
+ p.mbar = psi_to_mbar(info->psi);
+ ml = wet_volume(info->cuft, p);
}
if (role == Qt::DisplayRole) {
switch(index.column()) {