From b5daa8a3d8ee02d966bf5e5bc85d700722cabb57 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 13 Mar 2019 21:56:41 +0100 Subject: Dive site: scroll to changed dive site If the name of a dive site is edited, it might wander somewhere else in the table and thus out of view. Hook into the "dive site changed" signal and scroll there. The code is rather subtle as it depends on signals being called in a certain order: First the item is moved in the model, only then can we scroll to the correct place. Signed-off-by: Berthold Stoeger --- desktop-widgets/tab-widgets/TabDiveSite.cpp | 14 ++++++++++++++ desktop-widgets/tab-widgets/TabDiveSite.h | 1 + 2 files changed, 15 insertions(+) diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp index 5b65015da..815627e2b 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.cpp +++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp @@ -21,6 +21,10 @@ TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent) ui.diveSites->view()->setColumnHidden(i, true); connect(ui.diveSites, &TableView::addButtonClicked, this, &TabDiveSite::add); + + // Subtle: We depend on this slot being executed after the slot in the model. + // This is realized because the model was constructed as a member object and connects in the constructor. + connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &TabDiveSite::diveSiteChanged); } void TabDiveSite::updateData() @@ -53,3 +57,13 @@ void TabDiveSite::diveSiteAdded(struct dive_site *, int idx) ui.diveSites->view()->setCurrentIndex(localIdx); ui.diveSites->view()->edit(localIdx); } + +void TabDiveSite::diveSiteChanged(struct dive_site *ds, int field) +{ + int idx = get_divesite_idx(ds, &dive_site_table); + if (idx < 0) + return; + QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, field); + QModelIndex localIdx = model.mapFromSource(globalIdx); + ui.diveSites->view()->scrollTo(localIdx); +} diff --git a/desktop-widgets/tab-widgets/TabDiveSite.h b/desktop-widgets/tab-widgets/TabDiveSite.h index 32badadc3..a31b1629d 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.h +++ b/desktop-widgets/tab-widgets/TabDiveSite.h @@ -15,6 +15,7 @@ public: private slots: void add(); void diveSiteAdded(struct dive_site *, int idx); + void diveSiteChanged(struct dive_site *ds, int field); private: Ui::TabDiveSite ui; DiveSiteSortedModel model; -- cgit v1.2.3-70-g09d2