Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/kernel/qtooltip.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||||||||
34 | # include <private/qcore_mac_p.h> | - | ||||||||||||||||||
35 | #endif | - | ||||||||||||||||||
36 | - | |||||||||||||||||||
37 | #include <qapplication.h> | - | ||||||||||||||||||
38 | #include <qdesktopwidget.h> | - | ||||||||||||||||||
39 | #include <qevent.h> | - | ||||||||||||||||||
40 | #include <qlabel.h> | - | ||||||||||||||||||
41 | #include <qpointer.h> | - | ||||||||||||||||||
42 | #include <qstyle.h> | - | ||||||||||||||||||
43 | #include <qstyleoption.h> | - | ||||||||||||||||||
44 | #include <qstylepainter.h> | - | ||||||||||||||||||
45 | #include <qtimer.h> | - | ||||||||||||||||||
46 | #include <qtooltip.h> | - | ||||||||||||||||||
47 | #include <private/qeffects_p.h> | - | ||||||||||||||||||
48 | #include <qtextdocument.h> | - | ||||||||||||||||||
49 | #include <qdebug.h> | - | ||||||||||||||||||
50 | #include <private/qstylesheetstyle_p.h> | - | ||||||||||||||||||
51 | #ifndef QT_NO_TOOLTIP | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||||||||
54 | # include <private/qcore_mac_p.h> | - | ||||||||||||||||||
55 | #include <private/qt_cocoa_helpers_mac_p.h> | - | ||||||||||||||||||
56 | #endif | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | /*! | - | ||||||||||||||||||
61 | \class QToolTip | - | ||||||||||||||||||
62 | - | |||||||||||||||||||
63 | \brief The QToolTip class provides tool tips (balloon help) for any | - | ||||||||||||||||||
64 | widget. | - | ||||||||||||||||||
65 | - | |||||||||||||||||||
66 | \ingroup helpsystem | - | ||||||||||||||||||
67 | \inmodule QtWidgets | - | ||||||||||||||||||
68 | - | |||||||||||||||||||
69 | The tip is a short piece of text reminding the user of the | - | ||||||||||||||||||
70 | widget's function. It is drawn immediately below the given | - | ||||||||||||||||||
71 | position in a distinctive black-on-yellow color combination. The | - | ||||||||||||||||||
72 | tip can be any \l{QTextEdit}{rich text} formatted string. | - | ||||||||||||||||||
73 | - | |||||||||||||||||||
74 | Rich text displayed in a tool tip is implicitly word-wrapped unless | - | ||||||||||||||||||
75 | specified differently with \c{<p style='white-space:pre'>}. | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | The simplest and most common way to set a widget's tool tip is by | - | ||||||||||||||||||
78 | calling its QWidget::setToolTip() function. | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | It is also possible to show different tool tips for different | - | ||||||||||||||||||
81 | regions of a widget, by using a QHelpEvent of type | - | ||||||||||||||||||
82 | QEvent::ToolTip. Intercept the help event in your widget's \l | - | ||||||||||||||||||
83 | {QWidget::}{event()} function and call QToolTip::showText() with | - | ||||||||||||||||||
84 | the text you want to display. The \l{widgets/tooltips}{Tooltips} | - | ||||||||||||||||||
85 | example illustrates this technique. | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | If you are calling QToolTip::hideText(), or QToolTip::showText() | - | ||||||||||||||||||
88 | with an empty string, as a result of a \l{QEvent::}{ToolTip}-event you | - | ||||||||||||||||||
89 | should also call \l{QEvent::}{ignore()} on the event, to signal | - | ||||||||||||||||||
90 | that you don't want to start any tooltip specific modes. | - | ||||||||||||||||||
91 | - | |||||||||||||||||||
92 | Note that, if you want to show tooltips in an item view, the | - | ||||||||||||||||||
93 | model/view architecture provides functionality to set an item's | - | ||||||||||||||||||
94 | tool tip; e.g., the QTableWidgetItem::setToolTip() function. | - | ||||||||||||||||||
95 | However, if you want to provide custom tool tips in an item view, | - | ||||||||||||||||||
96 | you must intercept the help event in the | - | ||||||||||||||||||
97 | QAbstractItemView::viewportEvent() function and handle it yourself. | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | The default tool tip color and font can be customized with | - | ||||||||||||||||||
100 | setPalette() and setFont(). When a tooltip is currently on | - | ||||||||||||||||||
101 | display, isVisible() returns \c true and text() the currently visible | - | ||||||||||||||||||
102 | text. | - | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | \note Tool tips use the inactive color group of QPalette, because tool | - | ||||||||||||||||||
105 | tips are not active windows. | - | ||||||||||||||||||
106 | - | |||||||||||||||||||
107 | \sa QWidget::toolTip, QAction::toolTip, {Tool Tips Example} | - | ||||||||||||||||||
108 | */ | - | ||||||||||||||||||
109 | - | |||||||||||||||||||
110 | class QTipLabel : public QLabel | - | ||||||||||||||||||
111 | { | - | ||||||||||||||||||
112 | Q_OBJECT | - | ||||||||||||||||||
113 | public: | - | ||||||||||||||||||
114 | QTipLabel(const QString &text, QWidget *w, int msecDisplayTime); | - | ||||||||||||||||||
115 | ~QTipLabel(); | - | ||||||||||||||||||
116 | static QTipLabel *instance; | - | ||||||||||||||||||
117 | - | |||||||||||||||||||
118 | bool eventFilter(QObject *, QEvent *) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
119 | - | |||||||||||||||||||
120 | QBasicTimer hideTimer, expireTimer; | - | ||||||||||||||||||
121 | - | |||||||||||||||||||
122 | bool fadingOut; | - | ||||||||||||||||||
123 | - | |||||||||||||||||||
124 | void reuseTip(const QString &text, int msecDisplayTime); | - | ||||||||||||||||||
125 | void hideTip(); | - | ||||||||||||||||||
126 | void hideTipImmediately(); | - | ||||||||||||||||||
127 | void setTipRect(QWidget *w, const QRect &r); | - | ||||||||||||||||||
128 | void restartExpireTimer(int msecDisplayTime); | - | ||||||||||||||||||
129 | bool tipChanged(const QPoint &pos, const QString &text, QObject *o); | - | ||||||||||||||||||
130 | void placeTip(const QPoint &pos, QWidget *w); | - | ||||||||||||||||||
131 | - | |||||||||||||||||||
132 | static int getTipScreen(const QPoint &pos, QWidget *w); | - | ||||||||||||||||||
133 | protected: | - | ||||||||||||||||||
134 | void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
135 | void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
136 | void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
137 | void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
138 | - | |||||||||||||||||||
139 | #ifndef QT_NO_STYLE_STYLESHEET | - | ||||||||||||||||||
140 | public slots: | - | ||||||||||||||||||
141 | /** \internal | - | ||||||||||||||||||
142 | Cleanup the _q_stylesheet_parent propery. | - | ||||||||||||||||||
143 | */ | - | ||||||||||||||||||
144 | void styleSheetParentDestroyed() { | - | ||||||||||||||||||
145 | setProperty("_q_stylesheet_parent", QVariant()); | - | ||||||||||||||||||
146 | styleSheetParent = 0; | - | ||||||||||||||||||
147 | } never executed: end of block | 0 | ||||||||||||||||||
148 | - | |||||||||||||||||||
149 | private: | - | ||||||||||||||||||
150 | QWidget *styleSheetParent; | - | ||||||||||||||||||
151 | #endif | - | ||||||||||||||||||
152 | - | |||||||||||||||||||
153 | private: | - | ||||||||||||||||||
154 | QWidget *widget; | - | ||||||||||||||||||
155 | QRect rect; | - | ||||||||||||||||||
156 | }; | - | ||||||||||||||||||
157 | - | |||||||||||||||||||
158 | QTipLabel *QTipLabel::instance = 0; | - | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | QTipLabel::QTipLabel(const QString &text, QWidget *w, int msecDisplayTime) | - | ||||||||||||||||||
161 | #ifndef QT_NO_STYLE_STYLESHEET | - | ||||||||||||||||||
162 | : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget), styleSheetParent(0), widget(0) | - | ||||||||||||||||||
163 | #else | - | ||||||||||||||||||
164 | : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget), widget(0) | - | ||||||||||||||||||
165 | #endif | - | ||||||||||||||||||
166 | { | - | ||||||||||||||||||
167 | delete instance; | - | ||||||||||||||||||
168 | instance = this; | - | ||||||||||||||||||
169 | setForegroundRole(QPalette::ToolTipText); | - | ||||||||||||||||||
170 | setBackgroundRole(QPalette::ToolTipBase); | - | ||||||||||||||||||
171 | setPalette(QToolTip::palette()); | - | ||||||||||||||||||
172 | ensurePolished(); | - | ||||||||||||||||||
173 | setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this)); | - | ||||||||||||||||||
174 | setFrameStyle(QFrame::NoFrame); | - | ||||||||||||||||||
175 | setAlignment(Qt::AlignLeft); | - | ||||||||||||||||||
176 | setIndent(1); | - | ||||||||||||||||||
177 | qApp->installEventFilter(this); | - | ||||||||||||||||||
178 | setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0); | - | ||||||||||||||||||
179 | setMouseTracking(true); | - | ||||||||||||||||||
180 | fadingOut = false; | - | ||||||||||||||||||
181 | reuseTip(text, msecDisplayTime); | - | ||||||||||||||||||
182 | } never executed: end of block | 0 | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | void QTipLabel::restartExpireTimer(int msecDisplayTime) | - | ||||||||||||||||||
185 | { | - | ||||||||||||||||||
186 | int time = 10000 + 40 * qMax(0, text().length()-100); | - | ||||||||||||||||||
187 | if (msecDisplayTime > 0)
| 0 | ||||||||||||||||||
188 | time = msecDisplayTime; never executed: time = msecDisplayTime; | 0 | ||||||||||||||||||
189 | expireTimer.start(time, this); | - | ||||||||||||||||||
190 | hideTimer.stop(); | - | ||||||||||||||||||
191 | } never executed: end of block | 0 | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | void QTipLabel::reuseTip(const QString &text, int msecDisplayTime) | - | ||||||||||||||||||
194 | { | - | ||||||||||||||||||
195 | #ifndef QT_NO_STYLE_STYLESHEET | - | ||||||||||||||||||
196 | if (styleSheetParent){
| 0 | ||||||||||||||||||
197 | disconnect(styleSheetParent, SIGNAL(destroyed()), | - | ||||||||||||||||||
198 | QTipLabel::instance, SLOT(styleSheetParentDestroyed())); | - | ||||||||||||||||||
199 | styleSheetParent = 0; | - | ||||||||||||||||||
200 | } never executed: end of block | 0 | ||||||||||||||||||
201 | #endif | - | ||||||||||||||||||
202 | - | |||||||||||||||||||
203 | setWordWrap(Qt::mightBeRichText(text)); | - | ||||||||||||||||||
204 | setText(text); | - | ||||||||||||||||||
205 | QFontMetrics fm(font()); | - | ||||||||||||||||||
206 | QSize extra(1, 0); | - | ||||||||||||||||||
207 | // Make it look good with the default ToolTip font on Mac, which has a small descent. | - | ||||||||||||||||||
208 | if (fm.descent() == 2 && fm.ascent() >= 11)
| 0 | ||||||||||||||||||
209 | ++extra.rheight(); never executed: ++extra.rheight(); | 0 | ||||||||||||||||||
210 | resize(sizeHint() + extra); | - | ||||||||||||||||||
211 | restartExpireTimer(msecDisplayTime); | - | ||||||||||||||||||
212 | } never executed: end of block | 0 | ||||||||||||||||||
213 | - | |||||||||||||||||||
214 | void QTipLabel::paintEvent(QPaintEvent *ev) | - | ||||||||||||||||||
215 | { | - | ||||||||||||||||||
216 | QStylePainter p(this); | - | ||||||||||||||||||
217 | QStyleOptionFrame opt; | - | ||||||||||||||||||
218 | opt.init(this); | - | ||||||||||||||||||
219 | p.drawPrimitive(QStyle::PE_PanelTipLabel, opt); | - | ||||||||||||||||||
220 | p.end(); | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | QLabel::paintEvent(ev); | - | ||||||||||||||||||
223 | } never executed: end of block | 0 | ||||||||||||||||||
224 | - | |||||||||||||||||||
225 | void QTipLabel::resizeEvent(QResizeEvent *e) | - | ||||||||||||||||||
226 | { | - | ||||||||||||||||||
227 | QStyleHintReturnMask frameMask; | - | ||||||||||||||||||
228 | QStyleOption option; | - | ||||||||||||||||||
229 | option.init(this); | - | ||||||||||||||||||
230 | if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option, this, &frameMask))
| 0 | ||||||||||||||||||
231 | setMask(frameMask.region); never executed: setMask(frameMask.region); | 0 | ||||||||||||||||||
232 | - | |||||||||||||||||||
233 | QLabel::resizeEvent(e); | - | ||||||||||||||||||
234 | } never executed: end of block | 0 | ||||||||||||||||||
235 | - | |||||||||||||||||||
236 | void QTipLabel::mouseMoveEvent(QMouseEvent *e) | - | ||||||||||||||||||
237 | { | - | ||||||||||||||||||
238 | if (rect.isNull())
| 0 | ||||||||||||||||||
239 | return; never executed: return; | 0 | ||||||||||||||||||
240 | QPoint pos = e->globalPos(); | - | ||||||||||||||||||
241 | if (widget)
| 0 | ||||||||||||||||||
242 | pos = widget->mapFromGlobal(pos); never executed: pos = widget->mapFromGlobal(pos); | 0 | ||||||||||||||||||
243 | if (!rect.contains(pos))
| 0 | ||||||||||||||||||
244 | hideTip(); never executed: hideTip(); | 0 | ||||||||||||||||||
245 | QLabel::mouseMoveEvent(e); | - | ||||||||||||||||||
246 | } never executed: end of block | 0 | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | QTipLabel::~QTipLabel() | - | ||||||||||||||||||
249 | { | - | ||||||||||||||||||
250 | instance = 0; | - | ||||||||||||||||||
251 | } never executed: end of block | 0 | ||||||||||||||||||
252 | - | |||||||||||||||||||
253 | void QTipLabel::hideTip() | - | ||||||||||||||||||
254 | { | - | ||||||||||||||||||
255 | if (!hideTimer.isActive())
| 0 | ||||||||||||||||||
256 | hideTimer.start(300, this); never executed: hideTimer.start(300, this); | 0 | ||||||||||||||||||
257 | } never executed: end of block | 0 | ||||||||||||||||||
258 | - | |||||||||||||||||||
259 | void QTipLabel::hideTipImmediately() | - | ||||||||||||||||||
260 | { | - | ||||||||||||||||||
261 | close(); // to trigger QEvent::Close which stops the animation | - | ||||||||||||||||||
262 | deleteLater(); | - | ||||||||||||||||||
263 | } never executed: end of block | 0 | ||||||||||||||||||
264 | - | |||||||||||||||||||
265 | void QTipLabel::setTipRect(QWidget *w, const QRect &r) | - | ||||||||||||||||||
266 | { | - | ||||||||||||||||||
267 | if (!r.isNull() && !w)
| 0 | ||||||||||||||||||
268 | qWarning("QToolTip::setTipRect: Cannot pass null widget if rect is set"); never executed: QMessageLogger(__FILE__, 268, __PRETTY_FUNCTION__).warning("QToolTip::setTipRect: Cannot pass null widget if rect is set"); | 0 | ||||||||||||||||||
269 | else{ | - | ||||||||||||||||||
270 | widget = w; | - | ||||||||||||||||||
271 | rect = r; | - | ||||||||||||||||||
272 | } never executed: end of block | 0 | ||||||||||||||||||
273 | } | - | ||||||||||||||||||
274 | - | |||||||||||||||||||
275 | void QTipLabel::timerEvent(QTimerEvent *e) | - | ||||||||||||||||||
276 | { | - | ||||||||||||||||||
277 | if (e->timerId() == hideTimer.timerId()
| 0 | ||||||||||||||||||
278 | || e->timerId() == expireTimer.timerId()){
| 0 | ||||||||||||||||||
279 | hideTimer.stop(); | - | ||||||||||||||||||
280 | expireTimer.stop(); | - | ||||||||||||||||||
281 | #if defined(Q_DEAD_CODE_FROM_QT4_MAC) && !defined(QT_NO_EFFECTS) | - | ||||||||||||||||||
282 | if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip)){ | - | ||||||||||||||||||
283 | // Fade out tip on mac (makes it invisible). | - | ||||||||||||||||||
284 | // The tip will not be deleted until a new tip is shown. | - | ||||||||||||||||||
285 | - | |||||||||||||||||||
286 | // DRSWAT - Cocoa | - | ||||||||||||||||||
287 | macWindowFade(qt_mac_window_for(this)); | - | ||||||||||||||||||
288 | QTipLabel::instance->fadingOut = true; // will never be false again. | - | ||||||||||||||||||
289 | } | - | ||||||||||||||||||
290 | else | - | ||||||||||||||||||
291 | hideTipImmediately(); | - | ||||||||||||||||||
292 | #else | - | ||||||||||||||||||
293 | hideTipImmediately(); | - | ||||||||||||||||||
294 | #endif | - | ||||||||||||||||||
295 | } never executed: end of block | 0 | ||||||||||||||||||
296 | } never executed: end of block | 0 | ||||||||||||||||||
297 | - | |||||||||||||||||||
298 | bool QTipLabel::eventFilter(QObject *o, QEvent *e) | - | ||||||||||||||||||
299 | { | - | ||||||||||||||||||
300 | switch (e->type()) { | - | ||||||||||||||||||
301 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||||||||
302 | case QEvent::KeyPress: | - | ||||||||||||||||||
303 | case QEvent::KeyRelease: { | - | ||||||||||||||||||
304 | int key = static_cast<QKeyEvent *>(e)->key(); | - | ||||||||||||||||||
305 | Qt::KeyboardModifiers mody = static_cast<QKeyEvent *>(e)->modifiers(); | - | ||||||||||||||||||
306 | if (!(mody & Qt::KeyboardModifierMask) | - | ||||||||||||||||||
307 | && key != Qt::Key_Shift && key != Qt::Key_Control | - | ||||||||||||||||||
308 | && key != Qt::Key_Alt && key != Qt::Key_Meta) | - | ||||||||||||||||||
309 | hideTip(); | - | ||||||||||||||||||
310 | break; | - | ||||||||||||||||||
311 | } | - | ||||||||||||||||||
312 | #endif | - | ||||||||||||||||||
313 | case QEvent::Leave: never executed: case QEvent::Leave: | 0 | ||||||||||||||||||
314 | hideTip(); | - | ||||||||||||||||||
315 | break; never executed: break; | 0 | ||||||||||||||||||
316 | - | |||||||||||||||||||
317 | - | |||||||||||||||||||
318 | #if defined (Q_OS_QNX) // On QNX the window activate and focus events are delayed and will appear | - | ||||||||||||||||||
319 | // after the window is shown. | - | ||||||||||||||||||
320 | case QEvent::WindowActivate: | - | ||||||||||||||||||
321 | case QEvent::FocusIn: | - | ||||||||||||||||||
322 | return false; | - | ||||||||||||||||||
323 | case QEvent::WindowDeactivate: | - | ||||||||||||||||||
324 | if (o != this) | - | ||||||||||||||||||
325 | return false; | - | ||||||||||||||||||
326 | hideTipImmediately(); | - | ||||||||||||||||||
327 | break; | - | ||||||||||||||||||
328 | case QEvent::FocusOut: | - | ||||||||||||||||||
329 | if (reinterpret_cast<QWindow*>(o) != windowHandle()) | - | ||||||||||||||||||
330 | return false; | - | ||||||||||||||||||
331 | hideTipImmediately(); | - | ||||||||||||||||||
332 | break; | - | ||||||||||||||||||
333 | #else | - | ||||||||||||||||||
334 | case QEvent::WindowActivate: never executed: case QEvent::WindowActivate: | 0 | ||||||||||||||||||
335 | case QEvent::WindowDeactivate: never executed: case QEvent::WindowDeactivate: | 0 | ||||||||||||||||||
336 | case QEvent::FocusIn: never executed: case QEvent::FocusIn: | 0 | ||||||||||||||||||
337 | case QEvent::FocusOut: never executed: case QEvent::FocusOut: | 0 | ||||||||||||||||||
338 | #endif | - | ||||||||||||||||||
339 | case QEvent::Close: // For QTBUG-55523 (QQC) specifically: Hide tooltip when windows are closed never executed: case QEvent::Close: | 0 | ||||||||||||||||||
340 | case QEvent::MouseButtonPress: never executed: case QEvent::MouseButtonPress: | 0 | ||||||||||||||||||
341 | case QEvent::MouseButtonRelease: never executed: case QEvent::MouseButtonRelease: | 0 | ||||||||||||||||||
342 | case QEvent::MouseButtonDblClick: never executed: case QEvent::MouseButtonDblClick: | 0 | ||||||||||||||||||
343 | case QEvent::Wheel: never executed: case QEvent::Wheel: | 0 | ||||||||||||||||||
344 | hideTipImmediately(); | - | ||||||||||||||||||
345 | break; never executed: break; | 0 | ||||||||||||||||||
346 | - | |||||||||||||||||||
347 | case QEvent::MouseMove: never executed: case QEvent::MouseMove: | 0 | ||||||||||||||||||
348 | if (o == widget && !rect.isNull() && !rect.contains(static_cast<QMouseEvent*>(e)->pos()))
| 0 | ||||||||||||||||||
349 | hideTip(); never executed: hideTip(); | 0 | ||||||||||||||||||
350 | default: code before this statement never executed: default: never executed: default: | 0 | ||||||||||||||||||
351 | break; never executed: break; | 0 | ||||||||||||||||||
352 | } | - | ||||||||||||||||||
353 | return false; never executed: return false; | 0 | ||||||||||||||||||
354 | } | - | ||||||||||||||||||
355 | - | |||||||||||||||||||
356 | int QTipLabel::getTipScreen(const QPoint &pos, QWidget *w) | - | ||||||||||||||||||
357 | { | - | ||||||||||||||||||
358 | if (QApplication::desktop()->isVirtualDesktop())
| 0 | ||||||||||||||||||
359 | return QApplication::desktop()->screenNumber(pos); never executed: return QApplication::desktop()->screenNumber(pos); | 0 | ||||||||||||||||||
360 | else | - | ||||||||||||||||||
361 | return QApplication::desktop()->screenNumber(w); never executed: return QApplication::desktop()->screenNumber(w); | 0 | ||||||||||||||||||
362 | } | - | ||||||||||||||||||
363 | - | |||||||||||||||||||
364 | void QTipLabel::placeTip(const QPoint &pos, QWidget *w) | - | ||||||||||||||||||
365 | { | - | ||||||||||||||||||
366 | #ifndef QT_NO_STYLE_STYLESHEET | - | ||||||||||||||||||
367 | if (testAttribute(Qt::WA_StyleSheet) || (w && qobject_cast<QStyleSheetStyle *>(w->style()))) {
| 0 | ||||||||||||||||||
368 | //the stylesheet need to know the real parent | - | ||||||||||||||||||
369 | QTipLabel::instance->setProperty("_q_stylesheet_parent", QVariant::fromValue(w)); | - | ||||||||||||||||||
370 | //we force the style to be the QStyleSheetStyle, and force to clear the cache as well. | - | ||||||||||||||||||
371 | QTipLabel::instance->setStyleSheet(QLatin1String("/* */")); | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | // Set up for cleaning up this later... | - | ||||||||||||||||||
374 | QTipLabel::instance->styleSheetParent = w; | - | ||||||||||||||||||
375 | if (w) {
| 0 | ||||||||||||||||||
376 | connect(w, SIGNAL(destroyed()), | - | ||||||||||||||||||
377 | QTipLabel::instance, SLOT(styleSheetParentDestroyed())); | - | ||||||||||||||||||
378 | } never executed: end of block | 0 | ||||||||||||||||||
379 | } never executed: end of block | 0 | ||||||||||||||||||
380 | #endif //QT_NO_STYLE_STYLESHEET | - | ||||||||||||||||||
381 | - | |||||||||||||||||||
382 | - | |||||||||||||||||||
383 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||||||||
384 | // When in full screen mode, there is no Dock nor Menu so we can use | - | ||||||||||||||||||
385 | // the whole screen for displaying the tooltip. However when not in | - | ||||||||||||||||||
386 | // full screen mode we need to save space for the dock, so we use | - | ||||||||||||||||||
387 | // availableGeometry instead. | - | ||||||||||||||||||
388 | extern bool qt_mac_app_fullscreen; //qapplication_mac.mm | - | ||||||||||||||||||
389 | QRect screen; | - | ||||||||||||||||||
390 | if(qt_mac_app_fullscreen) | - | ||||||||||||||||||
391 | screen = QApplication::desktop()->screenGeometry(getTipScreen(pos, w)); | - | ||||||||||||||||||
392 | else | - | ||||||||||||||||||
393 | screen = QApplication::desktop()->availableGeometry(getTipScreen(pos, w)); | - | ||||||||||||||||||
394 | #else | - | ||||||||||||||||||
395 | QRect screen = QApplication::desktop()->screenGeometry(getTipScreen(pos, w)); | - | ||||||||||||||||||
396 | #endif | - | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | QPoint p = pos; | - | ||||||||||||||||||
399 | p += QPoint(2, | - | ||||||||||||||||||
400 | #ifdef Q_DEAD_CODE_FROM_QT4_WIN | - | ||||||||||||||||||
401 | 21 | - | ||||||||||||||||||
402 | #else | - | ||||||||||||||||||
403 | 16 | - | ||||||||||||||||||
404 | #endif | - | ||||||||||||||||||
405 | ); | - | ||||||||||||||||||
406 | if (p.x() + this->width() > screen.x() + screen.width())
| 0 | ||||||||||||||||||
407 | p.rx() -= 4 + this->width(); never executed: p.rx() -= 4 + this->width(); | 0 | ||||||||||||||||||
408 | if (p.y() + this->height() > screen.y() + screen.height())
| 0 | ||||||||||||||||||
409 | p.ry() -= 24 + this->height(); never executed: p.ry() -= 24 + this->height(); | 0 | ||||||||||||||||||
410 | if (p.y() < screen.y())
| 0 | ||||||||||||||||||
411 | p.setY(screen.y()); never executed: p.setY(screen.y()); | 0 | ||||||||||||||||||
412 | if (p.x() + this->width() > screen.x() + screen.width())
| 0 | ||||||||||||||||||
413 | p.setX(screen.x() + screen.width() - this->width()); never executed: p.setX(screen.x() + screen.width() - this->width()); | 0 | ||||||||||||||||||
414 | if (p.x() < screen.x())
| 0 | ||||||||||||||||||
415 | p.setX(screen.x()); never executed: p.setX(screen.x()); | 0 | ||||||||||||||||||
416 | if (p.y() + this->height() > screen.y() + screen.height())
| 0 | ||||||||||||||||||
417 | p.setY(screen.y() + screen.height() - this->height()); never executed: p.setY(screen.y() + screen.height() - this->height()); | 0 | ||||||||||||||||||
418 | this->move(p); | - | ||||||||||||||||||
419 | } never executed: end of block | 0 | ||||||||||||||||||
420 | - | |||||||||||||||||||
421 | bool QTipLabel::tipChanged(const QPoint &pos, const QString &text, QObject *o) | - | ||||||||||||||||||
422 | { | - | ||||||||||||||||||
423 | if (QTipLabel::instance->text() != text)
| 0 | ||||||||||||||||||
424 | return true; never executed: return true; | 0 | ||||||||||||||||||
425 | - | |||||||||||||||||||
426 | if (o != widget)
| 0 | ||||||||||||||||||
427 | return true; never executed: return true; | 0 | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | if (!rect.isNull())
| 0 | ||||||||||||||||||
430 | return !rect.contains(pos); never executed: return !rect.contains(pos); | 0 | ||||||||||||||||||
431 | else | - | ||||||||||||||||||
432 | return false; never executed: return false; | 0 | ||||||||||||||||||
433 | } | - | ||||||||||||||||||
434 | - | |||||||||||||||||||
435 | /*! | - | ||||||||||||||||||
436 | Shows \a text as a tool tip, with the global position \a pos as | - | ||||||||||||||||||
437 | the point of interest. The tool tip will be shown with a platform | - | ||||||||||||||||||
438 | specific offset from this point of interest. | - | ||||||||||||||||||
439 | - | |||||||||||||||||||
440 | If you specify a non-empty rect the tip will be hidden as soon | - | ||||||||||||||||||
441 | as you move your cursor out of this area. | - | ||||||||||||||||||
442 | - | |||||||||||||||||||
443 | The \a rect is in the coordinates of the widget you specify with | - | ||||||||||||||||||
444 | \a w. If the \a rect is not empty you must specify a widget. | - | ||||||||||||||||||
445 | Otherwise this argument can be 0 but it is used to determine the | - | ||||||||||||||||||
446 | appropriate screen on multi-head systems. | - | ||||||||||||||||||
447 | - | |||||||||||||||||||
448 | If \a text is empty the tool tip is hidden. If the text is the | - | ||||||||||||||||||
449 | same as the currently shown tooltip, the tip will \e not move. | - | ||||||||||||||||||
450 | You can force moving by first hiding the tip with an empty text, | - | ||||||||||||||||||
451 | and then showing the new tip at the new position. | - | ||||||||||||||||||
452 | */ | - | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w, const QRect &rect) | - | ||||||||||||||||||
455 | { | - | ||||||||||||||||||
456 | showText(pos, text, w, rect, -1); | - | ||||||||||||||||||
457 | } never executed: end of block | 0 | ||||||||||||||||||
458 | - | |||||||||||||||||||
459 | /*! | - | ||||||||||||||||||
460 | \since 5.2 | - | ||||||||||||||||||
461 | \overload | - | ||||||||||||||||||
462 | This is similar to QToolTip::showText(\a pos, \a text, \a w, \a rect) but with an extra parameter \a msecDisplayTime | - | ||||||||||||||||||
463 | that specifies how long the tool tip will be displayed, in milliseconds. | - | ||||||||||||||||||
464 | */ | - | ||||||||||||||||||
465 | - | |||||||||||||||||||
466 | void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w, const QRect &rect, int msecDisplayTime) | - | ||||||||||||||||||
467 | { | - | ||||||||||||||||||
468 | if (QTipLabel::instance && QTipLabel::instance->isVisible()){ // a tip does already exist
| 0 | ||||||||||||||||||
469 | if (text.isEmpty()){ // empty text means hide current tip
| 0 | ||||||||||||||||||
470 | QTipLabel::instance->hideTip(); | - | ||||||||||||||||||
471 | return; never executed: return; | 0 | ||||||||||||||||||
472 | } | - | ||||||||||||||||||
473 | else if (!QTipLabel::instance->fadingOut){
| 0 | ||||||||||||||||||
474 | // If the tip has changed, reuse the one | - | ||||||||||||||||||
475 | // that is showing (removes flickering) | - | ||||||||||||||||||
476 | QPoint localPos = pos; | - | ||||||||||||||||||
477 | if (w)
| 0 | ||||||||||||||||||
478 | localPos = w->mapFromGlobal(pos); never executed: localPos = w->mapFromGlobal(pos); | 0 | ||||||||||||||||||
479 | if (QTipLabel::instance->tipChanged(localPos, text, w)){
| 0 | ||||||||||||||||||
480 | QTipLabel::instance->reuseTip(text, msecDisplayTime); | - | ||||||||||||||||||
481 | QTipLabel::instance->setTipRect(w, rect); | - | ||||||||||||||||||
482 | QTipLabel::instance->placeTip(pos, w); | - | ||||||||||||||||||
483 | } never executed: end of block | 0 | ||||||||||||||||||
484 | return; never executed: return; | 0 | ||||||||||||||||||
485 | } | - | ||||||||||||||||||
486 | } never executed: end of block | 0 | ||||||||||||||||||
487 | - | |||||||||||||||||||
488 | if (!text.isEmpty()){ // no tip can be reused, create new tip:
| 0 | ||||||||||||||||||
489 | #ifndef Q_DEAD_CODE_FROM_QT4_WIN | - | ||||||||||||||||||
490 | new QTipLabel(text, w, msecDisplayTime); // sets QTipLabel::instance to itself | - | ||||||||||||||||||
491 | #else | - | ||||||||||||||||||
492 | // On windows, we can't use the widget as parent otherwise the window will be | - | ||||||||||||||||||
493 | // raised when the tooltip will be shown | - | ||||||||||||||||||
494 | new QTipLabel(text, QApplication::desktop()->screen(QTipLabel::getTipScreen(pos, w)), msecDisplayTime); | - | ||||||||||||||||||
495 | #endif | - | ||||||||||||||||||
496 | QTipLabel::instance->setTipRect(w, rect); | - | ||||||||||||||||||
497 | QTipLabel::instance->placeTip(pos, w); | - | ||||||||||||||||||
498 | QTipLabel::instance->setObjectName(QLatin1String("qtooltip_label")); | - | ||||||||||||||||||
499 | - | |||||||||||||||||||
500 | - | |||||||||||||||||||
501 | #if !defined(QT_NO_EFFECTS) && !defined(Q_DEAD_CODE_FROM_QT4_MAC) | - | ||||||||||||||||||
502 | if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip))
| 0 | ||||||||||||||||||
503 | qFadeEffect(QTipLabel::instance); never executed: qFadeEffect(QTipLabel::instance); | 0 | ||||||||||||||||||
504 | else if (QApplication::isEffectEnabled(Qt::UI_AnimateTooltip))
| 0 | ||||||||||||||||||
505 | qScrollEffect(QTipLabel::instance); never executed: qScrollEffect(QTipLabel::instance); | 0 | ||||||||||||||||||
506 | else | - | ||||||||||||||||||
507 | QTipLabel::instance->showNormal(); never executed: QTipLabel::instance->showNormal(); | 0 | ||||||||||||||||||
508 | #else | - | ||||||||||||||||||
509 | QTipLabel::instance->showNormal(); | - | ||||||||||||||||||
510 | #endif | - | ||||||||||||||||||
511 | } | - | ||||||||||||||||||
512 | } never executed: end of block | 0 | ||||||||||||||||||
513 | - | |||||||||||||||||||
514 | /*! | - | ||||||||||||||||||
515 | \overload | - | ||||||||||||||||||
516 | - | |||||||||||||||||||
517 | This is analogous to calling QToolTip::showText(\a pos, \a text, \a w, QRect()) | - | ||||||||||||||||||
518 | */ | - | ||||||||||||||||||
519 | - | |||||||||||||||||||
520 | void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w) | - | ||||||||||||||||||
521 | { | - | ||||||||||||||||||
522 | QToolTip::showText(pos, text, w, QRect()); | - | ||||||||||||||||||
523 | } never executed: end of block | 0 | ||||||||||||||||||
524 | - | |||||||||||||||||||
525 | - | |||||||||||||||||||
526 | /*! | - | ||||||||||||||||||
527 | \fn void QToolTip::hideText() | - | ||||||||||||||||||
528 | \since 4.2 | - | ||||||||||||||||||
529 | - | |||||||||||||||||||
530 | Hides the tool tip. This is the same as calling showText() with an | - | ||||||||||||||||||
531 | empty string. | - | ||||||||||||||||||
532 | - | |||||||||||||||||||
533 | \sa showText() | - | ||||||||||||||||||
534 | */ | - | ||||||||||||||||||
535 | - | |||||||||||||||||||
536 | - | |||||||||||||||||||
537 | /*! | - | ||||||||||||||||||
538 | \since 4.4 | - | ||||||||||||||||||
539 | - | |||||||||||||||||||
540 | Returns \c true if this tooltip is currently shown. | - | ||||||||||||||||||
541 | - | |||||||||||||||||||
542 | \sa showText() | - | ||||||||||||||||||
543 | */ | - | ||||||||||||||||||
544 | bool QToolTip::isVisible() | - | ||||||||||||||||||
545 | { | - | ||||||||||||||||||
546 | return (QTipLabel::instance != 0 && QTipLabel::instance->isVisible()); never executed: return (QTipLabel::instance != 0 && QTipLabel::instance->isVisible());
| 0 | ||||||||||||||||||
547 | } | - | ||||||||||||||||||
548 | - | |||||||||||||||||||
549 | /*! | - | ||||||||||||||||||
550 | \since 4.4 | - | ||||||||||||||||||
551 | - | |||||||||||||||||||
552 | Returns the tooltip text, if a tooltip is visible, or an | - | ||||||||||||||||||
553 | empty string if a tooltip is not visible. | - | ||||||||||||||||||
554 | */ | - | ||||||||||||||||||
555 | QString QToolTip::text() | - | ||||||||||||||||||
556 | { | - | ||||||||||||||||||
557 | if (QTipLabel::instance)
| 0 | ||||||||||||||||||
558 | return QTipLabel::instance->text(); never executed: return QTipLabel::instance->text(); | 0 | ||||||||||||||||||
559 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
560 | } | - | ||||||||||||||||||
561 | - | |||||||||||||||||||
562 | - | |||||||||||||||||||
563 | Q_GLOBAL_STATIC(QPalette, tooltip_palette) never executed: end of block never executed: guard.store(QtGlobalStatic::Destroyed); never executed: return &holder.value;
| 0 | ||||||||||||||||||
564 | - | |||||||||||||||||||
565 | /*! | - | ||||||||||||||||||
566 | Returns the palette used to render tooltips. | - | ||||||||||||||||||
567 | - | |||||||||||||||||||
568 | \note Tool tips use the inactive color group of QPalette, because tool | - | ||||||||||||||||||
569 | tips are not active windows. | - | ||||||||||||||||||
570 | */ | - | ||||||||||||||||||
571 | QPalette QToolTip::palette() | - | ||||||||||||||||||
572 | { | - | ||||||||||||||||||
573 | return *tooltip_palette(); never executed: return *tooltip_palette(); | 0 | ||||||||||||||||||
574 | } | - | ||||||||||||||||||
575 | - | |||||||||||||||||||
576 | /*! | - | ||||||||||||||||||
577 | \since 4.2 | - | ||||||||||||||||||
578 | - | |||||||||||||||||||
579 | Returns the font used to render tooltips. | - | ||||||||||||||||||
580 | */ | - | ||||||||||||||||||
581 | QFont QToolTip::font() | - | ||||||||||||||||||
582 | { | - | ||||||||||||||||||
583 | return QApplication::font("QTipLabel"); never executed: return QApplication::font("QTipLabel"); | 0 | ||||||||||||||||||
584 | } | - | ||||||||||||||||||
585 | - | |||||||||||||||||||
586 | /*! | - | ||||||||||||||||||
587 | \since 4.2 | - | ||||||||||||||||||
588 | - | |||||||||||||||||||
589 | Sets the \a palette used to render tooltips. | - | ||||||||||||||||||
590 | - | |||||||||||||||||||
591 | \note Tool tips use the inactive color group of QPalette, because tool | - | ||||||||||||||||||
592 | tips are not active windows. | - | ||||||||||||||||||
593 | */ | - | ||||||||||||||||||
594 | void QToolTip::setPalette(const QPalette &palette) | - | ||||||||||||||||||
595 | { | - | ||||||||||||||||||
596 | *tooltip_palette() = palette; | - | ||||||||||||||||||
597 | if (QTipLabel::instance)
| 0 | ||||||||||||||||||
598 | QTipLabel::instance->setPalette(palette); never executed: QTipLabel::instance->setPalette(palette); | 0 | ||||||||||||||||||
599 | } never executed: end of block | 0 | ||||||||||||||||||
600 | - | |||||||||||||||||||
601 | /*! | - | ||||||||||||||||||
602 | \since 4.2 | - | ||||||||||||||||||
603 | - | |||||||||||||||||||
604 | Sets the \a font used to render tooltips. | - | ||||||||||||||||||
605 | */ | - | ||||||||||||||||||
606 | void QToolTip::setFont(const QFont &font) | - | ||||||||||||||||||
607 | { | - | ||||||||||||||||||
608 | QApplication::setFont(font, "QTipLabel"); | - | ||||||||||||||||||
609 | } never executed: end of block | 0 | ||||||||||||||||||
610 | - | |||||||||||||||||||
611 | QT_END_NAMESPACE | - | ||||||||||||||||||
612 | - | |||||||||||||||||||
613 | #include "qtooltip.moc" | - | ||||||||||||||||||
614 | #endif // QT_NO_TOOLTIP | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |