diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index fff60ddea..36d9faf5a 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -103,6 +103,11 @@ void editDiveSiteCountry(dive_site *ds, const QString &value) execute(new EditDiveSiteCountry(ds, value)); } +void editDiveSiteLocation(dive_site *ds, const QString &value) +{ + execute(new EditDiveSiteLocation(ds, value)); +} + void addDiveSite(const QString &name) { execute(new AddDiveSite(name)); diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index 77209a6bb..782a46ea5 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -45,6 +45,7 @@ void editDiveSiteName(dive_site *ds, const QString &value); void editDiveSiteDescription(dive_site *ds, const QString &value); void editDiveSiteNotes(dive_site *ds, const QString &value); void editDiveSiteCountry(dive_site *ds, const QString &value); +void editDiveSiteLocation(dive_site *ds, const QString &value); void addDiveSite(const QString &name); } // namespace Command diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp index 5534bc5bb..5e2a8b18a 100644 --- a/desktop-widgets/command_divesite.cpp +++ b/desktop-widgets/command_divesite.cpp @@ -208,4 +208,40 @@ void EditDiveSiteCountry::undo() redo(); } +// Parse GPS text into location_t +static location_t parseGpsText(const QString &text) +{ + double lat, lon; + if (parseGpsText(text, &lat, &lon)) + return create_location(lat, lon); + return { {0}, {0} }; +} + +EditDiveSiteLocation::EditDiveSiteLocation(dive_site *dsIn, const QString &location) : ds(dsIn), + value(parseGpsText(location)) +{ + setText(tr("Edit dive site location")); +} + +bool EditDiveSiteLocation::workToBeDone() +{ + bool ok = has_location(&value); + bool old_ok = has_location(&ds->location); + if (ok != old_ok) + return true; + return ok && !same_location(&value, &ds->location); +} + +void EditDiveSiteLocation::redo() +{ + std::swap(value, ds->location); + emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::LOCATION); // Inform frontend of changed dive site. +} + +void EditDiveSiteLocation::undo() +{ + // Undo and redo do the same + redo(); +} + } // namespace Command diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h index f05bc765d..7ebc69ccb 100644 --- a/desktop-widgets/command_divesite.h +++ b/desktop-widgets/command_divesite.h @@ -92,6 +92,18 @@ private: QString value; // Value to be set }; +class EditDiveSiteLocation : public Base { +public: + EditDiveSiteLocation(dive_site *ds, const QString &location); +private: + bool workToBeDone() override; + void undo() override; + void redo() override; + + dive_site *ds; + location_t value; // Value to be set +}; + } // namespace Command #endif // COMMAND_DIVESITE_H diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index 19d2befd2..17e9d488e 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -142,6 +142,15 @@ void LocationInformationWidget::diveSiteChanged(struct dive_site *ds, int field) case LocationInformationModel::TAXONOMY: ui.diveSiteCountry->setText(taxonomy_get_country(&diveSite->taxonomy)); return; + case LocationInformationModel::LOCATION: + filter_model.setCoordinates(diveSite->location); + if (has_location(&diveSite->location)) { + enableLocationButtons(true); + ui.diveSiteCoordinates->setText(printGPSCoords(&diveSite->location)); + } else { + enableLocationButtons(false); + ui.diveSiteCoordinates->clear(); + } default: return; } @@ -181,14 +190,6 @@ bool parseGpsText(const QString &text, location_t &location) void LocationInformationWidget::acceptChanges() { - if (!diveSite) { - qWarning() << "did not have valid dive site in LocationInformationWidget"; - return; - } - - if (!ui.diveSiteCoordinates->text().isEmpty()) - parseGpsText(ui.diveSiteCoordinates->text(), diveSite->location); - mark_divelist_changed(true); resetState(); } @@ -250,22 +251,11 @@ void LocationInformationWidget::enableEdition() ui.diveSiteMessage->setText(tr("You are editing a dive site")); } -void LocationInformationWidget::on_diveSiteCoordinates_textChanged(const QString &text) +void LocationInformationWidget::on_diveSiteCoordinates_editingFinished() { if (!diveSite) return; - location_t location; - bool ok_old = has_location(&diveSite->location); - bool ok = parseGpsText(text, location); - if (ok != ok_old || !same_location(&location, &diveSite->location)) { - if (ok) { - markChangedWidget(ui.diveSiteCoordinates); - enableLocationButtons(true); - filter_model.setCoordinates(location); - } else { - enableLocationButtons(false); - } - } + Command::editDiveSiteLocation(diveSite, ui.diveSiteCoordinates->text()); } void LocationInformationWidget::on_diveSiteCountry_editingFinished() diff --git a/desktop-widgets/locationinformation.h b/desktop-widgets/locationinformation.h index 04be64654..dc77da9e6 100644 --- a/desktop-widgets/locationinformation.h +++ b/desktop-widgets/locationinformation.h @@ -30,7 +30,7 @@ public slots: void resetState(); void resetPallete(); void on_diveSiteCountry_editingFinished(); - void on_diveSiteCoordinates_textChanged(const QString& text); + void on_diveSiteCoordinates_editingFinished(); void on_diveSiteDescription_editingFinished(); void on_diveSiteName_editingFinished(); void on_diveSiteNotes_editingFinished(); diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp index 815627e2b..b8d7aa1c5 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.cpp +++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp @@ -17,7 +17,7 @@ TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent) ui.diveSites->view()->setSortingEnabled(true); // Show only the first few columns - for (int i = LocationInformationModel::COORDS; i < LocationInformationModel::COLUMNS; ++i) + for (int i = LocationInformationModel::LOCATION; i < LocationInformationModel::COLUMNS; ++i) ui.diveSites->view()->setColumnHidden(i, true); connect(ui.diveSites, &TableView::addButtonClicked, this, &TabDiveSite::add); diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp index 89f6dd764..c18f85932 100644 --- a/qt-models/divelocationmodel.cpp +++ b/qt-models/divelocationmodel.cpp @@ -92,7 +92,7 @@ QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, i case DIVESITE: return QVariant::fromValue((dive_site *)ds); // Not nice: casting away const case NAME: return ds->name; case NUM_DIVES: return ds->dives.nr; - case COORDS: return "TODO"; + case LOCATION: return "TODO"; case DESCRIPTION: return ds->description; case NOTES: return ds->name; case TAXONOMY: return "TODO"; diff --git a/qt-models/divelocationmodel.h b/qt-models/divelocationmodel.h index 881c376c8..60c5c993a 100644 --- a/qt-models/divelocationmodel.h +++ b/qt-models/divelocationmodel.h @@ -17,7 +17,7 @@ class LocationInformationModel : public QAbstractTableModel { public: // Common columns, roles and accessor function for all dive-site models. // Thus, different views can connect to different models. - enum Columns { REMOVE, NAME, DESCRIPTION, NUM_DIVES, COORDS, NOTES, DIVESITE, TAXONOMY, COLUMNS}; + enum Columns { REMOVE, NAME, DESCRIPTION, NUM_DIVES, LOCATION, NOTES, DIVESITE, TAXONOMY, COLUMNS}; enum Roles { DIVESITE_ROLE = Qt::UserRole + 1 }; static QVariant getDiveSiteData(const struct dive_site *ds, int column, int role);