Not everything can be done with the diveId

In commit c3fe1a9e9f ("Get rid of pointers to dive structures in the
UI") I was a bit too aggressive moving away from pointers to dives.

This is only needed for pointers that are held across operations that
could change the dive_table. I figured that it wouldn't hurt to get rid of
some more pointers as well, but it turns out I was wrong. The current dive
that we store in the Cylinder and Weight models can be a dive that isn't
in the dive_table at all: the multiEditEquipmentPlaceholder. And when
using the diveId we end up finding the original dive in the dive_table and
therefore modify the wrong structure.

This undoes two thirds of the above mentioned commit.

Reported-and-analyzed-by: Patrick Valsecchi <patrick@thus.ch>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-01-07 22:10:41 +08:00
parent a8db19f548
commit 409c2e9bcf
2 changed files with 6 additions and 30 deletions

View file

@ -62,7 +62,7 @@ void CleanerTableModel::setHeaderDataStrings(const QStringList& newHeaders)
headers = newHeaders; headers = newHeaders;
} }
CylindersModel::CylindersModel(QObject* parent): currentId(0), rows(0) CylindersModel::CylindersModel(QObject* parent): current(0), rows(0)
{ {
// enum{REMOVE, TYPE, SIZE, WORKINGPRESS, START, END, O2, HE, DEPTH}; // enum{REMOVE, TYPE, SIZE, WORKINGPRESS, START, END, O2, HE, DEPTH};
setHeaderDataStrings( QStringList() << "" << tr("Type") << tr("Size") << tr("WorkPress") << tr("StartPress") << tr("EndPress") << trUtf8("O" UTF8_SUBSCRIPT_2 "%") << tr("He%") << tr("Switch at")); setHeaderDataStrings( QStringList() << "" << tr("Type") << tr("Size") << tr("WorkPress") << tr("StartPress") << tr("EndPress") << trUtf8("O" UTF8_SUBSCRIPT_2 "%") << tr("He%") << tr("Switch at"));
@ -90,8 +90,6 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
if (!index.isValid() || index.row() >= MAX_CYLINDERS) if (!index.isValid() || index.row() >= MAX_CYLINDERS)
return ret; return ret;
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
cylinder_t *cyl = &current->cylinder[index.row()]; cylinder_t *cyl = &current->cylinder[index.row()];
switch (role) { switch (role) {
case Qt::FontRole: { case Qt::FontRole: {
@ -159,8 +157,6 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
cylinder_t* CylindersModel::cylinderAt(const QModelIndex& index) cylinder_t* CylindersModel::cylinderAt(const QModelIndex& index)
{ {
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
return &current->cylinder[index.row()]; return &current->cylinder[index.row()];
} }
@ -318,8 +314,6 @@ void CylindersModel::add()
} }
int row = rows; int row = rows;
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
fill_default_cylinder(&current->cylinder[row]); fill_default_cylinder(&current->cylinder[row]);
beginInsertRows(QModelIndex(), row, row); beginInsertRows(QModelIndex(), row, row);
rows++; rows++;
@ -329,8 +323,6 @@ void CylindersModel::add()
void CylindersModel::update() void CylindersModel::update()
{ {
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
setDive(current); setDive(current);
} }
@ -344,7 +336,6 @@ void CylindersModel::clear()
void CylindersModel::setDive(dive* d) void CylindersModel::setDive(dive* d)
{ {
struct dive *current = getDiveById(currentId);
if (current) if (current)
clear(); clear();
if (!d) if (!d)
@ -355,7 +346,7 @@ void CylindersModel::setDive(dive* d)
rows = i+1; rows = i+1;
} }
} }
currentId = d->id; current = d;
changed = false; changed = false;
if (rows > 0) { if (rows > 0) {
beginInsertRows(QModelIndex(), 0, rows-1); beginInsertRows(QModelIndex(), 0, rows-1);
@ -375,8 +366,6 @@ void CylindersModel::remove(const QModelIndex& index)
if (index.column() != REMOVE) { if (index.column() != REMOVE) {
return; return;
} }
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
cylinder_t *cyl = &current->cylinder[index.row()]; cylinder_t *cyl = &current->cylinder[index.row()];
if (DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)) { if (DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)) {
QMessageBox::warning(mainWindow(), TITLE_OR_TEXT( QMessageBox::warning(mainWindow(), TITLE_OR_TEXT(
@ -392,7 +381,7 @@ void CylindersModel::remove(const QModelIndex& index)
endRemoveRows(); endRemoveRows();
} }
WeightModel::WeightModel(QObject* parent): currentId(0), rows(0) WeightModel::WeightModel(QObject* parent): current(0), rows(0)
{ {
//enum Column {REMOVE, TYPE, WEIGHT}; //enum Column {REMOVE, TYPE, WEIGHT};
setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight")); setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight"));
@ -400,8 +389,6 @@ WeightModel::WeightModel(QObject* parent): currentId(0), rows(0)
weightsystem_t* WeightModel::weightSystemAt(const QModelIndex& index) weightsystem_t* WeightModel::weightSystemAt(const QModelIndex& index)
{ {
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
return &current->weightsystem[index.row()]; return &current->weightsystem[index.row()];
} }
@ -410,8 +397,6 @@ void WeightModel::remove(const QModelIndex& index)
if (index.column() != REMOVE) { if (index.column() != REMOVE) {
return; return;
} }
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly. beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly.
rows--; rows--;
remove_weightsystem(current, index.row()); remove_weightsystem(current, index.row());
@ -433,8 +418,6 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS)
return ret; return ret;
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
weightsystem_t *ws = &current->weightsystem[index.row()]; weightsystem_t *ws = &current->weightsystem[index.row()];
switch (role) { switch (role) {
@ -472,8 +455,6 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
// so we only implement the two columns we care about // so we only implement the two columns we care about
void WeightModel::passInData(const QModelIndex& index, const QVariant& value) void WeightModel::passInData(const QModelIndex& index, const QVariant& value)
{ {
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
weightsystem_t *ws = &current->weightsystem[index.row()]; weightsystem_t *ws = &current->weightsystem[index.row()];
if (index.column() == WEIGHT) { if (index.column() == WEIGHT) {
if (ws->weight.grams != value.toInt()) { if (ws->weight.grams != value.toInt()) {
@ -507,8 +488,6 @@ lbs:
bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role) bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
{ {
QString vString = value.toString(); QString vString = value.toString();
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
weightsystem_t *ws = &current->weightsystem[index.row()]; weightsystem_t *ws = &current->weightsystem[index.row()];
switch(index.column()) { switch(index.column()) {
case TYPE: case TYPE:
@ -570,14 +549,11 @@ void WeightModel::add()
void WeightModel::update() void WeightModel::update()
{ {
struct dive *current = getDiveById(currentId);
Q_ASSERT(current != NULL);
setDive(current); setDive(current);
} }
void WeightModel::setDive(dive* d) void WeightModel::setDive(dive* d)
{ {
struct dive *current = getDiveById(currentId);
if (current) if (current)
clear(); clear();
rows = 0; rows = 0;
@ -586,7 +562,7 @@ void WeightModel::setDive(dive* d)
rows = i+1; rows = i+1;
} }
} }
currentId = d->id; current = d;
changed = false; changed = false;
if (rows > 0) { if (rows > 0) {
beginInsertRows(QModelIndex(), 0, rows-1); beginInsertRows(QModelIndex(), 0, rows-1);

View file

@ -101,7 +101,7 @@ public slots:
void remove(const QModelIndex& index); void remove(const QModelIndex& index);
private: private:
int currentId; struct dive *current;
int rows; int rows;
}; };
@ -130,7 +130,7 @@ public slots:
void remove(const QModelIndex& index); void remove(const QModelIndex& index);
private: private:
int currentId; struct dive *current;
int rows; int rows;
}; };