selection: remove current_dive and dc_number access from tabwidgets

An attempt at limitting accesses to the globals current_dive and
dc_number. These globals do not make sense on mobile.

The parent widget of the tab-widgets remembers the currently
displayer dive and dive computer and the individual widgets
access these values from there.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-09-17 16:21:17 +02:00 committed by bstoeger
parent 8cd191c271
commit 6f03fc9689
8 changed files with 125 additions and 96 deletions

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "TabDiveInformation.h"
#include "maintab.h"
#include "ui_TabDiveInformation.h"
#include "desktop-widgets/mainwindow.h" // TODO: Only used temporarilly for edit mode changes
#include "profile-widget/profilewidget2.h"
#include "../tagwidget.h"
#include "commands/command.h"
@ -108,32 +108,33 @@ void TabDiveInformation::updateWaterTypeWidget()
// Update fields that depend on the dive profile
void TabDiveInformation::updateProfile()
{
ui->maxcnsText->setText(QString("%L1\%").arg(current_dive->maxcns));
ui->otuText->setText(QString("%L1").arg(current_dive->otu));
ui->maximumDepthText->setText(get_depth_string(current_dive->maxdepth, true));
ui->averageDepthText->setText(get_depth_string(current_dive->meandepth, true));
dive *currentDive = parent.currentDive;
ui->maxcnsText->setText(QString("%L1\%").arg(currentDive->maxcns));
ui->otuText->setText(QString("%L1").arg(currentDive->otu));
ui->maximumDepthText->setText(get_depth_string(currentDive->maxdepth, true));
ui->averageDepthText->setText(get_depth_string(currentDive->meandepth, true));
volume_t *gases = get_gas_used(current_dive);
volume_t *gases = get_gas_used(currentDive);
QString volumes;
std::vector<int> mean(current_dive->cylinders.nr), duration(current_dive->cylinders.nr);
struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number);
if (currentdc && current_dive->cylinders.nr >= 0)
per_cylinder_mean_depth(current_dive, currentdc, mean.data(), duration.data());
std::vector<int> mean(currentDive->cylinders.nr), duration(currentDive->cylinders.nr);
struct divecomputer *currentdc = parent.getCurrentDC();
if (currentdc && currentDive->cylinders.nr >= 0)
per_cylinder_mean_depth(currentDive, currentdc, mean.data(), duration.data());
volume_t sac;
QString gaslist, SACs, separator;
for (int i = 0; i < current_dive->cylinders.nr; i++) {
if (!is_cylinder_used(current_dive, i))
for (int i = 0; i < currentDive->cylinders.nr; i++) {
if (!is_cylinder_used(currentDive, i))
continue;
gaslist.append(separator); volumes.append(separator); SACs.append(separator);
separator = "\n";
gaslist.append(gasname(get_cylinder(current_dive, i)->gasmix));
gaslist.append(gasname(get_cylinder(currentDive, i)->gasmix));
if (!gases[i].mliter)
continue;
volumes.append(get_volume_string(gases[i], true));
if (duration[i]) {
sac.mliter = lrint(gases[i].mliter / (depth_to_atm(mean[i], current_dive) * duration[i] / 60));
sac.mliter = lrint(gases[i].mliter / (depth_to_atm(mean[i], currentDive) * duration[i] / 60));
SACs.append(get_volume_string(sac, true).append(tr("/min")));
}
}
@ -141,23 +142,23 @@ void TabDiveInformation::updateProfile()
ui->gasUsedText->setText(volumes);
ui->oxygenHeliumText->setText(gaslist);
ui->diveTimeText->setText(get_dive_duration_string(current_dive->duration.seconds, tr("h"), tr("min"), tr("sec"),
" ", current_dive->dc.divemode == FREEDIVE));
ui->diveTimeText->setText(get_dive_duration_string(currentDive->duration.seconds, tr("h"), tr("min"), tr("sec"),
" ", currentDive->dc.divemode == FREEDIVE));
ui->sacText->setText(current_dive->cylinders.nr > 0 && mean[0] && current_dive->dc.divemode != CCR ? SACs : QString());
ui->sacText->setText(currentDive->cylinders.nr > 0 && mean[0] && currentDive->dc.divemode != CCR ? SACs : QString());
if (current_dive->surface_pressure.mbar == 0) {
if (currentDive->surface_pressure.mbar == 0) {
ui->atmPressVal->clear(); // If no atm pressure for dive then clear text box
} else {
ui->atmPressVal->setEnabled(true);
ui->atmPressVal->setText(QString::number(current_dive->surface_pressure.mbar)); // else display atm pressure
ui->atmPressVal->setText(QString::number(currentDive->surface_pressure.mbar)); // else display atm pressure
}
}
// Update fields that depend on start of dive
void TabDiveInformation::updateWhen()
{
timestamp_t surface_interval = get_surface_interval(current_dive->when);
timestamp_t surface_interval = get_surface_interval(parent.currentDive->when);
if (surface_interval >= 0)
ui->surfaceIntervalText->setText(get_dive_surfint_string(surface_interval, tr("d"), tr("h"), tr("min")));
else
@ -182,11 +183,11 @@ int TabDiveInformation::updateSalinityComboIndex(int salinity)
// If dive->user_salinity != dive->salinity (i.e. dc value) then show the salinity-overwrite indicator
void TabDiveInformation::checkDcSalinityOverWritten()
{
const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number);
if (!current_dive || !currentdc)
const struct divecomputer *currentdc = parent.getCurrentDC();
if (!currentdc)
return;
int dc_value = currentdc->salinity;
int user_value = current_dive->user_salinity;
int user_value = parent.currentDive->user_salinity;
bool show_indicator = false;
if (!manualDive && user_value != 0 && user_value != dc_value)
show_indicator = true;
@ -229,7 +230,7 @@ void TabDiveInformation::updateData(const std::vector<dive *> &, dive *currentDi
}
checkDcSalinityOverWritten(); // If exclamation is needed (i.e. salinity overwrite by user), then show it
updateMode(currentDive);
updateMode();
ui->visibility->setCurrentStars(currentDive->visibility);
ui->wavesize->setCurrentStars(currentDive->wavesize);
ui->current->setCurrentStars(currentDive->current);
@ -259,7 +260,7 @@ void TabDiveInformation::on_waterTypeCombo_activated(int index)
{
Q_UNUSED(index)
int combobox_salinity = 0;
const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number);
const struct divecomputer *currentdc = parent.getCurrentDC();
if (!currentdc)
return;
int dc_salinity = currentdc->salinity;
@ -301,7 +302,7 @@ void TabDiveInformation::on_waterTypeCombo_activated(int index)
void TabDiveInformation::cylinderChanged(dive *d)
{
// If this isn't the current dive, do nothing
if (current_dive != d)
if (parent.currentDive != d)
return;
updateProfile();
}
@ -313,78 +314,81 @@ void TabDiveInformation::divesChanged(const QVector<dive *> &dives, DiveField fi
int salinity_value;
// If the current dive is not in list of changed dives, do nothing
if (!current_dive || !dives.contains(current_dive))
if (!parent.includesCurrentDive(dives))
return;
dive *currentDive = parent.currentDive;
if (field.visibility)
ui->visibility->setCurrentStars(current_dive->visibility);
ui->visibility->setCurrentStars(currentDive->visibility);
if (field.wavesize)
ui->wavesize->setCurrentStars(current_dive->wavesize);
ui->wavesize->setCurrentStars(currentDive->wavesize);
if (field.current)
ui->current->setCurrentStars(current_dive->current);
ui->current->setCurrentStars(currentDive->current);
if (field.surge)
ui->surge->setCurrentStars(current_dive->surge);
ui->surge->setCurrentStars(currentDive->surge);
if (field.chill)
ui->chill->setCurrentStars(current_dive->chill);
ui->chill->setCurrentStars(currentDive->chill);
if (field.mode)
updateMode(current_dive);
updateMode();
if (field.duration || field.depth || field.mode)
updateProfile();
if (field.air_temp)
ui->airtemp->setText(get_temperature_string(current_dive->airtemp, true));
ui->airtemp->setText(get_temperature_string(currentDive->airtemp, true));
if (field.water_temp)
ui->watertemp->setText(get_temperature_string(current_dive->watertemp, true));
ui->watertemp->setText(get_temperature_string(currentDive->watertemp, true));
if (field.atm_press)
ui->atmPressVal->setText(QString::number(current_dive->surface_pressure.mbar));
ui->atmPressVal->setText(QString::number(currentDive->surface_pressure.mbar));
if (field.salinity)
checkDcSalinityOverWritten();
if (current_dive->user_salinity)
salinity_value = current_dive->user_salinity;
if (currentDive->user_salinity)
salinity_value = currentDive->user_salinity;
else
salinity_value = current_dive->salinity;
salinity_value = currentDive->salinity;
ui->waterTypeCombo->setCurrentIndex(updateSalinityComboIndex(salinity_value));
ui->salinityText->setText(QString("%L1g/").arg(salinity_value / 10.0));
}
void TabDiveInformation::on_visibility_valueChanged(int value)
{
if (current_dive)
if (parent.currentDive)
divesEdited(Command::editVisibility(value, false));
}
void TabDiveInformation::on_wavesize_valueChanged(int value)
{
if (current_dive)
if (parent.currentDive)
divesEdited(Command::editWaveSize(value, false));
}
void TabDiveInformation::on_current_valueChanged(int value)
{
if (current_dive)
if (parent.currentDive)
divesEdited(Command::editCurrent(value, false));
}
void TabDiveInformation::on_surge_valueChanged(int value)
{
if (current_dive)
if (parent.currentDive)
divesEdited(Command::editSurge(value, false));
}
void TabDiveInformation::on_chill_valueChanged(int value)
{
if (current_dive)
if (parent.currentDive)
divesEdited(Command::editChill(value, false));
}
void TabDiveInformation::updateMode(struct dive *d)
void TabDiveInformation::updateMode()
{
ui->diveType->setCurrentIndex(get_dive_dc(d, dc_number)->divemode);
divecomputer *currentDC = parent.getCurrentDC();
if (currentDC)
ui->diveType->setCurrentIndex(currentDC->divemode);
}
void TabDiveInformation::diveModeChanged(int index)
{
if (current_dive)
divesEdited(Command::editMode(dc_number, (enum divemode_t)index, false));
if (parent.currentDive)
divesEdited(Command::editMode(parent.currentDC, (enum divemode_t)index, false));
}
void TabDiveInformation::on_airtemp_editingFinished()
@ -392,7 +396,7 @@ void TabDiveInformation::on_airtemp_editingFinished()
// If the field wasn't modified by the user, don't post a new undo command.
// Owing to rounding errors, this might lead to undo commands that have
// no user visible effects. These can be very confusing.
if (ui->airtemp->isModified() && current_dive)
if (ui->airtemp->isModified() && parent.currentDive)
divesEdited(Command::editAirTemp(parseTemperatureToMkelvin(ui->airtemp->text()), false));
}
@ -401,7 +405,7 @@ void TabDiveInformation::on_watertemp_editingFinished()
// If the field wasn't modified by the user, don't post a new undo command.
// Owing to rounding errors, this might lead to undo commands that have
// no user visible effects. These can be very confusing.
if (ui->watertemp->isModified() && current_dive)
if (ui->watertemp->isModified() && parent.currentDive)
divesEdited(Command::editWaterTemp(parseTemperatureToMkelvin(ui->watertemp->text()), false));
}
@ -420,13 +424,14 @@ void TabDiveInformation::updateTextBox(int event) // Either the text box has bee
{ // Either way this gets a numeric value and puts it on the text box atmPressVal,
pressure_t atmpress = { 0 }; // then stores it in dive->surface_pressure.The undo stack for the text box content is
double altitudeVal; // maintained even though two independent events trigger saving the text box contents.
if (current_dive) {
dive *currentDive = parent.currentDive;
if (currentDive) {
switch (ui->atmPressType->currentIndex()) {
case 0: // If atm pressure in mbar has been selected:
if (event == TEXT_EDITED) // this is only triggered by on_atmPressVal_editingFinished()
atmpress.mbar = ui->atmPressVal->text().toInt(); // use the specified mbar pressure
else // if no pressure has been typed, then show existing dive pressure
ui->atmPressVal->setText(QString::number(current_dive->surface_pressure.mbar));
ui->atmPressVal->setText(QString::number(currentDive->surface_pressure.mbar));
break;
case 1: // If an altitude has been specified:
if (event == TEXT_EDITED) { // this is only triggered by on_atmPressVal_editingFinished()
@ -451,7 +456,7 @@ void TabDiveInformation::updateTextBox(int event) // Either the text box has bee
}
break;
case 2: // i.e. event = COMBO_CHANGED, that is, the option "Use dc" was selected from combobox
atmpress = calculate_surface_pressure(current_dive); // re-calculate air pressure from dc data
atmpress = calculate_surface_pressure(currentDive); // re-calculate air pressure from dc data
ui->atmPressVal->setText(QString::number(atmpress.mbar)); // display it in text box
ui->atmPressType->setCurrentIndex(0); // reset combobox to mbar
break;