1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquicktooltip_p.h"
38#include "qquickpopup_p_p.h"
39#include "qquickpopupitem_p_p.h"
40#include "qquickcontrol_p_p.h"
41
42#include <QtCore/qbasictimer.h>
43#include <QtQml/qqmlinfo.h>
44#include <QtQml/qqmlengine.h>
45#include <QtQml/qqmlcontext.h>
46#include <QtQml/qqmlcomponent.h>
47#include <QtQuick/qquickwindow.h>
48
49QT_BEGIN_NAMESPACE
50
51/*!
52 \qmltype ToolTip
53 \inherits Popup
54//! \instantiates QQuickToolTip
55 \inqmlmodule QtQuick.Controls
56 \since 5.7
57 \ingroup qtquickcontrols2-popups
58 \brief Provides tool tips for any control.
59
60 A tool tip is a short piece of text that informs the user of a control's
61 function. It is typically placed above or below the parent control. The
62 tip text can be any \l{Rich Text Processing}{rich text} formatted string.
63
64 \image qtquickcontrols2-tooltip.png
65
66 \section2 Attached Tool Tips
67
68 The most straight-forward way to setup tool tips for controls is to
69 specify \l text and \l {visible}{visibility} via attached properties.
70 The following example illustrates this approach:
71
72 \snippet qtquickcontrols2-tooltip.qml 1
73
74 Under normal circumstances, there is only one tool tip visible at a time.
75 In order to save resources, all items that use the ToolTip attached property
76 share the same visual tool tip label instance. Even though the visuals are
77 shared, \c text, \c timeout and \c delay are stored individually for each item
78 that uses the respective attached property. However, multiple items cannot
79 make the shared tool tip visible at the same time. The shared tool tip is only
80 shown for the last item that made it visible. The position of the shared tool
81 tip is determined by the framework.
82
83 \section2 Delay and Timeout
84
85 Tool tips are typically transient in a sense that they are shown as a
86 result of a certain external event or user interaction, and they usually
87 hide after a certain timeout. It is possible to control the delay when
88 a tool tip is shown, and the timeout when it is hidden. This makes it
89 possible to implement varying strategies for showing and hiding tool tips.
90
91 For example, on touch screens, it is a common pattern to show a tool tip
92 as a result of pressing and holding down a button. The following example
93 demonstrates how to delay showing a tool tip until the press-and-hold
94 interval is reached. In this example, the tool tip hides as soon as the
95 button is released.
96
97 \snippet qtquickcontrols2-tooltip-pressandhold.qml 1
98
99 With pointer devices, however, it might be desired to show a tool tip as
100 a result of hovering a button for a while. The following example presents
101 how to show a tool tip after hovering a button for a second, and hide it
102 after a timeout of five seconds.
103
104 \snippet qtquickcontrols2-tooltip-hover.qml 1
105
106 \section2 Custom Tool Tips
107
108 Should one need more fine-grained control over the tool tip position, or
109 multiple simultaneous tool tip instances are needed, it is also possible
110 to create local tool tip instances. This way, it is possible to
111 \l {Customizing ToolTip}{customize} the tool tip, and the whole \l Popup
112 API is available. The following example presents a tool tip that presents
113 the value of a slider when the handle is dragged.
114
115 \image qtquickcontrols2-tooltip-slider.png
116
117 \snippet qtquickcontrols2-tooltip-slider.qml 1
118
119 \sa {Customizing ToolTip}, {Popup Controls}
120*/
121
122class QQuickToolTipPrivate : public QQuickPopupPrivate
123{
124 Q_DECLARE_PUBLIC(QQuickToolTip)
125
126public:
127 void startDelay();
128 void stopDelay();
129
130 void startTimeout();
131 void stopTimeout();
132
133 int delay = 0;
134 int timeout = -1;
135 QString text;
136 QBasicTimer delayTimer;
137 QBasicTimer timeoutTimer;
138};
139
140void QQuickToolTipPrivate::startDelay()
141{
142 Q_Q(QQuickToolTip);
143 if (delay > 0)
144 delayTimer.start(msec: delay, obj: q);
145}
146
147void QQuickToolTipPrivate::stopDelay()
148{
149 delayTimer.stop();
150}
151
152void QQuickToolTipPrivate::startTimeout()
153{
154 Q_Q(QQuickToolTip);
155 if (timeout > 0)
156 timeoutTimer.start(msec: timeout, obj: q);
157}
158
159void QQuickToolTipPrivate::stopTimeout()
160{
161 timeoutTimer.stop();
162}
163
164QQuickToolTip::QQuickToolTip(QQuickItem *parent)
165 : QQuickPopup(*(new QQuickToolTipPrivate), parent)
166{
167 Q_D(QQuickToolTip);
168 d->allowVerticalFlip = true;
169 d->allowHorizontalFlip = true;
170 d->popupItem->setHoverEnabled(false); // QTBUG-63644
171}
172
173/*!
174 \qmlproperty string QtQuick.Controls::ToolTip::text
175
176 This property holds the text shown on the tool tip.
177*/
178QString QQuickToolTip::text() const
179{
180 Q_D(const QQuickToolTip);
181 return d->text;
182}
183
184void QQuickToolTip::setText(const QString &text)
185{
186 Q_D(QQuickToolTip);
187 if (d->text == text)
188 return;
189
190 d->text = text;
191 maybeSetAccessibleName(name: text);
192 emit textChanged();
193}
194
195/*!
196 \qmlproperty int QtQuick.Controls::ToolTip::delay
197
198 This property holds the delay (milliseconds) after which the tool tip is
199 shown. A tooltip with a negative delay is shown immediately. The default
200 value is \c 0.
201
202 \sa {Delay and Timeout}
203*/
204int QQuickToolTip::delay() const
205{
206 Q_D(const QQuickToolTip);
207 return d->delay;
208}
209
210void QQuickToolTip::setDelay(int delay)
211{
212 Q_D(QQuickToolTip);
213 if (d->delay == delay)
214 return;
215
216 d->delay = delay;
217 emit delayChanged();
218}
219
220/*!
221 \qmlproperty int QtQuick.Controls::ToolTip::timeout
222
223 This property holds the timeout (milliseconds) after which the tool tip is
224 hidden. A tooltip with a negative timeout does not hide automatically. The
225 default value is \c -1.
226
227 \sa {Delay and Timeout}
228*/
229int QQuickToolTip::timeout() const
230{
231 Q_D(const QQuickToolTip);
232 return d->timeout;
233}
234
235void QQuickToolTip::setTimeout(int timeout)
236{
237 Q_D(QQuickToolTip);
238 if (d->timeout == timeout)
239 return;
240
241 d->timeout = timeout;
242
243 if (timeout <= 0)
244 d->stopTimeout();
245 else if (isVisible())
246 d->startTimeout();
247
248 emit timeoutChanged();
249}
250
251void QQuickToolTip::setVisible(bool visible)
252{
253 Q_D(QQuickToolTip);
254 if (visible) {
255 if (!d->visible) {
256 // We are being made visible, and we weren't before.
257 if (d->delay > 0) {
258 d->startDelay();
259 return;
260 }
261 } else {
262 // We are being made visible, even though we already were.
263 // We've probably been re-opened before our exit transition could finish.
264 // In that case, we need to manually start the timeout, as that is usually
265 // done in itemChange(), which won't be called in this situation.
266 d->startTimeout();
267 }
268 } else {
269 d->stopDelay();
270 }
271 QQuickPopup::setVisible(visible);
272}
273
274QQuickToolTipAttached *QQuickToolTip::qmlAttachedProperties(QObject *object)
275{
276 QQuickItem *item = qobject_cast<QQuickItem *>(object);
277 if (!item)
278 qmlWarning(me: object) << "ToolTip must be attached to an Item";
279
280 return new QQuickToolTipAttached(object);
281}
282
283/*!
284 \since QtQuick.Controls 2.5 (Qt 5.12)
285 \qmlmethod void QtQuick.Controls::ToolTip::show(string text, int timeout)
286
287 This method shows the \a text as a tooltip, which times out in
288 \a timeout (milliseconds).
289*/
290void QQuickToolTip::show(const QString &text, int ms)
291{
292 if (ms >= 0)
293 setTimeout(ms);
294 setText(text);
295 open();
296}
297
298/*!
299 \since QtQuick.Controls 2.5 (Qt 5.12)
300 \qmlmethod void QtQuick.Controls::ToolTip::hide()
301
302 This method hides the tooltip.
303*/
304void QQuickToolTip::hide()
305{
306 close();
307}
308
309QFont QQuickToolTip::defaultFont() const
310{
311 return QQuickTheme::font(scope: QQuickTheme::ToolTip);
312}
313
314QPalette QQuickToolTip::defaultPalette() const
315{
316 return QQuickTheme::palette(scope: QQuickTheme::ToolTip);
317}
318
319void QQuickToolTip::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
320{
321 Q_D(QQuickToolTip);
322 QQuickPopup::itemChange(change, data);
323 if (change == QQuickItem::ItemVisibleHasChanged) {
324 if (data.boolValue)
325 d->startTimeout();
326 else
327 d->stopTimeout();
328
329 QQuickToolTipAttached *attached = qobject_cast<QQuickToolTipAttached *>(object: qmlAttachedPropertiesObject<QQuickToolTip>(obj: d->parentItem, create: false));
330 if (attached)
331 emit attached->visibleChanged();
332 }
333}
334
335void QQuickToolTip::timerEvent(QTimerEvent *event)
336{
337 Q_D(QQuickToolTip);
338 if (event->timerId() == d->timeoutTimer.timerId()) {
339 d->stopTimeout();
340 QQuickPopup::setVisible(false);
341 return;
342 }
343 if (event->timerId() == d->delayTimer.timerId()) {
344 d->stopDelay();
345 QQuickPopup::setVisible(true);
346 return;
347 }
348 QQuickPopup::timerEvent(event);
349}
350
351#if QT_CONFIG(accessibility)
352QAccessible::Role QQuickToolTip::accessibleRole() const
353{
354 return QAccessible::ToolTip;
355}
356
357void QQuickToolTip::accessibilityActiveChanged(bool active)
358{
359 Q_D(QQuickToolTip);
360 QQuickPopup::accessibilityActiveChanged(active);
361
362 if (active)
363 maybeSetAccessibleName(name: d->text);
364}
365#endif
366
367class QQuickToolTipAttachedPrivate : public QObjectPrivate
368{
369 Q_DECLARE_PUBLIC(QQuickToolTipAttached)
370
371public:
372 QQuickToolTip *instance(bool create) const;
373
374 int delay = 0;
375 int timeout = -1;
376 QString text;
377};
378
379QQuickToolTip *QQuickToolTipAttachedPrivate::instance(bool create) const
380{
381 QQmlEngine *engine = qmlEngine(parent);
382 if (!engine)
383 return nullptr;
384
385 static const char *name = "_q_QQuickToolTip";
386
387 QQuickToolTip *tip = engine->property(name).value<QQuickToolTip *>();
388 if (!tip && create) {
389 // TODO: a cleaner way to create the instance? QQml(Meta)Type?
390 QQmlComponent component(engine);
391 component.setData("import QtQuick.Controls 2.4; ToolTip { }", baseUrl: QUrl());
392
393 QObject *object = component.create();
394 if (object)
395 object->setParent(engine);
396
397 tip = qobject_cast<QQuickToolTip *>(object);
398 if (!tip)
399 delete object;
400 else
401 engine->setProperty(name, value: QVariant::fromValue(value: object));
402 }
403 return tip;
404}
405
406QQuickToolTipAttached::QQuickToolTipAttached(QObject *parent)
407 : QObject(*(new QQuickToolTipAttachedPrivate), parent)
408{
409}
410
411/*!
412 \qmlattachedproperty string QtQuick.Controls::ToolTip::text
413
414 This attached property holds the text of the shared tool tip.
415 The property can be attached to any item.
416
417 \sa {Attached Tool Tips}
418*/
419QString QQuickToolTipAttached::text() const
420{
421 Q_D(const QQuickToolTipAttached);
422 return d->text;
423}
424
425void QQuickToolTipAttached::setText(const QString &text)
426{
427 Q_D(QQuickToolTipAttached);
428 if (d->text == text)
429 return;
430
431 d->text = text;
432 emit textChanged();
433
434 if (isVisible())
435 d->instance(create: true)->setText(text);
436}
437
438/*!
439 \qmlattachedproperty int QtQuick.Controls::ToolTip::delay
440
441 This attached property holds the delay (milliseconds) of the shared tool tip.
442 The property can be attached to any item.
443
444 \sa {Attached Tool Tips}, {Delay and Timeout}
445*/
446int QQuickToolTipAttached::delay() const
447{
448 Q_D(const QQuickToolTipAttached);
449 return d->delay;
450}
451
452void QQuickToolTipAttached::setDelay(int delay)
453{
454 Q_D(QQuickToolTipAttached);
455 if (d->delay == delay)
456 return;
457
458 d->delay = delay;
459 emit delayChanged();
460
461 if (isVisible())
462 d->instance(create: true)->setDelay(delay);
463}
464
465/*!
466 \qmlattachedproperty int QtQuick.Controls::ToolTip::timeout
467
468 This attached property holds the timeout (milliseconds) of the shared tool tip.
469 The property can be attached to any item.
470
471 \sa {Attached Tool Tips}, {Delay and Timeout}
472*/
473int QQuickToolTipAttached::timeout() const
474{
475 Q_D(const QQuickToolTipAttached);
476 return d->timeout;
477}
478
479void QQuickToolTipAttached::setTimeout(int timeout)
480{
481 Q_D(QQuickToolTipAttached);
482 if (d->timeout == timeout)
483 return;
484
485 d->timeout = timeout;
486 emit timeoutChanged();
487
488 if (isVisible())
489 d->instance(create: true)->setTimeout(timeout);
490}
491
492/*!
493 \qmlattachedproperty bool QtQuick.Controls::ToolTip::visible
494
495 This attached property holds whether the shared tool tip is visible.
496 The property can be attached to any item.
497
498 \sa {Attached Tool Tips}
499*/
500bool QQuickToolTipAttached::isVisible() const
501{
502 Q_D(const QQuickToolTipAttached);
503 QQuickToolTip *tip = d->instance(create: false);
504 if (!tip)
505 return false;
506
507 return tip->isVisible() && tip->parentItem() == parent();
508}
509
510void QQuickToolTipAttached::setVisible(bool visible)
511{
512 Q_D(QQuickToolTipAttached);
513 if (visible)
514 show(text: d->text);
515 else
516 hide();
517}
518
519/*!
520 \qmlattachedproperty ToolTip QtQuick.Controls::ToolTip::toolTip
521
522 This attached property holds the shared tool tip instance. The property
523 can be attached to any item.
524
525 \sa {Attached Tool Tips}
526*/
527QQuickToolTip *QQuickToolTipAttached::toolTip() const
528{
529 Q_D(const QQuickToolTipAttached);
530 return d->instance(create: true);
531}
532
533/*!
534 \qmlattachedmethod void QtQuick.Controls::ToolTip::show(string text, int timeout = -1)
535
536 This attached method shows the shared tooltip with \a text and \a timeout (milliseconds).
537 The method can be attached to any item.
538
539 \sa {Attached Tool Tips}
540*/
541void QQuickToolTipAttached::show(const QString &text, int ms)
542{
543 Q_D(QQuickToolTipAttached);
544 QQuickToolTip *tip = d->instance(create: true);
545 if (!tip)
546 return;
547
548 tip->resetWidth();
549 tip->resetHeight();
550 tip->setParentItem(qobject_cast<QQuickItem *>(object: parent()));
551 tip->setDelay(d->delay);
552 tip->setTimeout(ms >= 0 ? ms : d->timeout);
553 tip->show(text);
554}
555
556/*!
557 \qmlattachedmethod void QtQuick.Controls::ToolTip::hide()
558
559 This attached method hides the shared tooltip. The method can be attached to any item.
560
561 \sa {Attached Tool Tips}
562*/
563void QQuickToolTipAttached::hide()
564{
565 Q_D(QQuickToolTipAttached);
566 QQuickToolTip *tip = d->instance(create: false);
567 if (!tip)
568 return;
569 // check the parent item to prevent unexpectedly closing tooltip by new created invisible tooltip
570 if (parent() == tip->parentItem())
571 tip->close();
572}
573
574QT_END_NAMESPACE
575

source code of qtquickcontrols2/src/quicktemplates2/qquicktooltip.cpp