mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Profile: transport gas id and timestamp via lambda
When adding a gas change event via a context menu, the gas-id and timestamp were passed in two distinct ways. 1) The gas id was extracted from the text of the action. This meant doing rather complicated parsing. 2) The timestamp was passed via the "user data" of the action, which means transporting via "QVariant". There is a much simpler way to pass arbitrary data, that is strongly typed: lambdas. Instead of shoehorning the data onto the action in an archaic way, we can simply connect to a stateful lambda. That's what they're for after all. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
62b869b24a
commit
03f9e29146
2 changed files with 8 additions and 27 deletions
|
@ -1442,6 +1442,9 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// create the profile context menu
|
// create the profile context menu
|
||||||
|
QPointF scenePos = mapToScene(mapFromGlobal(event->globalPos()));
|
||||||
|
qreal sec_val = timeAxis->valueAt(scenePos);
|
||||||
|
int seconds = (sec_val < 0.0) ? 0 : (int)sec_val;
|
||||||
GasSelectionModel *model = GasSelectionModel::instance();
|
GasSelectionModel *model = GasSelectionModel::instance();
|
||||||
model->repopulate();
|
model->repopulate();
|
||||||
int rowCount = model->rowCount();
|
int rowCount = model->rowCount();
|
||||||
|
@ -1451,8 +1454,7 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
|
||||||
for (int i = 0; i < rowCount; i++) {
|
for (int i = 0; i < rowCount; i++) {
|
||||||
QAction *action = new QAction(&m);
|
QAction *action = new QAction(&m);
|
||||||
action->setText(model->data(model->index(i, 0), Qt::DisplayRole).toString() + QString(tr(" (cyl. %1)")).arg(i + 1));
|
action->setText(model->data(model->index(i, 0), Qt::DisplayRole).toString() + QString(tr(" (cyl. %1)")).arg(i + 1));
|
||||||
connect(action, SIGNAL(triggered(bool)), this, SLOT(changeGas()));
|
connect(action, &QAction::triggered, [this, i, seconds] { changeGas(i, seconds); } );
|
||||||
action->setData(event->globalPos());
|
|
||||||
gasChange->addAction(action);
|
gasChange->addAction(action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1464,10 +1466,7 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
|
||||||
splitAction->setData(event->globalPos());
|
splitAction->setData(event->globalPos());
|
||||||
const struct event *ev = NULL;
|
const struct event *ev = NULL;
|
||||||
enum divemode_t divemode = UNDEF_COMP_TYPE;
|
enum divemode_t divemode = UNDEF_COMP_TYPE;
|
||||||
QPointF scenePos = mapToScene(mapFromGlobal(event->globalPos()));
|
|
||||||
QString gas = action->text();
|
QString gas = action->text();
|
||||||
qreal sec_val = timeAxis->valueAt(scenePos);
|
|
||||||
int seconds = (sec_val < 0.0) ? 0 : (int)sec_val;
|
|
||||||
|
|
||||||
get_current_divemode(current_dc, seconds, &ev, &divemode);
|
get_current_divemode(current_dc, seconds, &ev, &divemode);
|
||||||
QMenu *changeMode = m.addMenu(tr("Change divemode"));
|
QMenu *changeMode = m.addMenu(tr("Change divemode"));
|
||||||
|
@ -1685,19 +1684,10 @@ void ProfileWidget2::splitDive()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProfileWidget2::changeGas()
|
void ProfileWidget2::changeGas(int tank, int seconds)
|
||||||
{
|
{
|
||||||
QAction *action = qobject_cast<QAction *>(sender());
|
if (!current_dive || tank < 0 || tank >= current_dive->cylinders.nr)
|
||||||
QPointF scenePos = mapToScene(mapFromGlobal(action->data().toPoint()));
|
return;
|
||||||
QString gas = action->text();
|
|
||||||
gas.remove(QRegExp(" \\(.*\\)"));
|
|
||||||
|
|
||||||
// backup the things on the dataModel, since we will clear that out.
|
|
||||||
struct gasmix gasmix;
|
|
||||||
qreal sec_val = timeAxis->valueAt(scenePos);
|
|
||||||
|
|
||||||
// no gas changes before the dive starts
|
|
||||||
int seconds = (sec_val < 0.0) ? 0 : (int)sec_val;
|
|
||||||
|
|
||||||
// if there is a gas change at this time stamp, remove it before adding the new one
|
// if there is a gas change at this time stamp, remove it before adding the new one
|
||||||
struct event *gasChangeEvent = current_dc->events;
|
struct event *gasChangeEvent = current_dc->events;
|
||||||
|
@ -1709,15 +1699,6 @@ void ProfileWidget2::changeGas()
|
||||||
gasChangeEvent = gasChangeEvent->next;
|
gasChangeEvent = gasChangeEvent->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
validate_gas(qPrintable(gas), &gasmix);
|
|
||||||
QRegExp rx("\\(\\D*(\\d+)");
|
|
||||||
int tank;
|
|
||||||
if (rx.indexIn(action->text()) > -1) {
|
|
||||||
tank = rx.cap(1).toInt() - 1; // we display the tank 1 based
|
|
||||||
} else {
|
|
||||||
qDebug() << "failed to parse tank number";
|
|
||||||
tank = get_gasidx(&displayed_dive, gasmix);
|
|
||||||
}
|
|
||||||
// add this both to the displayed dive and the current dive
|
// add this both to the displayed dive and the current dive
|
||||||
add_gas_switch_event(current_dive, current_dc, seconds, tank);
|
add_gas_switch_event(current_dive, current_dc, seconds, tank);
|
||||||
add_gas_switch_event(&displayed_dive, get_dive_dc(&displayed_dive, dc_number), seconds, tank);
|
add_gas_switch_event(&displayed_dive, get_dive_dc(&displayed_dive, dc_number), seconds, tank);
|
||||||
|
|
|
@ -115,7 +115,6 @@ slots: // Necessary to call from QAction's signals.
|
||||||
void removePictures(const QVector<QString> &fileUrls);
|
void removePictures(const QVector<QString> &fileUrls);
|
||||||
void setPlanState();
|
void setPlanState();
|
||||||
void setAddState();
|
void setAddState();
|
||||||
void changeGas();
|
|
||||||
void addSetpointChange();
|
void addSetpointChange();
|
||||||
void splitDive();
|
void splitDive();
|
||||||
void addBookmark();
|
void addBookmark();
|
||||||
|
@ -162,6 +161,7 @@ protected:
|
||||||
|
|
||||||
|
|
||||||
private: /*methods*/
|
private: /*methods*/
|
||||||
|
void changeGas(int tank, int seconds);
|
||||||
void fixBackgroundPos();
|
void fixBackgroundPos();
|
||||||
void scrollViewTo(const QPoint &pos);
|
void scrollViewTo(const QPoint &pos);
|
||||||
void setupSceneAndFlags();
|
void setupSceneAndFlags();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue