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,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "TabDiveNotes.h"
#include "maintab.h"
#include "core/divesite.h"
#include "core/qthelper.h"
#include "core/selection.h"
@ -97,35 +98,36 @@ void TabDiveNotes::closeWarning()
void TabDiveNotes::divesChanged(const QVector<dive *> &dives, DiveField field)
{
// 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.duration)
ui.duration->setText(render_seconds_to_string(current_dive->duration.seconds));
ui.duration->setText(render_seconds_to_string(currentDive->duration.seconds));
if (field.depth)
ui.depth->setText(get_depth_string(current_dive->maxdepth, true));
ui.depth->setText(get_depth_string(currentDive->maxdepth, true));
if (field.rating)
ui.rating->setCurrentStars(current_dive->rating);
ui.rating->setCurrentStars(currentDive->rating);
if (field.notes)
updateNotes(current_dive);
updateNotes(currentDive);
if (field.datetime) {
updateDateTime(current_dive);
DivePlannerPointsModel::instance()->getDiveplan().when = current_dive->when;
updateDateTime(currentDive);
DivePlannerPointsModel::instance()->getDiveplan().when = currentDive->when;
}
if (field.divesite)
updateDiveSite(current_dive);
updateDiveSite(currentDive);
if (field.tags)
ui.tagWidget->setText(get_taglist_string(current_dive->tag_list));
ui.tagWidget->setText(get_taglist_string(currentDive->tag_list));
if (field.buddy)
ui.buddy->setText(current_dive->buddy);
ui.buddy->setText(currentDive->buddy);
if (field.diveguide)
ui.diveguide->setText(current_dive->diveguide);
ui.diveguide->setText(currentDive->diveguide);
}
void TabDiveNotes::diveSiteEdited(dive_site *ds, int)
{
if (current_dive && current_dive->dive_site == ds)
updateDiveSite(current_dive);
if (parent.currentDive && parent.currentDive->dive_site == ds)
updateDiveSite(parent.currentDive);
}
// This function gets called if a trip-field gets updated by an undo command.
@ -303,7 +305,7 @@ void TabDiveNotes::divesEdited(int i)
void TabDiveNotes::on_buddy_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
divesEdited(Command::editBuddies(stringToList(ui.buddy->toPlainText()), false));
@ -311,7 +313,7 @@ void TabDiveNotes::on_buddy_editingFinished()
void TabDiveNotes::on_diveguide_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
divesEdited(Command::editDiveGuide(stringToList(ui.diveguide->toPlainText()), false));
@ -319,7 +321,7 @@ void TabDiveNotes::on_diveguide_editingFinished()
void TabDiveNotes::on_duration_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
// Duration editing is special: we only edit the current dive.
@ -328,7 +330,7 @@ void TabDiveNotes::on_duration_editingFinished()
void TabDiveNotes::on_depth_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
// Depth editing is special: we only edit the current dive.
@ -337,36 +339,36 @@ void TabDiveNotes::on_depth_editingFinished()
// Editing of the dive time is different. If multiple dives are edited,
// all dives are shifted by an offset.
static void shiftTime(QDateTime &dateTime)
static void shiftTime(QDateTime &dateTime, dive *currentDive)
{
timestamp_t when = dateTimeToTimestamp(dateTime);
if (current_dive && current_dive->when != when) {
timestamp_t offset = when - current_dive->when;
if (currentDive->when != when) {
timestamp_t offset = when - currentDive->when;
Command::shiftTime(getDiveSelection(), (int)offset);
}
}
void TabDiveNotes::on_dateEdit_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
QDateTime dateTime = timestampToDateTime(current_dive->when);
QDateTime dateTime = timestampToDateTime(parent.currentDive->when);
dateTime.setDate(ui.dateEdit->date());
shiftTime(dateTime);
shiftTime(dateTime, parent.currentDive);
}
void TabDiveNotes::on_timeEdit_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
QDateTime dateTime = timestampToDateTime(current_dive->when);
QDateTime dateTime = timestampToDateTime(parent.currentDive->when);
dateTime.setTime(ui.timeEdit->time());
shiftTime(dateTime);
shiftTime(dateTime, parent.currentDive);
}
void TabDiveNotes::on_tagWidget_editingFinished()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
divesEdited(Command::editTags(ui.tagWidget->getBlockStringList(), false));
@ -374,7 +376,7 @@ void TabDiveNotes::on_tagWidget_editingFinished()
void TabDiveNotes::on_location_diveSiteSelected()
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
struct dive_site *newDs = ui.location->currDiveSite();
@ -398,7 +400,7 @@ void TabDiveNotes::on_diveTripLocation_editingFinished()
void TabDiveNotes::on_notes_editingFinished()
{
if (!currentTrip && !current_dive)
if (!currentTrip && !parent.currentDive)
return;
QString html = ui.notes->toHtml();
@ -412,7 +414,7 @@ void TabDiveNotes::on_notes_editingFinished()
void TabDiveNotes::on_rating_valueChanged(int value)
{
if (ignoreInput || !current_dive)
if (ignoreInput || !parent.currentDive)
return;
divesEdited(Command::editRating(value, false));