Turn application state into enum

The application state was encoded in a QByteArray. Thus, there was
no compile-time checking. Typos would lead to silent failures.

Turn the application state into an enum. Use the enum-class construct,
so that the values don't polute the global namespace. Moreover,
this makes them strongly typed, i.e. they don't auto-convert to
integers.

A disadvantage is that the enums now have to be cast to int
explicitly when used to index an array.

Replace two hash-maps in MainWindow to arrays of fixed sizes.

Move the application-state details into their own files.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-05-10 19:51:43 +02:00 committed by Dirk Hohndel
parent 114b3d9d47
commit 75767c456a
11 changed files with 100 additions and 75 deletions

View file

@ -37,6 +37,8 @@ endif()
# compile the core library part in C, part in C++
set(SUBSURFACE_CORE_LIB_SRCS
applicationstate.cpp
applicationstate.h
checkcloudconnection.cpp
checkcloudconnection.h
cloudstorage.cpp

15
core/applicationstate.cpp Normal file
View file

@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-2.0
#include "applicationstate.h"
static ApplicationState appState = (ApplicationState)-1; // Set to an invalid value
ApplicationState getAppState()
{
return appState;
}
void setAppState(ApplicationState state)
{
appState = state;
}

21
core/applicationstate.h Normal file
View file

@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef APPLICATIONSTATE_H
#define APPLICATIONSTATE_H
// By using an enum class, the enum entries don't polute the global namespace.
// Moreover, they are strongly typed. This means that they are not auto-converted
// to integer types if e.g. used as array-indices.
enum class ApplicationState {
Default,
EditDive,
PlanDive,
EditPlannedDive,
EditDiveSite,
FilterDive,
Count
};
ApplicationState getAppState();
void setAppState(ApplicationState state);
#endif

View file

@ -12,6 +12,7 @@
#include "divecomputer.h"
#include "time.h"
#include "gettextfromc.h"
#include "applicationstate.h"
#include "metadata.h"
#include <sys/time.h>
#include "exif.h"
@ -1362,21 +1363,9 @@ extern "C" void parse_display_units(char *line)
qDebug() << line;
}
static QByteArray currentApplicationState;
QByteArray getCurrentAppState()
{
return currentApplicationState;
}
void setCurrentAppState(const QByteArray &state)
{
currentApplicationState = state;
}
extern "C" bool in_planner()
{
return currentApplicationState == "PlanDive" || currentApplicationState == "EditPlannedDive";
return getAppState() == ApplicationState::PlanDive || getAppState() == ApplicationState::EditPlannedDive;
}
extern "C" enum deco_mode decoMode()

View file

@ -40,8 +40,6 @@ volume_t string_to_volume(const char *str, pressure_t workp);
fraction_t string_to_fraction(const char *str);
int getCloudURL(QString &filename);
bool parseGpsText(const QString &gps_text, double *latitude, double *longitude);
QByteArray getCurrentAppState();
void setCurrentAppState(const QByteArray &state);
void init_proxy();
QString getUUID();
extern const QStringList videoExtensionsList;

View file

@ -161,7 +161,7 @@ void FilterWidget2::clearFilter()
void FilterWidget2::closeFilter()
{
MainWindow::instance()->setApplicationState("Default");
MainWindow::instance()->setApplicationState(ApplicationState::Default);
}
void FilterWidget2::temperatureChanged()

View file

@ -181,7 +181,7 @@ void LocationInformationWidget::acceptChanges()
{
MainWindow::instance()->diveList->setEnabled(true);
MainWindow::instance()->setEnabledToolbar(true);
MainWindow::instance()->setApplicationState("Default");
MainWindow::instance()->setApplicationState(ApplicationState::Default);
MapWidget::instance()->repopulateLabels();
MultiFilterSortModel::instance()->stopFilterDiveSites();
}

View file

@ -178,20 +178,20 @@ MainWindow::MainWindow() : QMainWindow(),
enabledList.push_back(enabled);
disabledList.push_back(disabled);
registerApplicationState("Default", mainTab.get(), profileContainer, diveList, mapWidget );
registerApplicationState("EditDive", mainTab.get(), profileContainer, diveList, mapWidget );
registerApplicationState("PlanDive", divePlannerWidget, profileContainer, divePlannerSettingsWidget, plannerDetails );
registerApplicationState("EditPlannedDive", divePlannerWidget, profileContainer, diveList, mapWidget );
registerApplicationState("EditDiveSite", diveSiteEdit, profileContainer, diveList, mapWidget);
registerApplicationState("FilterDive", mainTab.get(), profileContainer, diveList, &filterWidget2);
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("Default", enabledList, enabledList, enabledList, enabledList);
setStateProperties("EditDive", enabledList, enabledList, enabledList, enabledList);
setStateProperties("PlanDive", enabledList, enabledList, enabledList, enabledList);
setStateProperties("EditPlannedDive", enabledList, enabledList, enabledList, enabledList);
setStateProperties("EditDiveSite", enabledList, disabledList, disabledList, enabledList);
setStateProperties("FilterDive", enabledList, enabledList, enabledList, enabledList);
setApplicationState("Default");
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);
setWindowIcon(QIcon(":subsurface-icon"));
if (!QIcon::hasThemeIcon("window-close")) {
@ -369,9 +369,9 @@ void MainWindow::setupSocialNetworkMenu()
{
}
void MainWindow::setStateProperties(const QByteArray& state, const PropertyList& tl, const PropertyList& tr, const PropertyList& bl, const PropertyList& br)
void MainWindow::setStateProperties(ApplicationState state, const PropertyList& tl, const PropertyList& tr, const PropertyList& bl, const PropertyList& br)
{
stateProperties[state] = PropertiesForQuadrant(tl, tr, bl, br);
stateProperties[(int)state] = PropertiesForQuadrant(tl, tr, bl, br);
}
void MainWindow::editDiveSite(dive_site *ds)
@ -379,7 +379,7 @@ void MainWindow::editDiveSite(dive_site *ds)
if (!ds)
return;
diveSiteEdit->initFields(ds);
setApplicationState("EditDiveSite");
setApplicationState(ApplicationState::EditDiveSite);
}
void MainWindow::startDiveSiteEdit()
@ -403,7 +403,7 @@ void MainWindow::enableDisableOtherDCsActions()
void MainWindow::setDefaultState()
{
setApplicationState("Default");
setApplicationState(ApplicationState::Default);
if (mainTab->isEditing())
ui.bottomLeft->currentWidget()->setEnabled(false);
}
@ -422,7 +422,7 @@ void MainWindow::refreshDisplay(bool doRecreateDiveList)
if (doRecreateDiveList)
recreateDiveList();
setApplicationState("Default");
setApplicationState(ApplicationState::Default);
diveList->setEnabled(true);
diveList->setFocus();
WSInfoModel::instance()->update();
@ -713,7 +713,7 @@ void MainWindow::on_actionClose_triggered()
if (okToClose(tr("Please save or cancel the current dive edit before closing the file."))) {
closeCurrentFile();
DivePictureModel::instance()->updateDivePictures();
setApplicationState("Default");
setApplicationState(ApplicationState::Default);
recreateDiveList();
}
}
@ -753,7 +753,7 @@ void MainWindow::showProfile()
{
enableShortcuts();
graphics->setProfileState();
setApplicationState("Default");
setApplicationState(ApplicationState::Default);
}
void MainWindow::on_actionPreferences_triggered()
@ -839,7 +839,7 @@ void MainWindow::planCreated()
{
// make sure our UI is in a consistent state
showProfile();
setApplicationState("Default");
setApplicationState(ApplicationState::Default);
diveList->setEnabled(true);
diveList->setFocus();
}
@ -931,7 +931,7 @@ void MainWindow::on_actionReplanDive_triggered()
graphics->setPlanState();
graphics->clearHandlers();
setApplicationState("PlanDive");
setApplicationState(ApplicationState::PlanDive);
divePlannerWidget->setReplanButton(true);
divePlannerWidget->setupStartTime(QDateTime::fromMSecsSinceEpoch(1000 * current_dive->when, Qt::UTC));
if (current_dive->surface_pressure.mbar)
@ -950,7 +950,7 @@ void MainWindow::on_actionDivePlanner_triggered()
// put us in PLAN mode
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
setApplicationState("PlanDive");
setApplicationState(ApplicationState::PlanDive);
graphics->setPlanState();
@ -1808,16 +1808,16 @@ void MainWindow::editCurrentDive()
if (defaultDC == "manually added dive") {
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD);
graphics->setAddState();
setApplicationState("EditDive");
setApplicationState(ApplicationState::EditDive);
DivePlannerPointsModel::instance()->loadFromDive(d);
mainTab->enableEdition(MainTab::MANUALLY_ADDED_DIVE);
} else if (defaultDC == "planned dive") {
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
setApplicationState("EditPlannedDive");
setApplicationState(ApplicationState::EditPlannedDive);
DivePlannerPointsModel::instance()->loadFromDive(d);
mainTab->enableEdition(MainTab::MANUALLY_ADDED_DIVE);
} else {
setApplicationState("EditDive");
setApplicationState(ApplicationState::EditDive);
mainTab->enableEdition();
}
}
@ -1863,14 +1863,14 @@ void MainWindow::on_paste_triggered()
void MainWindow::on_actionFilterTags_triggered()
{
setApplicationState(getCurrentAppState() == "FilterDive" ? "Default" : "FilterDive");
setApplicationState(getAppState() == ApplicationState::FilterDive ? ApplicationState::Default : ApplicationState::FilterDive);
if (state == LIST_MAXIMIZED)
showFilterIfEnabled();
}
void MainWindow::showFilterIfEnabled()
{
if (getCurrentAppState() == "FilterDive") {
if (getAppState() == ApplicationState::FilterDive) {
const int appW = qApp->desktop()->size().width();
QList<int> profileFilterSizes = { round_int(appW * 0.7), round_int(appW * 0.3) };
ui.bottomSplitter->setSizes(profileFilterSizes);
@ -1878,9 +1878,9 @@ void MainWindow::showFilterIfEnabled()
ui.bottomSplitter->setSizes({ EXPANDED, COLLAPSED });
}
}
void MainWindow::registerApplicationState(const QByteArray& state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight)
void MainWindow::registerApplicationState(ApplicationState state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight)
{
applicationState[state] = WidgetForQuadrant(topLeft, topRight, bottomLeft, bottomRight);
applicationState[(int)state] = WidgetForQuadrant(topLeft, topRight, bottomLeft, bottomRight);
if (ui.topLeft->indexOf(topLeft) == -1 && topLeft) {
ui.topLeft->addWidget(topLeft);
}
@ -1895,38 +1895,35 @@ void MainWindow::registerApplicationState(const QByteArray& state, QWidget *topL
}
}
void MainWindow::setApplicationState(const QByteArray &state)
void MainWindow::setApplicationState(ApplicationState state)
{
if (!applicationState.keys().contains(state))
if (getAppState() == state)
return;
if (getCurrentAppState() == state)
return;
setCurrentAppState(state);
setAppState(state);
#define SET_CURRENT_INDEX( X ) \
if (applicationState[state].X) { \
ui.X->setCurrentWidget( applicationState[state].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[state].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[state].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[state].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[state].bottomRight) {
Q_FOREACH(const WidgetProperty& p, stateProperties[(int)state].bottomRight) {
ui.bottomRight->currentWidget()->setProperty( p.first.data(), p.second);
}
#undef SET_CURRENT_INDEX

View file

@ -19,6 +19,7 @@
#include "ui_plannerDetails.h"
#include "desktop-widgets/notificationwidget.h"
#include "desktop-widgets/filterwidget2.h"
#include "core/applicationstate.h"
#include "core/gpslocation.h"
#include "core/dive.h"
@ -56,7 +57,7 @@ public:
INFO_MAXIMIZED,
PROFILE_MAXIMIZED,
LIST_MAXIMIZED,
EDIT
EDIT,
};
MainWindow();
@ -75,8 +76,8 @@ public:
void setToolButtonsEnabled(bool enabled);
void printPlan();
void checkSurvey();
void setApplicationState(const QByteArray& state);
void setStateProperties(const QByteArray& state, const PropertyList& tl, const PropertyList& tr, const PropertyList& bl,const PropertyList& br);
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();
@ -212,7 +213,7 @@ private:
void toggleCollapsible(bool toggle);
void showFilterIfEnabled();
void updateLastUsedDir(const QString &s);
void registerApplicationState(const QByteArray& state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight);
void registerApplicationState(ApplicationState state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight);
void enterState(CurrentState);
bool filesAsArguments;
UpdateManager *updateManager;
@ -249,8 +250,8 @@ private:
PropertyList bottomRight;
};
QHash<QByteArray, WidgetForQuadrant> applicationState;
QHash<QByteArray, PropertiesForQuadrant> stateProperties;
WidgetForQuadrant applicationState[(size_t)ApplicationState::Count];
PropertiesForQuadrant stateProperties[(size_t)ApplicationState::Count];
GpsLocation *locationProvider;
QMenu *connections;

View file

@ -18,6 +18,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \
../../core/qtserialbluetooth.cpp \
../../core/plannernotes.c \
../../core/uemis-downloader.c \
../../core/applicationstate.cpp \
../../core/qthelper.cpp \
../../core/checkcloudconnection.cpp \
../../core/color.cpp \

View file

@ -5,6 +5,7 @@
#include "core/qthelper.h"
#include "core/subsurfacestartup.h"
#include "core/units.h"
#include "core/applicationstate.h"
#include <QDebug>
#define DEBUG 1
@ -469,7 +470,7 @@ void TestPlan::testVpmbMetric45m30minTx()
struct diveplan testPlan = {};
setupPlanVpmb45m30mTx(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -500,7 +501,7 @@ void TestPlan::testVpmbMetric60m10minTx()
struct diveplan testPlan = {};
setupPlanVpmb60m10mTx(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -531,7 +532,7 @@ void TestPlan::testVpmbMetric60m30minAir()
struct diveplan testPlan = {};
setupPlanVpmb60m30minAir(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -562,7 +563,7 @@ void TestPlan::testVpmbMetric60m30minEan50()
struct diveplan testPlan = {};
setupPlanVpmb60m30minEan50(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -599,7 +600,7 @@ void TestPlan::testVpmbMetric60m30minTx()
struct diveplan testPlan = {};
setupPlanVpmb60m30minTx(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -636,7 +637,7 @@ void TestPlan::testVpmbMetric100m60min()
struct diveplan testPlan = {};
setupPlanVpmb100m60min(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -680,7 +681,7 @@ void TestPlan::testMultipleGases()
struct diveplan testPlan = {};
setupPlanSeveralGases(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -706,7 +707,7 @@ void TestPlan::testVpmbMetricMultiLevelAir()
struct diveplan testPlan = {};
setupPlanVpmbMultiLevelAir(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -737,7 +738,7 @@ void TestPlan::testVpmbMetric100m10min()
struct diveplan testPlan = {};
setupPlanVpmb100m10min(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);
@ -784,7 +785,7 @@ void TestPlan::testVpmbMetricRepeat()
struct diveplan testPlan = {};
setupPlanVpmb30m20min(&testPlan);
setCurrentAppState("PlanDive");
setAppState(ApplicationState::PlanDive);
plan(&test_deco_state, &testPlan, &displayed_dive, 60, stoptable, &cache, 1, 0);