Add tooltip method to settings

This commit is contained in:
Dan Paulat 2023-09-14 23:53:11 -05:00
parent 9ea3ed47a6
commit 23740d2601
9 changed files with 194 additions and 23 deletions

View file

@ -160,12 +160,14 @@ set(HDR_TYPES source/scwx/qt/types/alert_types.hpp
source/scwx/qt/types/map_types.hpp source/scwx/qt/types/map_types.hpp
source/scwx/qt/types/qt_types.hpp source/scwx/qt/types/qt_types.hpp
source/scwx/qt/types/radar_product_record.hpp source/scwx/qt/types/radar_product_record.hpp
source/scwx/qt/types/text_event_key.hpp) source/scwx/qt/types/text_event_key.hpp
source/scwx/qt/types/text_types.hpp)
set(SRC_TYPES source/scwx/qt/types/alert_types.cpp set(SRC_TYPES source/scwx/qt/types/alert_types.cpp
source/scwx/qt/types/github_types.cpp source/scwx/qt/types/github_types.cpp
source/scwx/qt/types/map_types.cpp source/scwx/qt/types/map_types.cpp
source/scwx/qt/types/radar_product_record.cpp source/scwx/qt/types/radar_product_record.cpp
source/scwx/qt/types/text_event_key.cpp) source/scwx/qt/types/text_event_key.cpp
source/scwx/qt/types/text_types.cpp)
set(HDR_UI source/scwx/qt/ui/about_dialog.hpp set(HDR_UI source/scwx/qt/ui/about_dialog.hpp
source/scwx/qt/ui/alert_dialog.hpp source/scwx/qt/ui/alert_dialog.hpp
source/scwx/qt/ui/alert_dock_widget.hpp source/scwx/qt/ui/alert_dock_widget.hpp

View file

@ -1,4 +1,7 @@
#include <scwx/qt/settings/text_settings.hpp> #include <scwx/qt/settings/text_settings.hpp>
#include <scwx/qt/types/text_types.hpp>
#include <boost/algorithm/string.hpp>
namespace scwx namespace scwx
{ {
@ -14,20 +17,48 @@ class TextSettings::Impl
public: public:
explicit Impl() explicit Impl()
{ {
std::string defaultTooltipMethodValue =
types::GetTooltipMethodName(types::TooltipMethod::ImGui);
boost::to_lower(defaultTooltipMethodValue);
hoverTextWrap_.SetDefault(80); hoverTextWrap_.SetDefault(80);
hoverTextWrap_.SetMinimum(0); hoverTextWrap_.SetMinimum(0);
hoverTextWrap_.SetMaximum(999); 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() {} ~Impl() {}
SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"}; SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"};
SettingsVariable<std::string> tooltipMethod_ {"tooltip_method"};
}; };
TextSettings::TextSettings() : TextSettings::TextSettings() :
SettingsCategory("text"), p(std::make_unique<Impl>()) SettingsCategory("text"), p(std::make_unique<Impl>())
{ {
RegisterVariables({&p->hoverTextWrap_}); RegisterVariables({&p->hoverTextWrap_, &p->tooltipMethod_});
SetDefaults(); SetDefaults();
} }
TextSettings::~TextSettings() = default; TextSettings::~TextSettings() = default;
@ -40,6 +71,11 @@ SettingsVariable<std::int64_t>& TextSettings::hover_text_wrap() const
return p->hoverTextWrap_; return p->hoverTextWrap_;
} }
SettingsVariable<std::string>& TextSettings::tooltip_method() const
{
return p->tooltipMethod_;
}
TextSettings& TextSettings::Instance() TextSettings& TextSettings::Instance()
{ {
static TextSettings TextSettings_; static TextSettings TextSettings_;
@ -48,7 +84,8 @@ TextSettings& TextSettings::Instance()
bool operator==(const TextSettings& lhs, const TextSettings& rhs) 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 } // namespace settings

View file

@ -26,6 +26,7 @@ public:
TextSettings& operator=(TextSettings&&) noexcept; TextSettings& operator=(TextSettings&&) noexcept;
SettingsVariable<std::int64_t>& hover_text_wrap() const; SettingsVariable<std::int64_t>& hover_text_wrap() const;
SettingsVariable<std::string>& tooltip_method() const;
static TextSettings& Instance(); static TextSettings& Instance();

View 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

View 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

View file

@ -9,6 +9,7 @@
#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/types/alert_types.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/placefile_settings_widget.hpp>
#include <scwx/qt/ui/radar_site_dialog.hpp> #include <scwx/qt/ui/radar_site_dialog.hpp>
#include <scwx/qt/util/color.hpp> #include <scwx/qt/util/color.hpp>
@ -94,7 +95,8 @@ public:
&defaultAlertAction_, &defaultAlertAction_,
&updateNotificationsEnabled_, &updateNotificationsEnabled_,
&debugEnabled_, &debugEnabled_,
&hoverTextWrap_}} &hoverTextWrap_,
&tooltipMethod_}}
{ {
// Configure default alert phenomena colors // Configure default alert phenomena colors
auto& paletteSettings = manager::SettingsManager::palette_settings(); auto& paletteSettings = manager::SettingsManager::palette_settings();
@ -166,6 +168,7 @@ public:
inactiveAlertColors_ {}; inactiveAlertColors_ {};
settings::SettingsInterface<std::int64_t> hoverTextWrap_ {}; settings::SettingsInterface<std::int64_t> hoverTextWrap_ {};
settings::SettingsInterface<std::string> tooltipMethod_ {};
std::vector<settings::SettingsInterfaceBase*> settings_; std::vector<settings::SettingsInterfaceBase*> settings_;
}; };
@ -645,6 +648,42 @@ void SettingsDialogImpl::SetupTextTab()
hoverTextWrap_.SetSettingsVariable(textSettings.hover_text_wrap()); hoverTextWrap_.SetSettingsVariable(textSettings.hover_text_wrap());
hoverTextWrap_.SetEditWidget(self_->ui->hoverTextWrapSpinBox); hoverTextWrap_.SetEditWidget(self_->ui->hoverTextWrapSpinBox);
hoverTextWrap_.SetResetButton(self_->ui->resetHoverTextWrapButton); 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( QImage SettingsDialogImpl::GenerateColorTableImage(

View file

@ -459,21 +459,21 @@
<property name="bottomMargin"> <property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item row="0" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_8"> <widget class="QLabel" name="label_8">
<property name="text"> <property name="text">
<string>Hover text character wrap (0 to disable)</string> <string>Hover text character wrap (0 to disable)</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="1" column="1">
<widget class="QSpinBox" name="hoverTextWrapSpinBox"> <widget class="QSpinBox" name="hoverTextWrapSpinBox">
<property name="maximum"> <property name="maximum">
<number>999</number> <number>999</number>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="2"> <item row="1" column="2">
<widget class="QToolButton" name="resetHoverTextWrapButton"> <widget class="QToolButton" name="resetHoverTextWrapButton">
<property name="text"> <property name="text">
<string>...</string> <string>...</string>
@ -484,6 +484,27 @@
</property> </property>
</widget> </widget>
</item> </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> </layout>
</widget> </widget>
</item> </item>

View file

@ -2,6 +2,7 @@
#include <scwx/qt/manager/settings_manager.hpp> #include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/qt/settings/text_settings.hpp> #include <scwx/qt/settings/text_settings.hpp>
#include <scwx/qt/types/font_types.hpp> #include <scwx/qt/types/font_types.hpp>
#include <scwx/qt/types/text_types.hpp>
#include <scwx/qt/util/imgui.hpp> #include <scwx/qt/util/imgui.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
@ -23,15 +24,6 @@ namespace tooltip
static const std::string logPrefix_ = "scwx::qt::util::tooltip"; static const std::string logPrefix_ = "scwx::qt::util::tooltip";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_); 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<QLabel> tooltipLabel_ = nullptr;
static std::unique_ptr<QWidget> tooltipParent_ = nullptr; static std::unique_ptr<QWidget> tooltipParent_ = nullptr;
@ -68,8 +60,12 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
{ {
Initialize(); Initialize();
std::size_t textWidth = static_cast<std::size_t>( auto& textSettings = settings::TextSettings::Instance();
settings::TextSettings::Instance().hover_text_wrap().GetValue());
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 // Wrap text if enabled
std::string wrappedText {}; std::string wrappedText {};
@ -82,11 +78,11 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
// when not wrapping) // when not wrapping)
const std::string& displayText = (textWidth > 0) ? wrappedText : text; const std::string& displayText = (textWidth > 0) ? wrappedText : text;
if (tooltipMethod_ == TooltipMethod::ImGui) if (tooltipMethod == types::TooltipMethod::ImGui)
{ {
util::ImGui::Instance().DrawTooltip(displayText); util::ImGui::Instance().DrawTooltip(displayText);
} }
else if (tooltipMethod_ == TooltipMethod::QToolTip) else if (tooltipMethod == types::TooltipMethod::QToolTip)
{ {
static std::size_t id = 0; static std::size_t id = 0;
QToolTip::showText( QToolTip::showText(
@ -99,7 +95,7 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
{}, {},
std::numeric_limits<int>::max()); std::numeric_limits<int>::max());
} }
else if (tooltipMethod_ == TooltipMethod::QLabel) else if (tooltipMethod == types::TooltipMethod::QLabel)
{ {
// Get monospace font size // Get monospace font size
units::font_size::pixels<double> fontSize {16}; units::font_size::pixels<double> fontSize {16};

@ -1 +1 @@
Subproject commit 6d407be1b6f2e72490ef0d07da1e297994df8fe4 Subproject commit 33caca188b1007c643db75afa560fdfe348c0ee5