8#include <private/qguiapplication_p.h>
20#include <qpa/qplatformtheme.h>
21#include "private/qtextdocumentlayout_p.h"
23#if QT_CONFIG(accessibility)
24#include "qaccessible.h"
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
134 if (
const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
135 return theme->themeHint(QPlatformTheme::DropShadow).toBool();
140 : QWidget(parent, Qt::Popup),
141 widget(showTextFor), pressed(
false), text(txt)
145 setAttribute(Qt::WA_DeleteOnClose,
true);
146 setAttribute(Qt::WA_NoSystemBackground,
true);
148 setPalette(parent->palette());
149 setMouseTracking(
true);
150 setFocusPolicy(Qt::StrongFocus);
152 setCursor(Qt::ArrowCursor);
158 doc =
new QTextDocument();
159 doc->setUndoRedoEnabled(
false);
160 doc->setDefaultFont(QApplication::font(
this));
161#ifdef QT_NO_TEXTHTMLPARSER
162 doc->setPlainText(text);
166 doc->setUndoRedoEnabled(
false);
170 r.setSize(doc->size().toSize());
174 int sw = QGuiApplication::primaryScreen()->virtualGeometry().width() / 3;
180 r = fontMetrics().boundingRect(0, 0, sw, 1000,
181 Qt::AlignLeft | Qt::AlignTop
182 | Qt::TextWordWrap | Qt::TextExpandTabs,
187 resize(sz.boundedTo(screenSize));
200 if (e->button() == Qt::LeftButton && rect().contains(e->position().toPoint())) {
202 anchor = doc->documentLayout()->anchorAt(e->position() - QPointF(hMargin, vMargin));
212 if (widget && e->button() == Qt::LeftButton && doc && rect().contains(e->position().toPoint())) {
213 QString a = doc->documentLayout()->anchorAt(e->position() - QPointF(hMargin, vMargin));
218 if (!href.isEmpty()) {
219 QWhatsThisClickedEvent e(href);
220 if (QCoreApplication::sendEvent(widget, &e))
234 QString a = doc->documentLayout()->anchorAt(e->position() - QPointF(hMargin, vMargin));
236 setCursor(Qt::PointingHandCursor);
238 setCursor(Qt::ArrowCursor);
252 r.adjust(0, 0, -1, -1);
256 p.setPen(QPen(palette().toolTipText(), 0));
257 p.setBrush(palette().toolTipBase());
261 p.setPen(palette().brush(QPalette::Dark).color());
262 p.drawRect(1, 1, w-2, h-2);
264 p.setPen(palette().shadow().color());
265 p.drawPoint(w + 5, 6);
266 p.drawLine(w + 3, 6, w + 5, 8);
267 p.drawLine(w + 1, 6, w + 5, 10);
269 for(i=7; i < h; i += 2)
270 p.drawLine(w, i, w + 5, i + 5);
271 for(i = w - i + h; i > 6; i -= 2)
272 p.drawLine(i, h, i + 5, h + 5);
273 for(; i > 0 ; i -= 2)
274 p.drawLine(6, h + 6 - i, i + 5, h + 5);
276 r.adjust(0, 0, 1, 1);
277 p.setPen(palette().toolTipText().color());
281 p.translate(r.x(), r.y());
283 rect.translate(-r.x(), -r.y());
285 QAbstractTextDocumentLayout::PaintContext context;
286 context.palette.setBrush(QPalette::Text, context.palette.toolTipText());
287 doc->documentLayout()->draw(&p, context);
291 p.drawText(r, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap | Qt::TextExpandTabs, text);
295#if QT_CONFIG(imageformat_xpm)
296static const char *
const button_image[] = {
329 static void say(
QWidget *,
const QString &,
int x = 0,
int y = 0);
336 const QWidgetList toplevels = QApplication::topLevelWidgets();
337 for (
auto *w : toplevels)
338 QCoreApplication::sendEvent(w, e);
347 qApp->installEventFilter(
this);
349 QPoint pos = QCursor::pos();
350 if (
QWidget *w = QApplication::widgetAt(pos)) {
351 QHelpEvent e(QEvent::QueryWhatsThis, w->mapFromGlobal(pos), pos);
352 const bool sentEvent = QCoreApplication::sendEvent(w, &e);
356 QGuiApplication::setOverrideCursor((!sentEvent || !e.isAccepted())?
357 Qt::ForbiddenCursor:Qt::WhatsThisCursor);
359 QGuiApplication::setOverrideCursor(
Qt::WhatsThisCursor);
362#if QT_CONFIG(accessibility)
363 QAccessibleEvent event(
this, QAccessible::ContextHelpStart);
364 QAccessible::updateAccessibility(&event);
372 action->setChecked(
false);
375 QGuiApplication::restoreOverrideCursor();
377#if QT_CONFIG(accessibility)
378 QAccessibleEvent event(
this, QAccessible::ContextHelpEnd);
379 QAccessible::updateAccessibility(&event);
386 if (!o->isWidgetType())
389 bool customWhatsThis = w->testAttribute(Qt::WA_CustomWhatsThis);
391 case QEvent::MouseButtonPress:
393 QMouseEvent *me =
static_cast<QMouseEvent*>(e);
394 if (me->button() == Qt::RightButton || customWhatsThis)
396 QHelpEvent e(QEvent::WhatsThis, me->position().toPoint(), me->globalPosition().toPoint());
397 if (!QCoreApplication::sendEvent(w, &e) || !e.isAccepted())
402 case QEvent::MouseMove:
404 QMouseEvent *me =
static_cast<QMouseEvent*>(e);
405 QHelpEvent e(QEvent::QueryWhatsThis, me->position().toPoint(), me->globalPosition().toPoint());
406 const bool sentEvent = QCoreApplication::sendEvent(w, &e);
410 QGuiApplication::changeOverrideCursor((!sentEvent || !e.isAccepted())?
411 Qt::ForbiddenCursor:Qt::WhatsThisCursor);
415 case QEvent::MouseButtonRelease:
416 case QEvent::MouseButtonDblClick:
417 if (leaveOnMouseRelease && e->type() == QEvent::MouseButtonRelease)
418 QWhatsThis::leaveWhatsThisMode();
419 if (
static_cast<QMouseEvent*>(e)->button() == Qt::RightButton || customWhatsThis)
422 case QEvent::KeyPress:
424 QKeyEvent *kev =
static_cast<QKeyEvent *>(e);
425#if QT_CONFIG(shortcut)
426 if (kev->matches(QKeySequence::Cancel)) {
427 QWhatsThis::leaveWhatsThisMode();
431 if (customWhatsThis) {
433 }
else if (kev->key() == Qt::Key_Menu ||
434 (kev->key() == Qt::Key_F10 &&
435 kev->modifiers() == Qt::ShiftModifier)) {
438 }
else if (kev->key() != Qt::Key_Shift && kev->key() != Qt::Key_Alt
439 && kev->key() != Qt::Key_Control && kev->key() != Qt::Key_Meta) {
440 QWhatsThis::leaveWhatsThisMode();
450class QWhatsThisAction:
public QAction
455 explicit QWhatsThisAction(QObject* parent =
nullptr);
458 void actionTriggered();
461QWhatsThisAction::QWhatsThisAction(QObject *parent) : QAction(tr(
"What's This?"), parent)
463#if QT_CONFIG(imageformat_xpm)
464 QPixmap p(button_image);
468 connect(
this, &QWhatsThisAction::triggered,
this, &QWhatsThisAction::actionTriggered);
469#ifndef QT_NO_SHORTCUT
470 setShortcut(Qt::ShiftModifier | Qt::Key_F1);
474void QWhatsThisAction::actionTriggered()
477 QWhatsThis::enterWhatsThisMode();
478 QWhatsThisPrivate::instance->action =
this;
484
485
486
487
488
489
490
491
492
493
494void QWhatsThis::enterWhatsThisMode()
496 if (QWhatsThisPrivate::instance)
498 (
void)
new QWhatsThisPrivate;
499 QEvent e(QEvent::EnterWhatsThisMode);
500 QWhatsThisPrivate::notifyToplevels(&e);
504
505
506
507
508
509bool QWhatsThis::inWhatsThisMode()
511 return (QWhatsThisPrivate::instance !=
nullptr);
515
516
517
518
519
520
521
522
523void QWhatsThis::leaveWhatsThisMode()
525 delete QWhatsThisPrivate::instance;
526 QEvent e(QEvent::LeaveWhatsThisMode);
527 QWhatsThisPrivate::notifyToplevels(&e);
532 if (text.size() == 0)
535 QScreen *screen = widget ? widget->screen()
536 : QGuiApplication::screenAt(QPoint(x, y));
538 screen = QGuiApplication::primaryScreen();
539 QRect screenRect = screen->geometry();
544 int w = whatsThat->width();
545 int h = whatsThat->height();
546 int sx = screenRect.x();
547 int sy = screenRect.y();
553 pos = widget->mapToGlobal(QPoint(0,0));
555 if (widget && w > widget->width() + 16)
556 x = pos.x() + widget->width()/2 - w/2;
563 x = widget ? qMin(screenRect.right(), pos.x() + widget->width()) : screenRect.right();
570 if (widget && h > widget->height() + 16) {
571 y = pos.y() + widget->height() + 2;
573 if (y + h + 10 > screenRect.bottom())
581 y = widget ? qMin(screenRect.bottom(), pos.y() + widget->height()) : screenRect.bottom();
587 whatsThat->move(x, y);
589 whatsThat->grabKeyboard();
593
594
595
596
597
598
599void QWhatsThis::showText(
const QPoint &pos,
const QString &text, QWidget *w)
601 leaveWhatsThisMode();
602 QWhatsThisPrivate::say(w, text, pos.x(), pos.y());
606
607
608
609
610void QWhatsThis::hideText()
612 delete QWhatsThat::instance;
616
617
618
619
620
621
623QAction *QWhatsThis::createAction(QObject *parent)
625 return new QWhatsThisAction(parent);
631#include "qwhatsthis.moc"
The QHelpEvent class provides an event that is used to request helpful information about a particular...
\inmodule QtCore\reentrant
void keyPressEvent(QKeyEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
void mouseReleaseEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
void mouseMoveEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
void mousePressEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
static QWhatsThat * instance
static void say(QWidget *, const QString &, int x=0, int y=0)
static QWhatsThisPrivate * instance
static void notifyToplevels(QEvent *e)
bool eventFilter(QObject *, QEvent *) override
Filters events if this object has been installed as an event filter for the watched object.
Combined button and popup list for selecting options.
Q_GUI_EXPORT bool mightBeRichText(QAnyStringView)
Returns true if the string text is likely to be rich text; otherwise returns false.
static constexpr int hMargin
static constexpr int vMargin