From 76a41f45c7e69375f13b3942523e0f8753b1f9b7 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 2 Mar 2020 22:23:40 +0100 Subject: 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 --- profile-widget/profilewidget2.cpp | 21 ++++++--------------- profile-widget/profilewidget2.h | 6 +++--- 2 files changed, 9 insertions(+), 18 deletions(-) (limited to 'profile-widget') 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(sceneItem)) { QAction *action = new QAction(&m); action->setText(tr("Remove event")); - action->setData(QVariant::fromValue(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(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(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(sender()); - DiveEventItem *item = static_cast(action->data().value()); 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(sender()); - DiveEventItem *item = static_cast(action->data().value()); 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(sender()); - DiveEventItem *item = static_cast(action->data().value()); 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 &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; -- cgit v1.2.3-70-g09d2