Line | Source | Count |
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 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - |
40 | # include <private/qcore_mac_p.h> | - |
41 | #endif | - |
42 | | - |
43 | #include <qapplication.h> | - |
44 | #include <qdesktopwidget.h> | - |
45 | #include <qevent.h> | - |
46 | #include <qlabel.h> | - |
47 | #include <qpointer.h> | - |
48 | #include <qstyle.h> | - |
49 | #include <qstyleoption.h> | - |
50 | #include <qstylepainter.h> | - |
51 | #include <qtimer.h> | - |
52 | #include <qtooltip.h> | - |
53 | #include <private/qeffects_p.h> | - |
54 | #include <qtextdocument.h> | - |
55 | #include <qdebug.h> | - |
56 | #include <private/qstylesheetstyle_p.h> | - |
57 | #ifndef QT_NO_TOOLTIP | - |
58 | | - |
59 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - |
60 | # include <private/qcore_mac_p.h> | - |
61 | #include <private/qt_cocoa_helpers_mac_p.h> | - |
62 | #endif | - |
63 | | - |
64 | QT_BEGIN_NAMESPACE | - |
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 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | | - |
103 | | - |
104 | | - |
105 | | - |
106 | | - |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
115 | | - |
116 | class QTipLabel : public QLabel | - |
117 | { | - |
118 | Q_OBJECT | - |
119 | public: | - |
120 | QTipLabel(const QString &text, QWidget *w, int msecDisplayTime); | - |
121 | ~QTipLabel(); | - |
122 | static QTipLabel *instance; | - |
123 | | - |
124 | bool eventFilter(QObject *, QEvent *) Q_DECL_OVERRIDE; | - |
125 | | - |
126 | QBasicTimer hideTimer, expireTimer; | - |
127 | | - |
128 | bool fadingOut; | - |
129 | | - |
130 | void reuseTip(const QString &text, int msecDisplayTime); | - |
131 | void hideTip(); | - |
132 | void hideTipImmediately(); | - |
133 | void setTipRect(QWidget *w, const QRect &r); | - |
134 | void restartExpireTimer(int msecDisplayTime); | - |
135 | bool tipChanged(const QPoint &pos, const QString &text, QObject *o); | - |
136 | void placeTip(const QPoint &pos, QWidget *w); | - |
137 | | - |
138 | static int getTipScreen(const QPoint &pos, QWidget *w); | - |
139 | protected: | - |
140 | void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE; | - |
141 | void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE; | - |
142 | void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE; | - |
143 | void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE; | - |
144 | | - |
145 | #ifndef QT_NO_STYLE_STYLESHEET | - |
146 | public slots: | - |
147 | | - |
148 | | - |
149 | | - |
150 | void styleSheetParentDestroyed() { | - |
151 | setProperty("_q_stylesheet_parent", QVariant()); | - |
152 | styleSheetParent = 0; | - |
153 | } | - |
154 | | - |
155 | private: | - |
156 | QWidget *styleSheetParent; | - |
157 | #endif | - |
158 | | - |
159 | private: | - |
160 | QWidget *widget; | - |
161 | QRect rect; | - |
162 | }; | - |
163 | | - |
164 | QTipLabel *QTipLabel::instance = 0; | - |
165 | | - |
166 | QTipLabel::QTipLabel(const QString &text, QWidget *w, int msecDisplayTime) | - |
167 | #ifndef QT_NO_STYLE_STYLESHEET | - |
168 | : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget), styleSheetParent(0), widget(0) | - |
169 | #else | - |
170 | : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget), widget(0) | - |
171 | #endif | - |
172 | { | - |
173 | delete instance; | - |
174 | instance = this; | - |
175 | setForegroundRole(QPalette::ToolTipText); | - |
176 | setBackgroundRole(QPalette::ToolTipBase); | - |
177 | setPalette(QToolTip::palette()); | - |
178 | ensurePolished(); | - |
179 | setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this)); | - |
180 | setFrameStyle(QFrame::NoFrame); | - |
181 | setAlignment(Qt::AlignLeft); | - |
182 | setIndent(1); | - |
183 | qApp->installEventFilter(this); | - |
184 | setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0); | - |
185 | setMouseTracking(true); | - |
186 | fadingOut = false; | - |
187 | reuseTip(text, msecDisplayTime); | - |
188 | } | - |
189 | | - |
190 | void QTipLabel::restartExpireTimer(int msecDisplayTime) | - |
191 | { | - |
192 | int time = 10000 + 40 * qMax(0, text().length()-100); | - |
193 | if (msecDisplayTime > 0) | - |
194 | time = msecDisplayTime; | - |
195 | expireTimer.start(time, this); | - |
196 | hideTimer.stop(); | - |
197 | } | - |
198 | | - |
199 | void QTipLabel::reuseTip(const QString &text, int msecDisplayTime) | - |
200 | { | - |
201 | #ifndef QT_NO_STYLE_STYLESHEET | - |
202 | if (styleSheetParent){ | - |
203 | disconnect(styleSheetParent, SIGNAL(destroyed()), | - |
204 | QTipLabel::instance, SLOT(styleSheetParentDestroyed())); | - |
205 | styleSheetParent = 0; | - |
206 | } | - |
207 | #endif | - |
208 | | - |
209 | setWordWrap(Qt::mightBeRichText(text)); | - |
210 | setText(text); | - |
211 | QFontMetrics fm(font()); | - |
212 | QSize extra(1, 0); | - |
213 | | - |
214 | if (fm.descent() == 2 && fm.ascent() >= 11) | - |
215 | ++extra.rheight(); | - |
216 | resize(sizeHint() + extra); | - |
217 | restartExpireTimer(msecDisplayTime); | - |
218 | } | - |
219 | | - |
220 | void QTipLabel::paintEvent(QPaintEvent *ev) | - |
221 | { | - |
222 | QStylePainter p(this); | - |
223 | QStyleOptionFrame opt; | - |
224 | opt.init(this); | - |
225 | p.drawPrimitive(QStyle::PE_PanelTipLabel, opt); | - |
226 | p.end(); | - |
227 | | - |
228 | QLabel::paintEvent(ev); | - |
229 | } | - |
230 | | - |
231 | void QTipLabel::resizeEvent(QResizeEvent *e) | - |
232 | { | - |
233 | QStyleHintReturnMask frameMask; | - |
234 | QStyleOption option; | - |
235 | option.init(this); | - |
236 | if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option, this, &frameMask)) | - |
237 | setMask(frameMask.region); | - |
238 | | - |
239 | QLabel::resizeEvent(e); | - |
240 | } | - |
241 | | - |
242 | void QTipLabel::mouseMoveEvent(QMouseEvent *e) | - |
243 | { | - |
244 | if (rect.isNull()) | - |
245 | return; | - |
246 | QPoint pos = e->globalPos(); | - |
247 | if (widget) | - |
248 | pos = widget->mapFromGlobal(pos); | - |
249 | if (!rect.contains(pos)) | - |
250 | hideTip(); | - |
251 | QLabel::mouseMoveEvent(e); | - |
252 | } | - |
253 | | - |
254 | QTipLabel::~QTipLabel() | - |
255 | { | - |
256 | instance = 0; | - |
257 | } | - |
258 | | - |
259 | void QTipLabel::hideTip() | - |
260 | { | - |
261 | if (!hideTimer.isActive()) | - |
262 | hideTimer.start(300, this); | - |
263 | } | - |
264 | | - |
265 | void QTipLabel::hideTipImmediately() | - |
266 | { | - |
267 | close(); | - |
268 | deleteLater(); | - |
269 | } | - |
270 | | - |
271 | void QTipLabel::setTipRect(QWidget *w, const QRect &r) | - |
272 | { | - |
273 | if (Q_UNLIKELY(!r.isNull() && !w))) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
274 | qWarning("QToolTip::setTipRect: Cannot pass null widget if rect is set"); | - |
275 | else{return; never executed: return; | 0 |
276 | } | - |
277 | widget = w; | - |
278 | rect = r;} | - |
279 | } never executed: end of block | 0 |
280 | | - |
281 | void QTipLabel::timerEvent(QTimerEvent *e) | - |
282 | { | - |
283 | if (e->timerId() == hideTimer.timerId() | - |
284 | || e->timerId() == expireTimer.timerId()){ | - |
285 | hideTimer.stop(); | - |
286 | expireTimer.stop(); | - |
287 | #if defined(Q_DEAD_CODE_FROM_QT4_MAC) && !defined(QT_NO_EFFECTS) | - |
288 | if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip)){ | - |
289 | | - |
290 | | - |
291 | | - |
292 | | - |
293 | macWindowFade(qt_mac_window_for(this)); | - |
294 | QTipLabel::instance->fadingOut = true; | - |
295 | } | - |
296 | else | - |
297 | hideTipImmediately(); | - |
298 | #else | - |
299 | hideTipImmediately(); | - |
300 | #endif | - |
301 | } | - |
302 | } | - |
303 | | - |
304 | bool QTipLabel::eventFilter(QObject *o, QEvent *e) | - |
305 | { | - |
306 | switch (e->type()) { | - |
307 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - |
308 | case QEvent::KeyPress: | - |
309 | case QEvent::KeyRelease: { | - |
310 | int key = static_cast<QKeyEvent *>(e)->key(); | - |
311 | Qt::KeyboardModifiers mody = static_cast<QKeyEvent *>(e)->modifiers(); | - |
312 | if (!(mody & Qt::KeyboardModifierMask) | - |
313 | && key != Qt::Key_Shift && key != Qt::Key_Control | - |
314 | && key != Qt::Key_Alt && key != Qt::Key_Meta) | - |
315 | hideTip(); | - |
316 | break; | - |
317 | } | - |
318 | #endif | - |
319 | case QEvent::Leave: | - |
320 | hideTip(); | - |
321 | break; | - |
322 | | - |
323 | | - |
324 | #if defined (Q_OS_QNX) // On QNX the window activate and focus events are delayed and will appear | - |
325 | | - |
326 | case QEvent::WindowActivate: | - |
327 | case QEvent::FocusIn: | - |
328 | return false; | - |
329 | case QEvent::WindowDeactivate: | - |
330 | if (o != this) | - |
331 | return false; | - |
332 | hideTipImmediately(); | - |
333 | break; | - |
334 | case QEvent::FocusOut: | - |
335 | if (reinterpret_cast<QWindow*>(o) != windowHandle()) | - |
336 | return false; | - |
337 | hideTipImmediately(); | - |
338 | break; | - |
339 | #else | - |
340 | case QEvent::WindowActivate: | - |
341 | case QEvent::WindowDeactivate: | - |
342 | case QEvent::FocusIn: | - |
343 | case QEvent::FocusOut: | - |
344 | #endif | - |
345 | case QEvent::Close: | - |
346 | case QEvent::MouseButtonPress: | - |
347 | case QEvent::MouseButtonRelease: | - |
348 | case QEvent::MouseButtonDblClick: | - |
349 | case QEvent::Wheel: | - |
350 | hideTipImmediately(); | - |
351 | break; | - |
352 | | - |
353 | case QEvent::MouseMove: | - |
354 | if (o == widget && !rect.isNull() && !rect.contains(static_cast<QMouseEvent*>(e)->pos())) | - |
355 | hideTip(); | - |
356 | default: | - |
357 | break; | - |
358 | } | - |
359 | return false; | - |
360 | } | - |
361 | | - |
362 | int QTipLabel::getTipScreen(const QPoint &pos, QWidget *w) | - |
363 | { | - |
364 | if (QApplication::desktop()->isVirtualDesktop()) | - |
365 | return QApplication::desktop()->screenNumber(pos); | - |
366 | else | - |
367 | return QApplication::desktop()->screenNumber(w); | - |
368 | } | - |
369 | | - |
370 | void QTipLabel::placeTip(const QPoint &pos, QWidget *w) | - |
371 | { | - |
372 | #ifndef QT_NO_STYLE_STYLESHEET | - |
373 | if (testAttribute(Qt::WA_StyleSheet) || (w && qobject_cast<QStyleSheetStyle *>(w->style()))) { | - |
374 | | - |
375 | QTipLabel::instance->setProperty("_q_stylesheet_parent", QVariant::fromValue(w)); | - |
376 | | - |
377 | QTipLabel::instance->setStyleSheet(QLatin1String("/* */")); | - |
378 | | - |
379 | | - |
380 | QTipLabel::instance->styleSheetParent = w; | - |
381 | if (w) { | - |
382 | connect(w, SIGNAL(destroyed()), | - |
383 | QTipLabel::instance, SLOT(styleSheetParentDestroyed())); | - |
384 | } | - |
385 | } | - |
386 | #endif //QT_NO_STYLE_STYLESHEET | - |
387 | | - |
388 | | - |
389 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - |
390 | | - |
391 | | - |
392 | | - |
393 | | - |
394 | extern bool qt_mac_app_fullscreen; | - |
395 | QRect screen; | - |
396 | if(qt_mac_app_fullscreen) | - |
397 | screen = QApplication::desktop()->screenGeometry(getTipScreen(pos, w)); | - |
398 | else | - |
399 | screen = QApplication::desktop()->availableGeometry(getTipScreen(pos, w)); | - |
400 | #else | - |
401 | QRect screen = QApplication::desktop()->screenGeometry(getTipScreen(pos, w)); | - |
402 | #endif | - |
403 | | - |
404 | QPoint p = pos; | - |
405 | p += QPoint(2, | - |
406 | #ifdef Q_DEAD_CODE_FROM_QT4_WIN | - |
407 | 21 | - |
408 | #else | - |
409 | 16 | - |
410 | #endif | - |
411 | ); | - |
412 | if (p.x() + this->width() > screen.x() + screen.width()) | - |
413 | p.rx() -= 4 + this->width(); | - |
414 | if (p.y() + this->height() > screen.y() + screen.height()) | - |
415 | p.ry() -= 24 + this->height(); | - |
416 | if (p.y() < screen.y()) | - |
417 | p.setY(screen.y()); | - |
418 | if (p.x() + this->width() > screen.x() + screen.width()) | - |
419 | p.setX(screen.x() + screen.width() - this->width()); | - |
420 | if (p.x() < screen.x()) | - |
421 | p.setX(screen.x()); | - |
422 | if (p.y() + this->height() > screen.y() + screen.height()) | - |
423 | p.setY(screen.y() + screen.height() - this->height()); | - |
424 | this->move(p); | - |
425 | } | - |
426 | | - |
427 | bool QTipLabel::tipChanged(const QPoint &pos, const QString &text, QObject *o) | - |
428 | { | - |
429 | if (QTipLabel::instance->text() != text) | - |
430 | return true; | - |
431 | | - |
432 | if (o != widget) | - |
433 | return true; | - |
434 | | - |
435 | if (!rect.isNull()) | - |
436 | return !rect.contains(pos); | - |
437 | else | - |
438 | return false; | - |
439 | } | - |
440 | | - |
441 | | - |
442 | | - |
443 | | - |
444 | | - |
445 | | - |
446 | | - |
447 | | - |
448 | | - |
449 | | - |
450 | | - |
451 | | - |
452 | | - |
453 | | - |
454 | | - |
455 | | - |
456 | | - |
457 | | - |
458 | | - |
459 | | - |
460 | void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w, const QRect &rect) | - |
461 | { | - |
462 | showText(pos, text, w, rect, -1); | - |
463 | } | - |
464 | | - |
465 | | - |
466 | | - |
467 | | - |
468 | | - |
469 | | - |
470 | | - |
471 | | - |
472 | void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w, const QRect &rect, int msecDisplayTime) | - |
473 | { | - |
474 | if (QTipLabel::instance && QTipLabel::instance->isVisible()){ | - |
475 | if (text.isEmpty()){ | - |
476 | QTipLabel::instance->hideTip(); | - |
477 | return; | - |
478 | } | - |
479 | else if (!QTipLabel::instance->fadingOut){ | - |
480 | | - |
481 | | - |
482 | QPoint localPos = pos; | - |
483 | if (w) | - |
484 | localPos = w->mapFromGlobal(pos); | - |
485 | if (QTipLabel::instance->tipChanged(localPos, text, w)){ | - |
486 | QTipLabel::instance->reuseTip(text, msecDisplayTime); | - |
487 | QTipLabel::instance->setTipRect(w, rect); | - |
488 | QTipLabel::instance->placeTip(pos, w); | - |
489 | } | - |
490 | return; | - |
491 | } | - |
492 | } | - |
493 | | - |
494 | if (!text.isEmpty()){ | - |
495 | #ifndef Q_DEAD_CODE_FROM_QT4_WIN | - |
496 | new QTipLabel(text, w, msecDisplayTime); | - |
497 | #else | - |
498 | | - |
499 | | - |
500 | new QTipLabel(text, QApplication::desktop()->screen(QTipLabel::getTipScreen(pos, w)), msecDisplayTime); | - |
501 | #endif | - |
502 | QTipLabel::instance->setTipRect(w, rect); | - |
503 | QTipLabel::instance->placeTip(pos, w); | - |
504 | QTipLabel::instance->setObjectName(QLatin1String("qtooltip_label")); | - |
505 | | - |
506 | | - |
507 | #if !defined(QT_NO_EFFECTS) && !defined(Q_DEAD_CODE_FROM_QT4_MAC) | - |
508 | if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip)) | - |
509 | qFadeEffect(QTipLabel::instance); | - |
510 | else if (QApplication::isEffectEnabled(Qt::UI_AnimateTooltip)) | - |
511 | qScrollEffect(QTipLabel::instance); | - |
512 | else | - |
513 | QTipLabel::instance->showNormal(); | - |
514 | #else | - |
515 | QTipLabel::instance->showNormal(); | - |
516 | #endif | - |
517 | } | - |
518 | } | - |
519 | | - |
520 | | - |
521 | | - |
522 | | - |
523 | | - |
524 | | - |
525 | | - |
526 | void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w) | - |
527 | { | - |
528 | QToolTip::showText(pos, text, w, QRect()); | - |
529 | } | - |
530 | | - |
531 | | - |
532 | | - |
533 | | - |
534 | | - |
535 | | - |
536 | | - |
537 | | - |
538 | | - |
539 | | - |
540 | | - |
541 | | - |
542 | | - |
543 | | - |
544 | | - |
545 | | - |
546 | | - |
547 | | - |
548 | | - |
549 | | - |
550 | bool QToolTip::isVisible() | - |
551 | { | - |
552 | return (QTipLabel::instance != 0 && QTipLabel::instance->isVisible()); | - |
553 | } | - |
554 | | - |
555 | | - |
556 | | - |
557 | | - |
558 | | - |
559 | | - |
560 | | - |
561 | QString QToolTip::text() | - |
562 | { | - |
563 | if (QTipLabel::instance) | - |
564 | return QTipLabel::instance->text(); | - |
565 | return QString(); | - |
566 | } | - |
567 | | - |
568 | | - |
569 | Q_GLOBAL_STATIC(QPalette, tooltip_palette) | - |
570 | | - |
571 | | - |
572 | | - |
573 | | - |
574 | | - |
575 | | - |
576 | | - |
577 | QPalette QToolTip::palette() | - |
578 | { | - |
579 | return *tooltip_palette(); | - |
580 | } | - |
581 | | - |
582 | | - |
583 | | - |
584 | | - |
585 | | - |
586 | | - |
587 | QFont QToolTip::font() | - |
588 | { | - |
589 | return QApplication::font("QTipLabel"); | - |
590 | } | - |
591 | | - |
592 | | - |
593 | | - |
594 | | - |
595 | | - |
596 | | - |
597 | | - |
598 | | - |
599 | | - |
600 | void QToolTip::setPalette(const QPalette &palette) | - |
601 | { | - |
602 | *tooltip_palette() = palette; | - |
603 | if (QTipLabel::instance) | - |
604 | QTipLabel::instance->setPalette(palette); | - |
605 | } | - |
606 | | - |
607 | | - |
608 | | - |
609 | | - |
610 | | - |
611 | | - |
612 | void QToolTip::setFont(const QFont &font) | - |
613 | { | - |
614 | QApplication::setFont(font, "QTipLabel"); | - |
615 | } | - |
616 | | - |
617 | QT_END_NAMESPACE | - |
618 | | - |
619 | #include "qtooltip.moc" | - |
620 | #endif // QT_NO_TOOLTIP | - |
| | |