mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 16:00:08 +00:00
added full unit support for radius field, implemented such that future fields needing units can be done easily
This commit is contained in:
parent
c53836e91a
commit
146055fb99
7 changed files with 103 additions and 24 deletions
|
|
@ -8,8 +8,6 @@
|
|||
#include <scwx/util/logger.hpp>
|
||||
#include <scwx/qt/config/radar_site.hpp>
|
||||
#include <scwx/qt/settings/general_settings.hpp>
|
||||
#include <scwx/qt/settings/unit_settings.hpp>
|
||||
#include <scwx/qt/types/unit_types.hpp>
|
||||
|
||||
#include <boost/asio/post.hpp>
|
||||
#include <boost/asio/thread_pool.hpp>
|
||||
|
|
@ -140,17 +138,12 @@ void AlertManager::Impl::HandleAlert(const types::TextEventKey& key,
|
|||
}
|
||||
|
||||
settings::AudioSettings& audioSettings = settings::AudioSettings::Instance();
|
||||
settings::UnitSettings& unitSettings = settings::UnitSettings::Instance();
|
||||
types::LocationMethod locationMethod = types::GetLocationMethod(
|
||||
audioSettings.alert_location_method().GetValue());
|
||||
common::Coordinate currentCoordinate = CurrentCoordinate(locationMethod);
|
||||
std::string alertCounty = audioSettings.alert_county().GetValue();
|
||||
|
||||
types::DistanceUnits radiusUnits =
|
||||
types::GetDistanceUnitsFromName(unitSettings.distance_units().GetValue());
|
||||
double radiusScale = types::GetDistanceUnitsScale(radiusUnits);
|
||||
auto alertRadius = units::length::kilometers<double>(
|
||||
audioSettings.alert_radius().GetValue() / radiusScale);
|
||||
auto alertRadius = units::length::kilometers<double>(
|
||||
audioSettings.alert_radius().GetValue());
|
||||
|
||||
logger_->debug("alertRadius: {}", (double)alertRadius);
|
||||
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ public:
|
|||
|
||||
void UpdateEditWidget();
|
||||
void UpdateResetButton();
|
||||
void UpdateUnitLabel();
|
||||
|
||||
SettingsInterface<T>* self_;
|
||||
|
||||
|
|
@ -49,9 +50,14 @@ public:
|
|||
std::unique_ptr<QObject> context_ {std::make_unique<QObject>()};
|
||||
QWidget* editWidget_ {nullptr};
|
||||
QAbstractButton* resetButton_ {nullptr};
|
||||
QLabel* unitLabel_ {nullptr};
|
||||
|
||||
std::function<std::string(const T&)> mapFromValue_ {nullptr};
|
||||
std::function<T(const std::string&)> mapToValue_ {nullptr};
|
||||
|
||||
double unitScale_ {1};
|
||||
const std::string * unitAbbreiation_ {nullptr};
|
||||
bool unitEnabled_ {false};
|
||||
};
|
||||
|
||||
template<class T>
|
||||
|
|
@ -381,6 +387,11 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget)
|
|||
p->context_.get(),
|
||||
[this](double d)
|
||||
{
|
||||
if (p->unitEnabled_)
|
||||
{
|
||||
d = d / p->unitScale_;
|
||||
}
|
||||
|
||||
const T value = p->variable_->GetValue();
|
||||
const std::optional<T> staged = p->variable_->GetStaged();
|
||||
|
||||
|
|
@ -448,6 +459,11 @@ void SettingsInterface<T>::SetResetButton(QAbstractButton* button)
|
|||
p->UpdateResetButton();
|
||||
}
|
||||
}
|
||||
template<class T>
|
||||
void SettingsInterface<T>::SetUnitLabel(QLabel* label)
|
||||
{
|
||||
p->unitLabel_ = label;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::SetMapFromValueFunction(
|
||||
|
|
@ -463,6 +479,17 @@ void SettingsInterface<T>::SetMapToValueFunction(
|
|||
p->mapToValue_ = function;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::SetUnit(const double& scale,
|
||||
const std::string& abbreviation)
|
||||
{
|
||||
p->unitScale_ = scale;
|
||||
p->unitAbbreiation_ = &abbreviation;
|
||||
p->unitEnabled_ = true;
|
||||
p->UpdateEditWidget();
|
||||
p->UpdateUnitLabel();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
template<class U>
|
||||
void SettingsInterface<T>::Impl::SetWidgetText(U* widget, const T& currentValue)
|
||||
|
|
@ -559,11 +586,27 @@ void SettingsInterface<T>::Impl::UpdateEditWidget()
|
|||
{
|
||||
if constexpr (std::is_floating_point_v<T>)
|
||||
{
|
||||
doubleSpinBox->setValue(static_cast<double>(currentValue));
|
||||
double doubleValue = static_cast<double>(currentValue);
|
||||
if (unitEnabled_)
|
||||
{
|
||||
doubleValue = doubleValue * unitScale_;
|
||||
}
|
||||
doubleSpinBox->setValue(doubleValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::Impl::UpdateUnitLabel()
|
||||
{
|
||||
if (unitLabel_ == nullptr || !unitEnabled_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
unitLabel_->setText(QString::fromStdString(*unitAbbreiation_));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::Impl::UpdateResetButton()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class QLabel;
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
|
|
@ -91,6 +93,13 @@ public:
|
|||
*/
|
||||
void SetResetButton(QAbstractButton* button) override;
|
||||
|
||||
/**
|
||||
* Sets the label for units from the settings dialog.
|
||||
*
|
||||
* @param label Unit label
|
||||
*/
|
||||
void SetUnitLabel(QLabel* label);
|
||||
|
||||
/**
|
||||
* If the edit widget displays a different value than what is stored in the
|
||||
* settings variable, a mapping function must be provided in order to convert
|
||||
|
|
@ -109,6 +118,14 @@ public:
|
|||
*/
|
||||
void SetMapToValueFunction(std::function<T(const std::string&)> function);
|
||||
|
||||
/**
|
||||
* Sets the unit to be used by this setting.
|
||||
*
|
||||
* @param scale The radio of the current unit to the base unit
|
||||
* @param abbreviation The abreviation to be displayed
|
||||
*/
|
||||
void SetUnit(const double& scale, const std::string& abbreviation);
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> p;
|
||||
|
|
|
|||
|
|
@ -101,12 +101,12 @@ static const std::unordered_map<DistanceUnits, std::string> distanceUnitsName_ {
|
|||
{DistanceUnits::User, "User-defined"},
|
||||
{DistanceUnits::Unknown, "?"}};
|
||||
|
||||
static constexpr auto distanceUnitsBase_ = units::kilometers<float> {1.0f};
|
||||
static const std::unordered_map<DistanceUnits, float> distanceUnitsScale_ {
|
||||
static constexpr auto distanceUnitsBase_ = units::kilometers<double> {1.0f};
|
||||
static const std::unordered_map<DistanceUnits, double> distanceUnitsScale_ {
|
||||
{DistanceUnits::Kilometers,
|
||||
(distanceUnitsBase_ / units::kilometers<float> {1.0f})},
|
||||
(distanceUnitsBase_ / units::kilometers<double> {1.0f})},
|
||||
{DistanceUnits::Miles,
|
||||
(distanceUnitsBase_ / units::miles<float> {1.0f})},
|
||||
(distanceUnitsBase_ / units::miles<double> {1.0f})},
|
||||
{DistanceUnits::User, 1.0f},
|
||||
{DistanceUnits::Unknown, 1.0f}};
|
||||
|
||||
|
|
@ -179,7 +179,7 @@ const std::string& GetDistanceUnitsName(DistanceUnits units)
|
|||
return distanceUnitsName_.at(units);
|
||||
}
|
||||
|
||||
float GetDistanceUnitsScale(DistanceUnits units)
|
||||
double GetDistanceUnitsScale(DistanceUnits units)
|
||||
{
|
||||
return distanceUnitsScale_.at(units);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ float GetSpeedUnitsScale(SpeedUnits units);
|
|||
const std::string& GetDistanceUnitsAbbreviation(DistanceUnits units);
|
||||
const std::string& GetDistanceUnitsName(DistanceUnits units);
|
||||
DistanceUnits GetDistanceUnitsFromName(const std::string& name);
|
||||
float GetDistanceUnitsScale(DistanceUnits units);
|
||||
double GetDistanceUnitsScale(DistanceUnits units);
|
||||
|
||||
} // namespace types
|
||||
} // namespace qt
|
||||
|
|
|
|||
|
|
@ -14,12 +14,14 @@
|
|||
#include <scwx/qt/settings/palette_settings.hpp>
|
||||
#include <scwx/qt/settings/settings_interface.hpp>
|
||||
#include <scwx/qt/settings/text_settings.hpp>
|
||||
#include <scwx/qt/settings/unit_settings.hpp>
|
||||
#include <scwx/qt/types/alert_types.hpp>
|
||||
#include <scwx/qt/types/font_types.hpp>
|
||||
#include <scwx/qt/types/location_types.hpp>
|
||||
#include <scwx/qt/types/qt_types.hpp>
|
||||
#include <scwx/qt/types/text_types.hpp>
|
||||
#include <scwx/qt/types/time_types.hpp>
|
||||
#include <scwx/qt/types/unit_types.hpp>
|
||||
#include <scwx/qt/ui/county_dialog.hpp>
|
||||
#include <scwx/qt/ui/radar_site_dialog.hpp>
|
||||
#include <scwx/qt/ui/serial_port_dialog.hpp>
|
||||
|
|
@ -999,6 +1001,23 @@ void SettingsDialogImpl::SetupAudioTab()
|
|||
alertAudioRadius_.SetEditWidget(self_->ui->alertAudioRadiusSpinBox);
|
||||
alertAudioRadius_.SetResetButton(
|
||||
self_->ui->resetAlertAudioRadiusButton);
|
||||
alertAudioRadius_.SetUnitLabel(self_->ui->alertAudioRadiusUnitsLabel);
|
||||
|
||||
auto alertAudioRadiusUpdateUnits = [this](const std::string& newValue)
|
||||
{
|
||||
types::DistanceUnits radiusUnits =
|
||||
types::GetDistanceUnitsFromName(newValue);
|
||||
double radiusScale = types::GetDistanceUnitsScale(radiusUnits);
|
||||
std::string abbreviation =
|
||||
types::GetDistanceUnitsAbbreviation(radiusUnits);
|
||||
|
||||
alertAudioRadius_.SetUnit(radiusScale, abbreviation);
|
||||
};
|
||||
settings::UnitSettings::Instance()
|
||||
.distance_units()
|
||||
.RegisterValueStagedCallback(alertAudioRadiusUpdateUnits);
|
||||
alertAudioRadiusUpdateUnits(
|
||||
settings::UnitSettings::Instance().distance_units().GetValue());
|
||||
|
||||
auto& alertAudioPhenomena = types::GetAlertAudioPhenomena();
|
||||
auto alertAudioLayout =
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@
|
|||
</sizepolicy>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>3</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="general">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
|
|
@ -135,9 +135,9 @@
|
|||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>-113</y>
|
||||
<width>511</width>
|
||||
<height>669</height>
|
||||
<y>0</y>
|
||||
<width>270</width>
|
||||
<height>647</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
|
|
@ -610,8 +610,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>98</width>
|
||||
<height>28</height>
|
||||
<width>80</width>
|
||||
<height>18</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
|
|
@ -883,7 +883,7 @@
|
|||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="label_28">
|
||||
<widget class="QLabel" name="alertAudioRadiusLabel">
|
||||
<property name="text">
|
||||
<string>Radius</string>
|
||||
</property>
|
||||
|
|
@ -892,7 +892,7 @@
|
|||
<item row="4" column="1" colspan="2">
|
||||
<widget class="QDoubleSpinBox" name="alertAudioRadiusSpinBox">
|
||||
<property name="decimals">
|
||||
<number>4</number>
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<double>0.000000000000000</double>
|
||||
|
|
@ -916,6 +916,13 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="3">
|
||||
<widget class="QLabel" name="alertAudioRadiusUnitsLabel">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue