diff --git a/scwx-qt/source/scwx/qt/util/tooltip.cpp b/scwx-qt/source/scwx/qt/util/tooltip.cpp index efe7c3dc..cc5d0ff1 100644 --- a/scwx-qt/source/scwx/qt/util/tooltip.cpp +++ b/scwx-qt/source/scwx/qt/util/tooltip.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -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 labelTooltip_ = nullptr; +static std::unique_ptr tooltipLabel_ = nullptr; +static std::unique_ptr tooltipParent_ = nullptr; void Initialize() { @@ -40,11 +43,19 @@ void Initialize() return; } - labelTooltip_ = std::make_unique(); - labelTooltip_->setWindowFlag(Qt::ToolTip); - labelTooltip_->setMargin(6); - labelTooltip_->setAttribute(Qt::WidgetAttribute::WA_TranslucentBackground); - labelTooltip_->setStyleSheet( + tooltipParent_ = std::make_unique(); + 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(); + 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", "
")), - nullptr, + tooltipParent_.get(), {}, std::numeric_limits::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(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