Dive site: on first addition of dive site, edit name

When adding a dive site, enter the name field of the new dive site.
Thus, when adding a new dive site, the user can immediately edit the
name.

The code is rather subtle: It hooks into the dive site added signal
before executing the command and unhooks afterwards. This only works,
because signals are executed in order of connect - thus the model
adds the index first and only *then* is the field edited.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-03-13 21:36:31 +01:00 committed by Dirk Hohndel
parent 56dcbd9588
commit aac8eacfa2
2 changed files with 21 additions and 0 deletions

View file

@ -1,5 +1,7 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "TabDiveSite.h" #include "TabDiveSite.h"
#include "core/subsurface-qt/DiveListNotifier.h"
#include "core/divesite.h"
#include "qt-models/divelocationmodel.h" #include "qt-models/divelocationmodel.h"
#include "desktop-widgets/command.h" #include "desktop-widgets/command.h"
@ -31,5 +33,23 @@ void TabDiveSite::clear()
void TabDiveSite::add() void TabDiveSite::add()
{ {
// This is mighty dirty: We hook into the "dive site added" signal and
// select the name field of the added dive site when the command sends
// the signal. This works only because we know that the model added the
// connection first. Very subtle!
// After the command has finished, the signal is disconnected so that dive
// site names are not selected on regular redo / undo.
connect(&diveListNotifier, &DiveListNotifier::diveSiteAdded, this, &TabDiveSite::diveSiteAdded);
Command::addDiveSite(tr("New dive site")); Command::addDiveSite(tr("New dive site"));
disconnect(&diveListNotifier, &DiveListNotifier::diveSiteAdded, this, &TabDiveSite::diveSiteAdded);
}
void TabDiveSite::diveSiteAdded(struct dive_site *, int idx)
{
if (idx < 0)
return;
QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, LocationInformationModel::NAME);
QModelIndex localIdx = model.mapFromSource(globalIdx);
ui.diveSites->view()->setCurrentIndex(localIdx);
ui.diveSites->view()->edit(localIdx);
} }

View file

@ -14,6 +14,7 @@ public:
void clear() override; void clear() override;
private slots: private slots:
void add(); void add();
void diveSiteAdded(struct dive_site *, int idx);
private: private:
Ui::TabDiveSite ui; Ui::TabDiveSite ui;
DiveSiteSortedModel model; DiveSiteSortedModel model;