diff options
-rw-r--r-- | core/settings/qPrefUnit.cpp | 94 | ||||
-rw-r--r-- | core/settings/qPrefUnit.h | 66 | ||||
-rw-r--r-- | desktop-widgets/preferences/preferences_units.cpp | 2 | ||||
-rw-r--r-- | mobile-widgets/qmlmanager.cpp | 5 | ||||
-rw-r--r-- | tests/testplannershared.cpp | 8 | ||||
-rw-r--r-- | tests/testqPrefUnits.cpp | 68 |
6 files changed, 60 insertions, 183 deletions
diff --git a/core/settings/qPrefUnit.cpp b/core/settings/qPrefUnit.cpp index 8a3bb50f7..c4f32490f 100644 --- a/core/settings/qPrefUnit.cpp +++ b/core/settings/qPrefUnit.cpp @@ -28,75 +28,24 @@ void qPrefUnits::loadSync(bool doSync) HANDLE_PREFERENCE_BOOL(Units, "coordinates", coordinates_traditional); -QString qPrefUnits::duration_units() -{ - return prefs.units.duration_units == units::DURATION::ALWAYS_HOURS ? QStringLiteral("hours") : - prefs.units.duration_units == units::DURATION::MINUTES_ONLY ? QStringLiteral("minutes") : - QStringLiteral("mixed"); -} -void qPrefUnits::set_duration_units(const QString& value) -{ - set_duration_units(value == QStringLiteral("hours") ? units::DURATION::ALWAYS_HOURS : - value == QStringLiteral("minutes")? units::DURATION::MINUTES_ONLY : - units::DURATION::MIXED); - emit instance()->duration_unitsStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::DURATION, duration_units, units.); DISK_LOADSYNC_ENUM_EXT(Units, "duration_units", units::DURATION, duration_units, units.); -QString qPrefUnits::length() -{ - return prefs.units.length == units::LENGTH::METERS ? QStringLiteral("meters") : QStringLiteral("feet"); -} -void qPrefUnits::set_length(const QString& value) -{ - set_length(value == QStringLiteral("meters") ? units::LENGTH::METERS : units::LENGTH::FEET); - emit instance()->lengthStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::LENGTH, length, units.); DISK_LOADSYNC_ENUM_EXT(Units, "length", units::LENGTH, length, units.); -QString qPrefUnits::pressure() -{ - return prefs.units.pressure == units::PRESSURE::BAR ? QStringLiteral("bar") : QStringLiteral("psi"); -} -void qPrefUnits::set_pressure(const QString& value) -{ - set_pressure(value == QStringLiteral("bar") ? units::PRESSURE::BAR : units::PRESSURE::PSI); - emit instance()->pressureStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::PRESSURE, pressure, units.); DISK_LOADSYNC_ENUM_EXT(Units, "pressure", units::PRESSURE, pressure, units.); HANDLE_PREFERENCE_BOOL_EXT(Units, "show_units_table", show_units_table, units.); -QString qPrefUnits::temperature() -{ - return prefs.units.temperature == units::TEMPERATURE::CELSIUS ? QStringLiteral("celcius") : QStringLiteral("fahrenheit"); -} -void qPrefUnits::set_temperature(const QString& value) -{ - set_temperature(value == QStringLiteral("celcius") ? units::TEMPERATURE::CELSIUS : units::TEMPERATURE::FAHRENHEIT); - emit instance()->temperatureStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::TEMPERATURE, temperature, units.); DISK_LOADSYNC_ENUM_EXT(Units, "temperature", units::TEMPERATURE, temperature, units.); -QString qPrefUnits::unit_system() -{ - return prefs.unit_system == METRIC ? QStringLiteral("metric") : - prefs.unit_system == IMPERIAL ? QStringLiteral("imperial") : - QStringLiteral("personalized"); -} -void qPrefUnits::set_unit_system(const QString& value) -{ - set_unit_system(value == QStringLiteral("metric") ? METRIC : value == QStringLiteral("imperial")? IMPERIAL : PERSONALIZE); - emit instance()->unit_systemStringChanged(value); -} void qPrefUnits::set_unit_system(unit_system_values value) { - if (value == METRIC) { - prefs.unit_system = METRIC; + prefs.unit_system = value; + if (prefs.unit_system == METRIC) { prefs.units = SI_units; // make sure all types are updated when changing @@ -105,8 +54,7 @@ void qPrefUnits::set_unit_system(unit_system_values value) set_length(units::LENGTH::METERS); set_pressure(units::PRESSURE::BAR); set_temperature(units::TEMPERATURE::CELSIUS); - } else if (value == IMPERIAL) { - prefs.unit_system = IMPERIAL; + } else if (prefs.unit_system == IMPERIAL) { prefs.units = IMPERIAL_units; // make sure all types are updated when changing @@ -120,50 +68,14 @@ void qPrefUnits::set_unit_system(unit_system_values value) } disk_unit_system(true); emit instance()->unit_systemChanged(value); - emit instance()->unit_systemStringChanged(unit_system()); - emit instance()->volumeChanged(prefs.units.volume); - emit instance()->volumeStringChanged(volume()); - emit instance()->weightChanged(prefs.units.weight); - emit instance()->weightStringChanged(weight()); - emit instance()->lengthChanged(prefs.units.length); - emit instance()->lengthStringChanged(length()); - emit instance()->temperatureChanged(prefs.units.temperature); - emit instance()->temperatureStringChanged(temperature()); } DISK_LOADSYNC_ENUM(Units, "unit_system", unit_system_values, unit_system); -QString qPrefUnits::vertical_speed_time() -{ - return prefs.units.vertical_speed_time == units::TIME::MINUTES ? QStringLiteral("minutes") : QStringLiteral("seconds"); -} -void qPrefUnits::set_vertical_speed_time(const QString& value) -{ - set_vertical_speed_time(value == QStringLiteral("minutes") ? units::TIME::MINUTES : units::TIME::SECONDS); - emit instance()->vertical_speed_timeStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::TIME, vertical_speed_time, units.); DISK_LOADSYNC_ENUM_EXT(Units, "vertical_speed_time", units::TIME, vertical_speed_time, units.); -QString qPrefUnits::volume() -{ - return prefs.units.volume == units::VOLUME::LITER ? QStringLiteral("liter") : QStringLiteral("cuft"); -} -void qPrefUnits::set_volume(const QString& value) -{ - set_volume(value == QStringLiteral("liter") ? units::VOLUME::LITER : units::VOLUME::CUFT); - emit instance()->volumeStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::VOLUME, volume, units.); DISK_LOADSYNC_ENUM_EXT(Units, "volume", units::VOLUME, volume, units.); -QString qPrefUnits::weight() -{ - return prefs.units.weight == units::WEIGHT::KG ? QStringLiteral("kg") : QStringLiteral("lbs"); -} -void qPrefUnits::set_weight(const QString& value) -{ - set_weight(value == QStringLiteral("kg") ? units::WEIGHT::KG : units::WEIGHT::LBS); - emit instance()->weightStringChanged(value); -} SET_PREFERENCE_ENUM_EXT(Units, units::WEIGHT, weight, units.); DISK_LOADSYNC_ENUM_EXT(Units, "weight", units::WEIGHT, weight, units.); diff --git a/core/settings/qPrefUnit.h b/core/settings/qPrefUnit.h index 467117ef2..5e81bd77a 100644 --- a/core/settings/qPrefUnit.h +++ b/core/settings/qPrefUnit.h @@ -9,15 +9,7 @@ class qPrefUnits : public QObject { Q_OBJECT Q_PROPERTY(bool coordinates_traditional READ coordinates_traditional WRITE set_coordinates_traditional NOTIFY coordinates_traditionalChanged) - Q_PROPERTY(QString duration_units READ duration_units WRITE set_duration_units NOTIFY duration_unitsStringChanged) - Q_PROPERTY(QString length READ length WRITE set_length NOTIFY lengthStringChanged) - Q_PROPERTY(QString pressure READ pressure WRITE set_pressure NOTIFY pressureStringChanged) Q_PROPERTY(bool show_units_table READ show_units_table WRITE set_show_units_table NOTIFY show_units_tableChanged) - Q_PROPERTY(QString temperature READ temperature WRITE set_temperature NOTIFY temperatureStringChanged) - Q_PROPERTY(QString unit_system READ unit_system WRITE set_unit_system NOTIFY unit_systemStringChanged) - Q_PROPERTY(QString vertical_speed_time READ vertical_speed_time WRITE set_vertical_speed_time NOTIFY vertical_speed_timeStringChanged) - Q_PROPERTY(QString volume READ volume WRITE set_volume NOTIFY volumeStringChanged) - Q_PROPERTY(QString weight READ weight WRITE set_weight NOTIFY weightStringChanged) public: static qPrefUnits *instance(); @@ -29,63 +21,40 @@ public: public: static bool coordinates_traditional() { return prefs.coordinates_traditional; } - static QString duration_units(); - static QString length(); - static QString pressure(); + static units::DURATION duration_units() { return prefs.units.duration_units; } + static units::LENGTH length() { return prefs.units.length; } + static units::PRESSURE pressure() { return prefs.units.pressure; } static bool show_units_table() { return prefs.units.show_units_table; } - static QString temperature(); - static QString unit_system(); - static QString vertical_speed_time(); - static QString volume(); - static QString weight(); + static unit_system_values unit_system() { return prefs.unit_system; } + static units::TEMPERATURE temperature() { return prefs.units.temperature; } + static units::TIME vertical_speed_time() { return prefs.units.vertical_speed_time; } + static units::VOLUME volume() { return prefs.units.volume; } + static units::WEIGHT weight() { return prefs.units.weight; } public slots: static void set_coordinates_traditional(bool value); static void set_duration_units(units::DURATION value); - static void set_duration_units(const QString& value); static void set_length(units::LENGTH value); - static void set_length(const QString& value); static void set_pressure(units::PRESSURE value); - static void set_pressure(const QString& value); static void set_show_units_table(bool value); static void set_temperature(units::TEMPERATURE value); - static void set_temperature(const QString& value); static void set_unit_system(unit_system_values value); - static void set_unit_system(const QString& value); static void set_vertical_speed_time(units::TIME value); - static void set_vertical_speed_time(const QString& value); static void set_volume(units::VOLUME value); - static void set_volume(const QString& value); static void set_weight(units::WEIGHT value); - static void set_weight(const QString& value); signals: - // Normally the same signal name are used with different parameters: - // void weightChanged(int value); - // void weightChanged(const QString& value); - // This works perfect, however connect() cannot automatically determine - // which signal to catch, for that purpose SIGNAL() / SLOT() macros are used, - // since they include the parameter type. - // However there is a design decision, not to use the macros, so - // signal must have unique names. void coordinates_traditionalChanged(bool value); - void duration_unitsChanged(int value); - void duration_unitsStringChanged(const QString& value); - void lengthChanged(int value); - void lengthStringChanged(const QString& value); - void pressureChanged(int value); - void pressureStringChanged(const QString& value); + void duration_unitsChanged(units::DURATION value); + void lengthChanged(units::LENGTH value); + void pressureChanged(units::PRESSURE value); void show_units_tableChanged(bool value); - void temperatureChanged(int value); - void temperatureStringChanged(const QString& value); - void unit_systemChanged(int value); - void unit_systemStringChanged(const QString& value); - void vertical_speed_timeChanged(int value); - void vertical_speed_timeStringChanged(const QString& value); - void volumeChanged(int value); - void volumeStringChanged(const QString& value); - void weightChanged(int value); - void weightStringChanged(const QString& value); + void temperatureChanged(units::TEMPERATURE value); + void unit_systemChanged(unit_system_values value); + void vertical_speed_timeChanged(units::TIME value); + void volumeChanged(units::VOLUME value); + void weightChanged(units::WEIGHT value); + private: qPrefUnits() {} @@ -100,5 +69,4 @@ private: static void disk_volume(bool doSync); static void disk_weight(bool doSync); }; - #endif diff --git a/desktop-widgets/preferences/preferences_units.cpp b/desktop-widgets/preferences/preferences_units.cpp index db23efd37..ccb7620cf 100644 --- a/desktop-widgets/preferences/preferences_units.cpp +++ b/desktop-widgets/preferences/preferences_units.cpp @@ -50,7 +50,7 @@ void PreferencesUnits::syncSettings() QString unitSystem[] = {"metric", "imperial", "personal"}; short unitValue = ui->metric->isChecked() ? METRIC : (ui->imperial->isChecked() ? IMPERIAL : PERSONALIZE); - qPrefUnits::set_unit_system(unitSystem[unitValue]); + qPrefUnits::set_unit_system((unit_system_values)unitValue); qPrefUnits::set_temperature(ui->fahrenheit->isChecked() ? units::FAHRENHEIT : units::CELSIUS); qPrefUnits::set_length(ui->feet->isChecked() ? units::FEET : units::METERS); qPrefUnits::set_pressure(ui->psi->isChecked() ? units::PSI : units::BAR); diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index bb773d930..c29cbe345 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -323,10 +323,7 @@ void QMLManager::openLocalThenRemote(QString url) setLoadFromCloud(true); if (qPrefCloudStorage::cloud_verification_status() == qPrefCloudStorage::CS_UNKNOWN) qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_VERIFIED); - if (git_prefs.unit_system == IMPERIAL) - qPrefUnits::set_unit_system("imperial"); - else if (git_prefs.unit_system == METRIC) - qPrefUnits::set_unit_system("metric"); + qPrefUnits::set_unit_system(git_prefs.unit_system); qPrefTechnicalDetails::set_tankbar(git_prefs.tankbar); qPrefTechnicalDetails::set_dcceiling(git_prefs.dcceiling); qPrefTechnicalDetails::set_show_ccr_setpoint(git_prefs.show_ccr_setpoint); diff --git a/tests/testplannershared.cpp b/tests/testplannershared.cpp index a0af6dc37..d9b03c247 100644 --- a/tests/testplannershared.cpp +++ b/tests/testplannershared.cpp @@ -61,7 +61,7 @@ void TestPlannerShared::test_rates() { // Set system to use meters - qPrefUnits::set_unit_system(QStringLiteral("metric")); + qPrefUnits::set_unit_system(METRIC); plannerShared::set_ascratelast6m(16); QCOMPARE(qPrefDivePlanner::ascratelast6m(), 267); @@ -109,7 +109,7 @@ void TestPlannerShared::test_rates() QCOMPARE(plannerShared::descrate(), 10); // Set system to use feet - qPrefUnits::set_unit_system(QStringLiteral("imperial")); + qPrefUnits::set_unit_system(IMPERIAL); plannerShared::set_ascratelast6m(33); QCOMPARE(qPrefDivePlanner::ascratelast6m(), 168); @@ -197,7 +197,7 @@ void TestPlannerShared::test_gas() QCOMPARE(plannerShared::problemsolvingtime(), 6); // Set system to use meters - qPrefUnits::set_unit_system(QStringLiteral("metric")); + qPrefUnits::set_unit_system(METRIC); plannerShared::set_bottomsac(30); QCOMPARE(qPrefDivePlanner::bottomsac(), 30000); @@ -245,7 +245,7 @@ void TestPlannerShared::test_gas() QCOMPARE(plannerShared::bestmixend(), 10); // Set system to use feet - qPrefUnits::set_unit_system(QStringLiteral("imperial")); + qPrefUnits::set_unit_system(IMPERIAL); plannerShared::set_bottomsac(0.9); QCOMPARE(qPrefDivePlanner::bottomsac(), 25485); diff --git a/tests/testqPrefUnits.cpp b/tests/testqPrefUnits.cpp index c5635732b..81f9f2f24 100644 --- a/tests/testqPrefUnits.cpp +++ b/tests/testqPrefUnits.cpp @@ -34,14 +34,14 @@ void TestQPrefUnits::test_struct_get() prefs.units.weight = units::KG; QCOMPARE(tst->coordinates_traditional(), true); - QCOMPARE(tst->duration_units(), QStringLiteral("mixed")); - QCOMPARE(tst->length(), QStringLiteral("meters")); - QCOMPARE(tst->pressure(), QStringLiteral("bar")); + QCOMPARE(tst->duration_units(), units::MIXED); + QCOMPARE(tst->length(), units::METERS); + QCOMPARE(tst->pressure(), units::BAR); QCOMPARE(tst->show_units_table(), true); - QCOMPARE(tst->temperature(), QStringLiteral("celcius")); - QCOMPARE(tst->vertical_speed_time(), QStringLiteral("seconds")); - QCOMPARE(tst->volume(), QStringLiteral("liter")); - QCOMPARE(tst->weight(), QStringLiteral("kg")); + QCOMPARE(tst->temperature(), units::CELSIUS); + QCOMPARE(tst->vertical_speed_time(), units::SECONDS); + QCOMPARE(tst->volume(), units::LITER); + QCOMPARE(tst->weight(), units::KG); } void TestQPrefUnits::test_set_struct() @@ -159,9 +159,9 @@ void TestQPrefUnits::test_multiple() auto tst = qPrefUnits::instance(); QCOMPARE(tst->length(), qPrefUnits::length()); - QCOMPARE(tst->length(), QStringLiteral("meters")); + QCOMPARE(tst->length(), units::METERS); QCOMPARE(tst->pressure(), qPrefUnits::pressure()); - QCOMPARE(tst->pressure(), QStringLiteral("bar")); + QCOMPARE(tst->pressure(), units::BAR); } void TestQPrefUnits::test_unit_system() @@ -170,22 +170,22 @@ void TestQPrefUnits::test_unit_system() auto tst = qPrefUnits::instance(); - tst->set_unit_system("metric"); + tst->set_unit_system(METRIC); QCOMPARE(prefs.unit_system, METRIC); - QCOMPARE(tst->unit_system(), QStringLiteral("metric")); - tst->set_unit_system("imperial"); + QCOMPARE(tst->unit_system(), METRIC); + tst->set_unit_system(IMPERIAL); QCOMPARE(prefs.unit_system, IMPERIAL); - QCOMPARE(tst->unit_system(), QStringLiteral("imperial")); - tst->set_unit_system("personalized"); + QCOMPARE(tst->unit_system(), IMPERIAL); + tst->set_unit_system(PERSONALIZE); QCOMPARE(prefs.unit_system, PERSONALIZE); - QCOMPARE(tst->unit_system(), QStringLiteral("personalized")); + QCOMPARE(tst->unit_system(), PERSONALIZE); prefs.unit_system = METRIC; - QCOMPARE(tst->unit_system(), QStringLiteral("metric")); + QCOMPARE(tst->unit_system(), METRIC); prefs.unit_system = IMPERIAL; - QCOMPARE(tst->unit_system(), QStringLiteral("imperial")); + QCOMPARE(tst->unit_system(), IMPERIAL); prefs.unit_system = PERSONALIZE; - QCOMPARE(tst->unit_system(), QStringLiteral("personalized")); + QCOMPARE(tst->unit_system(), PERSONALIZE); } #define TEST(METHOD, VALUE) \ @@ -203,17 +203,19 @@ void TestQPrefUnits::test_oldPreferences() units->set_volume(units::LITER); units->set_temperature(units::CELSIUS); units->set_weight(units::KG); - units->set_unit_system(QStringLiteral("metric")); + units->set_unit_system(METRIC); units->set_coordinates_traditional(false); units->set_vertical_speed_time(units::SECONDS); - TEST(units->length(), QStringLiteral("meters")); - TEST(units->pressure(), QStringLiteral("bar")); - TEST(units->volume(), QStringLiteral("liter")); - TEST(units->temperature(), QStringLiteral("celcius")); - TEST(units->weight(), QStringLiteral("kg")); - TEST(units->vertical_speed_time(), QStringLiteral("seconds")); - TEST(units->unit_system(), QStringLiteral("metric")); + TEST(units->length(), units::METERS); + TEST(units->pressure(), units::BAR); + TEST(units->volume(), units::LITER); + TEST(units->temperature(), units::CELSIUS); + TEST(units->weight(), units::KG); + TEST(units->vertical_speed_time(), units::SECONDS); + TEST(units->unit_system(), METRIC); + TEST(units->vertical_speed_time(), units::SECONDS); + TEST(units->unit_system(), METRIC); TEST(units->coordinates_traditional(), false); units->set_length(units::FEET); @@ -222,16 +224,14 @@ void TestQPrefUnits::test_oldPreferences() units->set_temperature(units::FAHRENHEIT); units->set_weight(units::LBS); units->set_vertical_speed_time(units::MINUTES); - units->set_unit_system(QStringLiteral("fake-metric-system")); units->set_coordinates_traditional(true); - TEST(units->length(), QStringLiteral("feet")); - TEST(units->pressure(), QStringLiteral("psi")); - TEST(units->volume(), QStringLiteral("cuft")); - TEST(units->temperature(), QStringLiteral("fahrenheit")); - TEST(units->weight(), QStringLiteral("lbs")); - TEST(units->vertical_speed_time(), QStringLiteral("minutes")); - TEST(units->unit_system(), QStringLiteral("personalized")); + TEST(units->length(), units::FEET); + TEST(units->pressure(), units::PSI); + TEST(units->volume(), units::CUFT); + TEST(units->temperature(), units::FAHRENHEIT); + TEST(units->weight(), units::LBS); + TEST(units->vertical_speed_time(), units::MINUTES); TEST(units->coordinates_traditional(), true); } |