selection: move current dc logic to profile widget

The current dc global makes no sense on mobile. Therefore,
move the logic of the currently displayed dive computer
to the profile widget and remove the dc_number global
variable.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-09-17 19:07:35 +02:00 committed by bstoeger
parent 19baae449d
commit cad80e5a53
10 changed files with 92 additions and 66 deletions

View file

@ -527,7 +527,7 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS
std::vector<dive *> selection = getDiveSelection();
updateSelection(selection, addToSelection, removeFromSelection);
dive *newCurrent = selection.empty() ? nullptr : selection.front();
setSelectionCore(selection, newCurrent, -1);
setSelectionCore(selection, newCurrent);
// Display the new, processed, selection
QTreeView::selectionChanged(selectionModel()->selection(), newDeselected);

View file

@ -548,7 +548,6 @@ PlannerWidgets::PlannerWidgets()
void PlannerWidgets::planDive(dive *currentDive)
{
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
dc_number = 0;
// create a simple starting dive, using the first gas from the just copied cylinders
DivePlannerPointsModel::instance()->createSimpleDive(&displayed_dive);

View file

@ -300,14 +300,6 @@ void MainWindow::enableDisableCloudActions()
ui.actionCloudstoragesave->setEnabled(prefs.cloud_verification_status == qPrefCloudStorage::CS_VERIFIED);
}
void MainWindow::enableDisableOtherDCsActions()
{
bool nr = number_of_computers(current_dive) > 1;
enableShortcuts();
ui.actionNextDC->setEnabled(nr);
ui.actionPreviousDC->setEnabled(nr);
}
void MainWindow::setDefaultState()
{
setApplicationState(ApplicationState::Default);
@ -333,10 +325,19 @@ void MainWindow::updateAutogroup()
void MainWindow::divesSelected(const std::vector<dive *> &selection, dive *currentDive, int currentDC)
{
mainTab->updateDiveInfo(selection, currentDive, currentDC);
if (currentDive)
enableDisableOtherDCsActions();
profile->plotCurrentDive();
// We call plotDive first, so that the profile can decide which
// dive computer to plot. The plotted dive computer is then
// used for displaying data in the tab-widgets.
profile->plotDive(currentDive, currentDC);
mainTab->updateDiveInfo(selection, profile->d, profile->dc);
// Activate cursor keys to switch through DCs if there are more than one DC.
if (currentDive) {
bool nr = number_of_computers(current_dive) > 1;
enableShortcuts();
ui.actionNextDC->setEnabled(nr);
ui.actionPreviousDC->setEnabled(nr);
}
}
void MainWindow::on_actionNew_triggered()
@ -665,8 +666,8 @@ void MainWindow::on_actionReplanDive_triggered()
disableShortcuts(true);
copy_dive(current_dive, &displayed_dive); // Planning works on a copy of the dive (for now).
profile->setPlanState(&displayed_dive, dc_number);
plannerWidgets->replanDive(dc_number);
profile->setPlanState(&displayed_dive, profile->dc);
plannerWidgets->replanDive(profile->dc);
}
void MainWindow::on_actionDivePlanner_triggered()
@ -825,20 +826,16 @@ void MainWindow::restoreSplitterSizes()
void MainWindow::on_actionPreviousDC_triggered()
{
unsigned nrdc = number_of_computers(current_dive);
dc_number = (dc_number + nrdc - 1) % nrdc;
profile->plotCurrentDive();
profile->prevDC();
// TODO: remove
mainTab->updateDiveInfo(getDiveSelection(), current_dive, dc_number);
mainTab->updateDiveInfo(getDiveSelection(), profile->d, profile->dc);
}
void MainWindow::on_actionNextDC_triggered()
{
unsigned nrdc = number_of_computers(current_dive);
dc_number = (dc_number + 1) % nrdc;
profile->plotCurrentDive();
profile->nextDC();
// TODO: remove
mainTab->updateDiveInfo(getDiveSelection(), current_dive, dc_number);
mainTab->updateDiveInfo(getDiveSelection(), profile->d, profile->dc);
}
void MainWindow::on_actionFullScreen_triggered(bool checked)

View file

@ -75,7 +75,6 @@ public:
void enterPreviousState();
NotificationWidget *getNotificationWidget();
void enableDisableCloudActions();
void enableDisableOtherDCsActions();
void editDiveSite(dive_site *ds);
void setEnabledToolbar(bool arg1);

View file

@ -52,7 +52,7 @@ void EmptyView::resizeEvent(QResizeEvent *)
update();
}
ProfileWidget::ProfileWidget() : originalDive(nullptr), placingCommand(false)
ProfileWidget::ProfileWidget() : d(nullptr), dc(0), originalDive(nullptr), placingCommand(false)
{
ui.setupUi(this);
@ -183,34 +183,72 @@ void ProfileWidget::setDive(const struct dive *d)
void ProfileWidget::plotCurrentDive()
{
plotDive(d, dc);
}
void ProfileWidget::plotDive(dive *dIn, int dcIn)
{
d = dIn;
if (dcIn >= 0)
dc = 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);
// Exit edit mode if the dive changed
if (editedDive && (originalDive != current_dive || editedDc != dc_number))
if (editedDive && (originalDive != d || editedDc != dc))
exitEditMode();
// If this is a manually added dive and we are not in the planner
// or already editing the dive, switch to edit mode.
if (current_dive && !editedDive &&
if (d && !editedDive &&
DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING) {
struct divecomputer *dc = get_dive_dc(current_dive, dc_number);
if (dc && is_manually_added_dc(dc) && dc->samples)
struct divecomputer *comp = get_dive_dc(d, dc);
if (comp && is_manually_added_dc(comp) && comp->samples)
editDive();
}
setEnabledToolbar(current_dive != nullptr);
setEnabledToolbar(d != nullptr);
if (editedDive) {
view->plotDive(editedDive.get(), editedDc);
setDive(editedDive.get());
} else if (current_dive) {
view->setProfileState(current_dive, dc_number);
} else if (d) {
view->setProfileState(d, dc);
view->resetZoom(); // when switching dive, reset the zoomLevel
view->plotDive(current_dive, dc_number);
setDive(current_dive);
view->plotDive(d, dc);
setDive(d);
} else {
view->clear();
stack->setCurrentIndex(0);
}
}
void ProfileWidget::nextDC()
{
rotateDC(1);
}
void ProfileWidget::prevDC()
{
rotateDC(-1);
}
void ProfileWidget::rotateDC(int dir)
{
if (!d)
return;
int numDC = number_of_computers(d);
int newDC = (dc + dir) % numDC;
if (newDC < 0)
newDC += numDC;
if (newDC == dc)
return;
plotDive(d, newDC);
}
void ProfileWidget::divesChanged(const QVector<dive *> &dives, DiveField field)
{
// If the current dive is not in list of changed dives, do nothing.
@ -218,13 +256,13 @@ void ProfileWidget::divesChanged(const QVector<dive *> &dives, DiveField field)
// Also, if we are currently placing a command, don't do anything.
// Note that we cannot use Command::placingCommand(), because placing
// a depth or time change on the maintab requires an update.
if (!current_dive || !dives.contains(current_dive) || !(field.duration || field.depth) || placingCommand)
if (!d || !dives.contains(d) || !(field.duration || field.depth) || placingCommand)
return;
// If were editing the current dive and not currently
// placing command, we have to update the edited dive.
if (editedDive) {
copy_dive(current_dive, editedDive.get());
copy_dive(d, editedDive.get());
// TODO: Holy moly that function sends too many signals. Fix it!
DivePlannerPointsModel::instance()->loadFromDive(editedDive.get(), editedDc);
}
@ -254,9 +292,9 @@ void ProfileWidget::unsetProfTissues()
void ProfileWidget::editDive()
{
editedDive.reset(alloc_dive());
editedDc = dc_number;
copy_dive(current_dive, editedDive.get()); // Work on a copy of the dive
originalDive = current_dive;
editedDc = dc;
copy_dive(d, editedDive.get()); // Work on a copy of the dive
originalDive = d;
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD);
DivePlannerPointsModel::instance()->loadFromDive(editedDive.get(), editedDc);
view->setEditState(editedDive.get(), editedDc);
@ -267,7 +305,7 @@ void ProfileWidget::exitEditMode()
if (!editedDive)
return;
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING);
view->setProfileState(current_dive, dc_number); // switch back to original dive before erasing the copy.
view->setProfileState(d, dc); // switch back to original dive before erasing the copy.
editedDive.reset();
originalDive = nullptr;
}

View file

@ -23,9 +23,14 @@ public:
ProfileWidget();
~ProfileWidget();
std::unique_ptr<ProfileWidget2> view;
void plotDive(struct dive *d, int dc); // Attempt to keep DC number id dc < 0
void plotCurrentDive();
void setPlanState(const struct dive *d, int dc);
void setEnabledToolbar(bool enabled);
void nextDC();
void prevDC();
dive *d;
int dc;
private
slots:
void divesChanged(const QVector<dive *> &dives, DiveField field);
@ -47,8 +52,9 @@ private:
void setDive(const struct dive *d);
void editDive();
void exitEditMode();
void rotateDC(int dir);
std::unique_ptr<dive, DiveDeleter> editedDive;
unsigned int editedDc;
int editedDc;
dive *originalDive;
bool placingCommand;
};