diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-03-02 22:23:40 +0100 |
---|---|---|
committer | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-04-07 00:13:35 +0200 |
commit | 76a41f45c7e69375f13b3942523e0f8753b1f9b7 (patch) | |
tree | 24c31356dc3782eec48e4eb9003ed045a8c006f1 | |
parent | 83d10ce89a1544044ea907e39f498183adb554f2 (diff) | |
download | subsurface-76a41f45c7e69375f13b3942523e0f8753b1f9b7.tar.gz |
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>
-rw-r--r-- | profile-widget/profilewidget2.cpp | 21 | ||||
-rw-r--r-- | profile-widget/profilewidget2.h | 6 |
2 files changed, 9 insertions, 18 deletions
diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index 8da3de72c..53a9e3e1f 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -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"), diff --git a/profile-widget/profilewidget2.h b/profile-widget/profilewidget2.h index 342032567..bc049781d 100644 --- a/profile-widget/profilewidget2.h +++ b/profile-widget/profilewidget2.h @@ -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; |