-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtooltipmanager.cpp
More file actions
74 lines (62 loc) · 2.43 KB
/
tooltipmanager.cpp
File metadata and controls
74 lines (62 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "tooltipmanager.h"
#include <QToolTip>
#include <QGuiApplication>
#include <QTimer>
#include <QQuickWindow>
#include <QFontMetrics>
#include <QQmlContext>
#include <QQuickView>
TooltipManager::TooltipManager(QObject *parent) : QObject(parent) {}
void TooltipManager::showText(const QPoint &pos, const QString &text)
{
QQuickWindow *window = qobject_cast<QQuickWindow *>(QGuiApplication::focusWindow());
if (!window) return;
QQuickItem *tooltipItem = new QQuickItem(window->contentItem());
tooltipItem->setParentItem(window->contentItem());
tooltipItem->setWidth(200);
tooltipItem->setHeight(50);
QQuickItem *textItem = new QQuickItem(tooltipItem);
textItem->setParentItem(tooltipItem);
textItem->setWidth(tooltipItem->width());
textItem->setHeight(tooltipItem->height());
textItem->setProperty("text", text);
tooltipItem->setPosition(pos);
tooltipItem->setParentItem(window->contentItem());
tooltipItem->setProperty("transientParent", QVariant::fromValue(window));
}
void TooltipManager::hide()
{
QToolTip::hideText();
}
void TooltipManager::showAboveOrBelow(QQuickItem *item, const QString &text)
{
if (!item || !item->window() || text.isEmpty()) {
return;
}
QQuickWindow *window = item->window();
QQuickItem *content = window->contentItem();
QScreen *screen = window->screen();
if (!screen) {
screen = QGuiApplication::primaryScreen();
}
if (!screen) return;
QFontMetrics metrics(QToolTip::font());
QRect textRect = metrics.boundingRect(QRect(), Qt::AlignJustify, text);
const qreal itemCenterY = window->y() + item->mapToItem(content, 0, item->height() / 2.0).y();
const qreal screenCenterY = screen->geometry().y() + screen->geometry().height() / 2.0;
QPoint screenPos;
if (itemCenterY < screenCenterY) {
QPointF localPoint(item->width() / 2.0, item->height());
QPointF windowPoint = item->mapToItem(content, localPoint);
screenPos = window->position() + windowPoint.toPoint();
screenPos.ry() -= 10;
screenPos.rx() -= (textRect.width() / 2);
} else {
QPointF localPoint(item->width() / 2.0, 0);
QPointF windowPoint = item->mapToItem(content, localPoint);
screenPos = window->position() + windowPoint.toPoint();
screenPos.ry() -= (textRect.height() + 10);
screenPos.rx() -= (textRect.width() / 2);
}
QToolTip::showText(screenPos, text);
}