Tooltip styling updates

This commit is contained in:
Dan Paulat 2023-09-12 22:58:17 -05:00
parent df4500478c
commit 10a1755056

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/util/imgui.hpp> #include <scwx/qt/util/imgui.hpp>
#include <scwx/util/logger.hpp>
#include <TextFlow.hpp> #include <TextFlow.hpp>
#include <QGuiApplication> #include <QGuiApplication>
@ -19,6 +20,7 @@ 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_);
enum class TooltipMethod enum class TooltipMethod
{ {
@ -29,7 +31,8 @@ enum class TooltipMethod
static TooltipMethod tooltipMethod_ = TooltipMethod::ImGui; static TooltipMethod tooltipMethod_ = TooltipMethod::ImGui;
static std::unique_ptr<QLabel> labelTooltip_ = nullptr; static std::unique_ptr<QLabel> tooltipLabel_ = nullptr;
static std::unique_ptr<QWidget> tooltipParent_ = nullptr;
void Initialize() void Initialize()
{ {
@ -40,11 +43,19 @@ void Initialize()
return; return;
} }
labelTooltip_ = std::make_unique<QLabel>(); tooltipParent_ = std::make_unique<QWidget>();
labelTooltip_->setWindowFlag(Qt::ToolTip); tooltipParent_->setStyleSheet(
labelTooltip_->setMargin(6); "QToolTip"
labelTooltip_->setAttribute(Qt::WidgetAttribute::WA_TranslucentBackground); "{"
labelTooltip_->setStyleSheet( "background-color: rgba(15, 15, 15, 191);"
"border: 1px solid rgba(110, 110, 128, 128);"
"color: rgba(255, 255, 255, 204);"
"}");
tooltipLabel_ = std::make_unique<QLabel>();
tooltipLabel_->setWindowFlag(Qt::ToolTip);
tooltipLabel_->setContentsMargins(6, 4, 6, 4);
tooltipLabel_->setStyleSheet(
"background-color: rgba(15, 15, 15, 191);" "background-color: rgba(15, 15, 15, 191);"
"border: 1px solid rgba(110, 110, 128, 128);" "border: 1px solid rgba(110, 110, 128, 128);"
"color: rgba(255, 255, 255, 204);"); "color: rgba(255, 255, 255, 204);");
@ -83,7 +94,7 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
.arg(++id) .arg(++id)
.arg("Inconsolata") .arg("Inconsolata")
.arg(QString::fromStdString(displayText).replace("\n", "<br/>")), .arg(QString::fromStdString(displayText).replace("\n", "<br/>")),
nullptr, tooltipParent_.get(),
{}, {},
std::numeric_limits<int>::max()); std::numeric_limits<int>::max());
} }
@ -103,9 +114,9 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
} }
// Configure the label // Configure the label
labelTooltip_->setFont( tooltipLabel_->setFont(
QFont("Inconsolata", static_cast<int>(std::round(fontSize * 0.72)))); QFont("Inconsolata", static_cast<int>(std::round(fontSize * 0.72))));
labelTooltip_->setText(QString::fromStdString(displayText)); tooltipLabel_->setText(QString::fromStdString(displayText));
// Get the screen the label will be displayed on // Get the screen the label will be displayed on
QScreen* screen = QGuiApplication::screenAt(mouseGlobalPos.toPoint()); QScreen* screen = QGuiApplication::screenAt(mouseGlobalPos.toPoint());
@ -115,22 +126,23 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
} }
// Default offset for label // Default offset for label
const QPoint offset {2, 24}; const QPoint offset {25, 0};
// Get starting label position (below and to the right) // Get starting label position (below and to the right)
QPoint p = mouseGlobalPos.toPoint() + offset; QPoint p = mouseGlobalPos.toPoint() + offset;
// Adjust position if necessary // Adjust position if necessary
const QRect r = screen->geometry(); const QRect r = screen->geometry();
if (p.x() + labelTooltip_->width() > r.x() + r.width()) if (p.x() + tooltipLabel_->width() > r.x() + r.width())
{ {
// If the label extends beyond the right of the screen, move it left // If the label extends beyond the right of the screen, move it left
p.rx() -= 4 + labelTooltip_->width(); p.rx() -= offset.x() * 2 + tooltipLabel_->width();
} }
if (p.y() + labelTooltip_->height() > r.y() + r.height()) if (p.y() + tooltipLabel_->height() > r.y() + r.height())
{ {
// If the label extends beyond the bottom of the screen, move it up // If the label extends beyond the bottom of the screen, move it up
p.ry() -= 24 + labelTooltip_->height(); // p.ry() -= offset.y() * 2 + tooltipLabel_->height();
// Don't, let it fall through and clamp instead
} }
// Clamp the label within the screen // Clamp the label within the screen
@ -138,26 +150,26 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
{ {
p.setY(r.y()); p.setY(r.y());
} }
if (p.x() + labelTooltip_->width() > r.x() + r.width()) if (p.x() + tooltipLabel_->width() > r.x() + r.width())
{ {
p.setX(r.x() + r.width() - labelTooltip_->width()); p.setX(r.x() + r.width() - tooltipLabel_->width());
} }
if (p.x() < r.x()) if (p.x() < r.x())
{ {
p.setX(r.x()); p.setX(r.x());
} }
if (p.y() + labelTooltip_->height() > r.y() + r.height()) if (p.y() + tooltipLabel_->height() > r.y() + r.height())
{ {
p.setY(r.y() + r.height() - labelTooltip_->height()); p.setY(r.y() + r.height() - tooltipLabel_->height());
} }
// Move the label to the calculated offset // Move the tooltip to the calculated offset
labelTooltip_->move(p); tooltipLabel_->move(p);
// Show the label // Show the tooltip
if (labelTooltip_->isHidden()) if (tooltipLabel_->isHidden())
{ {
labelTooltip_->show(); tooltipLabel_->show();
} }
} }
} }
@ -170,7 +182,7 @@ void Hide()
QToolTip::hideText(); QToolTip::hideText();
// TooltipMethod::QLabel // TooltipMethod::QLabel
labelTooltip_->hide(); tooltipLabel_->hide();
} }
} // namespace tooltip } // namespace tooltip