core: convert cylinder_t and cylinder_table to C++

This had to be done simultaneously, because the table macros
do not work properly with C++ objects.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-28 21:31:11 +02:00 committed by bstoeger
parent 284582d2e8
commit 28520da655
48 changed files with 593 additions and 710 deletions

View file

@ -602,7 +602,10 @@ void DiveGasPressureItem::replot(const dive *d, int fromIn, int toIn, bool in_pl
bool showDescriptions = false;
for (int cyl = 0; cyl < pInfo.nr_cylinders; cyl++) {
showDescriptions = showDescriptions || same_gasmix_cylinder(get_cylinder(d, cyl), cyl, d, true) != -1;
const cylinder_t *c = get_cylinder(d, cyl);
if (!c)
continue;
showDescriptions = showDescriptions || (c && same_gasmix_cylinder(*c, cyl, d, true) != -1);
if (act_segments[cyl].polygon.empty())
continue;
act_segments[cyl].cyl = cyl;
@ -655,7 +658,7 @@ void DiveGasPressureItem::plotGasValue(double mbar, double sec, const cylinder_t
QString gas = get_gas_string(cylinder->gasmix);
QString label;
if (showDescription)
label = QStringLiteral("(%1) %2").arg(cylinder->type.description, gas);
label = QStringLiteral("(%1) %2").arg(QString::fromStdString(cylinder->type.description), gas);
else
label = gas;
auto text = std::make_unique<DiveTextItem>(dpr, 1.0, align, this);

View file

@ -502,13 +502,11 @@ struct int ProfileWidget2::getEntryFromPos(QPointF pos)
#ifndef SUBSURFACE_MOBILE
/// Prints cylinder information for display.
/// eg : "Cyl 1 (AL80 EAN32)"
static QString printCylinderDescription(int i, const cylinder_t *cylinder)
static QString printCylinderDescription(int i, const cylinder_t &cylinder)
{
QString label = gettextFromC::tr("Cyl") + QString(" %1").arg(i+1);
if( cylinder != NULL ) {
QString mix = get_gas_string(cylinder->gasmix);
label += QString(" (%2 %3)").arg(cylinder->type.description).arg(mix);
}
QString mix = get_gas_string(cylinder.gasmix);
label += QString(" (%2 %3)").arg(QString::fromStdString(cylinder.type.description)).arg(mix);
return label;
}
@ -562,18 +560,16 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
if (d && item && item->ev.is_gaschange()) {
int eventTime = item->ev.time.seconds;
QMenu *gasChange = m.addMenu(tr("Edit Gas Change"));
for (int i = 0; i < d->cylinders.nr; i++) {
const cylinder_t *cylinder = get_cylinder(d, i);
QString label = printCylinderDescription(i, cylinder);
gasChange->addAction(label, [this, i, eventTime] { addGasSwitch(i, eventTime); });
for (auto [i, cyl]: enumerated_range(d->cylinders)) {
QString label = printCylinderDescription(i, cyl);
gasChange->addAction(label, [this, idx = i, eventTime] { addGasSwitch(idx, eventTime); });
}
} else if (d && d->cylinders.nr > 1) {
} else if (d && d->cylinders.size() > 1) {
// if we have more than one gas, offer to switch to another one
QMenu *gasChange = m.addMenu(tr("Add gas change"));
for (int i = 0; i < d->cylinders.nr; i++) {
const cylinder_t *cylinder = get_cylinder(d, i);
QString label = printCylinderDescription(i, cylinder);
gasChange->addAction(label, [this, i, seconds] { addGasSwitch(i, seconds); });
for (auto [i, cyl]: enumerated_range(d->cylinders)) {
QString label = printCylinderDescription(i, cyl);
gasChange->addAction(label, [this, idx = i, seconds] { addGasSwitch(idx, seconds); });
}
}
m.addAction(tr("Add setpoint change"), [this, seconds]() { ProfileWidget2::addSetpointChange(seconds); });
@ -763,7 +759,7 @@ void ProfileWidget2::splitDive(int seconds)
void ProfileWidget2::addGasSwitch(int tank, int seconds)
{
if (!d || tank < 0 || tank >= d->cylinders.nr)
if (!d || tank < 0 || static_cast<size_t>(tank) >= d->cylinders.size())
return;
Command::addGasSwitch(mutable_dive(), dc, seconds, tank);
@ -923,7 +919,7 @@ void ProfileWidget2::repositionDiveHandlers()
QLineF line(p1, p2);
QPointF pos = line.pointAt(0.5);
gases[i]->setPos(pos);
if (datapoint.cylinderid >= 0 && datapoint.cylinderid < d->cylinders.nr)
if (datapoint.cylinderid >= 0 && datapoint.cylinderid < static_cast<int>(d->cylinders.size()))
gases[i]->setText(get_gas_string(get_cylinder(d, datapoint.cylinderid)->gasmix));
else
gases[i]->setText(QString());

View file

@ -76,7 +76,7 @@ void TankItem::setData(const struct dive *d, const struct divecomputer *dc, int
return;
// Bail if there are no cylinders
if (d->cylinders.nr <= 0)
if (d->cylinders.empty())
return;
// start with the first gasmix and at the start of the plotted range