From 611bae344111845bfa8bd676c0fad49d1c051c10 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 3 Jul 2014 14:45:01 -0700 Subject: [PATCH] UI restructure: use displayed_dive for add dive and plan dive This gets rid of the stagingDive and stops the constant adding and removing of dives from the divelist (that was an INSANE design, seriously). When adding or planning a dive all work is now done in the dedicated displayed_dive. Add dive mostly works - when the user clicks save the dive is added to the dive list and selected. Plan dive is mostly untested. It passed trivial "start planner, save" testing so it's not entirely broken, but I'm sure there's more work to be done there. Signed-off-by: Dirk Hohndel --- planner.c | 14 ++--- qt-ui/diveplanner.cpp | 138 ++++++++++++++---------------------------- qt-ui/diveplanner.h | 3 - qt-ui/maintab.cpp | 46 +++++++++++--- qt-ui/mainwindow.cpp | 47 +++++++------- qt-ui/mainwindow.h | 2 - 6 files changed, 113 insertions(+), 137 deletions(-) diff --git a/planner.c b/planner.c index 0c398bacb..a3f0c0b62 100644 --- a/planner.c +++ b/planner.c @@ -257,14 +257,14 @@ static void create_dive_from_plan(struct diveplan *diveplan) printf("in create_dive_from_plan\n"); dump_plan(diveplan); #endif - // clear out the dive, fill in the basics and get started - clear_dive(&displayed_dive); - displayed_dive.when = diveplan->when; - displayed_dive.dc.surface_pressure.mbar = diveplan->surface_pressure; - dc = &displayed_dive.dc; - dc->model = "planned dive"; /* do not translate here ! */ - dp = diveplan->dp; + // reset the cylinders and clear out the samples of the displayed dive so we can restart reset_cylinders(&displayed_dive); + dc = &displayed_dive.dc; + free(dc->sample); + dc->sample = NULL; + dc->samples = 0; + dc->alloc_samples = 0; + dp = diveplan->dp; cyl = &displayed_dive.cylinder[0]; oldgasmix = cyl->gasmix; sample = prepare_sample(dc); diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp index 845961daa..d0f208010 100644 --- a/qt-ui/diveplanner.cpp +++ b/qt-ui/diveplanner.cpp @@ -66,9 +66,13 @@ void DivePlannerPointsModel::removeSelectedPoints(const QVector &rows) void DivePlannerPointsModel::createSimpleDive() { struct gasmix gas = { 0 }; + + // set the start time of the plan + diveplan.when = displayed_dive.when; + if (isPlanner()) // let's use the gas from the first cylinder - gas = stagingDive->cylinder[0].gasmix; + gas = displayed_dive.cylinder[0].gasmix; // If we're in drop_stone_mode, don't add a first point. // It will be added implicit. @@ -110,15 +114,6 @@ void DivePlannerPointsModel::loadFromDive(dive *d) // but this whole section needs to be rewritten, anyway copy_samples(&d->dc, &backupDive.dc); copy_events(&d->dc, &backupDive.dc); - copy_cylinders(d, stagingDive, false); // this way the correct cylinder data is shown - - - -// FIXME -- need to get rid of stagingDIve -// the following now uses displayed_dive !!!! - - - CylindersModel::instance()->updateDive(); int lasttime = 0; @@ -139,56 +134,39 @@ void DivePlannerPointsModel::restoreBackupDive() memcpy(current_dive, &backupDive, sizeof(struct dive)); } -void DivePlannerPointsModel::copyCylinders(dive *d) -{ - copy_cylinders(stagingDive, d, false); -} - // copy the tanks from the current dive, or the default cylinder // or an unknown cylinder // setup the cylinder widget accordingly void DivePlannerPointsModel::setupCylinders() { - if (!stagingDive) + if (mode == PLAN && current_dive) { + // take the used cylinders from the selected dive as starting point + CylindersModel::instance()->copyFromDive(current_dive); + copy_cylinders(current_dive, &displayed_dive, true); + reset_cylinders(&displayed_dive); return; - - if (stagingDive != current_dive) { - // we are planning a dive - if (current_dive) { - // take the used cylinders from the selected dive as starting point - CylindersModel::instance()->copyFromDive(current_dive); - copy_cylinders(current_dive, stagingDive, true); - reset_cylinders(stagingDive); - return; - } else { - if (!same_string(prefs.default_cylinder, "")) { - fill_default_cylinder(&stagingDive->cylinder[0]); - } else { - // roughly an AL80 - stagingDive->cylinder[0].type.description = strdup(tr("unknown").toUtf8().constData()); - stagingDive->cylinder[0].type.size.mliter = 11100; - stagingDive->cylinder[0].type.workingpressure.mbar = 207000; - } - } } - reset_cylinders(stagingDive); - CylindersModel::instance()->copyFromDive(stagingDive); + if (!same_string(prefs.default_cylinder, "")) { + fill_default_cylinder(&displayed_dive.cylinder[0]); + } else { + // roughly an AL80 + displayed_dive.cylinder[0].type.description = strdup(tr("unknown").toUtf8().constData()); + displayed_dive.cylinder[0].type.size.mliter = 11100; + displayed_dive.cylinder[0].type.workingpressure.mbar = 207000; + } + reset_cylinders(&displayed_dive); + CylindersModel::instance()->copyFromDive(&displayed_dive); } QStringList &DivePlannerPointsModel::getGasList() { - struct dive *activeDive = isPlanner() ? stagingDive : current_dive; static QStringList list; list.clear(); - if (!activeDive) { - list.push_back(tr("AIR")); - } else { - for (int i = 0; i < MAX_CYLINDERS; i++) { - cylinder_t *cyl = &activeDive->cylinder[i]; - if (cylinder_nodata(cyl)) - break; - list.push_back(gasToStr(cyl->gasmix)); - } + for (int i = 0; i < MAX_CYLINDERS; i++) { + cylinder_t *cyl = &displayed_dive.cylinder[i]; + if (cylinder_nodata(cyl)) + break; + list.push_back(gasToStr(cyl->gasmix)); } return list; } @@ -501,8 +479,6 @@ void PlannerSettingsWidget::setDecoPo2(double po2) void DivePlannerPointsModel::setPlanMode(Mode m) { mode = m; - if (m == NOTHING) - stagingDive = NULL; // the planner may reset our GF settings that are used to show deco // reset them to what's in the preferences if (m != PLAN) @@ -643,7 +619,7 @@ int DivePlannerPointsModel::rowCount(const QModelIndex &parent) const return divepoints.count(); } -DivePlannerPointsModel::DivePlannerPointsModel(QObject *parent) : QAbstractTableModel(parent), mode(NOTHING), tempDive(NULL), stagingDive(NULL) +DivePlannerPointsModel::DivePlannerPointsModel(QObject *parent) : QAbstractTableModel(parent), mode(NOTHING) { memset(&diveplan, 0, sizeof(diveplan)); memset(&backupDive, 0, sizeof(backupDive)); @@ -767,7 +743,7 @@ bool DivePlannerPointsModel::addGas(struct gasmix mix) sanitize_gasmix(&mix); for (int i = 0; i < MAX_CYLINDERS; i++) { - cylinder_t *cyl = &stagingDive->cylinder[i]; + cylinder_t *cyl = &displayed_dive.cylinder[i]; if (cylinder_nodata(cyl)) { fill_default_cylinder(cyl); cyl->gasmix = mix; @@ -827,7 +803,7 @@ int DivePlannerPointsModel::addStop(int milimeters, int seconds, gasmix *gas_in, milimeters = M_OR_FT(5, 15); // 5m / 15ft seconds = 600; // 10 min //Default to the first defined gas, if we got one. - cylinder_t *cyl = &stagingDive->cylinder[0]; + cylinder_t *cyl = &displayed_dive.cylinder[0]; if (cyl) gas = cyl->gasmix; } @@ -927,12 +903,16 @@ void DivePlannerPointsModel::cancelPlan() return; } } - - if (mode != ADD) // for ADD stagingDive points at current_dive - free(stagingDive); - stagingDive = NULL; // always reset the stagingDive to NULL setPlanMode(NOTHING); diveplan.dp = NULL; + + + + + // somewhere, somehow we need to make sure that the current_dive is displayed again + + + emit planCanceled(); } @@ -953,7 +933,7 @@ QVector > DivePlannerPointsModel::collectGases(struct dive *d) } void DivePlannerPointsModel::rememberTanks() { - oldGases = collectGases(stagingDive); + oldGases = collectGases(&displayed_dive); } bool DivePlannerPointsModel::tankInUse(struct gasmix gasmix) @@ -976,7 +956,7 @@ void DivePlannerPointsModel::tanksUpdated() // "did a gas change on us". So we look through the diveplan to // see if there is a gas that is now missing and if there is, we // replace it with the matching new gas. - QVector > gases = collectGases(stagingDive); + QVector > gases = collectGases(&displayed_dive); if (gases.count() == oldGases.count()) { // either nothing relevant changed, or exactly ONE gasmix changed for (int i = 0; i < gases.count(); i++) { @@ -1005,12 +985,6 @@ void DivePlannerPointsModel::tanksUpdated() void DivePlannerPointsModel::clear() { - Q_ASSERT(stagingDive == 0); - if (mode == ADD) { - stagingDive = current_dive; - } else { - stagingDive = alloc_dive(); - } bool oldRecalc = setRecalc(false); @@ -1033,13 +1007,7 @@ void DivePlannerPointsModel::clear() void DivePlannerPointsModel::createTemporaryPlan() { - // This needs to be done in the following steps: // Get the user-input and calculate the dive info - // Not sure if this is the place to create the diveplan... - // We just start with a surface node at time = 0 - if (!stagingDive) - return; - //TODO: this thingy looks like it could be a good C-based function diveplan.dp = NULL; int lastIndex = -1; for (int i = 0; i < rowCount(); i++) { @@ -1054,11 +1022,12 @@ void DivePlannerPointsModel::createTemporaryPlan() if (p.entered) plan_add_segment(&diveplan, deltaT, p.depth, p.gasmix, p.po2, true); } + + // what does the cache do??? char *cache = NULL; - tempDive = NULL; struct divedatapoint *dp = NULL; for (int i = 0; i < MAX_CYLINDERS; i++) { - cylinder_t *cyl = &stagingDive->cylinder[i]; + cylinder_t *cyl = &displayed_dive.cylinder[i]; if (cyl->depth.mm) { dp = create_dp(0, cyl->depth.mm, cyl->gasmix, 0); if (diveplan.dp) { @@ -1074,19 +1043,12 @@ void DivePlannerPointsModel::createTemporaryPlan() dump_plan(&diveplan); #endif if (plannerModel->recalcQ() && !diveplan_empty(&diveplan)) { - plan(&diveplan, &cache, &tempDive, stagingDive, isPlanner(), false); - MainWindow::instance()->setPlanNotes(tempDive->notes); - if (mode == ADD || mode == PLAN) { - // copy the samples and events of the first dive computer, but don't overwrite the cylinders - // FIXME this needs to be rewritten - copy_samples(&tempDive->dc, ¤t_dive->dc); - copy_events(&tempDive->dc, ¤t_dive->dc); - copy_cylinders(tempDive, current_dive, false); - } + plan(&diveplan, &cache, isPlanner(), false); } // throw away the cache free(cache); #if DEBUG_PLAN + save_dive(stderr, &displayed_dive); dump_plan(&diveplan); #endif } @@ -1095,8 +1057,6 @@ void DivePlannerPointsModel::deleteTemporaryPlan() { deleteTemporaryPlan(diveplan.dp); diveplan.dp = NULL; - delete_single_dive(get_divenr(tempDive)); - tempDive = NULL; } void DivePlannerPointsModel::deleteTemporaryPlan(struct divedatapoint *dp) @@ -1110,27 +1070,21 @@ void DivePlannerPointsModel::deleteTemporaryPlan(struct divedatapoint *dp) void DivePlannerPointsModel::createPlan() { - // Ok, so, here the diveplan creates a dive, - // puts it on the dive list, and we need to remember - // to not delete it later. mumble. ;p + // Ok, so, here the diveplan creates a dive char *cache = NULL; - tempDive = NULL; - bool oldRecalc = plannerModel->setRecalc(false); removeDeco(); createTemporaryPlan(); plannerModel->setRecalc(oldRecalc); //TODO: C-based function here? - plan(&diveplan, &cache, &tempDive, stagingDive, isPlanner(), true); - record_dive(tempDive); + plan(&diveplan, &cache, isPlanner(), true); + record_dive(clone_dive(&displayed_dive)); mark_divelist_changed(true); // Remove and clean the diveplan, so we don't delete // the dive by mistake. diveplan.dp = NULL; setPlanMode(NOTHING); - free(stagingDive); - stagingDive = NULL; planCreated(); } diff --git a/qt-ui/diveplanner.h b/qt-ui/diveplanner.h index 02c401822..f2ed4f582 100644 --- a/qt-ui/diveplanner.h +++ b/qt-ui/diveplanner.h @@ -46,7 +46,6 @@ public: void tanksUpdated(); void rememberTanks(); bool tankInUse(struct gasmix gasmix); - void copyCylinders(struct dive *d); void setupCylinders(); /** * @return the row number. @@ -100,11 +99,9 @@ private: Mode mode; bool recalc; QVector divepoints; - struct dive *tempDive; struct dive backupDive; void deleteTemporaryPlan(struct divedatapoint *dp); QVector backupSamples; // For editing added dives. - struct dive *stagingDive; QVector > oldGases; bool drop_stone_mode; QDateTime startTime; diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index f53a70e6e..4495731be 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -249,7 +249,7 @@ void MainTab::updateTextLabels(bool showUnits) void MainTab::enableEdition(EditMode newEditMode) { - if (current_dive == NULL || editMode != NONE) + if (((newEditMode == DIVE || newEditMode == NONE) && current_dive == NULL) || editMode != NONE) return; modified = false; if ((newEditMode == DIVE || newEditMode == NONE) && @@ -257,12 +257,22 @@ void MainTab::enableEdition(EditMode newEditMode) strcmp(current_dive->dc.model, "manually added dive") == 0) { // editCurrentDive will call enableEdition with newEditMode == MANUALLY_ADDED_DIVE // so exit this function here after editCurrentDive() returns + + + + // FIXME : can we get rid of this recursive crap? + + + MainWindow::instance()->editCurrentDive(); return; } MainWindow::instance()->dive_list()->setEnabled(false); - if (amount_selected == 1) + + // only setup the globe for editing if we are editing exactly one existing dive + if (amount_selected == 1 && newEditMode != ADD) MainWindow::instance()->globe()->prepareForGetDiveCoordinates(); + if (MainWindow::instance() && MainWindow::instance()->dive_list()->selectedTrips().count() == 1) { // we are editing trip location and notes displayMessage(tr("This trip is being edited.")); @@ -276,8 +286,6 @@ void MainTab::enableEdition(EditMode newEditMode) } else { displayMessage(tr("This dive is being edited.")); } - // editedDive already contains the current dive (we set this up in updateDiveInfo), - // so all we need to do is update the editMode if necessary editMode = newEditMode != NONE ? newEditMode : DIVE; } } @@ -598,14 +606,24 @@ void MainTab::reload() void MainTab::acceptChanges() { - int i; + int i, addedId = -1; struct dive *d; tabBar()->setTabIcon(0, QIcon()); // Notes tabBar()->setTabIcon(1, QIcon()); // Equipment hideMessage(); ui.equipmentTab->setEnabled(true); - /* now figure out if things have changed */ - if (MainWindow::instance() && MainWindow::instance()->dive_list()->selectedTrips().count() == 1) { + if (editMode == ADD) { + // we need to add the dive we just created to the dive list and select it. + // Easy, right? + struct dive *added_dive = clone_dive(&displayed_dive); + record_dive(added_dive); + addedId = added_dive->id; + // unselect everything as far as the UI is concerned - we'll fix that below + MainWindow::instance()->dive_list()->unselectDives(); + selected_dive = get_divenr(added_dive); + amount_selected = 1; + } else if (MainWindow::instance() && MainWindow::instance()->dive_list()->selectedTrips().count() == 1) { + /* now figure out if things have changed */ if (!same_string(displayed_dive.notes, current_dive->divetrip->notes)) { current_dive->divetrip->notes = strdup(displayed_dive.notes); mark_divelist_changed(true); @@ -670,9 +688,13 @@ void MainTab::acceptChanges() } if (tagsChanged(&displayed_dive, cd)) saveTags(); + +#if 0 // with the new architecture this shouldn't be needed anymore if (editMode == MANUALLY_ADDED_DIVE) { DivePlannerPointsModel::instance()->copyCylinders(cd); - } else if (editMode != ADD && cylindersModel->changed) { + } else +#endif + if (editMode != ADD && cylindersModel->changed) { mark_divelist_changed(true); MODIFY_SELECTED_DIVES( for (int i = 0; i < MAX_CYLINDERS; i++) { @@ -733,6 +755,12 @@ void MainTab::acceptChanges() int scrolledBy = MainWindow::instance()->dive_list()->verticalScrollBar()->sliderPosition(); resetPallete(); if (editMode == ADD || editMode == MANUALLY_ADDED_DIVE) { + // now let's resort the dive list and make sure the newly added dive is still selected + sort_table(&dive_table); + MainWindow::instance()->dive_list()->unselectDives(); + int newDiveNr = get_divenr(get_dive_by_uniq_id(addedId)); + MainWindow::instance()->dive_list()->selectDive(newDiveNr, true); +#if 0 // it's tricky to keep the right dive selected; // first remember which one is selected in the current sort order // and unselect all dives @@ -753,7 +781,7 @@ void MainTab::acceptChanges() // selected - but that may not be the right one, so select the one // we remembered instead MainWindow::instance()->dive_list()->selectDive(rememberSelected, true); - +#endif editMode = NONE; MainWindow::instance()->refreshDisplay(); MainWindow::instance()->graphics()->replot(); diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 04f94671b..0eb753203 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -63,7 +63,6 @@ MainWindow::MainWindow() : QMainWindow(), yearlyStatsModel(0), state(VIEWALL), updateManager(0), - fakeDiveId(0), survey(0) { Q_ASSERT_X(m_Instance == NULL, "MainWindow", "MainWindow recreated!"); @@ -392,48 +391,38 @@ bool MainWindow::plannerStateClean() return true; } +// setup displayed_dive so we can start planning with it void MainWindow::createFakeDiveForAddAndPlan() { - // now cheat - create one dive that we use to store the info tab data in - //TODO: C-function create_temporary_dive ? - struct dive *dive = alloc_dive(); - fakeDiveId = dive->id; - dive->when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset(); - dive->dc.model = "manually added dive"; // don't translate! this is stored in the XML file + clear_dive(&displayed_dive); + displayed_dive.when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset() + 3600; + displayed_dive.dc.model = "manually added dive"; // don't translate! this is stored in the XML file + // now show the mostly empty main tab + ui.InfoWidget->updateDiveInfo(); - dive->latitude.udeg = 0; - dive->longitude.udeg = 0; - record_dive(dive); +#if 0 // don't want to do any of this, I think // select this new dive (but remember the old selection ui.ListWidget->rememberSelection(); ui.ListWidget->unselectDives(); ui.ListWidget->reload(DiveTripModel::CURRENT); ui.ListWidget->selectDives(QList() << dive_table.nr - 1); ui.InfoWidget->updateDiveInfo(); -} - -void MainWindow::removeFakeDiveForAddAndPlan() -{ - int idx; - - if (!fakeDiveId || - (idx = get_idx_by_uniq_id(fakeDiveId)) == dive_table.nr) - return; - delete_single_dive(idx); + showProfile(); +#endif } void MainWindow::planCanceled() { - removeFakeDiveForAddAndPlan(); showProfile(); +#if 0 // shouldn't need this ui.ListWidget->reload(DiveTripModel::CURRENT); ui.ListWidget->restoreSelection(); +#endif refreshDisplay(); } void MainWindow::planCreated() { - removeFakeDiveForAddAndPlan(); showProfile(); refreshDisplay(); } @@ -501,16 +490,26 @@ void MainWindow::on_actionAddDive_triggered() ui.ListWidget->endSearch(); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD); - createFakeDiveForAddAndPlan(); + clear_dive(&displayed_dive); + displayed_dive.when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset() + 3600; + displayed_dive.dc.model = "manually added dive"; // don't translate! this is stored in the XML file + // setup the dive cylinders + DivePlannerPointsModel::instance()->setupCylinders(); + + // now show the mostly empty main tab + ui.InfoWidget->updateDiveInfo(); + + // show main tab ui.InfoWidget->setCurrentIndex(0); + ui.InfoWidget->addDiveStarted(); ui.infoPane->setCurrentIndex(MAINTAB); ui.newProfile->setAddState(); DivePlannerPointsModel::instance()->clear(); DivePlannerPointsModel::instance()->createSimpleDive(); - ui.ListWidget->reload(DiveTripModel::CURRENT); + ui.newProfile->plotDive(); } void MainWindow::on_actionRenumber_triggered() diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h index 8bdb47e99..d0406ff15 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -156,7 +156,6 @@ slots: void recreateDiveList(); void showProfile(); void editCurrentDive(); - void removeFakeDiveForAddAndPlan(); void planCanceled(); void planCreated(); @@ -185,7 +184,6 @@ private: bool plannerStateClean(); void createFakeDiveForAddAndPlan(); - int fakeDiveId; QDialog *survey; };