diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-05-10 20:51:25 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-05-12 12:33:55 -0700 |
commit | ca6aa3813956b5e8be68b86ed36a5786b3ee746f (patch) | |
tree | 3c6760a3deebcc1dae8be6c1141fd9ed300c992f /desktop-widgets/mainwindow.cpp | |
parent | 75767c456a2889d213621866a9f7fbb108f3366d (diff) | |
download | subsurface-ca6aa3813956b5e8be68b86ed36a5786b3ee746f.tar.gz |
Mainwindow: simplify application-state code
The way the application state would enable/disable widgets was very
"dynamic". A property-list would be generated and put in a set
of arrays. Very hard to figure out what is going on.
Replace these property-list by flags and explicit old-fashioned boolean
expressions.
Join the two arrays (widget- and property-lists) into an array of
a unified data structure.
Replace the macro that sets the widgets by a simple static function.
Factor out the four loops that added widgets to the quadrants into
a simple static function.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/mainwindow.cpp')
-rw-r--r-- | desktop-widgets/mainwindow.cpp | 103 |
1 files changed, 40 insertions, 63 deletions
diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index c47571031..38314f06c 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -171,26 +171,18 @@ MainWindow::MainWindow() : QMainWindow(), diveSiteEdit = new LocationInformationWidget(this); - std::pair<QByteArray, QVariant> enabled = std::make_pair("enabled", QVariant(true)); - std::pair<QByteArray, QVariant> disabled = std::make_pair("enabled", QVariant(false)); - PropertyList enabledList; - PropertyList disabledList; - enabledList.push_back(enabled); - disabledList.push_back(disabled); - - registerApplicationState(ApplicationState::Default, mainTab.get(), profileContainer, diveList, mapWidget ); - registerApplicationState(ApplicationState::EditDive, mainTab.get(), profileContainer, diveList, mapWidget ); - registerApplicationState(ApplicationState::PlanDive, divePlannerWidget, profileContainer, divePlannerSettingsWidget, plannerDetails ); - registerApplicationState(ApplicationState::EditPlannedDive, divePlannerWidget, profileContainer, diveList, mapWidget ); - registerApplicationState(ApplicationState::EditDiveSite, diveSiteEdit, profileContainer, diveList, mapWidget); - registerApplicationState(ApplicationState::FilterDive, mainTab.get(), profileContainer, diveList, &filterWidget2); - - setStateProperties(ApplicationState::Default, enabledList, enabledList, enabledList, enabledList); - setStateProperties(ApplicationState::EditDive, enabledList, enabledList, enabledList, enabledList); - setStateProperties(ApplicationState::PlanDive, enabledList, enabledList, enabledList, enabledList); - setStateProperties(ApplicationState::EditPlannedDive, enabledList, enabledList, enabledList, enabledList); - setStateProperties(ApplicationState::EditDiveSite, enabledList, disabledList, disabledList, enabledList); - setStateProperties(ApplicationState::FilterDive, enabledList, enabledList, enabledList, enabledList); + registerApplicationState(ApplicationState::Default, { { mainTab.get(), FLAG_NONE }, { profileContainer, FLAG_NONE }, + { diveList, FLAG_NONE }, { mapWidget, FLAG_NONE } }); + registerApplicationState(ApplicationState::EditDive, { { mainTab.get(), FLAG_NONE }, { profileContainer, FLAG_NONE }, + { diveList, FLAG_NONE }, { mapWidget, FLAG_NONE } }); + registerApplicationState(ApplicationState::PlanDive, { { divePlannerWidget, FLAG_NONE }, { profileContainer, FLAG_NONE }, + { divePlannerSettingsWidget, FLAG_NONE }, { plannerDetails, FLAG_NONE } }); + registerApplicationState(ApplicationState::EditPlannedDive, { { divePlannerWidget, FLAG_NONE }, { profileContainer, FLAG_NONE }, + { diveList, FLAG_NONE }, { mapWidget, FLAG_NONE } }); + registerApplicationState(ApplicationState::EditDiveSite, { { diveSiteEdit, FLAG_NONE }, { profileContainer, FLAG_DISABLED }, + { diveList, FLAG_DISABLED }, { mapWidget, FLAG_NONE } }); + registerApplicationState(ApplicationState::FilterDive, { { mainTab.get(), FLAG_NONE }, { profileContainer, FLAG_NONE }, + { diveList, FLAG_NONE }, { &filterWidget2, FLAG_NONE } }); setApplicationState(ApplicationState::Default); setWindowIcon(QIcon(":subsurface-icon")); @@ -369,11 +361,6 @@ void MainWindow::setupSocialNetworkMenu() { } -void MainWindow::setStateProperties(ApplicationState state, const PropertyList& tl, const PropertyList& tr, const PropertyList& bl, const PropertyList& br) -{ - stateProperties[(int)state] = PropertiesForQuadrant(tl, tr, bl, br); -} - void MainWindow::editDiveSite(dive_site *ds) { if (!ds) @@ -1878,20 +1865,30 @@ void MainWindow::showFilterIfEnabled() ui.bottomSplitter->setSizes({ EXPANDED, COLLAPSED }); } } -void MainWindow::registerApplicationState(ApplicationState state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight) + +void MainWindow::addWidgets(const Quadrant &q, QStackedWidget *stack) { - applicationState[(int)state] = WidgetForQuadrant(topLeft, topRight, bottomLeft, bottomRight); - if (ui.topLeft->indexOf(topLeft) == -1 && topLeft) { - ui.topLeft->addWidget(topLeft); - } - if (ui.topRight->indexOf(topRight) == -1 && topRight) { - ui.topRight->addWidget(topRight); - } - if (ui.bottomLeft->indexOf(bottomLeft) == -1 && bottomLeft) { - ui.bottomLeft->addWidget(bottomLeft); - } - if(ui.bottomRight->indexOf(bottomRight) == -1 && bottomRight) { - ui.bottomRight->addWidget(bottomRight); + if (q.widget && stack->indexOf(q.widget) == -1) + stack->addWidget(q.widget); +} + +void MainWindow::registerApplicationState(ApplicationState state, Quadrants q) +{ + applicationState[(int)state] = q; + addWidgets(q.topLeft, ui.topLeft); + addWidgets(q.topRight, ui.topRight); + addWidgets(q.bottomLeft, ui.bottomLeft); + addWidgets(q.bottomRight, ui.bottomRight); +} + +void MainWindow::setQuadrant(const Quadrant &q, QStackedWidget *stack) +{ + if (q.widget) { + stack->setCurrentWidget(q.widget); + stack->show(); + q.widget->setEnabled(!(q.flags & FLAG_DISABLED)); + } else { + stack->hide(); } } @@ -1902,31 +1899,11 @@ void MainWindow::setApplicationState(ApplicationState state) setAppState(state); -#define SET_CURRENT_INDEX( X ) \ - if (applicationState[(int)state].X) { \ - ui.X->setCurrentWidget( applicationState[(int)state].X); \ - ui.X->show(); \ - } else { \ - ui.X->hide(); \ - } - - SET_CURRENT_INDEX( topLeft ) - Q_FOREACH(const WidgetProperty& p, stateProperties[(int)state].topLeft) { - ui.topLeft->currentWidget()->setProperty( p.first.data(), p.second); - } - SET_CURRENT_INDEX( topRight ) - Q_FOREACH(const WidgetProperty& p, stateProperties[(int)state].topRight) { - ui.topRight->currentWidget()->setProperty( p.first.data(), p.second); - } - SET_CURRENT_INDEX( bottomLeft ) - Q_FOREACH(const WidgetProperty& p, stateProperties[(int)state].bottomLeft) { - ui.bottomLeft->currentWidget()->setProperty( p.first.data(), p.second); - } - SET_CURRENT_INDEX( bottomRight ) - Q_FOREACH(const WidgetProperty& p, stateProperties[(int)state].bottomRight) { - ui.bottomRight->currentWidget()->setProperty( p.first.data(), p.second); - } -#undef SET_CURRENT_INDEX + const Quadrants &quadrants = applicationState[(int)state]; + setQuadrant(quadrants.topLeft, ui.topLeft); + setQuadrant(quadrants.topRight, ui.topRight); + setQuadrant(quadrants.bottomLeft, ui.bottomLeft); + setQuadrant(quadrants.bottomRight, ui.bottomRight); } void MainWindow::showProgressBar() |