11#include <QtCore/qbasictimer.h>
12#include <QtQml/qqmlinfo.h>
13#include <QtQml/qqmlengine.h>
14#include <QtQml/qqmlcontext.h>
15#include <QtQml/qqmlcomponent.h>
16#include <QtQuick/qquickwindow.h>
18#include <QtCore/q26numeric.h>
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
100#ifdef QT_BUILD_INTERNAL
101Q_CONSTINIT Q_AUTOTEST_EXPORT
109#ifdef QT_BUILD_INTERNAL
110Q_CONSTINIT Q_AUTOTEST_EXPORT
118 Q_DECLARE_PUBLIC(QQuickToolTip)
142 windowFlags = Qt::ToolTip;
144 qt_quicktooltipattachedprivate_delay = q26::saturating_cast<
int>(
145 qGuiApp->styleHints()->toolTipWakeUpDelay().count()
148#if QT_CONFIG(wayland)
149 extendedWindowType = QNativeInterface::Private::QWaylandWindow::ToolTip;
152 wmWindowType = QNativeInterface::Private::QXcbWindow::Tooltip;
161 delayTimer.start(delay, q);
173 timeoutTimer.start(timeout, q);
183 QQuickPopupPrivate::opened();
187QQuickToolTip::QQuickToolTip(QQuickItem *parent)
188 : QQuickPopup(*(
new QQuickToolTipPrivate), parent)
191 d->allowVerticalFlip =
true;
192 d->allowHorizontalFlip =
true;
193 d->popupItem->setHoverEnabled(
false);
197
198
199
200
201QString QQuickToolTip::text()
const
203 Q_D(
const QQuickToolTip);
207void QQuickToolTip::setText(
const QString &text)
214 maybeSetAccessibleName(text);
219
220
221
222
223
224
225
226
227int QQuickToolTip::delay()
const
229 Q_D(
const QQuickToolTip);
233void QQuickToolTip::setDelay(
int delay)
236 if (d->delay == delay)
244
245
246
247
248
249
250
251
252int QQuickToolTip::timeout()
const
254 Q_D(
const QQuickToolTip);
258void QQuickToolTip::setTimeout(
int timeout)
261 if (d->timeout == timeout)
264 d->timeout = timeout;
271 emit timeoutChanged();
274void QQuickToolTip::setVisible(
bool visible)
288 QQuickPopup::setVisible(visible);
291QQuickToolTipAttached *QQuickToolTip::qmlAttachedProperties(QObject *object)
293 return new QQuickToolTipAttached(object);
297
298
299
300
301
302
303void QQuickToolTip::show(
const QString &text,
int ms)
312
313
314
315
316
317void QQuickToolTip::hide()
322QFont QQuickToolTip::defaultFont()
const
324 return QQuickTheme::font(QQuickTheme::ToolTip);
327void QQuickToolTip::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
330 QQuickPopup::itemChange(change, data);
331 if (change == QQuickItem::ItemVisibleHasChanged) {
335 QQuickToolTipAttached *attached = qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(d->parentItem,
false));
337 emit attached->visibleChanged();
341void QQuickToolTip::timerEvent(QTimerEvent *event)
344 if (event->timerId() == d->timeoutTimer.timerId()) {
346 QQuickPopup::setVisible(
false);
349 if (event->timerId() == d->delayTimer.timerId()) {
351 QQuickPopup::setVisible(
true);
354 QQuickPopup::timerEvent(event);
357#if QT_CONFIG(accessibility)
358QAccessible::Role QQuickToolTip::accessibleRole()
const
360 return QAccessible::ToolTip;
363void QQuickToolTip::accessibilityActiveChanged(
bool active)
366 QQuickPopup::accessibilityActiveChanged(active);
369 maybeSetAccessibleName(d->text);
373QQuickToolTip *QQuickToolTipAttachedPrivate::instance(
bool create)
const
375 QQmlEngine *engine = qmlEngine(parent);
381 static const char *name =
"_q_shared_QQuickToolTip";
383 QQuickToolTip *tip = engine->property(name).value<QQuickToolTip *>();
384 if (!tip && create) {
385 QQmlComponent component(engine,
"QtQuick.Controls",
"ToolTip");
387 QObject *object = component.create();
389 object->setParent(engine);
391 tip = qobject_cast<QQuickToolTip *>(object);
395 engine->setProperty(name, QVariant::fromValue(object));
400void QQuickToolTipAttachedPrivate::maybeSetVisibleImplicitly(
401 const QObject *attachee,
bool visible)
403 auto *toolTipAttached = qobject_cast<QQuickToolTipAttached *>(
404 qmlAttachedPropertiesObject<QQuickToolTip>(attachee,
false));
406 if (!toolTipAttached)
409 auto *toolTipAttachedPrivate = toolTipAttached->d_func();
412 if (toolTipAttachedPrivate->isVisibleExplicitlySet()
413 || toolTipAttachedPrivate->policy == QQuickToolTip::Manual) {
419 const bool effectiveVisible = visible ? visible && !toolTipAttachedPrivate->text.isEmpty() :
false;
420 toolTipAttachedPrivate->setVisible(effectiveVisible, QQml::PropertyUtils::State::ImplicitlySet);
423void QQuickToolTipAttachedPrivate::setVisible(
bool visible, QQml::PropertyUtils::State propertyState)
425 Q_Q(QQuickToolTipAttached);
426 if (warnIfAttacheeIsNotAnItem(QStringLiteral(
"setVisible")))
429 explicitVisible = isExplicitlySet(propertyState);
431 if (!complete && visible) {
442bool QQuickToolTipAttachedPrivate::isVisibleExplicitlySet()
const
444 return explicitVisible;
447void QQuickToolTipAttachedPrivate::setDelay(
int delay, QQml::PropertyUtils::State propertyState)
449 Q_Q(QQuickToolTipAttached);
450 if (warnIfAttacheeIsNotAnItem(QStringLiteral(
"setDelay")))
453 explicitDelay = isExplicitlySet(propertyState);
455 if (
this->delay == delay)
459 emit q->delayChanged();
462 instance(
true)->setDelay(delay);
465bool QQuickToolTipAttachedPrivate::isDelayExplicitlySet()
const
467 return explicitDelay;
470void QQuickToolTipAttachedPrivate::setTimeout(
int timeout, QQml::PropertyUtils::State propertyState)
472 Q_Q(QQuickToolTipAttached);
473 if (warnIfAttacheeIsNotAnItem(QStringLiteral(
"setTimeout")))
476 explicitTimeout = isExplicitlySet(propertyState);
478 if (
this->timeout == timeout)
481 this->timeout = timeout;
482 emit q->timeoutChanged();
485 instance(
true)->setTimeout(timeout);
488bool QQuickToolTipAttachedPrivate::isTimeoutExplicitlySet()
const
490 return explicitTimeout;
493void QQuickToolTipAttachedPrivate::inheritPolicy(QQuickToolTip::Policy policy)
495 Q_Q(QQuickToolTipAttached);
496 if (
this->policy == policy)
499 this->policy = policy;
501 emit q->policyChanged();
504void QQuickToolTipAttachedPrivate::propagatePolicy()
506 Q_Q(QQuickToolTipAttached);
507 const auto attachedToolTipChildren = q->attachedChildren();
508 for (QtPrivate::QQuickAttachedPropertyPropagator *child : attachedToolTipChildren) {
509 auto *attachedToolTipChild = qobject_cast<QQuickToolTipAttached *>(child);
510 if (attachedToolTipChild)
511 attachedToolTipChild->d_func()->inheritPolicy(policy);
516
517
518
519
520
521
522
523bool QQuickToolTipAttachedPrivate::warnIfAttacheeIsNotAnItem(
const QString &functionName)
525 QQuickItem *item = qobject_cast<QQuickItem *>(parent);
529 qmlWarning(parent).nospace().noquote() <<
"The attached function ToolTip::" << functionName
530 <<
" can only be called when the attachee derives from Item";
534int QQuickToolTipAttachedPrivate::calculateTimeout(
const QString &text)
536 if (Q_UNLIKELY(qt_quicktooltipattachedprivate_short_timeout)) {
542 return 10000 + 40 * qMax(0, text.length() - 100);
545QQuickToolTipAttached::QQuickToolTipAttached(QObject *parent)
546 : QtPrivate::QQuickAttachedPropertyPropagator(*(
new QQuickToolTipAttachedPrivate), parent)
552
553
554
555
556
557
558
559QString QQuickToolTipAttached::text()
const
561 Q_D(
const QQuickToolTipAttached);
565void QQuickToolTipAttached::setText(
const QString &text)
567 Q_D(QQuickToolTipAttached);
568 if (d->warnIfAttacheeIsNotAnItem(QStringLiteral(
"setText")))
577 d->instance(
true)->setText(text);
581
582
583
584
585
586
587
588
589
590
591int QQuickToolTipAttached::delay()
const
593 Q_D(
const QQuickToolTipAttached);
594 return d->explicitDelay || d->policy == QQuickToolTip::Manual ? d->delay
595 : qt_quicktooltipattachedprivate_delay;
598void QQuickToolTipAttached::setDelay(
int delay)
600 Q_D(QQuickToolTipAttached);
601 d->setDelay(delay, QQml::PropertyUtils::State::ExplicitlySet);
605
606
607
608
609
610
611
612
613
614
615int QQuickToolTipAttached::timeout()
const
617 Q_D(
const QQuickToolTipAttached);
618 return d->explicitTimeout || d->policy == QQuickToolTip::Manual ? d->timeout
619 : d->calculateTimeout(d->text);
622void QQuickToolTipAttached::setTimeout(
int timeout)
624 Q_D(QQuickToolTipAttached);
625 d->setTimeout(timeout, QQml::PropertyUtils::State::ExplicitlySet);
629
630
631
632
633
634
635
636bool QQuickToolTipAttached::isVisible()
const
638 Q_D(
const QQuickToolTipAttached);
639 QQuickToolTip *tip = d->instance(
false);
643 return tip->isVisible() && tip->parentItem() == parent();
646void QQuickToolTipAttached::setVisible(
bool visible)
648 Q_D(QQuickToolTipAttached);
649 d->setVisible(visible, QQml::PropertyUtils::State::ExplicitlySet);
653
654
655
656
657
658
659
660QQuickToolTip *QQuickToolTipAttached::toolTip()
const
662 Q_D(
const QQuickToolTipAttached);
663 return d->instance(
true);
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709QQuickToolTip::Policy QQuickToolTipAttached::policy()
const
711 Q_D(
const QQuickToolTipAttached);
715void QQuickToolTipAttached::setVisiblePolicy(QQuickToolTip::Policy policy)
717 Q_D(QQuickToolTipAttached);
718 if (d->policy == policy)
722 d->propagatePolicy();
723 emit policyChanged();
726void QQuickToolTipAttached::resetVisiblePolicy()
728 setVisiblePolicy(QQuickToolTip::Automatic);
732
733
734
735
736
737
738
739void QQuickToolTipAttached::show(
const QString &text,
int ms)
741 Q_D(QQuickToolTipAttached);
742 if (d->warnIfAttacheeIsNotAnItem(QStringLiteral(
"show")))
745 QQuickToolTip *tip = d->instance(
true);
751 tip->setParentItem(qobject_cast<QQuickItem *>(parent()));
752 tip->setDelay(delay());
753 tip->setTimeout(ms >= 0 ? ms : timeout());
758
759
760
761
762
763
764void QQuickToolTipAttached::hide()
766 Q_D(QQuickToolTipAttached);
767 QQuickToolTip *tip = d->instance(
false);
771 if (parent() == tip->parentItem())
775void QQuickToolTipAttached::attachedParentChange(QQuickAttachedPropertyPropagator *newParent,
776 QQuickAttachedPropertyPropagator *)
778 auto *attachedToolTipParent = qobject_cast<QQuickToolTipAttached *>(newParent);
779 if (!attachedToolTipParent)
782 Q_D(QQuickToolTipAttached);
783 d->inheritPolicy(attachedToolTipParent->policy());
786void QQuickToolTipAttached::classBegin()
788 Q_D(QQuickToolTipAttached);
792void QQuickToolTipAttached::componentComplete()
794 Q_D(QQuickToolTipAttached);
797 if (d->pendingShow) {
798 d->pendingShow =
false;
805#include "moc_qquicktooltip_p.cpp"
Combined button and popup list for selecting options.