UI restructure: get rid of "current" pointer in cylinders and weights

Instead always display the display_dive.

Now the only thing left to slay is the stagingDive...

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-07-02 22:38:08 -07:00
parent a86aca0378
commit 6b1b2bc919
4 changed files with 59 additions and 49 deletions

View file

@ -111,7 +111,16 @@ void DivePlannerPointsModel::loadFromDive(dive *d)
copy_samples(&d->dc, &backupDive.dc); copy_samples(&d->dc, &backupDive.dc);
copy_events(&d->dc, &backupDive.dc); copy_events(&d->dc, &backupDive.dc);
copy_cylinders(d, stagingDive, false); // this way the correct cylinder data is shown copy_cylinders(d, stagingDive, false); // this way the correct cylinder data is shown
CylindersModel::instance()->setDive(stagingDive);
// FIXME -- need to get rid of stagingDIve
// the following now uses displayed_dive !!!!
CylindersModel::instance()->updateDive();
int lasttime = 0; int lasttime = 0;
// we start with the first gas and see if it was changed // we start with the first gas and see if it was changed
struct gasmix gas = backupDive.cylinder[0].gasmix; struct gasmix gas = backupDive.cylinder[0].gasmix;
@ -768,7 +777,15 @@ bool DivePlannerPointsModel::addGas(struct gasmix mix)
modpO2.mbar = prefs.decopo2; modpO2.mbar = prefs.decopo2;
cyl->depth = gas_mod(&mix, modpO2, M_OR_FT(3,10)); cyl->depth = gas_mod(&mix, modpO2, M_OR_FT(3,10));
CylindersModel::instance()->setDive(stagingDive);
// FIXME -- need to get rid of stagingDIve
// the following now uses displayed_dive !!!!
CylindersModel::instance()->updateDive();
return true; return true;
} }
if (!gasmix_distance(&cyl->gasmix, &mix)) if (!gasmix_distance(&cyl->gasmix, &mix))
@ -995,7 +1012,16 @@ void DivePlannerPointsModel::clear()
stagingDive = alloc_dive(); stagingDive = alloc_dive();
} }
bool oldRecalc = setRecalc(false); bool oldRecalc = setRecalc(false);
CylindersModel::instance()->setDive(stagingDive);
// FIXME -- need to get rid of stagingDIve
// the following now uses displayed_dive !!!!
CylindersModel::instance()->updateDive();
if (rowCount() > 0) { if (rowCount() > 0) {
beginRemoveRows(QModelIndex(), 0, rowCount() - 1); beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
divepoints.clear(); divepoints.clear();

View file

@ -432,8 +432,8 @@ void MainTab::updateDiveInfo(bool clear)
ui.LocationLabel->setText(tr("Location")); ui.LocationLabel->setText(tr("Location"));
ui.NotesLabel->setText(tr("Notes")); ui.NotesLabel->setText(tr("Notes"));
ui.equipmentTab->setEnabled(true); ui.equipmentTab->setEnabled(true);
cylindersModel->setDive(&displayed_dive); cylindersModel->updateDive();
weightModel->setDive(&displayed_dive); weightModel->updateDive();
taglist_get_tagstring(displayed_dive.tag_list, buf, 1024); taglist_get_tagstring(displayed_dive.tag_list, buf, 1024);
ui.tagWidget->setText(QString(buf)); ui.tagWidget->setText(QString(buf));
} }
@ -828,8 +828,8 @@ void MainTab::rejectChanges()
} }
if (selected_dive >= 0) { if (selected_dive >= 0) {
copy_dive(current_dive, &displayed_dive); copy_dive(current_dive, &displayed_dive);
cylindersModel->setDive(&displayed_dive); cylindersModel->updateDive();
weightModel->setDive(&displayed_dive); weightModel->updateDive();
} else { } else {
cylindersModel->clear(); cylindersModel->clear();
weightModel->clear(); weightModel->clear();

View file

@ -63,7 +63,7 @@ void CleanerTableModel::setHeaderDataStrings(const QStringList &newHeaders)
headers = newHeaders; headers = newHeaders;
} }
CylindersModel::CylindersModel(QObject *parent) : current(0), rows(0) CylindersModel::CylindersModel(QObject *parent) : 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%") setHeaderDataStrings(QStringList() << "" << tr("Type") << tr("Size") << tr("WorkPress") << tr("StartPress") << tr("EndPress") << trUtf8("O" UTF8_SUBSCRIPT_2 "%") << tr("He%")
@ -93,7 +93,7 @@ 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;
cylinder_t *cyl = &current->cylinder[index.row()]; cylinder_t *cyl = &displayed_dive.cylinder[index.row()];
switch (role) { switch (role) {
case Qt::FontRole: { case Qt::FontRole: {
QFont font = defaultModelFont(); QFont font = defaultModelFont();
@ -164,7 +164,7 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
cylinder_t *CylindersModel::cylinderAt(const QModelIndex &index) cylinder_t *CylindersModel::cylinderAt(const QModelIndex &index)
{ {
return &current->cylinder[index.row()]; return &displayed_dive.cylinder[index.row()];
} }
// this is our magic 'pass data in' function that allows the delegate to get // this is our magic 'pass data in' function that allows the delegate to get
@ -285,19 +285,14 @@ void CylindersModel::add()
} }
int row = rows; int row = rows;
fill_default_cylinder(&current->cylinder[row]); fill_default_cylinder(&displayed_dive.cylinder[row]);
current->cylinder[row].manually_added = true; displayed_dive.cylinder[row].manually_added = true;
beginInsertRows(QModelIndex(), row, row); beginInsertRows(QModelIndex(), row, row);
rows++; rows++;
changed = true; changed = true;
endInsertRows(); endInsertRows();
} }
void CylindersModel::update()
{
setDive(current);
}
void CylindersModel::clear() void CylindersModel::clear()
{ {
if (rows > 0) { if (rows > 0) {
@ -306,19 +301,19 @@ void CylindersModel::clear()
} }
} }
void CylindersModel::setDive(dive *d) void CylindersModel::updateDive()
{ {
if (current)
clear(); clear();
if (!d) if (dive_table.nr == 0)
return; return;
rows = 0; rows = 0;
for (int i = 0; i < MAX_CYLINDERS; i++) { for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!cylinder_none(&d->cylinder[i]) && if (!cylinder_none(&displayed_dive.cylinder[i]) &&
(prefs.display_unused_tanks || cylinder_is_used(d, &d->cylinder[i]) || d->cylinder[i].manually_added)) (prefs.display_unused_tanks ||
cylinder_is_used(&displayed_dive, &displayed_dive.cylinder[i]) ||
displayed_dive.cylinder[i].manually_added))
rows = i + 1; rows = i + 1;
} }
current = d;
changed = false; changed = false;
if (rows > 0) { if (rows > 0) {
beginInsertRows(QModelIndex(), 0, rows - 1); beginInsertRows(QModelIndex(), 0, rows - 1);
@ -354,11 +349,11 @@ void CylindersModel::remove(const QModelIndex &index)
if (index.column() != REMOVE) { if (index.column() != REMOVE) {
return; return;
} }
cylinder_t *cyl = &current->cylinder[index.row()]; cylinder_t *cyl = &displayed_dive.cylinder[index.row()];
if ((DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING && if ((DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING &&
DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix)) || DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix)) ||
(DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING && (DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING &&
(cyl->manually_added || (current_dive && cylinder_is_used(current_dive, cyl))))) { (cyl->manually_added || cylinder_is_used(&displayed_dive, cyl)))) {
QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT( QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(
tr("Cylinder cannot be removed"), tr("Cylinder cannot be removed"),
tr("This gas in use. Only cylinders that are not used in the dive can be removed.")), tr("This gas in use. Only cylinders that are not used in the dive can be removed.")),
@ -367,12 +362,12 @@ void CylindersModel::remove(const QModelIndex &index)
} }
beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly. beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly.
rows--; rows--;
remove_cylinder(current, index.row()); remove_cylinder(&displayed_dive, index.row());
changed = true; changed = true;
endRemoveRows(); endRemoveRows();
} }
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent), current(0), rows(0) WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent), 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"));
@ -380,7 +375,7 @@ WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent), current(0
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index) weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
{ {
return &current->weightsystem[index.row()]; return &displayed_dive.weightsystem[index.row()];
} }
void WeightModel::remove(const QModelIndex &index) void WeightModel::remove(const QModelIndex &index)
@ -390,7 +385,7 @@ void WeightModel::remove(const QModelIndex &index)
} }
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(&displayed_dive, index.row());
changed = true; changed = true;
endRemoveRows(); endRemoveRows();
} }
@ -409,7 +404,7 @@ 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;
weightsystem_t *ws = &current->weightsystem[index.row()]; weightsystem_t *ws = &displayed_dive.weightsystem[index.row()];
switch (role) { switch (role) {
case Qt::FontRole: case Qt::FontRole:
@ -446,7 +441,7 @@ 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)
{ {
weightsystem_t *ws = &current->weightsystem[index.row()]; weightsystem_t *ws = &displayed_dive.weightsystem[index.row()];
if (index.column() == WEIGHT) { if (index.column() == WEIGHT) {
if (ws->weight.grams != value.toInt()) { if (ws->weight.grams != value.toInt()) {
ws->weight.grams = value.toInt(); ws->weight.grams = value.toInt();
@ -574,7 +569,7 @@ fraction_t string_to_fraction(const char *str)
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();
weightsystem_t *ws = &current->weightsystem[index.row()]; weightsystem_t *ws = &displayed_dive.weightsystem[index.row()];
switch (index.column()) { switch (index.column()) {
case TYPE: case TYPE:
if (!value.isNull()) { if (!value.isNull()) {
@ -634,22 +629,15 @@ void WeightModel::add()
endInsertRows(); endInsertRows();
} }
void WeightModel::update() void WeightModel::updateDive()
{ {
setDive(current);
}
void WeightModel::setDive(dive *d)
{
if (current)
clear(); clear();
rows = 0; rows = 0;
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) { for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
if (!weightsystem_none(&d->weightsystem[i])) { if (!weightsystem_none(&displayed_dive.weightsystem[i])) {
rows = i + 1; rows = i + 1;
} }
} }
current = d;
changed = false; changed = false;
if (rows > 0) { if (rows > 0) {
beginInsertRows(QModelIndex(), 0, rows - 1); beginInsertRows(QModelIndex(), 0, rows - 1);

View file

@ -116,8 +116,7 @@ public:
void passInData(const QModelIndex &index, const QVariant &value); void passInData(const QModelIndex &index, const QVariant &value);
void add(); void add();
void clear(); void clear();
void update(); void updateDive();
void setDive(struct dive *d);
void copyFromDive(struct dive *d); void copyFromDive(struct dive *d);
cylinder_t *cylinderAt(const QModelIndex &index); cylinder_t *cylinderAt(const QModelIndex &index);
bool changed; bool changed;
@ -127,7 +126,6 @@ slots:
void remove(const QModelIndex &index); void remove(const QModelIndex &index);
private: private:
struct dive *current;
int rows; int rows;
}; };
@ -151,8 +149,7 @@ public:
void passInData(const QModelIndex &index, const QVariant &value); void passInData(const QModelIndex &index, const QVariant &value);
void add(); void add();
void clear(); void clear();
void update(); void updateDive();
void setDive(struct dive *d);
weightsystem_t *weightSystemAt(const QModelIndex &index); weightsystem_t *weightSystemAt(const QModelIndex &index);
bool changed; bool changed;
@ -161,7 +158,6 @@ slots:
void remove(const QModelIndex &index); void remove(const QModelIndex &index);
private: private:
struct dive *current;
int rows; int rows;
}; };