core: move number_of_divecomputers to struct dive

Feels natural in a C++ code base.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-30 17:16:14 +02:00 committed by bstoeger
parent e90251b0cf
commit 6e29c00f35
8 changed files with 12 additions and 12 deletions

View file

@ -906,7 +906,7 @@ MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
DeleteDiveComputer::DeleteDiveComputer(dive *d, int dc_num)
: DiveComputerBase(d, divelog.dives.clone_delete_divecomputer(*d, dc_num),
dc_num, std::min((int)number_of_computers(d) - 1, dc_num))
dc_num, std::min(d->number_of_computers() - 1, dc_num))
{
setText(Command::Base::tr("delete dive computer"));
}

View file

@ -2515,9 +2515,9 @@ std::string get_dive_location(const struct dive *dive)
return ds ? ds->name : std::string();
}
unsigned int number_of_computers(const struct dive *dive)
int dive::number_of_computers() const
{
return dive ? static_cast<int>(dive->dcs.size()) : 1;
return static_cast<int>(dcs.size());
}
struct divecomputer *get_dive_dc(struct dive *dive, int nr)

View file

@ -83,6 +83,7 @@ struct dive {
bool cache_is_valid() const;
void clear();
int number_of_computers() const;
void fixup_no_cylinder(); /* to fix cylinders, we need the divelist (to calculate cns) */
timestamp_t endtime() const; /* maximum over divecomputers (with samples) */
duration_t totaltime() const; /* maximum over divecomputers (with samples) */
@ -162,7 +163,6 @@ extern int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null);
extern struct dive_site *get_dive_site_for_dive(const struct dive *dive);
extern std::string get_dive_country(const struct dive *dive);
extern std::string get_dive_location(const struct dive *dive);
extern unsigned int number_of_computers(const struct dive *dive);
extern struct divecomputer *get_dive_dc(struct dive *dive, int nr);
extern const struct divecomputer *get_dive_dc(const struct dive *dive, int nr);

View file

@ -328,7 +328,7 @@ void MainWindow::divesSelected(const std::vector<dive *> &selection, dive *curre
// Activate cursor keys to switch through DCs if there are more than one DC.
if (currentDive) {
bool nr = number_of_computers(current_dive) > 1;
bool nr = currentDive->number_of_computers() > 1;
enableShortcuts();
ui.actionNextDC->setEnabled(nr);
ui.actionPreviousDC->setEnabled(nr);

View file

@ -209,7 +209,7 @@ void ProfileWidget::plotDive(dive *dIn, int dcIn)
// The following is valid because number_of_computers is always at least 1.
if (d)
dc = std::min(dc, (int)number_of_computers(current_dive) - 1);
dc = std::min(dc, d->number_of_computers() - 1);
// Exit edit mode if the dive changed
if (endEditMode)
@ -253,7 +253,7 @@ void ProfileWidget::rotateDC(int dir)
{
if (!d)
return;
int numDC = number_of_computers(d);
int numDC = d->number_of_computers();
int newDC = (dc + dir) % numDC;
if (newDC < 0)
newDC += numDC;

View file

@ -581,7 +581,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM
dcText = tr("Manually added dive");
else if (dcText.isEmpty())
dcText = tr("Unknown dive computer");
int nr = number_of_computers(d);
int nr = d->number_of_computers();
if (nr > 1)
dcText += tr(" (#%1 of %2)").arg(dc + 1).arg(nr);
diveComputerText->set(dcText, getColor(TIME_TEXT, isGrayscale));

View file

@ -533,13 +533,13 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
QGraphicsItem *sceneItem = itemAt(mapFromGlobal(event->globalPos()));
if (isDiveTextItem(sceneItem, profileScene->diveComputerText)) {
const struct divecomputer *currentdc = get_dive_dc(d, dc);
if (!currentdc->deviceid && dc == 0 && number_of_computers(d) == 1)
if (!currentdc->deviceid && dc == 0 && d->number_of_computers() == 1)
// nothing to do, can't rename, delete or reorder
return;
// create menu to show when right clicking on dive computer name
if (dc > 0)
m.addAction(tr("Make first dive computer"), this, &ProfileWidget2::makeFirstDC);
if (number_of_computers(d) > 1) {
if (d->number_of_computers() > 1) {
m.addAction(tr("Delete this dive computer"), this, &ProfileWidget2::deleteCurrentDC);
m.addAction(tr("Split this dive computer into own dive"), this, &ProfileWidget2::splitCurrentDC);
}

View file

@ -147,7 +147,7 @@ void QMLProfile::rotateDC(int dir)
struct dive *d = divelog.dives.get_by_uniq_id(m_diveId);
if (!d)
return;
int numDC = number_of_computers(d);
int numDC = d->number_of_computers();
if (numDC == 1)
return;
m_dc = (m_dc + dir) % numDC;
@ -159,5 +159,5 @@ void QMLProfile::rotateDC(int dir)
int QMLProfile::numDC() const
{
struct dive *d = divelog.dives.get_by_uniq_id(m_diveId);
return d ? number_of_computers(d) : 0;
return d ? d->number_of_computers() : 0;
}