cleanup: use lambdas to transport DiveEventItem to actions

The removeEvent(), hideEvents() and editName() actions need
the DiveEventItem they are applied to. This was transported
via QAction's user-data, which means casting to void and
back.

By using lambdas instead, this can be made perfectly type-safe:
First we are 100% sure that we have a DiveEventItem because
we check the result of a dynamic_cast<>. Then we can pass
it to the even using its proper type.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-03-02 22:23:40 +01:00
parent 83d10ce89a
commit 76a41f45c7
2 changed files with 9 additions and 18 deletions

View file

@ -1490,20 +1490,17 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
if (DiveEventItem *item = dynamic_cast<DiveEventItem *>(sceneItem)) {
QAction *action = new QAction(&m);
action->setText(tr("Remove event"));
action->setData(QVariant::fromValue<void *>(item)); // so we know what to remove.
connect(action, SIGNAL(triggered(bool)), this, SLOT(removeEvent()));
connect(action, &QAction::triggered, [this,item] { removeEvent(item); });
m.addAction(action);
action = new QAction(&m);
action->setText(tr("Hide similar events"));
action->setData(QVariant::fromValue<void *>(item));
connect(action, SIGNAL(triggered(bool)), this, SLOT(hideEvents()));
connect(action, &QAction::triggered, [this, item] { hideEvents(item); });
m.addAction(action);
struct event *dcEvent = item->getEvent();
if (dcEvent->type == SAMPLE_EVENT_BOOKMARK) {
action = new QAction(&m);
action->setText(tr("Edit name"));
action->setData(QVariant::fromValue<void *>(item));
connect(action, SIGNAL(triggered(bool)), this, SLOT(editName()));
connect(action, &QAction::triggered, [this, item] { editName(item); });
m.addAction(action);
}
#if 0 // TODO::: FINISH OR DISABLE
@ -1575,10 +1572,8 @@ void ProfileWidget2::makeFirstDC()
Command::moveDiveComputerToFront(current_dive, dc_number);
}
void ProfileWidget2::hideEvents()
void ProfileWidget2::hideEvents(DiveEventItem *item)
{
QAction *action = qobject_cast<QAction *>(sender());
DiveEventItem *item = static_cast<DiveEventItem *>(action->data().value<void *>());
struct event *event = item->getEvent();
if (QMessageBox::question(this,
@ -1610,10 +1605,8 @@ void ProfileWidget2::unhideEvents()
item->show();
}
void ProfileWidget2::removeEvent()
void ProfileWidget2::removeEvent(DiveEventItem *item)
{
QAction *action = qobject_cast<QAction *>(sender());
DiveEventItem *item = static_cast<DiveEventItem *>(action->data().value<void *>());
struct event *event = item->getEvent();
if (QMessageBox::question(this, TITLE_OR_TEXT(
@ -1730,10 +1723,8 @@ double ProfileWidget2::getFontPrintScale()
}
#ifndef SUBSURFACE_MOBILE
void ProfileWidget2::editName()
void ProfileWidget2::editName(DiveEventItem *item)
{
QAction *action = qobject_cast<QAction *>(sender());
DiveEventItem *item = static_cast<DiveEventItem *>(action->data().value<void *>());
struct event *event = item->getEvent();
bool ok;
QString newName = QInputDialog::getText(this, tr("Edit name of bookmark"),

View file

@ -115,10 +115,7 @@ slots: // Necessary to call from QAction's signals.
void removePictures(const QVector<QString> &fileUrls);
void setPlanState();
void setAddState();
void hideEvents();
void unhideEvents();
void removeEvent();
void editName();
void makeFirstDC();
void deleteCurrentDC();
void splitCurrentDC();
@ -175,6 +172,9 @@ private:
void addBookmark(int seconds);
void splitDive(int seconds);
void addSetpointChange(int seconds);
void removeEvent(DiveEventItem *item);
void hideEvents(DiveEventItem *item);
void editName(DiveEventItem *item);
private:
DivePlotDataModel *dataModel;
int zoomLevel;