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/settings/text_settings.hpp>
#include <scwx/qt/util/imgui.hpp>
#include <scwx/util/logger.hpp>
#include <TextFlow.hpp>
#include <QGuiApplication>
@ -19,6 +20,7 @@ namespace tooltip
{
static const std::string logPrefix_ = "scwx::qt::util::tooltip";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
enum class TooltipMethod
{
@ -29,7 +31,8 @@ enum class TooltipMethod
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()
{
@ -40,11 +43,19 @@ void Initialize()
return;
}
labelTooltip_ = std::make_unique<QLabel>();
labelTooltip_->setWindowFlag(Qt::ToolTip);
labelTooltip_->setMargin(6);
labelTooltip_->setAttribute(Qt::WidgetAttribute::WA_TranslucentBackground);
labelTooltip_->setStyleSheet(
tooltipParent_ = std::make_unique<QWidget>();
tooltipParent_->setStyleSheet(
"QToolTip"
"{"
"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);"
"border: 1px solid rgba(110, 110, 128, 128);"
"color: rgba(255, 255, 255, 204);");
@ -83,7 +94,7 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
.arg(++id)
.arg("Inconsolata")
.arg(QString::fromStdString(displayText).replace("\n", "<br/>")),
nullptr,
tooltipParent_.get(),
{},
std::numeric_limits<int>::max());
}
@ -103,9 +114,9 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
}
// Configure the label
labelTooltip_->setFont(
tooltipLabel_->setFont(
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
QScreen* screen = QGuiApplication::screenAt(mouseGlobalPos.toPoint());
@ -115,22 +126,23 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
}
// Default offset for label
const QPoint offset {2, 24};
const QPoint offset {25, 0};
// Get starting label position (below and to the right)
QPoint p = mouseGlobalPos.toPoint() + offset;
// Adjust position if necessary
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
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
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
@ -138,26 +150,26 @@ void Show(const std::string& text, const QPointF& mouseGlobalPos)
{
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())
{
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
labelTooltip_->move(p);
// Move the tooltip to the calculated offset
tooltipLabel_->move(p);
// Show the label
if (labelTooltip_->isHidden())
// Show the tooltip
if (tooltipLabel_->isHidden())
{
labelTooltip_->show();
tooltipLabel_->show();
}
}
}
@ -170,7 +182,7 @@ void Hide()
QToolTip::hideText();
// TooltipMethod::QLabel
labelTooltip_->hide();
tooltipLabel_->hide();
}
} // namespace tooltip