| 1 | // Copyright 2020 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/ToolTipControls/BalloonTip.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | |
| 8 | #include <QApplication> |
| 9 | #include <QBitmap> |
| 10 | #include <QBrush> |
| 11 | #include <QCursor> |
| 12 | #include <QFont> |
| 13 | #include <QLabel> |
| 14 | #include <QPainter> |
| 15 | #include <QPainterPath> |
| 16 | #include <QPen> |
| 17 | #include <QPoint> |
| 18 | #include <QRect> |
| 19 | #include <QScreen> |
| 20 | #include <QSize> |
| 21 | #include <QString> |
| 22 | #include <QVBoxLayout> |
| 23 | |
| 24 | #if defined(__APPLE__) |
| 25 | #include <QToolTip> |
| 26 | #endif |
| 27 | |
| 28 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 29 | #include "DolphinQt/Settings.h" |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | std::unique_ptr<BalloonTip> s_the_balloon_tip = nullptr; |
| 34 | // Remember the parent ToolTipWidget so cursor-related events can see whether the cursor is inside |
| 35 | // the parent's bounding box or not. Use this variable instead of BalloonTip's parent() member |
| 36 | // because the ToolTipWidget isn't responsible for deleting the BalloonTip and so doesn't set its |
| 37 | // parent member. |
| 38 | QWidget* s_parent = nullptr; |
| 39 | } // namespace |
| 40 | |
| 41 | void BalloonTip::ShowBalloon(const QString& title, const QString& message, |
| 42 | const QPoint& target_arrow_tip_position, QWidget* const parent, |
| 43 | const ShowArrow show_arrow, const int border_width) |
| 44 | { |
| 45 | HideBalloon(); |
| 46 | if (message.isEmpty() && title.isEmpty()) |
| 47 | return; |
| 48 | |
| 49 | #if defined(__APPLE__) |
| 50 | QString the_message = message; |
| 51 | the_message.replace(QStringLiteral("<dolphin_emphasis>"), QStringLiteral("<b>")); |
| 52 | the_message.replace(QStringLiteral("</dolphin_emphasis>"), QStringLiteral("</b>")); |
| 53 | QToolTip::showText(target_arrow_tip_position, the_message, parent); |
| 54 | #else |
| 55 | s_the_balloon_tip = std::make_unique<BalloonTip>(PrivateTag{}, title, message, parent); |
| 56 | s_the_balloon_tip->UpdateBoundsAndRedraw(target_arrow_tip_position, show_arrow, border_width); |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | void BalloonTip::HideBalloon() |
| 61 | { |
| 62 | s_parent = nullptr; |
| 63 | #if defined(__APPLE__) |
| 64 | QToolTip::hideText(); |
| 65 | #else |
| 66 | if (s_the_balloon_tip == nullptr) |
| 67 | return; |
| 68 | s_the_balloon_tip->hide(); |
| 69 | s_the_balloon_tip.reset(); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | BalloonTip::BalloonTip(PrivateTag, const QString& title, QString message, QWidget* const parent) |
| 74 | : QWidget(nullptr, Qt::ToolTip) |
| 75 | { |
| 76 | s_parent = parent; |
| 77 | setMouseTracking(true); |
| 78 | |
| 79 | QColor window_color; |
| 80 | QColor text_color; |
| 81 | QColor dolphin_emphasis; |
| 82 | Settings::Instance().GetToolTipStyle(window_color, text_color, dolphin_emphasis, m_border_color, |
| 83 | parent->palette(), palette()); |
| 84 | const auto style_sheet = QStringLiteral("background-color: #%1; color: #%2;") |
| 85 | .arg(window_color.rgba(), 0, 16) |
| 86 | .arg(text_color.rgba(), 0, 16); |
| 87 | setStyleSheet(style_sheet); |
| 88 | |
| 89 | // Replace text in our our message if specific "tags" are used |
| 90 | message.replace(QStringLiteral("<dolphin_emphasis>"), |
| 91 | QStringLiteral("<font color=\"#%1\"><b>").arg(dolphin_emphasis.rgba(), 0, 16)); |
| 92 | message.replace(QStringLiteral("</dolphin_emphasis>"), QStringLiteral("</b></font>")); |
| 93 | |
| 94 | auto* const balloontip_layout = new QVBoxLayout; |
| 95 | balloontip_layout->setSizeConstraint(QLayout::SetFixedSize); |
| 96 | setLayout(balloontip_layout); |
| 97 | |
| 98 | const auto create_label = [=](const QString& text) { |
| 99 | QLabel* const label = new QLabel; |
| 100 | balloontip_layout->addWidget(label); |
| 101 | |
| 102 | label->setText(text); |
| 103 | label->setTextFormat(Qt::RichText); |
| 104 | |
| 105 | const int max_width = label->screen()->availableGeometry().width() / 3; |
| 106 | label->setMaximumWidth(max_width); |
| 107 | |
| 108 | if (label->sizeHint().width() > max_width) |
| 109 | { |
| 110 | label->setWordWrap(true); |
| 111 | label->setMinimumWidth(max_width); |
| 112 | } |
| 113 | |
| 114 | return label; |
| 115 | }; |
| 116 | |
| 117 | if (!title.isEmpty()) |
| 118 | { |
| 119 | QLabel* const title_label = create_label(title); |
| 120 | |
| 121 | QFont title_font = title_label->font(); |
| 122 | title_font.setBold(true); |
| 123 | title_label->setFont(title_font); |
| 124 | } |
| 125 | |
| 126 | if (!message.isEmpty()) |
| 127 | create_label(message); |
| 128 | } |
| 129 | |
| 130 | bool BalloonTip::IsCursorInsideWidgetBoundingBox(const QWidget& widget) |
| 131 | { |
| 132 | const QPoint local_cursor_position = widget.mapFromGlobal(QCursor::pos()); |
| 133 | return widget.rect().contains(local_cursor_position); |
| 134 | } |
| 135 | |
| 136 | bool BalloonTip::IsCursorOnBalloonTip() |
| 137 | { |
| 138 | return s_the_balloon_tip != nullptr && |
| 139 | QApplication::widgetAt(QCursor::pos()) == s_the_balloon_tip.get(); |
| 140 | } |
| 141 | |
| 142 | bool BalloonTip::IsWidgetBalloonTipActive(const QWidget& widget) |
| 143 | { |
| 144 | return &widget == s_parent; |
| 145 | } |
| 146 | |
| 147 | // Hiding the balloon causes the BalloonTip widget to be deleted. Triggering that deletion while |
| 148 | // inside a BalloonTip event handler leads to a use-after-free crash or worse, so queue the deletion |
| 149 | // for later. |
| 150 | static void QueueHideBalloon() |
| 151 | { |
| 152 | QueueOnObject(s_parent, BalloonTip::HideBalloon); |
| 153 | } |
| 154 | |
| 155 | void BalloonTip::enterEvent(QEnterEvent* const event) |
| 156 | { |
| 157 | if (!IsCursorInsideWidgetBoundingBox(*s_parent)) |
| 158 | QueueHideBalloon(); |
| 159 | |
| 160 | QWidget::enterEvent(event); |
| 161 | } |
| 162 | |
| 163 | void BalloonTip::mouseMoveEvent(QMouseEvent* const event) |
| 164 | { |
| 165 | if (!IsCursorInsideWidgetBoundingBox(*s_parent)) |
| 166 | QueueHideBalloon(); |
| 167 | |
| 168 | QWidget::mouseMoveEvent(event); |
| 169 | } |
| 170 | |
| 171 | void BalloonTip::leaveEvent(QEvent* const event) |
| 172 | { |
| 173 | if (QApplication::widgetAt(QCursor::pos()) != s_parent) |
| 174 | QueueHideBalloon(); |
| 175 | |
| 176 | QWidget::leaveEvent(event); |
| 177 | } |
| 178 | |
| 179 | void BalloonTip::paintEvent(QPaintEvent* const event) |
| 180 | { |
| 181 | QPainter painter(this); |
| 182 | painter.drawPixmap(rect(), m_pixmap); |
| 183 | |
| 184 | QWidget::paintEvent(event); |
| 185 | } |
| 186 | |
| 187 | void BalloonTip::UpdateBoundsAndRedraw(const QPoint& target_arrow_tip_position, |
| 188 | const ShowArrow show_arrow, const int border_full_width) |
| 189 | { |
| 190 | const float border_half_width = border_full_width / 2.0; |
| 191 | |
| 192 | // This should be odd so that the arrow tip is a single pixel wide. |
| 193 | const int arrow_full_width = 35; |
| 194 | const float arrow_half_width = arrow_full_width / 2.0; |
| 195 | // The y distance between the inner edge of the rectangle border and the inner tip of the arrow |
| 196 | // border, and also the distance between the outer edge of the rectangle border and the outer tip |
| 197 | // of the arrow border |
| 198 | const int arrow_height = (1 + arrow_full_width) / 2; |
| 199 | |
| 200 | // Distance between the label layout and the inner rectangle border edge |
| 201 | const int balloon_interior_padding = 12; |
| 202 | // Prevent the corners of the label layout from portruding into the rounded rectangle corners at |
| 203 | // larger border sizes. |
| 204 | const int rounded_corner_margin = border_half_width / 4; |
| 205 | const int horizontal_margin = |
| 206 | border_full_width + rounded_corner_margin + balloon_interior_padding; |
| 207 | const int vertical_margin = horizontal_margin + arrow_height; |
| 208 | |
| 209 | // Create enough space around the layout containing the title and message to draw the balloon and |
| 210 | // both arrow tips (at most one of which will be visible) |
| 211 | layout()->setContentsMargins(horizontal_margin, vertical_margin, horizontal_margin, |
| 212 | vertical_margin); |
| 213 | |
| 214 | QSize size_hint = sizeHint(); |
| 215 | |
| 216 | // These positions represent the middle of each edge of the BalloonTip's rounded rectangle |
| 217 | const float rect_width = size_hint.width() - border_full_width; |
| 218 | const float rect_height = size_hint.height() - border_full_width - 2 * arrow_height; |
| 219 | const float rect_top = border_half_width + arrow_height; |
| 220 | const float rect_bottom = rect_top + rect_height; |
| 221 | const float rect_left = border_half_width; |
| 222 | // rect_right isn't used for anything |
| 223 | |
| 224 | // Qt defines the radius of a rounded rectangle as "the radius of the ellipses defining the |
| 225 | // corner". Unlike the rectangle's edges this corresponds to the outside of the rounded curve |
| 226 | // instead of its middle, so we add the full width to the inner radius instead of the half width |
| 227 | const float corner_base_inner_radius = 7.0; |
| 228 | const float corner_outer_radius = corner_base_inner_radius + border_full_width; |
| 229 | |
| 230 | // This value is arbitrary but works well. |
| 231 | const int base_arrow_x_offset = 34; |
| 232 | // Adjust the offset inward to compensate for the border and rounded corner widths. This ensures |
| 233 | // the arrow is on the flat part of the top/bottom border. |
| 234 | const int adjusted_arrow_x_offset = |
| 235 | base_arrow_x_offset + border_full_width + static_cast<int>(border_half_width); |
| 236 | // If the border is wide enough (or the BalloonTip small enough) the offset might end up past the |
| 237 | // midpoint; if that happens just use the midpoint instead |
| 238 | const int centered_arrow_x_offset = (size_hint.width() - arrow_full_width) / 2; |
| 239 | // If the arrow is on the left this is the distance between the left edge of the BalloonTip and |
| 240 | // the left edge of the arrow interior; otherwise the distance between the right edges. |
| 241 | const int arrow_nearest_edge_x_offset = |
| 242 | std::min(adjusted_arrow_x_offset, centered_arrow_x_offset); |
| 243 | const int arrow_tip_x_offset = arrow_nearest_edge_x_offset + arrow_half_width; |
| 244 | |
| 245 | // The BalloonTip should be contained entirely within the screen that contains the target |
| 246 | // position. |
| 247 | QScreen* screen = QGuiApplication::screenAt(target_arrow_tip_position); |
| 248 | if (screen == nullptr) |
| 249 | { |
| 250 | // If the target position isn't on any screen (which can happen if the window is partly off the |
| 251 | // screen and the user hovers over the label) then use the screen containing the cursor instead. |
| 252 | screen = QGuiApplication::screenAt(QCursor::pos()); |
| 253 | } |
| 254 | const QRect screen_rect = screen->geometry(); |
| 255 | |
| 256 | QPainterPath rect_path; |
| 257 | rect_path.addRoundedRect(rect_left, rect_top, rect_width, rect_height, corner_outer_radius, |
| 258 | corner_outer_radius); |
| 259 | |
| 260 | // The default pen cap style Qt::SquareCap extends drawn lines one half width before the starting |
| 261 | // point and one half width after the ending point such that the starting and ending points are |
| 262 | // surrounded by drawn pixels in both dimensions instead of just for the width. This is a |
| 263 | // reasonable default but we need to draw lines precisely, and this behavior causes problems when |
| 264 | // drawing lines of length and width 1 (which we do for the arrow interior, and also the arrow |
| 265 | // border when border_full_width is 1). For those lines to correctly end up as a pixel we would |
| 266 | // need to offset the start and end points by 0.5 inward. However, doing that would lead them to |
| 267 | // be at the same point, and if the endpoints of a line are the same Qt simply doesn't draw it |
| 268 | // regardless of the cap style. |
| 269 | // |
| 270 | // Using Qt::FlatCap instead fixes the issue. |
| 271 | |
| 272 | m_pixmap = QPixmap(size_hint); |
| 273 | |
| 274 | QPen border_pen(m_border_color, border_full_width); |
| 275 | border_pen.setCapStyle(Qt::FlatCap); |
| 276 | |
| 277 | QPainter balloon_painter(&m_pixmap); |
| 278 | balloon_painter.setPen(border_pen); |
| 279 | balloon_painter.setBrush(palette().color(QPalette::Window)); |
| 280 | balloon_painter.drawPath(rect_path); |
| 281 | |
| 282 | QBitmap mask_bitmap(size_hint); |
| 283 | mask_bitmap.fill(Qt::color0); |
| 284 | |
| 285 | QPen mask_pen(Qt::color1, border_full_width); |
| 286 | mask_pen.setCapStyle(Qt::FlatCap); |
| 287 | |
| 288 | QPainter mask_painter(&mask_bitmap); |
| 289 | mask_painter.setPen(mask_pen); |
| 290 | mask_painter.setBrush(QBrush(Qt::color1)); |
| 291 | mask_painter.drawPath(rect_path); |
| 292 | |
| 293 | const bool arrow_at_bottom = |
| 294 | target_arrow_tip_position.y() - size_hint.height() + arrow_height >= 0; |
| 295 | const bool arrow_at_left = |
| 296 | target_arrow_tip_position.x() + size_hint.width() - arrow_tip_x_offset < screen_rect.width(); |
| 297 | |
| 298 | const float arrow_base_y = |
| 299 | arrow_at_bottom ? rect_bottom - border_half_width : rect_top + border_half_width; |
| 300 | |
| 301 | const float arrow_tip_vertical_offset = arrow_at_bottom ? arrow_height : -arrow_height; |
| 302 | const float arrow_tip_interior_y = arrow_base_y + arrow_tip_vertical_offset; |
| 303 | const float arrow_tip_exterior_y = |
| 304 | arrow_tip_interior_y + (arrow_at_bottom ? border_full_width : -border_full_width); |
| 305 | const float arrow_base_left_edge_x = |
| 306 | arrow_at_left ? arrow_nearest_edge_x_offset : |
| 307 | size_hint.width() - arrow_nearest_edge_x_offset - arrow_full_width; |
| 308 | const float arrow_base_right_edge_x = arrow_base_left_edge_x + arrow_full_width; |
| 309 | const float arrow_tip_x = arrow_base_left_edge_x + arrow_half_width; |
| 310 | |
| 311 | if (show_arrow == ShowArrow::Yes) |
| 312 | { |
| 313 | // Drawing diagonal lines in Qt is filled with edge cases and inexplicable behavior. Getting it |
| 314 | // to do what you want at one border size is simple enough, but doing so flexibly is an exercise |
| 315 | // in futility. Some examples: |
| 316 | // * For some values of x, diagonal lines of width x and x+1 are drawn exactly the same. |
| 317 | // * When drawing a triangle where p1 and p3 have exactly the same y value, they can be drawn at |
| 318 | // different heights. |
| 319 | // * Lines of width 1 sometimes get drawn one pixel past where they should even with FlatCap, |
| 320 | // but only on the left side (regardless of which direction the stroke was). |
| 321 | // |
| 322 | // Instead of dealing with all that, fake it with vertical lines which are much better behaved. |
| 323 | // Draw a bunch of vertical lines with width 1 to form the arrow border and interior. |
| 324 | |
| 325 | QPainterPath arrow_border_path; |
| 326 | QPainterPath arrow_interior_fill_path; |
| 327 | const float y_end_offset = arrow_at_bottom ? border_full_width : -border_full_width; |
| 328 | |
| 329 | // Draw the arrow border and interior lines from the outside inward. Each loop iteration draws |
| 330 | // one pair of border lines and one pair of interior lines. |
| 331 | for (int i = 1; i <= arrow_half_width; i++) |
| 332 | { |
| 333 | const float x_offset_from_arrow_base_edge = i - 0.5; |
| 334 | const float border_y_start = arrow_base_y + (arrow_at_bottom ? i : -i); |
| 335 | const float border_y_end = border_y_start + y_end_offset; |
| 336 | const float interior_y_start = arrow_base_y; |
| 337 | const float interior_y_end = border_y_start; |
| 338 | const float left_line_x = arrow_base_left_edge_x + x_offset_from_arrow_base_edge; |
| 339 | const float right_line_x = arrow_base_right_edge_x - x_offset_from_arrow_base_edge; |
| 340 | |
| 341 | arrow_border_path.moveTo(left_line_x, border_y_start); |
| 342 | arrow_border_path.lineTo(left_line_x, border_y_end); |
| 343 | |
| 344 | arrow_border_path.moveTo(right_line_x, border_y_start); |
| 345 | arrow_border_path.lineTo(right_line_x, border_y_end); |
| 346 | |
| 347 | arrow_interior_fill_path.moveTo(left_line_x, interior_y_start); |
| 348 | arrow_interior_fill_path.lineTo(left_line_x, interior_y_end); |
| 349 | |
| 350 | arrow_interior_fill_path.moveTo(right_line_x, interior_y_start); |
| 351 | arrow_interior_fill_path.lineTo(right_line_x, interior_y_end); |
| 352 | } |
| 353 | // The middle border line |
| 354 | arrow_border_path.moveTo(arrow_tip_x, arrow_tip_interior_y); |
| 355 | arrow_border_path.lineTo(arrow_tip_x, arrow_tip_interior_y + y_end_offset); |
| 356 | |
| 357 | // The middle interior line |
| 358 | arrow_interior_fill_path.moveTo(arrow_tip_x, arrow_base_y); |
| 359 | arrow_interior_fill_path.lineTo(arrow_tip_x, arrow_tip_interior_y); |
| 360 | |
| 361 | border_pen.setWidth(1); |
| 362 | |
| 363 | balloon_painter.setPen(border_pen); |
| 364 | balloon_painter.drawPath(arrow_border_path); |
| 365 | |
| 366 | QPen arrow_interior_fill_pen(palette().color(QPalette::Window), 1); |
| 367 | arrow_interior_fill_pen.setCapStyle(Qt::FlatCap); |
| 368 | balloon_painter.setPen(arrow_interior_fill_pen); |
| 369 | balloon_painter.drawPath(arrow_interior_fill_path); |
| 370 | |
| 371 | mask_pen.setWidth(1); |
| 372 | mask_painter.setPen(mask_pen); |
| 373 | |
| 374 | mask_painter.drawPath(arrow_border_path); |
| 375 | mask_painter.drawPath(arrow_interior_fill_path); |
| 376 | } |
| 377 | |
| 378 | setMask(mask_bitmap); |
| 379 | |
| 380 | // Place the arrow tip at the target position whether the arrow tip is drawn or not |
| 381 | const int target_balloontip_global_x = |
| 382 | target_arrow_tip_position.x() - static_cast<int>(arrow_tip_x); |
| 383 | const int rightmost_valid_balloontip_global_x = |
| 384 | screen_rect.left() + screen_rect.width() - size_hint.width(); |
| 385 | // If the balloon would extend off the screen, push it left or right until it's not |
| 386 | const int actual_balloontip_global_x = |
| 387 | std::max(screen_rect.left(), |
| 388 | std::min(rightmost_valid_balloontip_global_x, target_balloontip_global_x)); |
| 389 | // The tip pixel should be in the middle of the control, and arrow_tip_exterior_y is at the bottom |
| 390 | // of that pixel. When arrow_at_bottom is true the arrow is above arrow_tip_exterior_y and so the |
| 391 | // tip pixel is in the right place, but when it's false the arrow is below arrow_tip_exterior_y |
| 392 | // so the tip pixel would be the one below that. Make this adjustment to fix that. |
| 393 | const int tip_pixel_adjustment = arrow_at_bottom ? 0 : 1; |
| 394 | const int actual_balloontip_global_y = |
| 395 | target_arrow_tip_position.y() - arrow_tip_exterior_y - tip_pixel_adjustment; |
| 396 | |
| 397 | move(actual_balloontip_global_x, actual_balloontip_global_y); |
| 398 | |
| 399 | show(); |
| 400 | } |