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.cpp237
1 files changed, 221 insertions, 16 deletions
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 7ccbe1e49..6c6d0b133 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -14,8 +14,6 @@
#include <QFont>
#include <QIcon>
-extern struct tank_info tank_info[100];
-
CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent), current(0), rows(0)
{
}
@@ -111,6 +109,28 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
return ret;
}
+// this is our magic 'pass data in' function that allows the delegate to get
+// the data here without silly unit conversions;
+// so we only implement the two columns we care about
+void CylindersModel::passInData(const QModelIndex& index, const QVariant& value)
+{
+ cylinder_t *cyl = &current->cylinder[index.row()];
+ switch(index.column()) {
+ case SIZE:
+ if (cyl->type.size.mliter != value.toInt()) {
+ cyl->type.size.mliter = value.toInt();
+ mark_divelist_changed(TRUE);
+ }
+ break;
+ case WORKINGPRESS:
+ if (cyl->type.workingpressure.mbar != value.toInt()) {
+ cyl->type.workingpressure.mbar = value.toInt();
+ mark_divelist_changed(TRUE);
+ }
+ break;
+ }
+}
+
#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)
@@ -119,7 +139,8 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
switch(index.column()) {
case TYPE:
if (!value.isNull()) {
- char *text = value.toByteArray().data();
+ QByteArray ba = value.toByteArray();
+ const char *text = ba.constData();
if (!cyl->type.description || strcmp(cyl->type.description, text)) {
cyl->type.description = strdup(text);
mark_divelist_changed(TRUE);
@@ -130,20 +151,29 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
if (CHANGED(toDouble, "cuft", "l")) {
// if units are CUFT then this value is meaningless until we have working pressure
if (value.toDouble() != 0.0) {
+ TankInfoModel *tanks = TankInfoModel::instance();
+ QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description);
if (prefs.units.volume == prefs.units.CUFT) {
- if (cyl->type.workingpressure.mbar == 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 (!matches.isEmpty())
+ tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0);
+ }
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);
+ if (!matches.isEmpty())
+ tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl->type.size.mliter);
}
} else {
if (cyl->type.size.mliter != value.toDouble() * 1000.0) {
mark_divelist_changed(TRUE);
cyl->type.size.mliter = value.toDouble() * 1000.0;
+ if (!matches.isEmpty())
+ tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl->type.size.mliter);
}
}
}
@@ -152,10 +182,14 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
case WORKINGPRESS:
if (CHANGED(toDouble, "psi", "bar")) {
if (value.toDouble() != 0.0) {
+ TankInfoModel *tanks = TankInfoModel::instance();
+ QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description);
if (prefs.units.pressure == prefs.units.PSI)
cyl->type.workingpressure.mbar = psi_to_mbar(value.toDouble());
else
cyl->type.workingpressure.mbar = value.toDouble() * 1000;
+ if (!matches.isEmpty())
+ tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0);
mark_divelist_changed(TRUE);
}
}
@@ -267,6 +301,10 @@ void CylindersModel::remove(const QModelIndex& index)
endRemoveRows();
}
+WeightModel::WeightModel(QObject* parent): QAbstractTableModel(parent), current(0), rows(0)
+{
+}
+
void WeightModel::remove(const QModelIndex& index)
{
if (index.column() != REMOVE) {
@@ -298,7 +336,7 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS)
return ret;
- weightsystem_t *ws = &current_dive->weightsystem[index.row()];
+ weightsystem_t *ws = &current->weightsystem[index.row()];
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch(index.column()) {
@@ -316,19 +354,49 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
return ret;
}
+// this is our magic 'pass data in' function that allows the delegate to get
+// the data here without silly unit conversions;
+// so we only implement the two columns we care about
+void WeightModel::passInData(const QModelIndex& index, const QVariant& value)
+{
+ weightsystem_t *ws = &current->weightsystem[index.row()];
+ if (index.column() == WEIGHT) {
+ if (ws->weight.grams != value.toInt()) {
+ ws->weight.grams = value.toInt();
+ mark_divelist_changed(TRUE);
+ }
+ }
+}
+
bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
- weightsystem_t *ws = &current_dive->weightsystem[index.row()];
+ weightsystem_t *ws = &current->weightsystem[index.row()];
switch(index.column()) {
- case TYPE:{
- QByteArray desc = value.toByteArray();
- ws->description = strdup(desc.data());
+ case TYPE:
+ if (!value.isNull()) {
+ QByteArray ba = value.toString().toUtf8();
+ const char *text = ba.constData();
+ if (!ws->description || strcmp(ws->description, text)) {
+ ws->description = strdup(text);
+ mark_divelist_changed(TRUE);
+ }
+ }
break;
- }
case WEIGHT:
- ws->weight.grams = value.toInt() *1000;
+ if (CHANGED(toDouble, "kg", "lbs")) {
+ if (prefs.units.weight == prefs.units.LBS)
+ ws->weight.grams = lbs_to_grams(value.toDouble());
+ else
+ ws->weight.grams = value.toDouble() * 1000.0;
+ // now update the ws_info
+ WSInfoModel *wsim = WSInfoModel::instance();
+ QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, ws->description);
+ if (!matches.isEmpty())
+ wsim->setData(wsim->index(matches.first().row(), WSInfoModel::GR), ws->weight.grams);
+ }
break;
}
+ return QAbstractItemModel::setData(index, value, role);
}
Qt::ItemFlags WeightModel::flags(const QModelIndex& index) const
@@ -398,10 +466,147 @@ void WeightModel::setDive(dive* d)
endInsertRows();
}
-void TankInfoModel::add(const QString& description)
+WSInfoModel* WSInfoModel::instance()
+{
+ static WSInfoModel *self = new WSInfoModel();
+ return self;
+}
+
+bool WSInfoModel::insertRows(int row, int count, const QModelIndex& parent)
+{
+ beginInsertRows(parent, rowCount(), rowCount());
+ rows += count;
+ endInsertRows();
+ return true;
+}
+
+bool WSInfoModel::setData(const QModelIndex& index, const QVariant& value, int role)
+{
+ struct ws_info *info = &ws_info[index.row()];
+ switch(index.column()) {
+ case DESCRIPTION:
+ info->name = strdup(value.toByteArray().data());
+ break;
+ case GR:
+ info->grams = value.toInt();
+ break;
+ }
+ return TRUE;
+}
+
+void WSInfoModel::clear()
+{
+}
+
+int WSInfoModel::columnCount(const QModelIndex& parent) const
+{
+ return 2;
+}
+
+QVariant WSInfoModel::data(const QModelIndex& index, int role) const
+{
+ QVariant ret;
+ if (!index.isValid()) {
+ return ret;
+ }
+ struct ws_info *info = &ws_info[index.row()];
+
+ int gr = info->grams;
+
+ if (role == Qt::DisplayRole || role == Qt::EditRole) {
+ switch(index.column()) {
+ case GR:
+ ret = gr;
+ break;
+ case DESCRIPTION:
+ ret = QString(info->name);
+ break;
+ }
+ }
+ return ret;
+}
+
+QVariant WSInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ QVariant ret;
+
+ if (orientation != Qt::Horizontal)
+ return ret;
+
+ if (role == Qt::DisplayRole) {
+ switch(section) {
+ case GR:
+ ret = tr("kg");
+ break;
+ case DESCRIPTION:
+ ret = tr("Description");
+ break;
+ }
+ }
+ return ret;
+}
+
+int WSInfoModel::rowCount(const QModelIndex& parent) const
+{
+ return rows+1;
+}
+
+WSInfoModel::WSInfoModel() : QAbstractTableModel(), rows(-1)
+{
+ struct ws_info *info = ws_info;
+ for (info = ws_info; info->name; info++, rows++);
+
+ if (rows > -1) {
+ beginInsertRows(QModelIndex(), 0, rows);
+ endInsertRows();
+ }
+}
+
+void WSInfoModel::update()
+{
+ if (rows > -1) {
+ beginRemoveRows(QModelIndex(), 0, rows);
+ endRemoveRows();
+ rows = -1;
+ }
+ struct ws_info *info = ws_info;
+ for (info = ws_info; info->name; info++, rows++);
+
+ if (rows > -1) {
+ beginInsertRows(QModelIndex(), 0, rows);
+ endInsertRows();
+ }
+}
+
+TankInfoModel* TankInfoModel::instance()
+{
+ static TankInfoModel *self = new TankInfoModel();
+ return self;
+}
+
+bool TankInfoModel::insertRows(int row, int count, const QModelIndex& parent)
+{
+ beginInsertRows(parent, rowCount(), rowCount());
+ rows += count;
+ endInsertRows();
+ return true;
+}
+
+bool TankInfoModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
- // When the user `creates` a new one on the combobox.
- // for now, empty till dirk cleans the GTK code.
+ struct tank_info *info = &tank_info[index.row()];
+ switch(index.column()) {
+ case DESCRIPTION:
+ info->name = strdup(value.toByteArray().data());
+ break;
+ case ML:
+ info->ml = value.toInt();
+ break;
+ case BAR:
+ info->bar = value.toInt();
+ break;
+ }
+ return TRUE;
}
void TankInfoModel::clear()
@@ -430,7 +635,7 @@ QVariant TankInfoModel::data(const QModelIndex& index, int role) const
p.mbar = psi_to_mbar(info->psi);
ml = wet_volume(info->cuft, p);
}
- if (role == Qt::DisplayRole) {
+ if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch(index.column()) {
case BAR:
ret = bar;
@@ -478,7 +683,6 @@ TankInfoModel::TankInfoModel() : QAbstractTableModel(), rows(-1)
{
struct tank_info *info = tank_info;
for (info = tank_info; info->name; info++, rows++);
-
if (rows > -1) {
beginInsertRows(QModelIndex(), 0, rows);
endInsertRows();
@@ -490,6 +694,7 @@ void TankInfoModel::update()
if (rows > -1) {
beginRemoveRows(QModelIndex(), 0, rows);
endRemoveRows();
+ rows = -1;
}
struct tank_info *info = tank_info;
for (info = tank_info; info->name; info++, rows++);