selection: move current dive and divecomputer to selection.cpp

This tries to encapsulate the management of the current dive and
divecomputer in the selection code. The current dive is alreay
set by setSelection(). Add a new parameter to also set the
current divecomputer. If -1 is passed, then the current
computer number is remained. This will allow us to audit the code.
Because for now, the whole "current dive computer" thing seems
to be ill-defined.

This fixes a bug: the dive-computer number wasn't validated
when making a new dive the current dive. The new code has some
drawbacks though: when selecting a whole trip, the validation
will be called for all dives in the trip and thus the dive computer
number will depend on the dive with the lowest amount of dive
computers in the trip. This will need to be fixed.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-04-04 18:57:28 +02:00 committed by bstoeger
parent b56b7abcf5
commit 9f455b1457
23 changed files with 68 additions and 43 deletions

View file

@ -455,7 +455,7 @@ void AddDive::redoit()
sort_trip_table(divelog.trips); // Though unlikely, adding a dive may reorder trips
// Select the newly added dive
setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0]);
setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0], -1);
}
void AddDive::undoit()
@ -465,7 +465,7 @@ void AddDive::undoit()
sort_trip_table(divelog.trips); // Though unlikely, removing a dive may reorder trips
// ...and restore the selection
setSelection(selection, currentDive);
setSelection(selection, currentDive, -1);
}
ImportDives::ImportDives(struct divelog *log, int flags, const QString &source)
@ -540,7 +540,7 @@ void ImportDives::redoit()
divesToAdd = removeDives(divesAndSitesToRemove);
// Select the newly added dives
setSelection(divesAndSitesToRemoveNew.dives, divesAndSitesToRemoveNew.dives.back());
setSelection(divesAndSitesToRemoveNew.dives, divesAndSitesToRemoveNew.dives.back(), -1);
// Remember dives and sites to remove
divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew);
@ -571,7 +571,7 @@ void ImportDives::undoit()
divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew);
// ...and restore the selection
setSelection(selection, currentDive);
setSelection(selection, currentDive, -1);
// Remove devices
for (const device &dev: devicesToAddAndRemove.devices)
@ -608,7 +608,7 @@ void DeleteDive::undoit()
// Select all re-added dives and make the first one current
dive *currentDive = !divesToDelete.dives.empty() ? divesToDelete.dives[0] : nullptr;
setSelection(divesToDelete.dives, currentDive);
setSelection(divesToDelete.dives, currentDive, -1);
}
void DeleteDive::redoit()
@ -653,7 +653,7 @@ void ShiftTime::redoit()
emit diveListNotifier.divesChanged(dives, DiveField::DATETIME);
// Select the changed dives
setSelection(diveList, diveList[0]);
setSelection(diveList, diveList[0], -1);
// Negate the time-shift so that the next call does the reverse
timeChanged = -timeChanged;
@ -689,7 +689,7 @@ void RenumberDives::undoit()
dives.reserve(divesToRenumber.size());
for (const QPair<dive *, int> &item: divesToRenumber)
dives.push_back(item.first);
setSelection(dives, dives[0]);
setSelection(dives, dives[0], -1);
}
bool RenumberDives::workToBeDone()
@ -718,7 +718,7 @@ void TripBase::redoit()
dives.reserve(divesToMove.divesToMove.size());
for (const DiveToTrip &item: divesToMove.divesToMove)
dives.push_back(item.dive);
setSelection(dives, dives[0]);
setSelection(dives, dives[0], -1);
}
void TripBase::undoit()
@ -847,7 +847,7 @@ void SplitDivesBase::redoit()
unsplitDive = removeDives(diveToSplit);
// Select split dives and make first dive current
setSelection(divesToUnsplit.dives, divesToUnsplit.dives[0]);
setSelection(divesToUnsplit.dives, divesToUnsplit.dives[0], -1);
}
void SplitDivesBase::undoit()
@ -857,7 +857,7 @@ void SplitDivesBase::undoit()
splitDives = removeDives(divesToUnsplit);
// Select unsplit dive and make it current
setSelection(diveToSplit.dives, diveToSplit.dives[0] );
setSelection(diveToSplit.dives, diveToSplit.dives[0], -1);
}
static std::array<dive *, 2> doSplitDives(const dive *d, duration_t time)
@ -895,8 +895,9 @@ SplitDiveComputer::SplitDiveComputer(dive *d, int dc_num) : SplitDivesBase(d, sp
setText(Command::Base::tr("split dive computer"));
}
DiveComputerBase::DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_after_in) : dc_nr_before(dc_number),
dc_nr_after(dc_nr_after_in)
DiveComputerBase::DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_before, int dc_nr_after) :
dc_nr_before(dc_nr_before),
dc_nr_after(dc_nr_after)
{
if (!new_dive)
return;
@ -930,11 +931,9 @@ void DiveComputerBase::redoit()
diveToAdd = removeDives(diveToRemove);
diveToRemove = std::move(addedDive);
dc_number = dc_nr_after;
// Select added dive and make it current.
// This automatically replots the profile.
setSelection(diveToRemove.dives, diveToRemove.dives[0]);
setSelection(diveToRemove.dives, diveToRemove.dives[0], dc_nr_after);
std::swap(dc_nr_before, dc_nr_after);
}
@ -946,13 +945,13 @@ void DiveComputerBase::undoit()
}
MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
: DiveComputerBase(d, make_first_dc(d, dc_num), 0)
: DiveComputerBase(d, make_first_dc(d, dc_num), dc_num, 0)
{
setText(Command::Base::tr("move dive computer to front"));
}
DeleteDiveComputer::DeleteDiveComputer(dive *d, int dc_num)
: DiveComputerBase(d, clone_delete_divecomputer(d, dc_num), std::min((int)number_of_computers(d) - 1, dc_num))
: DiveComputerBase(d, clone_delete_divecomputer(d, dc_num), dc_num, std::min((int)number_of_computers(d) - 1, dc_num))
{
setText(Command::Base::tr("delete dive computer"));
}
@ -1059,7 +1058,7 @@ void MergeDives::redoit()
unmergedDives = removeDives(divesToMerge);
// Select merged dive and make it current
setSelection(diveToUnmerge.dives, diveToUnmerge.dives[0]);
setSelection(diveToUnmerge.dives, diveToUnmerge.dives[0], -1);
}
void MergeDives::undoit()
@ -1069,7 +1068,7 @@ void MergeDives::undoit()
renumberDives(divesToRenumber);
// Select unmerged dives and make first one current
setSelection(divesToMerge.dives, divesToMerge.dives[0]);
setSelection(divesToMerge.dives, divesToMerge.dives[0], -1);
}
} // namespace Command