mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 15:40:06 +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/util/logger.hpp> | ||||||
| #include <scwx/qt/config/radar_site.hpp> | #include <scwx/qt/config/radar_site.hpp> | ||||||
| #include <scwx/qt/settings/general_settings.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/post.hpp> | ||||||
| #include <boost/asio/thread_pool.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::AudioSettings& audioSettings = settings::AudioSettings::Instance(); | ||||||
|    settings::UnitSettings&  unitSettings  = settings::UnitSettings::Instance(); |  | ||||||
|    types::LocationMethod    locationMethod = types::GetLocationMethod( |    types::LocationMethod    locationMethod = types::GetLocationMethod( | ||||||
|       audioSettings.alert_location_method().GetValue()); |       audioSettings.alert_location_method().GetValue()); | ||||||
|    common::Coordinate currentCoordinate = CurrentCoordinate(locationMethod); |    common::Coordinate currentCoordinate = CurrentCoordinate(locationMethod); | ||||||
|    std::string        alertCounty = audioSettings.alert_county().GetValue(); |    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>( |    auto               alertRadius = units::length::kilometers<double>( | ||||||
|       audioSettings.alert_radius().GetValue() / radiusScale); |       audioSettings.alert_radius().GetValue()); | ||||||
| 
 | 
 | ||||||
|    logger_->debug("alertRadius: {}", (double)alertRadius); |    logger_->debug("alertRadius: {}", (double)alertRadius); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -40,6 +40,7 @@ public: | ||||||
| 
 | 
 | ||||||
|    void UpdateEditWidget(); |    void UpdateEditWidget(); | ||||||
|    void UpdateResetButton(); |    void UpdateResetButton(); | ||||||
|  |    void UpdateUnitLabel(); | ||||||
| 
 | 
 | ||||||
|    SettingsInterface<T>* self_; |    SettingsInterface<T>* self_; | ||||||
| 
 | 
 | ||||||
|  | @ -49,9 +50,14 @@ public: | ||||||
|    std::unique_ptr<QObject> context_ {std::make_unique<QObject>()}; |    std::unique_ptr<QObject> context_ {std::make_unique<QObject>()}; | ||||||
|    QWidget*                 editWidget_ {nullptr}; |    QWidget*                 editWidget_ {nullptr}; | ||||||
|    QAbstractButton*         resetButton_ {nullptr}; |    QAbstractButton*         resetButton_ {nullptr}; | ||||||
|  |    QLabel*                  unitLabel_ {nullptr}; | ||||||
| 
 | 
 | ||||||
|    std::function<std::string(const T&)> mapFromValue_ {nullptr}; |    std::function<std::string(const T&)> mapFromValue_ {nullptr}; | ||||||
|    std::function<T(const std::string&)> mapToValue_ {nullptr}; |    std::function<T(const std::string&)> mapToValue_ {nullptr}; | ||||||
|  | 
 | ||||||
|  |    double unitScale_ {1}; | ||||||
|  |    const std::string * unitAbbreiation_ {nullptr}; | ||||||
|  |    bool unitEnabled_ {false}; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| template<class T> | template<class T> | ||||||
|  | @ -381,6 +387,11 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget) | ||||||
|             p->context_.get(), |             p->context_.get(), | ||||||
|             [this](double d) |             [this](double d) | ||||||
|             { |             { | ||||||
|  |                if (p->unitEnabled_) | ||||||
|  |                { | ||||||
|  |                   d = d / p->unitScale_; | ||||||
|  |                } | ||||||
|  | 
 | ||||||
|                const T                value  = p->variable_->GetValue(); |                const T                value  = p->variable_->GetValue(); | ||||||
|                const std::optional<T> staged = p->variable_->GetStaged(); |                const std::optional<T> staged = p->variable_->GetStaged(); | ||||||
| 
 | 
 | ||||||
|  | @ -448,6 +459,11 @@ void SettingsInterface<T>::SetResetButton(QAbstractButton* button) | ||||||
|       p->UpdateResetButton(); |       p->UpdateResetButton(); | ||||||
|    } |    } | ||||||
| } | } | ||||||
|  | template<class T> | ||||||
|  | void SettingsInterface<T>::SetUnitLabel(QLabel* label) | ||||||
|  | { | ||||||
|  |    p->unitLabel_ = label; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| template<class T> | template<class T> | ||||||
| void SettingsInterface<T>::SetMapFromValueFunction( | void SettingsInterface<T>::SetMapFromValueFunction( | ||||||
|  | @ -463,6 +479,17 @@ void SettingsInterface<T>::SetMapToValueFunction( | ||||||
|    p->mapToValue_ = function; |    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 T> | ||||||
| template<class U> | template<class U> | ||||||
| void SettingsInterface<T>::Impl::SetWidgetText(U* widget, const T& currentValue) | 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>) |       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> | template<class T> | ||||||
| void SettingsInterface<T>::Impl::UpdateResetButton() | void SettingsInterface<T>::Impl::UpdateResetButton() | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -7,6 +7,8 @@ | ||||||
| #include <string> | #include <string> | ||||||
| #include <vector> | #include <vector> | ||||||
| 
 | 
 | ||||||
|  | class QLabel; | ||||||
|  | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
| { | { | ||||||
| namespace qt | namespace qt | ||||||
|  | @ -91,6 +93,13 @@ public: | ||||||
|     */ |     */ | ||||||
|    void SetResetButton(QAbstractButton* button) override; |    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 |     * 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 |     * 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); |    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: | private: | ||||||
|    class Impl; |    class Impl; | ||||||
|    std::unique_ptr<Impl> p; |    std::unique_ptr<Impl> p; | ||||||
|  |  | ||||||
|  | @ -101,12 +101,12 @@ static const std::unordered_map<DistanceUnits, std::string> distanceUnitsName_ { | ||||||
|    {DistanceUnits::User, "User-defined"}, |    {DistanceUnits::User, "User-defined"}, | ||||||
|    {DistanceUnits::Unknown, "?"}}; |    {DistanceUnits::Unknown, "?"}}; | ||||||
| 
 | 
 | ||||||
| static constexpr auto distanceUnitsBase_ = units::kilometers<float> {1.0f}; | static constexpr auto distanceUnitsBase_ = units::kilometers<double> {1.0f}; | ||||||
| static const std::unordered_map<DistanceUnits, float> distanceUnitsScale_ { | static const std::unordered_map<DistanceUnits, double> distanceUnitsScale_ { | ||||||
|    {DistanceUnits::Kilometers, |    {DistanceUnits::Kilometers, | ||||||
|     (distanceUnitsBase_ / units::kilometers<float> {1.0f})}, |     (distanceUnitsBase_ / units::kilometers<double> {1.0f})}, | ||||||
|    {DistanceUnits::Miles, |    {DistanceUnits::Miles, | ||||||
|     (distanceUnitsBase_ / units::miles<float> {1.0f})}, |     (distanceUnitsBase_ / units::miles<double> {1.0f})}, | ||||||
|    {DistanceUnits::User, 1.0f}, |    {DistanceUnits::User, 1.0f}, | ||||||
|    {DistanceUnits::Unknown, 1.0f}}; |    {DistanceUnits::Unknown, 1.0f}}; | ||||||
| 
 | 
 | ||||||
|  | @ -179,7 +179,7 @@ const std::string& GetDistanceUnitsName(DistanceUnits units) | ||||||
|    return distanceUnitsName_.at(units); |    return distanceUnitsName_.at(units); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| float GetDistanceUnitsScale(DistanceUnits units) | double GetDistanceUnitsScale(DistanceUnits units) | ||||||
| { | { | ||||||
|    return distanceUnitsScale_.at(units); |    return distanceUnitsScale_.at(units); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -88,7 +88,7 @@ float              GetSpeedUnitsScale(SpeedUnits units); | ||||||
| const std::string& GetDistanceUnitsAbbreviation(DistanceUnits units); | const std::string& GetDistanceUnitsAbbreviation(DistanceUnits units); | ||||||
| const std::string& GetDistanceUnitsName(DistanceUnits units); | const std::string& GetDistanceUnitsName(DistanceUnits units); | ||||||
| DistanceUnits      GetDistanceUnitsFromName(const std::string& name); | DistanceUnits      GetDistanceUnitsFromName(const std::string& name); | ||||||
| float              GetDistanceUnitsScale(DistanceUnits units); | double             GetDistanceUnitsScale(DistanceUnits units); | ||||||
| 
 | 
 | ||||||
| } // namespace types
 | } // namespace types
 | ||||||
| } // namespace qt
 | } // namespace qt
 | ||||||
|  |  | ||||||
|  | @ -14,12 +14,14 @@ | ||||||
| #include <scwx/qt/settings/palette_settings.hpp> | #include <scwx/qt/settings/palette_settings.hpp> | ||||||
| #include <scwx/qt/settings/settings_interface.hpp> | #include <scwx/qt/settings/settings_interface.hpp> | ||||||
| #include <scwx/qt/settings/text_settings.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/alert_types.hpp> | ||||||
| #include <scwx/qt/types/font_types.hpp> | #include <scwx/qt/types/font_types.hpp> | ||||||
| #include <scwx/qt/types/location_types.hpp> | #include <scwx/qt/types/location_types.hpp> | ||||||
| #include <scwx/qt/types/qt_types.hpp> | #include <scwx/qt/types/qt_types.hpp> | ||||||
| #include <scwx/qt/types/text_types.hpp> | #include <scwx/qt/types/text_types.hpp> | ||||||
| #include <scwx/qt/types/time_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/county_dialog.hpp> | ||||||
| #include <scwx/qt/ui/radar_site_dialog.hpp> | #include <scwx/qt/ui/radar_site_dialog.hpp> | ||||||
| #include <scwx/qt/ui/serial_port_dialog.hpp> | #include <scwx/qt/ui/serial_port_dialog.hpp> | ||||||
|  | @ -999,6 +1001,23 @@ void SettingsDialogImpl::SetupAudioTab() | ||||||
|    alertAudioRadius_.SetEditWidget(self_->ui->alertAudioRadiusSpinBox); |    alertAudioRadius_.SetEditWidget(self_->ui->alertAudioRadiusSpinBox); | ||||||
|    alertAudioRadius_.SetResetButton( |    alertAudioRadius_.SetResetButton( | ||||||
|       self_->ui->resetAlertAudioRadiusButton); |       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& alertAudioPhenomena = types::GetAlertAudioPhenomena(); | ||||||
|    auto  alertAudioLayout = |    auto  alertAudioLayout = | ||||||
|  |  | ||||||
|  | @ -122,7 +122,7 @@ | ||||||
|           </sizepolicy> |           </sizepolicy> | ||||||
|          </property> |          </property> | ||||||
|          <property name="currentIndex"> |          <property name="currentIndex"> | ||||||
|           <number>0</number> |           <number>3</number> | ||||||
|          </property> |          </property> | ||||||
|          <widget class="QWidget" name="general"> |          <widget class="QWidget" name="general"> | ||||||
|           <layout class="QVBoxLayout" name="verticalLayout_2"> |           <layout class="QVBoxLayout" name="verticalLayout_2"> | ||||||
|  | @ -135,9 +135,9 @@ | ||||||
|               <property name="geometry"> |               <property name="geometry"> | ||||||
|                <rect> |                <rect> | ||||||
|                 <x>0</x> |                 <x>0</x> | ||||||
|                 <y>-113</y> |                 <y>0</y> | ||||||
|                 <width>511</width> |                 <width>270</width> | ||||||
|                 <height>669</height> |                 <height>647</height> | ||||||
|                </rect> |                </rect> | ||||||
|               </property> |               </property> | ||||||
|               <layout class="QVBoxLayout" name="verticalLayout"> |               <layout class="QVBoxLayout" name="verticalLayout"> | ||||||
|  | @ -610,8 +610,8 @@ | ||||||
|                    <rect> |                    <rect> | ||||||
|                     <x>0</x> |                     <x>0</x> | ||||||
|                     <y>0</y> |                     <y>0</y> | ||||||
|                     <width>98</width> |                     <width>80</width> | ||||||
|                     <height>28</height> |                     <height>18</height> | ||||||
|                    </rect> |                    </rect> | ||||||
|                   </property> |                   </property> | ||||||
|                   <layout class="QGridLayout" name="gridLayout_3"> |                   <layout class="QGridLayout" name="gridLayout_3"> | ||||||
|  | @ -883,7 +883,7 @@ | ||||||
|                </widget> |                </widget> | ||||||
|               </item> |               </item> | ||||||
|               <item row="4" column="0"> |               <item row="4" column="0"> | ||||||
|                <widget class="QLabel" name="label_28"> |                <widget class="QLabel" name="alertAudioRadiusLabel"> | ||||||
|                 <property name="text"> |                 <property name="text"> | ||||||
|                  <string>Radius</string> |                  <string>Radius</string> | ||||||
|                 </property> |                 </property> | ||||||
|  | @ -892,7 +892,7 @@ | ||||||
|               <item row="4" column="1" colspan="2"> |               <item row="4" column="1" colspan="2"> | ||||||
|                <widget class="QDoubleSpinBox" name="alertAudioRadiusSpinBox"> |                <widget class="QDoubleSpinBox" name="alertAudioRadiusSpinBox"> | ||||||
|                 <property name="decimals"> |                 <property name="decimals"> | ||||||
|                  <number>4</number> |                  <number>2</number> | ||||||
|                 </property> |                 </property> | ||||||
|                 <property name="minimum"> |                 <property name="minimum"> | ||||||
|                  <double>0.000000000000000</double> |                  <double>0.000000000000000</double> | ||||||
|  | @ -916,6 +916,13 @@ | ||||||
|                 </property> |                 </property> | ||||||
|                </widget> |                </widget> | ||||||
|               </item> |               </item> | ||||||
|  |               <item row="4" column="3"> | ||||||
|  |                <widget class="QLabel" name="alertAudioRadiusUnitsLabel"> | ||||||
|  |                 <property name="text"> | ||||||
|  |                  <string/> | ||||||
|  |                 </property> | ||||||
|  |                </widget> | ||||||
|  |               </item> | ||||||
|              </layout> |              </layout> | ||||||
|             </widget> |             </widget> | ||||||
|            </item> |            </item> | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 AdenKoperczak
						AdenKoperczak