From ca6aa3813956b5e8be68b86ed36a5786b3ee746f Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 10 May 2019 20:51:25 +0200 Subject: 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 --- desktop-widgets/mainwindow.cpp | 103 ++++++++++++++++------------------------- desktop-widgets/mainwindow.h | 36 +++++++------- 2 files changed, 57 insertions(+), 82 deletions(-) (limited to 'desktop-widgets') 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 enabled = std::make_pair("enabled", QVariant(true)); - std::pair 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() diff --git a/desktop-widgets/mainwindow.h b/desktop-widgets/mainwindow.h index a929cf764..67a253eca 100644 --- a/desktop-widgets/mainwindow.h +++ b/desktop-widgets/mainwindow.h @@ -77,7 +77,6 @@ public: void printPlan(); void checkSurvey(); void setApplicationState(ApplicationState state); - void setStateProperties(ApplicationState state, const PropertyList& tl, const PropertyList& tr, const PropertyList& bl,const PropertyList& br); bool inPlanner(); NotificationWidget *getNotificationWidget(); void enableDisableCloudActions(); @@ -213,7 +212,6 @@ private: void toggleCollapsible(bool toggle); void showFilterIfEnabled(); void updateLastUsedDir(const QString &s); - void registerApplicationState(ApplicationState state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight); void enterState(CurrentState); bool filesAsArguments; UpdateManager *updateManager; @@ -231,27 +229,27 @@ private: QStringList recentFiles; QAction *actionsRecent[NUM_RECENT_FILES]; - struct WidgetForQuadrant { - WidgetForQuadrant(QWidget *tl = 0, QWidget *tr = 0, QWidget *bl = 0, QWidget *br = 0) : - topLeft(tl), topRight(tr), bottomLeft(bl), bottomRight(br) {} - QWidget *topLeft; - QWidget *topRight; - QWidget *bottomLeft; - QWidget *bottomRight; + enum { + FLAG_NONE = 0, + FLAG_DISABLED = 1 + }; + + struct Quadrant { + QWidget *widget; + int flags; }; - struct PropertiesForQuadrant { - PropertiesForQuadrant(){} - PropertiesForQuadrant(const PropertyList& tl, const PropertyList& tr,const PropertyList& bl,const PropertyList& br) : - topLeft(tl), topRight(tr), bottomLeft(bl), bottomRight(br) {} - PropertyList topLeft; - PropertyList topRight; - PropertyList bottomLeft; - PropertyList bottomRight; + struct Quadrants { + Quadrant topLeft; + Quadrant topRight; + Quadrant bottomLeft; + Quadrant bottomRight; }; - WidgetForQuadrant applicationState[(size_t)ApplicationState::Count]; - PropertiesForQuadrant stateProperties[(size_t)ApplicationState::Count]; + Quadrants applicationState[(size_t)ApplicationState::Count]; + static void setQuadrant(const Quadrant &, QStackedWidget *); + static void addWidgets(const Quadrant &, QStackedWidget *); + void registerApplicationState(ApplicationState state, Quadrants q); GpsLocation *locationProvider; QMenu *connections; -- cgit v1.2.3-70-g09d2