mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 00:30:05 +00:00 
			
		
		
		
	Add tooltip method to settings
This commit is contained in:
		
							parent
							
								
									9ea3ed47a6
								
							
						
					
					
						commit
						23740d2601
					
				
					 9 changed files with 194 additions and 23 deletions
				
			
		|  | @ -1,4 +1,7 @@ | |||
| #include <scwx/qt/settings/text_settings.hpp> | ||||
| #include <scwx/qt/types/text_types.hpp> | ||||
| 
 | ||||
| #include <boost/algorithm/string.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
|  | @ -14,20 +17,48 @@ class TextSettings::Impl | |||
| public: | ||||
|    explicit Impl() | ||||
|    { | ||||
|       std::string defaultTooltipMethodValue = | ||||
|          types::GetTooltipMethodName(types::TooltipMethod::ImGui); | ||||
| 
 | ||||
|       boost::to_lower(defaultTooltipMethodValue); | ||||
| 
 | ||||
|       hoverTextWrap_.SetDefault(80); | ||||
|       hoverTextWrap_.SetMinimum(0); | ||||
|       hoverTextWrap_.SetMaximum(999); | ||||
|       tooltipMethod_.SetDefault(defaultTooltipMethodValue); | ||||
| 
 | ||||
|       tooltipMethod_.SetValidator( | ||||
|          [](const std::string& value) | ||||
|          { | ||||
|             for (types::TooltipMethod tooltipMethod : | ||||
|                  types::TooltipMethodIterator()) | ||||
|             { | ||||
|                // If the value is equal to a lower case alert action name
 | ||||
|                std::string tooltipMethodName = | ||||
|                   types::GetTooltipMethodName(tooltipMethod); | ||||
|                boost::to_lower(tooltipMethodName); | ||||
|                if (value == tooltipMethodName) | ||||
|                { | ||||
|                   // Regard as a match, valid
 | ||||
|                   return true; | ||||
|                } | ||||
|             } | ||||
| 
 | ||||
|             // No match found, invalid
 | ||||
|             return false; | ||||
|          }); | ||||
|    } | ||||
| 
 | ||||
|    ~Impl() {} | ||||
| 
 | ||||
|    SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"}; | ||||
|    SettingsVariable<std::string>  tooltipMethod_ {"tooltip_method"}; | ||||
| }; | ||||
| 
 | ||||
| TextSettings::TextSettings() : | ||||
|     SettingsCategory("text"), p(std::make_unique<Impl>()) | ||||
| { | ||||
|    RegisterVariables({&p->hoverTextWrap_}); | ||||
|    RegisterVariables({&p->hoverTextWrap_, &p->tooltipMethod_}); | ||||
|    SetDefaults(); | ||||
| } | ||||
| TextSettings::~TextSettings() = default; | ||||
|  | @ -40,6 +71,11 @@ SettingsVariable<std::int64_t>& TextSettings::hover_text_wrap() const | |||
|    return p->hoverTextWrap_; | ||||
| } | ||||
| 
 | ||||
| SettingsVariable<std::string>& TextSettings::tooltip_method() const | ||||
| { | ||||
|    return p->tooltipMethod_; | ||||
| } | ||||
| 
 | ||||
| TextSettings& TextSettings::Instance() | ||||
| { | ||||
|    static TextSettings TextSettings_; | ||||
|  | @ -48,7 +84,8 @@ TextSettings& TextSettings::Instance() | |||
| 
 | ||||
| bool operator==(const TextSettings& lhs, const TextSettings& rhs) | ||||
| { | ||||
|    return (lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_); | ||||
|    return (lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_ && | ||||
|            lhs.p->tooltipMethod_ == rhs.p->tooltipMethod_); | ||||
| } | ||||
| 
 | ||||
| } // namespace settings
 | ||||
|  |  | |||
|  | @ -26,6 +26,7 @@ public: | |||
|    TextSettings& operator=(TextSettings&&) noexcept; | ||||
| 
 | ||||
|    SettingsVariable<std::int64_t>& hover_text_wrap() const; | ||||
|    SettingsVariable<std::string>& tooltip_method() const; | ||||
| 
 | ||||
|    static TextSettings& Instance(); | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										45
									
								
								scwx-qt/source/scwx/qt/types/text_types.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								scwx-qt/source/scwx/qt/types/text_types.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,45 @@ | |||
| #include <scwx/qt/types/text_types.hpp> | ||||
| 
 | ||||
| #include <unordered_map> | ||||
| 
 | ||||
| #include <boost/algorithm/string.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace types | ||||
| { | ||||
| 
 | ||||
| static const std::unordered_map<TooltipMethod, std::string> tooltipMethodName_ { | ||||
|    {TooltipMethod::ImGui, "ImGui"}, | ||||
|    {TooltipMethod::QToolTip, "Native Tooltip"}, | ||||
|    {TooltipMethod::QLabel, "Floating Label"}, | ||||
|    {TooltipMethod::Unknown, "?"}}; | ||||
| 
 | ||||
| TooltipMethod GetTooltipMethod(const std::string& name) | ||||
| { | ||||
|    auto result = std::find_if( | ||||
|       tooltipMethodName_.cbegin(), | ||||
|       tooltipMethodName_.cend(), | ||||
|       [&](const std::pair<TooltipMethod, std::string>& pair) -> bool | ||||
|       { return boost::iequals(pair.second, name); }); | ||||
| 
 | ||||
|    if (result != tooltipMethodName_.cend()) | ||||
|    { | ||||
|       return result->first; | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       return TooltipMethod::Unknown; | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| std::string GetTooltipMethodName(TooltipMethod tooltipMethod) | ||||
| { | ||||
|    return tooltipMethodName_.at(tooltipMethod); | ||||
| } | ||||
| 
 | ||||
| } // namespace types
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										30
									
								
								scwx-qt/source/scwx/qt/types/text_types.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								scwx-qt/source/scwx/qt/types/text_types.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <scwx/util/iterator.hpp> | ||||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace types | ||||
| { | ||||
| 
 | ||||
| enum class TooltipMethod | ||||
| { | ||||
|    ImGui, | ||||
|    QToolTip, | ||||
|    QLabel, | ||||
|    Unknown | ||||
| }; | ||||
| typedef scwx::util:: | ||||
|    Iterator<TooltipMethod, TooltipMethod::ImGui, TooltipMethod::QLabel> | ||||
|       TooltipMethodIterator; | ||||
| 
 | ||||
| TooltipMethod GetTooltipMethod(const std::string& name); | ||||
| std::string   GetTooltipMethodName(TooltipMethod tooltipMethod); | ||||
| 
 | ||||
| } // namespace types
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
|  | @ -9,6 +9,7 @@ | |||
| #include <scwx/qt/settings/settings_interface.hpp> | ||||
| #include <scwx/qt/settings/text_settings.hpp> | ||||
| #include <scwx/qt/types/alert_types.hpp> | ||||
| #include <scwx/qt/types/text_types.hpp> | ||||
| #include <scwx/qt/ui/placefile_settings_widget.hpp> | ||||
| #include <scwx/qt/ui/radar_site_dialog.hpp> | ||||
| #include <scwx/qt/util/color.hpp> | ||||
|  | @ -94,7 +95,8 @@ public: | |||
|           &defaultAlertAction_, | ||||
|           &updateNotificationsEnabled_, | ||||
|           &debugEnabled_, | ||||
|           &hoverTextWrap_}} | ||||
|           &hoverTextWrap_, | ||||
|           &tooltipMethod_}} | ||||
|    { | ||||
|       // Configure default alert phenomena colors
 | ||||
|       auto& paletteSettings = manager::SettingsManager::palette_settings(); | ||||
|  | @ -166,6 +168,7 @@ public: | |||
|       inactiveAlertColors_ {}; | ||||
| 
 | ||||
|    settings::SettingsInterface<std::int64_t> hoverTextWrap_ {}; | ||||
|    settings::SettingsInterface<std::string>  tooltipMethod_ {}; | ||||
| 
 | ||||
|    std::vector<settings::SettingsInterfaceBase*> settings_; | ||||
| }; | ||||
|  | @ -645,6 +648,42 @@ void SettingsDialogImpl::SetupTextTab() | |||
|    hoverTextWrap_.SetSettingsVariable(textSettings.hover_text_wrap()); | ||||
|    hoverTextWrap_.SetEditWidget(self_->ui->hoverTextWrapSpinBox); | ||||
|    hoverTextWrap_.SetResetButton(self_->ui->resetHoverTextWrapButton); | ||||
| 
 | ||||
|    for (const auto& tooltipMethod : types::TooltipMethodIterator()) | ||||
|    { | ||||
|       self_->ui->tooltipMethodComboBox->addItem( | ||||
|          QString::fromStdString(types::GetTooltipMethodName(tooltipMethod))); | ||||
|    } | ||||
| 
 | ||||
|    tooltipMethod_.SetSettingsVariable(textSettings.tooltip_method()); | ||||
|    tooltipMethod_.SetMapFromValueFunction( | ||||
|       [](const std::string& text) -> std::string | ||||
|       { | ||||
|          for (types::TooltipMethod tooltipMethod : | ||||
|               types::TooltipMethodIterator()) | ||||
|          { | ||||
|             const std::string tooltipMethodName = | ||||
|                types::GetTooltipMethodName(tooltipMethod); | ||||
| 
 | ||||
|             if (boost::iequals(text, tooltipMethodName)) | ||||
|             { | ||||
|                // Return tooltip method label
 | ||||
|                return tooltipMethodName; | ||||
|             } | ||||
|          } | ||||
| 
 | ||||
|          // Tooltip method label not found, return unknown
 | ||||
|          return "?"; | ||||
|       }); | ||||
|    tooltipMethod_.SetMapToValueFunction( | ||||
|       [](std::string text) -> std::string | ||||
|       { | ||||
|          // Convert label to lower case and return
 | ||||
|          boost::to_lower(text); | ||||
|          return text; | ||||
|       }); | ||||
|    tooltipMethod_.SetEditWidget(self_->ui->tooltipMethodComboBox); | ||||
|    tooltipMethod_.SetResetButton(self_->ui->resetTooltipMethodButton); | ||||
| } | ||||
| 
 | ||||
| QImage SettingsDialogImpl::GenerateColorTableImage( | ||||
|  |  | |||
|  | @ -459,21 +459,21 @@ | |||
|              <property name="bottomMargin"> | ||||
|               <number>0</number> | ||||
|              </property> | ||||
|              <item row="0" column="0"> | ||||
|              <item row="1" column="0"> | ||||
|               <widget class="QLabel" name="label_8"> | ||||
|                <property name="text"> | ||||
|                 <string>Hover text character wrap (0 to disable)</string> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="0" column="1"> | ||||
|              <item row="1" column="1"> | ||||
|               <widget class="QSpinBox" name="hoverTextWrapSpinBox"> | ||||
|                <property name="maximum"> | ||||
|                 <number>999</number> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="0" column="2"> | ||||
|              <item row="1" column="2"> | ||||
|               <widget class="QToolButton" name="resetHoverTextWrapButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|  | @ -484,6 +484,27 @@ | |||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="0" column="0"> | ||||
|               <widget class="QLabel" name="label_9"> | ||||
|                <property name="text"> | ||||
|                 <string>Tooltip Method</string> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="0" column="1"> | ||||
|               <widget class="QComboBox" name="tooltipMethodComboBox"/> | ||||
|              </item> | ||||
|              <item row="0" column="2"> | ||||
|               <widget class="QToolButton" name="resetTooltipMethodButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|                </property> | ||||
|                <property name="icon"> | ||||
|                 <iconset resource="../../../../scwx-qt.qrc"> | ||||
|                  <normaloff>:/res/icons/font-awesome-6/rotate-left-solid.svg</normaloff>:/res/icons/font-awesome-6/rotate-left-solid.svg</iconset> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|             </layout> | ||||
|            </widget> | ||||
|           </item> | ||||
|  |  | |||
|  | @ -2,6 +2,7 @@ | |||
| #include <scwx/qt/manager/settings_manager.hpp> | ||||
| #include <scwx/qt/settings/text_settings.hpp> | ||||
| #include <scwx/qt/types/font_types.hpp> | ||||
| #include <scwx/qt/types/text_types.hpp> | ||||
| #include <scwx/qt/util/imgui.hpp> | ||||
| #include <scwx/util/logger.hpp> | ||||
| 
 | ||||
|  | @ -23,15 +24,6 @@ namespace tooltip | |||
| static const std::string logPrefix_ = "scwx::qt::util::tooltip"; | ||||
| static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||
| 
 | ||||
| enum class TooltipMethod | ||||
| { | ||||
|    ImGui, | ||||
|    QToolTip, | ||||
|    QLabel | ||||
| }; | ||||
| 
 | ||||
| static TooltipMethod tooltipMethod_ = TooltipMethod::ImGui; | ||||
| 
 | ||||
| static std::unique_ptr<QLabel>  tooltipLabel_  = nullptr; | ||||
| static std::unique_ptr<QWidget> tooltipParent_ = nullptr; | ||||
| 
 | ||||
|  | @ -68,8 +60,12 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos) | |||
| { | ||||
|    Initialize(); | ||||
| 
 | ||||
|    std::size_t textWidth = static_cast<std::size_t>( | ||||
|       settings::TextSettings::Instance().hover_text_wrap().GetValue()); | ||||
|    auto& textSettings = settings::TextSettings::Instance(); | ||||
| 
 | ||||
|    std::size_t textWidth = | ||||
|       static_cast<std::size_t>(textSettings.hover_text_wrap().GetValue()); | ||||
|    types::TooltipMethod tooltipMethod = | ||||
|       types::GetTooltipMethod(textSettings.tooltip_method().GetValue()); | ||||
| 
 | ||||
|    // Wrap text if enabled
 | ||||
|    std::string wrappedText {}; | ||||
|  | @ -82,11 +78,11 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos) | |||
|    // when not wrapping)
 | ||||
|    const std::string& displayText = (textWidth > 0) ? wrappedText : text; | ||||
| 
 | ||||
|    if (tooltipMethod_ == TooltipMethod::ImGui) | ||||
|    if (tooltipMethod == types::TooltipMethod::ImGui) | ||||
|    { | ||||
|       util::ImGui::Instance().DrawTooltip(displayText); | ||||
|    } | ||||
|    else if (tooltipMethod_ == TooltipMethod::QToolTip) | ||||
|    else if (tooltipMethod == types::TooltipMethod::QToolTip) | ||||
|    { | ||||
|       static std::size_t id = 0; | ||||
|       QToolTip::showText( | ||||
|  | @ -99,7 +95,7 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos) | |||
|          {}, | ||||
|          std::numeric_limits<int>::max()); | ||||
|    } | ||||
|    else if (tooltipMethod_ == TooltipMethod::QLabel) | ||||
|    else if (tooltipMethod == types::TooltipMethod::QLabel) | ||||
|    { | ||||
|       // Get monospace font size
 | ||||
|       units::font_size::pixels<double> fontSize {16}; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat