Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | __attribute__((visibility("default"))) void qDeleteInEventHandler(QObject *o); | - |
7 | | - |
8 | class QWhatsThat : public QWidget | - |
9 | { | - |
10 | public: template <typename ThisObject> inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const { int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i + 1; } | - |
11 | #pragma GCC diagnostic push | - |
12 | static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); static inline QString tr(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } __attribute__ ((__deprecated__)) static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } private: __attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); | - |
13 | #pragma GCC diagnostic pop | - |
14 | struct QPrivateSignal {}; | - |
15 | | - |
16 | public: | - |
17 | QWhatsThat(const QString& txt, QWidget* parent, QWidget *showTextFor); | - |
18 | ~QWhatsThat() ; | - |
19 | | - |
20 | static QWhatsThat *instance; | - |
21 | | - |
22 | protected: | - |
23 | void showEvent(QShowEvent *e) override; | - |
24 | void mousePressEvent(QMouseEvent*) override; | - |
25 | void mouseReleaseEvent(QMouseEvent*) override; | - |
26 | void mouseMoveEvent(QMouseEvent*) override; | - |
27 | void keyPressEvent(QKeyEvent*) override; | - |
28 | void paintEvent(QPaintEvent*) override; | - |
29 | | - |
30 | private: | - |
31 | QPointer<QWidget>widget; | - |
32 | bool pressed; | - |
33 | QString text; | - |
34 | QTextDocument* doc; | - |
35 | QString anchor; | - |
36 | QPixmap background; | - |
37 | }; | - |
38 | | - |
39 | QWhatsThat *QWhatsThat::instance = 0; | - |
40 | | - |
41 | | - |
42 | static int shadowWidth = 6; | - |
43 | static const int vMargin = 8; | - |
44 | static const int hMargin = 12; | - |
45 | | - |
46 | static inline bool dropShadow() | - |
47 | { | - |
48 | if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) | - |
49 | return theme->themeHint(QPlatformTheme::DropShadow).toBool(); | - |
50 | return false; | - |
51 | } | - |
52 | | - |
53 | QWhatsThat::QWhatsThat(const QString& txt, QWidget* parent, QWidget *showTextFor) | - |
54 | : QWidget(parent, Qt::Popup), | - |
55 | widget(showTextFor), pressed(false), text(txt) | - |
56 | { | - |
57 | delete instance; | - |
58 | instance = this; | - |
59 | setAttribute(Qt::WA_DeleteOnClose, true); | - |
60 | setAttribute(Qt::WA_NoSystemBackground, true); | - |
61 | if (parent) | - |
62 | setPalette(parent->palette()); | - |
63 | setMouseTracking(true); | - |
64 | setFocusPolicy(Qt::StrongFocus); | - |
65 | | - |
66 | setCursor(Qt::ArrowCursor); | - |
67 | | - |
68 | QRect r; | - |
69 | doc = 0; | - |
70 | ensurePolished(); | - |
71 | if (Qt::mightBeRichText(text)) { | - |
72 | doc = new QTextDocument(); | - |
73 | doc->setUndoRedoEnabled(false); | - |
74 | doc->setDefaultFont(QApplication::font(this)); | - |
75 | | - |
76 | | - |
77 | | - |
78 | doc->setHtml(text); | - |
79 | | - |
80 | doc->setUndoRedoEnabled(false); | - |
81 | doc->adjustSize(); | - |
82 | r.setTop(0); | - |
83 | r.setLeft(0); | - |
84 | r.setSize(doc->size().toSize()); | - |
85 | } | - |
86 | else | - |
87 | { | - |
88 | int sw = QApplication::desktop()->width() / 3; | - |
89 | if (sw < 200) | - |
90 | sw = 200; | - |
91 | else if (sw > 300) | - |
92 | sw = 300; | - |
93 | | - |
94 | r = fontMetrics().boundingRect(0, 0, sw, 1000, | - |
95 | Qt::AlignLeft + Qt::AlignTop | - |
96 | + Qt::TextWordWrap + Qt::TextExpandTabs, | - |
97 | text); | - |
98 | } | - |
99 | shadowWidth = dropShadow() ? 0 : 6; | - |
100 | resize(r.width() + 2*hMargin + shadowWidth, r.height() + 2*vMargin + shadowWidth); | - |
101 | } | - |
102 | | - |
103 | QWhatsThat::~QWhatsThat() | - |
104 | { | - |
105 | instance = 0; | - |
106 | if (doc) | - |
107 | delete doc; | - |
108 | } | - |
109 | | - |
110 | void QWhatsThat::showEvent(QShowEvent *) | - |
111 | { | - |
112 | background = QGuiApplication::primaryScreen()->grabWindow(QApplication::desktop()->internalWinId(), | - |
113 | x(), y(), width(), height()); | - |
114 | } | - |
115 | | - |
116 | void QWhatsThat::mousePressEvent(QMouseEvent* e) | - |
117 | { | - |
118 | pressed = true; | - |
119 | if (e->button() == Qt::LeftButton && rect().contains(e->pos())) { | - |
120 | if (doc) | - |
121 | anchor = doc->documentLayout()->anchorAt(e->pos() - QPoint(hMargin, vMargin)); | - |
122 | return; | - |
123 | } | - |
124 | close(); | - |
125 | } | - |
126 | | - |
127 | void QWhatsThat::mouseReleaseEvent(QMouseEvent* e) | - |
128 | { | - |
129 | if (!pressed) | - |
130 | return; | - |
131 | if (widget && e->button() == Qt::LeftButton && doc && rect().contains(e->pos())) { | - |
132 | QString a = doc->documentLayout()->anchorAt(e->pos() - QPoint(hMargin, vMargin)); | - |
133 | QString href; | - |
134 | if (anchor == a) | - |
135 | href = a; | - |
136 | anchor.clear(); | - |
137 | if (!href.isEmpty()) { | - |
138 | QWhatsThisClickedEvent e(href); | - |
139 | if (QApplication::sendEvent(widget, &e)) | - |
140 | return; | - |
141 | } | - |
142 | } | - |
143 | close(); | - |
144 | } | - |
145 | | - |
146 | void QWhatsThat::mouseMoveEvent(QMouseEvent* e) | - |
147 | { | - |
148 | | - |
149 | | - |
150 | | - |
151 | if (!doc) | - |
152 | return; | - |
153 | QString a = doc->documentLayout()->anchorAt(e->pos() - QPoint(hMargin, vMargin)); | - |
154 | if (!a.isEmpty()) | - |
155 | setCursor(Qt::PointingHandCursor); | - |
156 | else | - |
157 | setCursor(Qt::ArrowCursor); | - |
158 | | - |
159 | } | - |
160 | | - |
161 | void QWhatsThat::keyPressEvent(QKeyEvent*) | - |
162 | { | - |
163 | close(); | - |
164 | } | - |
165 | | - |
166 | void QWhatsThat::paintEvent(QPaintEvent*) | - |
167 | { | - |
168 | const bool drawShadow = dropShadow(); | - |
169 | | - |
170 | QRect r = rect(); | - |
171 | r.adjust(0, 0, -1, -1); | - |
172 | if (drawShadow) | - |
173 | r.adjust(0, 0, -shadowWidth, -shadowWidth); | - |
174 | QPainter p(this); | - |
175 | p.drawPixmap(0, 0, background); | - |
176 | p.setPen(QPen(palette().toolTipText(), 0)); | - |
177 | p.setBrush(palette().toolTipBase()); | - |
178 | p.drawRect(r); | - |
179 | int w = r.width(); | - |
180 | int h = r.height(); | - |
181 | p.setPen(palette().brush(QPalette::Dark).color()); | - |
182 | p.drawRect(1, 1, w-2, h-2); | - |
183 | if (drawShadow) { | - |
184 | p.setPen(palette().shadow().color()); | - |
185 | p.drawPoint(w + 5, 6); | - |
186 | p.drawLine(w + 3, 6, w + 5, 8); | - |
187 | p.drawLine(w + 1, 6, w + 5, 10); | - |
188 | int i; | - |
189 | for(i=7; i < h; i += 2) | - |
190 | p.drawLine(w, i, w + 5, i + 5); | - |
191 | for(i = w - i + h; i > 6; i -= 2) | - |
192 | p.drawLine(i, h, i + 5, h + 5); | - |
193 | for(; i > 0 ; i -= 2) | - |
194 | p.drawLine(6, h + 6 - i, i + 5, h + 5); | - |
195 | } | - |
196 | r.adjust(0, 0, 1, 1); | - |
197 | p.setPen(palette().toolTipText().color()); | - |
198 | r.adjust(hMargin, vMargin, -hMargin, -vMargin); | - |
199 | | - |
200 | if (doc) { | - |
201 | p.translate(r.x(), r.y()); | - |
202 | QRect rect = r; | - |
203 | rect.translate(-r.x(), -r.y()); | - |
204 | p.setClipRect(rect); | - |
205 | QAbstractTextDocumentLayout::PaintContext context; | - |
206 | context.palette.setBrush(QPalette::Text, context.palette.toolTipText()); | - |
207 | doc->documentLayout()->draw(&p, context); | - |
208 | } | - |
209 | else | - |
210 | { | - |
211 | p.drawText(r, Qt::AlignLeft + Qt::AlignTop + Qt::TextWordWrap + Qt::TextExpandTabs, text); | - |
212 | } | - |
213 | } | - |
214 | | - |
215 | static const char * const button_image[] = { | - |
216 | "16 16 3 1", | - |
217 | " c None", | - |
218 | "o c #000000", | - |
219 | "a c #000080", | - |
220 | "o aaaaa ", | - |
221 | "oo aaa aaa ", | - |
222 | "ooo aaa aaa", | - |
223 | "oooo aa aa", | - |
224 | "ooooo aa aa", | - |
225 | "oooooo a aaa", | - |
226 | "ooooooo aaa ", | - |
227 | "oooooooo aaa ", | - |
228 | "ooooooooo aaa ", | - |
229 | "ooooo aaa ", | - |
230 | "oo ooo ", | - |
231 | "o ooo aaa ", | - |
232 | " ooo aaa ", | - |
233 | " ooo ", | - |
234 | " ooo ", | - |
235 | " ooo "}; | - |
236 | | - |
237 | class QWhatsThisPrivate : public QObject | - |
238 | { | - |
239 | public: | - |
240 | QWhatsThisPrivate(); | - |
241 | ~QWhatsThisPrivate(); | - |
242 | static QWhatsThisPrivate *instance; | - |
243 | bool eventFilter(QObject *, QEvent *) override; | - |
244 | QPointer<QAction> action; | - |
245 | static void say(QWidget *, const QString &, int x = 0, int y = 0); | - |
246 | static void notifyToplevels(QEvent *e); | - |
247 | bool leaveOnMouseRelease; | - |
248 | }; | - |
249 | | - |
250 | void QWhatsThisPrivate::notifyToplevels(QEvent *e) | - |
251 | { | - |
252 | const QWidgetList toplevels = QApplication::topLevelWidgets(); | - |
253 | for (int i = 0; i < toplevels.count(); ++i) { | - |
| QWidgetauto *w =: toplevels.at(i);) | |
254 | QApplication::sendEvent(w, e); never executed: QApplication::sendEvent(w, e); | 0 |
| }} never executed: end of block | |
256 | | - |
257 | QWhatsThisPrivate *QWhatsThisPrivate::instance = 0; | - |
258 | | - |
259 | QWhatsThisPrivate::QWhatsThisPrivate() | - |
260 | : leaveOnMouseRelease(false) | - |
261 | { | - |
262 | instance = this; | - |
263 | (static_cast<QApplication *>(QCoreApplication::instance()))->installEventFilter(this); | - |
264 | | - |
265 | QPoint pos = QCursor::pos(); | - |
266 | if (QWidget *w = QApplication::widgetAt(pos)) { | - |
267 | QHelpEvent e(QEvent::QueryWhatsThis, w->mapFromGlobal(pos), pos); | - |
268 | bool sentEvent = QApplication::sendEvent(w, &e); | - |
269 | | - |
270 | | - |
271 | | - |
272 | QApplication::setOverrideCursor((!sentEvent || !e.isAccepted())? | - |
273 | Qt::ForbiddenCursor:Qt::WhatsThisCursor); | - |
274 | } else { | - |
275 | QApplication::setOverrideCursor(Qt::WhatsThisCursor); | - |
276 | | - |
277 | } | - |
278 | | - |
279 | QAccessibleEvent event(this, QAccessible::ContextHelpStart); | - |
280 | QAccessible::updateAccessibility(&event); | - |
281 | | - |
282 | } | - |
283 | | - |
284 | QWhatsThisPrivate::~QWhatsThisPrivate() | - |
285 | { | - |
286 | if (action) | - |
287 | action->setChecked(false); | - |
288 | | - |
289 | QApplication::restoreOverrideCursor(); | - |
290 | | - |
291 | | - |
292 | QAccessibleEvent event(this, QAccessible::ContextHelpEnd); | - |
293 | QAccessible::updateAccessibility(&event); | - |
294 | | - |
295 | instance = 0; | - |
296 | } | - |
297 | | - |
298 | bool QWhatsThisPrivate::eventFilter(QObject *o, QEvent *e) | - |
299 | { | - |
300 | if (!o->isWidgetType()) | - |
301 | return false; | - |
302 | QWidget * w = static_cast<QWidget *>(o); | - |
303 | bool customWhatsThis = w->testAttribute(Qt::WA_CustomWhatsThis); | - |
304 | switch (e->type()) { | - |
305 | case QEvent::MouseButtonPress: | - |
306 | { | - |
307 | QMouseEvent *me = static_cast<QMouseEvent*>(e); | - |
308 | if (me->button() == Qt::RightButton || customWhatsThis) | - |
309 | return false; | - |
310 | QHelpEvent e(QEvent::WhatsThis, me->pos(), me->globalPos()); | - |
311 | if (!QApplication::sendEvent(w, &e) || !e.isAccepted()) | - |
312 | leaveOnMouseRelease = true; | - |
313 | | - |
314 | } break; | - |
315 | | - |
316 | case QEvent::MouseMove: | - |
317 | { | - |
318 | QMouseEvent *me = static_cast<QMouseEvent*>(e); | - |
319 | QHelpEvent e(QEvent::QueryWhatsThis, me->pos(), me->globalPos()); | - |
320 | bool sentEvent = QApplication::sendEvent(w, &e); | - |
321 | | - |
322 | | - |
323 | | - |
324 | QApplication::changeOverrideCursor((!sentEvent || !e.isAccepted())? | - |
325 | Qt::ForbiddenCursor:Qt::WhatsThisCursor); | - |
326 | | - |
327 | } | - |
328 | | - |
329 | case QEvent::MouseButtonRelease: | - |
330 | case QEvent::MouseButtonDblClick: | - |
331 | if (leaveOnMouseRelease && e->type() == QEvent::MouseButtonRelease) | - |
332 | QWhatsThis::leaveWhatsThisMode(); | - |
333 | if (static_cast<QMouseEvent*>(e)->button() == Qt::RightButton || customWhatsThis) | - |
334 | return false; | - |
335 | break; | - |
336 | case QEvent::KeyPress: | - |
337 | { | - |
338 | QKeyEvent* kev = (QKeyEvent*)e; | - |
339 | | - |
340 | if (kev->matches(QKeySequence::Cancel)) { | - |
341 | QWhatsThis::leaveWhatsThisMode(); | - |
342 | return true; | - |
343 | } else if (customWhatsThis) { | - |
344 | return false; | - |
345 | } else if (kev->key() == Qt::Key_Menu || | - |
346 | (kev->key() == Qt::Key_F10 && | - |
347 | kev->modifiers() == Qt::ShiftModifier)) { | - |
348 | | - |
349 | return false; | - |
350 | } else if (kev->key() != Qt::Key_Shift && kev->key() != Qt::Key_Alt | - |
351 | && kev->key() != Qt::Key_Control && kev->key() != Qt::Key_Meta) { | - |
352 | QWhatsThis::leaveWhatsThisMode(); | - |
353 | } | - |
354 | } break; | - |
355 | default: | - |
356 | return false; | - |
357 | } | - |
358 | return true; | - |
359 | } | - |
360 | | - |
361 | class QWhatsThisAction: public QAction | - |
362 | { | - |
363 | public: template <typename ThisObject> inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const { int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i + 1; } | - |
364 | #pragma GCC diagnostic push | - |
365 | static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); static inline QString tr(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } __attribute__ ((__deprecated__)) static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } private: __attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); | - |
366 | #pragma GCC diagnostic pop | - |
367 | struct QPrivateSignal {}; | - |
368 | | - |
369 | public: | - |
370 | explicit QWhatsThisAction(QObject* parent = 0); | - |
371 | | - |
372 | private : | - |
373 | void actionTriggered(); | - |
374 | }; | - |
375 | | - |
376 | QWhatsThisAction::QWhatsThisAction(QObject *parent) : QAction(tr("What's This?"), parent) | - |
377 | { | - |
378 | | - |
379 | QPixmap p(button_image); | - |
380 | setIcon(p); | - |
381 | | - |
382 | setCheckable(true); | - |
383 | connect(this, qFlagLocation("2""triggered()" "\0" __FILE__ ":" "501""505"), this, qFlagLocation("1""actionTriggered()" "\0" __FILE__ ":" "501""505")); | - |
384 | | - |
385 | setShortcut(Qt::ShiftModifier + Qt::Key_F1); | - |
386 | | - |
387 | } | - |
388 | | - |
389 | void QWhatsThisAction::actionTriggered() | - |
390 | { | - |
391 | if (isChecked()) { | - |
392 | QWhatsThis::enterWhatsThisMode(); | - |
393 | QWhatsThisPrivate::instance->action = this; | - |
394 | } | - |
395 | } | - |
396 | void QWhatsThis::enterWhatsThisMode() | - |
397 | { | - |
398 | if (QWhatsThisPrivate::instance) | - |
399 | return; | - |
400 | (void) new QWhatsThisPrivate; | - |
401 | QEvent e(QEvent::EnterWhatsThisMode); | - |
402 | QWhatsThisPrivate::notifyToplevels(&e); | - |
403 | } | - |
404 | | - |
405 | | - |
406 | | - |
407 | | - |
408 | | - |
409 | | - |
410 | | - |
411 | bool QWhatsThis::inWhatsThisMode() | - |
412 | { | - |
413 | return (QWhatsThisPrivate::instance != 0); | - |
414 | } | - |
415 | void QWhatsThis::leaveWhatsThisMode() | - |
416 | { | - |
417 | delete QWhatsThisPrivate::instance; | - |
418 | QEvent e(QEvent::LeaveWhatsThisMode); | - |
419 | QWhatsThisPrivate::notifyToplevels(&e); | - |
420 | } | - |
421 | | - |
422 | void QWhatsThisPrivate::say(QWidget * widget, const QString &text, int x, int y) | - |
423 | { | - |
424 | if (text.size() == 0) | - |
425 | return; | - |
426 | | - |
427 | QWhatsThat *whatsThat = new QWhatsThat( | - |
428 | text, | - |
429 | | - |
430 | | - |
431 | | - |
432 | 0, | - |
433 | | - |
434 | widget | - |
435 | ); | - |
436 | | - |
437 | | - |
438 | | - |
439 | | - |
440 | int scr = (widget ? | - |
441 | QApplication::desktop()->screenNumber(widget) : | - |
442 | | - |
443 | | - |
444 | | - |
445 | QApplication::desktop()->screenNumber(QPoint(x,y)) | - |
446 | | - |
447 | ); | - |
448 | QRect screen = QApplication::desktop()->screenGeometry(scr); | - |
449 | | - |
450 | int w = whatsThat->width(); | - |
451 | int h = whatsThat->height(); | - |
452 | int sx = screen.x(); | - |
453 | int sy = screen.y(); | - |
454 | | - |
455 | | - |
456 | | - |
457 | QPoint pos; | - |
458 | if (widget) | - |
459 | pos = widget->mapToGlobal(QPoint(0,0)); | - |
460 | | - |
461 | if (widget && w > widget->width() + 16) | - |
462 | x = pos.x() + widget->width()/2 - w/2; | - |
463 | else | - |
464 | x = x - w/2; | - |
465 | | - |
466 | | - |
467 | | - |
468 | if (x + w + shadowWidth > sx+screen.width()) | - |
469 | x = (widget? (qMin(screen.width(), | - |
470 | pos.x() + widget->width()) | - |
471 | ) : screen.width()) | - |
472 | - w; | - |
473 | | - |
474 | if (x < sx) | - |
475 | x = sx; | - |
476 | | - |
477 | if (widget && h > widget->height() + 16) { | - |
478 | y = pos.y() + widget->height() + 2; | - |
479 | | - |
480 | if (y + h + 10 > sy+screen.height()) | - |
481 | y = pos.y() + 2 - shadowWidth - h; | - |
482 | } | - |
483 | y = y + 2; | - |
484 | | - |
485 | | - |
486 | | - |
487 | if (y + h + shadowWidth > sy+screen.height()) | - |
488 | y = (widget ? (qMin(screen.height(), | - |
489 | pos.y() + widget->height()) | - |
490 | ) : screen.height()) | - |
491 | - h; | - |
492 | if (y < sy) | - |
493 | y = sy; | - |
494 | | - |
495 | whatsThat->move(x, y); | - |
496 | whatsThat->show(); | - |
497 | whatsThat->grabKeyboard(); | - |
498 | } | - |
499 | void QWhatsThis::showText(const QPoint &pos, const QString &text, QWidget *w) | - |
500 | { | - |
501 | leaveWhatsThisMode(); | - |
502 | QWhatsThisPrivate::say(w, text, pos.x(), pos.y()); | - |
503 | } | - |
504 | | - |
505 | | - |
506 | | - |
507 | | - |
508 | | - |
509 | | - |
510 | void QWhatsThis::hideText() | - |
511 | { | - |
512 | qDeleteInEventHandler(QWhatsThat::instance); | - |
513 | } | - |
514 | QAction *QWhatsThis::createAction(QObject *parent) | - |
515 | { | - |
516 | return new QWhatsThisAction(parent); | - |
517 | } | - |
518 | | - |
519 | | - |
520 | | - |
| | |