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>
This commit is contained in:
Berthold Stoeger 2019-05-10 20:51:25 +02:00 committed by Dirk Hohndel
parent 75767c456a
commit ca6aa38139
2 changed files with 57 additions and 82 deletions

View file

@ -171,26 +171,18 @@ MainWindow::MainWindow() : QMainWindow(),
diveSiteEdit = new LocationInformationWidget(this); diveSiteEdit = new LocationInformationWidget(this);
std::pair<QByteArray, QVariant> enabled = std::make_pair("enabled", QVariant(true)); registerApplicationState(ApplicationState::Default, { { mainTab.get(), FLAG_NONE }, { profileContainer, FLAG_NONE },
std::pair<QByteArray, QVariant> disabled = std::make_pair("enabled", QVariant(false)); { diveList, FLAG_NONE }, { mapWidget, FLAG_NONE } });
PropertyList enabledList; registerApplicationState(ApplicationState::EditDive, { { mainTab.get(), FLAG_NONE }, { profileContainer, FLAG_NONE },
PropertyList disabledList; { diveList, FLAG_NONE }, { mapWidget, FLAG_NONE } });
enabledList.push_back(enabled); registerApplicationState(ApplicationState::PlanDive, { { divePlannerWidget, FLAG_NONE }, { profileContainer, FLAG_NONE },
disabledList.push_back(disabled); { divePlannerSettingsWidget, FLAG_NONE }, { plannerDetails, FLAG_NONE } });
registerApplicationState(ApplicationState::EditPlannedDive, { { divePlannerWidget, FLAG_NONE }, { profileContainer, FLAG_NONE },
registerApplicationState(ApplicationState::Default, mainTab.get(), profileContainer, diveList, mapWidget ); { diveList, FLAG_NONE }, { mapWidget, FLAG_NONE } });
registerApplicationState(ApplicationState::EditDive, mainTab.get(), profileContainer, diveList, mapWidget ); registerApplicationState(ApplicationState::EditDiveSite, { { diveSiteEdit, FLAG_NONE }, { profileContainer, FLAG_DISABLED },
registerApplicationState(ApplicationState::PlanDive, divePlannerWidget, profileContainer, divePlannerSettingsWidget, plannerDetails ); { diveList, FLAG_DISABLED }, { mapWidget, FLAG_NONE } });
registerApplicationState(ApplicationState::EditPlannedDive, divePlannerWidget, profileContainer, diveList, mapWidget ); registerApplicationState(ApplicationState::FilterDive, { { mainTab.get(), FLAG_NONE }, { profileContainer, FLAG_NONE },
registerApplicationState(ApplicationState::EditDiveSite, diveSiteEdit, profileContainer, diveList, mapWidget); { diveList, FLAG_NONE }, { &filterWidget2, FLAG_NONE } });
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);
setApplicationState(ApplicationState::Default); setApplicationState(ApplicationState::Default);
setWindowIcon(QIcon(":subsurface-icon")); 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) void MainWindow::editDiveSite(dive_site *ds)
{ {
if (!ds) if (!ds)
@ -1878,20 +1865,30 @@ void MainWindow::showFilterIfEnabled()
ui.bottomSplitter->setSizes({ EXPANDED, COLLAPSED }); 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 (q.widget && stack->indexOf(q.widget) == -1)
if (ui.topLeft->indexOf(topLeft) == -1 && topLeft) { stack->addWidget(q.widget);
ui.topLeft->addWidget(topLeft); }
}
if (ui.topRight->indexOf(topRight) == -1 && topRight) { void MainWindow::registerApplicationState(ApplicationState state, Quadrants q)
ui.topRight->addWidget(topRight); {
} applicationState[(int)state] = q;
if (ui.bottomLeft->indexOf(bottomLeft) == -1 && bottomLeft) { addWidgets(q.topLeft, ui.topLeft);
ui.bottomLeft->addWidget(bottomLeft); addWidgets(q.topRight, ui.topRight);
} addWidgets(q.bottomLeft, ui.bottomLeft);
if(ui.bottomRight->indexOf(bottomRight) == -1 && bottomRight) { addWidgets(q.bottomRight, ui.bottomRight);
ui.bottomRight->addWidget(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); setAppState(state);
#define SET_CURRENT_INDEX( X ) \ const Quadrants &quadrants = applicationState[(int)state];
if (applicationState[(int)state].X) { \ setQuadrant(quadrants.topLeft, ui.topLeft);
ui.X->setCurrentWidget( applicationState[(int)state].X); \ setQuadrant(quadrants.topRight, ui.topRight);
ui.X->show(); \ setQuadrant(quadrants.bottomLeft, ui.bottomLeft);
} else { \ setQuadrant(quadrants.bottomRight, ui.bottomRight);
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
} }
void MainWindow::showProgressBar() void MainWindow::showProgressBar()

View file

@ -77,7 +77,6 @@ public:
void printPlan(); void printPlan();
void checkSurvey(); void checkSurvey();
void setApplicationState(ApplicationState state); void setApplicationState(ApplicationState state);
void setStateProperties(ApplicationState state, const PropertyList& tl, const PropertyList& tr, const PropertyList& bl,const PropertyList& br);
bool inPlanner(); bool inPlanner();
NotificationWidget *getNotificationWidget(); NotificationWidget *getNotificationWidget();
void enableDisableCloudActions(); void enableDisableCloudActions();
@ -213,7 +212,6 @@ private:
void toggleCollapsible(bool toggle); void toggleCollapsible(bool toggle);
void showFilterIfEnabled(); void showFilterIfEnabled();
void updateLastUsedDir(const QString &s); void updateLastUsedDir(const QString &s);
void registerApplicationState(ApplicationState state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight);
void enterState(CurrentState); void enterState(CurrentState);
bool filesAsArguments; bool filesAsArguments;
UpdateManager *updateManager; UpdateManager *updateManager;
@ -231,27 +229,27 @@ private:
QStringList recentFiles; QStringList recentFiles;
QAction *actionsRecent[NUM_RECENT_FILES]; QAction *actionsRecent[NUM_RECENT_FILES];
struct WidgetForQuadrant { enum {
WidgetForQuadrant(QWidget *tl = 0, QWidget *tr = 0, QWidget *bl = 0, QWidget *br = 0) : FLAG_NONE = 0,
topLeft(tl), topRight(tr), bottomLeft(bl), bottomRight(br) {} FLAG_DISABLED = 1
QWidget *topLeft;
QWidget *topRight;
QWidget *bottomLeft;
QWidget *bottomRight;
}; };
struct PropertiesForQuadrant { struct Quadrant {
PropertiesForQuadrant(){} QWidget *widget;
PropertiesForQuadrant(const PropertyList& tl, const PropertyList& tr,const PropertyList& bl,const PropertyList& br) : int flags;
topLeft(tl), topRight(tr), bottomLeft(bl), bottomRight(br) {}
PropertyList topLeft;
PropertyList topRight;
PropertyList bottomLeft;
PropertyList bottomRight;
}; };
WidgetForQuadrant applicationState[(size_t)ApplicationState::Count]; struct Quadrants {
PropertiesForQuadrant stateProperties[(size_t)ApplicationState::Count]; Quadrant topLeft;
Quadrant topRight;
Quadrant bottomLeft;
Quadrant bottomRight;
};
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; GpsLocation *locationProvider;
QMenu *connections; QMenu *connections;