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 | | - |
40 | #include "qpainter.h" | - |
41 | #include "qevent.h" | - |
42 | #include "qdrawutil.h" | - |
43 | #include "qapplication.h" | - |
44 | #include "qabstractbutton.h" | - |
45 | #include "qstyle.h" | - |
46 | #include "qstyleoption.h" | - |
47 | #include <limits.h> | - |
48 | #include "qaction.h" | - |
49 | #include "qclipboard.h" | - |
50 | #include <qdebug.h> | - |
51 | #include <qurl.h> | - |
52 | #include "qlabel_p.h" | - |
53 | #include "private/qstylesheetstyle_p.h" | - |
54 | #include <qmath.h> | - |
55 | | - |
56 | #ifndef QT_NO_ACCESSIBILITY | - |
57 | #include <qaccessible.h> | - |
58 | #endif | - |
59 | | - |
60 | QT_BEGIN_NAMESPACE | - |
61 | | - |
62 | QLabelPrivate::QLabelPrivate() | - |
63 | : QFramePrivate(), | - |
64 | sh(), | - |
65 | msh(), | - |
66 | text(), | - |
67 | pixmap(Q_NULLPTR), | - |
68 | scaledpixmap(Q_NULLPTR), | - |
69 | cachedimage(Q_NULLPTR), | - |
70 | #ifndef QT_NO_PICTURE | - |
71 | picture(Q_NULLPTR), | - |
72 | #endif | - |
73 | #ifndef QT_NO_MOVIE | - |
74 | movie(), | - |
75 | #endif | - |
76 | control(Q_NULLPTR), | - |
77 | shortcutCursor(), | - |
78 | #ifndef QT_NO_CURSOR | - |
79 | cursor(), | - |
80 | #endif | - |
81 | #ifndef QT_NO_SHORTCUT | - |
82 | buddy(), | - |
83 | shortcutId(0), | - |
84 | #endif | - |
85 | textformat(Qt::AutoText), | - |
86 | textInteractionFlags(Qt::LinksAccessibleByMouse), | - |
87 | sizePolicy(), | - |
88 | margin(0), | - |
89 | align(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextExpandTabs), | - |
90 | indent(-1), | - |
91 | valid_hints(false), | - |
92 | scaledcontents(false), | - |
93 | textLayoutDirty(false), | - |
94 | textDirty(false), | - |
95 | isRichText(false), | - |
96 | isTextLabel(false), | - |
97 | hasShortcut(), | - |
98 | #ifndef QT_NO_CURSOR | - |
99 | validCursor(false), | - |
100 | onAnchor(false), | - |
101 | #endif | - |
102 | openExternalLinks(false) | - |
103 | { | - |
104 | } | - |
105 | | - |
106 | QLabelPrivate::~QLabelPrivate() | - |
107 | { | - |
108 | } | - |
109 | | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
115 | | - |
116 | | - |
117 | | - |
118 | | - |
119 | | - |
120 | | - |
121 | | - |
122 | | - |
123 | | - |
124 | | - |
125 | | - |
126 | | - |
127 | | - |
128 | | - |
129 | | - |
130 | | - |
131 | | - |
132 | | - |
133 | | - |
134 | | - |
135 | | - |
136 | | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | | - |
144 | | - |
145 | | - |
146 | | - |
147 | | - |
148 | | - |
149 | | - |
150 | | - |
151 | | - |
152 | | - |
153 | | - |
154 | | - |
155 | | - |
156 | | - |
157 | | - |
158 | | - |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | | - |
164 | | - |
165 | | - |
166 | | - |
167 | | - |
168 | | - |
169 | | - |
170 | | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | | - |
176 | | - |
177 | | - |
178 | | - |
179 | | - |
180 | | - |
181 | | - |
182 | | - |
183 | | - |
184 | | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | | - |
190 | | - |
191 | | - |
192 | | - |
193 | | - |
194 | | - |
195 | | - |
196 | | - |
197 | | - |
198 | #ifndef QT_NO_PICTURE | - |
199 | | - |
200 | | - |
201 | | - |
202 | | - |
203 | | - |
204 | const QPicture *QLabel::picture() const | - |
205 | { | - |
206 | Q_D(const QLabel); | - |
207 | return d->picture; | - |
208 | } | - |
209 | #endif | - |
210 | | - |
211 | | - |
212 | | - |
213 | | - |
214 | | - |
215 | | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | QLabel::QLabel(QWidget *parent, Qt::WindowFlags f) | - |
221 | : QFrame(*new QLabelPrivate(), parent, f) | - |
222 | { | - |
223 | Q_D(QLabel); | - |
224 | d->init(); | - |
225 | } | - |
226 | | - |
227 | | - |
228 | | - |
229 | | - |
230 | | - |
231 | | - |
232 | | - |
233 | | - |
234 | | - |
235 | QLabel::QLabel(const QString &text, QWidget *parent, Qt::WindowFlags f) | - |
236 | : QFrame(*new QLabelPrivate(), parent, f) | - |
237 | { | - |
238 | Q_D(QLabel); | - |
239 | d->init(); | - |
240 | setText(text); | - |
241 | } | - |
242 | | - |
243 | | - |
244 | | - |
245 | | - |
246 | | - |
247 | | - |
248 | | - |
249 | QLabel::~QLabel() | - |
250 | { | - |
251 | Q_D(QLabel); | - |
252 | d->clearContents(); | - |
253 | } | - |
254 | | - |
255 | void QLabelPrivate::init() | - |
256 | { | - |
257 | Q_Q(QLabel); | - |
258 | | - |
259 | q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, | - |
260 | QSizePolicy::Label)); | - |
261 | setLayoutItemMargins(QStyle::SE_LabelLayoutItem); | - |
262 | } | - |
263 | | - |
264 | | - |
265 | | - |
266 | | - |
267 | | - |
268 | | - |
269 | | - |
270 | | - |
271 | | - |
272 | | - |
273 | | - |
274 | | - |
275 | | - |
276 | | - |
277 | | - |
278 | | - |
279 | | - |
280 | | - |
281 | | - |
282 | | - |
283 | | - |
284 | | - |
285 | | - |
286 | | - |
287 | | - |
288 | | - |
289 | | - |
290 | | - |
291 | | - |
292 | | - |
293 | | - |
294 | void QLabel::setText(const QString &text) | - |
295 | { | - |
296 | Q_D(QLabel); | - |
297 | if (d->text == text) | - |
298 | return; | - |
299 | | - |
300 | QWidgetTextControl *oldControl = d->control; | - |
301 | d->control = 0; | - |
302 | | - |
303 | d->clearContents(); | - |
304 | d->text = text; | - |
305 | d->isTextLabel = true; | - |
306 | d->textDirty = true; | - |
307 | d->isRichText = d->textformat == Qt::RichText | - |
308 | || (d->textformat == Qt::AutoText && Qt::mightBeRichText(d->text)); | - |
309 | | - |
310 | d->control = oldControl; | - |
311 | | - |
312 | if (d->needTextControl()) { | - |
313 | d->ensureTextControl(); | - |
314 | } else { | - |
315 | delete d->control; | - |
316 | d->control = 0; | - |
317 | } | - |
318 | | - |
319 | if (d->isRichText) { | - |
320 | setMouseTracking(true); | - |
321 | } else { | - |
322 | | - |
323 | } | - |
324 | | - |
325 | #ifndef QT_NO_SHORTCUT | - |
326 | if (d->buddy) | - |
327 | d->updateShortcut(); | - |
328 | #endif | - |
329 | | - |
330 | d->updateLabel(); | - |
331 | | - |
332 | #ifndef QT_NO_ACCESSIBILITY | - |
333 | if (accessibleName().isEmpty()) { | - |
334 | QAccessibleEvent event(this, QAccessible::NameChanged); | - |
335 | QAccessible::updateAccessibility(&event); | - |
336 | } | - |
337 | #endif | - |
338 | } | - |
339 | | - |
340 | QString QLabel::text() const | - |
341 | { | - |
342 | Q_D(const QLabel); | - |
343 | return d->text; | - |
344 | } | - |
345 | | - |
346 | | - |
347 | | - |
348 | | - |
349 | | - |
350 | void QLabel::clear() | - |
351 | { | - |
352 | Q_D(QLabel); | - |
353 | d->clearContents(); | - |
354 | d->updateLabel(); | - |
355 | } | - |
356 | | - |
357 | | - |
358 | | - |
359 | | - |
360 | | - |
361 | | - |
362 | | - |
363 | | - |
364 | | - |
365 | | - |
366 | void QLabel::setPixmap(const QPixmap &pixmap) | - |
367 | { | - |
368 | Q_D(QLabel); | - |
369 | if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { | - |
370 | d->clearContents(); | - |
371 | d->pixmap = new QPixmap(pixmap); | - |
372 | } | - |
373 | | - |
374 | if (d->pixmap->depth() == 1 && !d->pixmap->mask()) | - |
375 | d->pixmap->setMask(*((QBitmap *)d->pixmap)); | - |
376 | | - |
377 | d->updateLabel(); | - |
378 | } | - |
379 | | - |
380 | const QPixmap *QLabel::pixmap() const | - |
381 | { | - |
382 | Q_D(const QLabel); | - |
383 | return d->pixmap; | - |
384 | } | - |
385 | | - |
386 | #ifndef QT_NO_PICTURE | - |
387 | | - |
388 | | - |
389 | | - |
390 | | - |
391 | | - |
392 | | - |
393 | | - |
394 | | - |
395 | | - |
396 | void QLabel::setPicture(const QPicture &picture) | - |
397 | { | - |
398 | Q_D(QLabel); | - |
399 | d->clearContents(); | - |
400 | d->picture = new QPicture(picture); | - |
401 | | - |
402 | d->updateLabel(); | - |
403 | } | - |
404 | #endif // QT_NO_PICTURE | - |
405 | | - |
406 | | - |
407 | | - |
408 | | - |
409 | | - |
410 | | - |
411 | | - |
412 | | - |
413 | | - |
414 | | - |
415 | | - |
416 | | - |
417 | void QLabel::setNum(int num) | - |
418 | { | - |
419 | QString str; | - |
420 | str.setNum(num); | - |
421 | setText(str); | - |
422 | } | - |
423 | | - |
424 | | - |
425 | | - |
426 | | - |
427 | | - |
428 | | - |
429 | | - |
430 | | - |
431 | | - |
432 | | - |
433 | | - |
434 | | - |
435 | | - |
436 | | - |
437 | void QLabel::setNum(double num) | - |
438 | { | - |
439 | QString str; | - |
440 | str.setNum(num); | - |
441 | setText(str); | - |
442 | } | - |
443 | | - |
444 | | - |
445 | | - |
446 | | - |
447 | | - |
448 | | - |
449 | | - |
450 | | - |
451 | | - |
452 | | - |
453 | void QLabel::setAlignment(Qt::Alignment alignment) | - |
454 | { | - |
455 | Q_D(QLabel); | - |
456 | if (alignment == (d->align & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask))) | - |
457 | return; | - |
458 | d->align = (d->align & ~(Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask)) | - |
459 | | (alignment & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask)); | - |
460 | | - |
461 | d->updateLabel(); | - |
462 | } | - |
463 | | - |
464 | | - |
465 | Qt::Alignment QLabel::alignment() const | - |
466 | { | - |
467 | Q_D(const QLabel); | - |
468 | return QFlag(d->align & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask)); | - |
469 | } | - |
470 | | - |
471 | | - |
472 | | - |
473 | | - |
474 | | - |
475 | | - |
476 | | - |
477 | | - |
478 | | - |
479 | | - |
480 | | - |
481 | | - |
482 | | - |
483 | void QLabel::setWordWrap(bool on) | - |
484 | { | - |
485 | Q_D(QLabel); | - |
486 | if (on) | - |
487 | d->align |= Qt::TextWordWrap; | - |
488 | else | - |
489 | d->align &= ~Qt::TextWordWrap; | - |
490 | | - |
491 | d->updateLabel(); | - |
492 | } | - |
493 | | - |
494 | bool QLabel::wordWrap() const | - |
495 | { | - |
496 | Q_D(const QLabel); | - |
497 | return d->align & Qt::TextWordWrap; | - |
498 | } | - |
499 | | - |
500 | | - |
501 | | - |
502 | | - |
503 | | - |
504 | | - |
505 | | - |
506 | | - |
507 | | - |
508 | | - |
509 | | - |
510 | | - |
511 | | - |
512 | | - |
513 | | - |
514 | | - |
515 | | - |
516 | | - |
517 | | - |
518 | | - |
519 | | - |
520 | | - |
521 | void QLabel::setIndent(int indent) | - |
522 | { | - |
523 | Q_D(QLabel); | - |
524 | d->indent = indent; | - |
525 | d->updateLabel(); | - |
526 | } | - |
527 | | - |
528 | int QLabel::indent() const | - |
529 | { | - |
530 | Q_D(const QLabel); | - |
531 | return d->indent; | - |
532 | } | - |
533 | | - |
534 | | - |
535 | | - |
536 | | - |
537 | | - |
538 | | - |
539 | | - |
540 | | - |
541 | | - |
542 | | - |
543 | | - |
544 | | - |
545 | | - |
546 | int QLabel::margin() const | - |
547 | { | - |
548 | Q_D(const QLabel); | - |
549 | return d->margin; | - |
550 | } | - |
551 | | - |
552 | void QLabel::setMargin(int margin) | - |
553 | { | - |
554 | Q_D(QLabel); | - |
555 | if (d->margin == margin) | - |
556 | return; | - |
557 | d->margin = margin; | - |
558 | d->updateLabel(); | - |
559 | } | - |
560 | | - |
561 | | - |
562 | | - |
563 | | - |
564 | | - |
565 | QSize QLabelPrivate::sizeForWidth(int w) const | - |
566 | { | - |
567 | Q_Q(const QLabel); | - |
568 | if(q->minimumWidth() > 0) | - |
569 | w = qMax(w, q->minimumWidth()); | - |
570 | QSize contentsMargin(leftmargin + rightmargin, topmargin + bottommargin); | - |
571 | | - |
572 | QRect br; | - |
573 | | - |
574 | int hextra = 2 * margin; | - |
575 | int vextra = hextra; | - |
576 | QFontMetrics fm = q->fontMetrics(); | - |
577 | | - |
578 | if (pixmap && !pixmap->isNull()) { | - |
579 | br = pixmap->rect(); | - |
580 | br.setSize(br.size() / pixmap->devicePixelRatio()); | - |
581 | #ifndef QT_NO_PICTURE | - |
582 | } else if (picture && !picture->isNull()) { | - |
583 | br = picture->boundingRect(); | - |
584 | #endif | - |
585 | #ifndef QT_NO_MOVIE | - |
586 | } else if (movie && !movie->currentPixmap().isNull()) { | - |
587 | br = movie->currentPixmap().rect(); | - |
588 | br.setSize(br.size() / movie->currentPixmap().devicePixelRatio()); | - |
589 | #endif | - |
590 | } else if (isTextLabel) { | - |
591 | int align = QStyle::visualAlignment(textDirection(), QFlag(this->align)); | - |
592 | | - |
593 | int m = indent; | - |
594 | | - |
595 | if (m < 0 && q->frameWidth()) | - |
596 | m = fm.width(QLatin1Char('x')) - margin*2; | - |
597 | if (m > 0) { | - |
598 | if ((align & Qt::AlignLeft) || (align & Qt::AlignRight)) | - |
599 | hextra += m; | - |
600 | if ((align & Qt::AlignTop) || (align & Qt::AlignBottom)) | - |
601 | vextra += m; | - |
602 | } | - |
603 | | - |
604 | if (control) { | - |
605 | ensureTextLayouted(); | - |
606 | const qreal oldTextWidth = control->textWidth(); | - |
607 | | - |
608 | if (align & Qt::TextWordWrap) { | - |
609 | if (w >= 0) { | - |
610 | w = qMax(w-hextra-contentsMargin.width(), 0); | - |
611 | control->setTextWidth(w); | - |
612 | } else { | - |
613 | control->adjustSize(); | - |
614 | } | - |
615 | } else { | - |
616 | control->setTextWidth(-1); | - |
617 | } | - |
618 | | - |
619 | QSizeF controlSize = control->size(); | - |
620 | br = QRect(QPoint(0, 0), QSize(qCeil(controlSize.width()), qCeil(controlSize.height()))); | - |
621 | | - |
622 | | - |
623 | control->setTextWidth(oldTextWidth); | - |
624 | } else { | - |
625 | | - |
626 | | - |
627 | int flags = align & ~(Qt::AlignVCenter | Qt::AlignHCenter); | - |
628 | if (hasShortcut) { | - |
629 | flags |= Qt::TextShowMnemonic; | - |
630 | QStyleOption opt; | - |
631 | opt.initFrom(q); | - |
632 | if (!q->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, q)) | - |
633 | flags |= Qt::TextHideMnemonic; | - |
634 | } | - |
635 | | - |
636 | bool tryWidth = (w < 0) && (align & Qt::TextWordWrap); | - |
637 | if (tryWidth) | - |
638 | w = qMin(fm.averageCharWidth() * 80, q->maximumSize().width()); | - |
639 | else if (w < 0) | - |
640 | w = 2000; | - |
641 | w -= (hextra + contentsMargin.width()); | - |
642 | br = fm.boundingRect(0, 0, w ,2000, flags, text); | - |
643 | if (tryWidth && br.height() < 4*fm.lineSpacing() && br.width() > w/2) | - |
644 | br = fm.boundingRect(0, 0, w/2, 2000, flags, text); | - |
645 | if (tryWidth && br.height() < 2*fm.lineSpacing() && br.width() > w/4) | - |
646 | br = fm.boundingRect(0, 0, w/4, 2000, flags, text); | - |
647 | } | - |
648 | } else { | - |
649 | br = QRect(QPoint(0, 0), QSize(fm.averageCharWidth(), fm.lineSpacing())); | - |
650 | } | - |
651 | | - |
652 | const QSize contentsSize(br.width() + hextra, br.height() + vextra); | - |
653 | return (contentsSize + contentsMargin).expandedTo(q->minimumSize()); | - |
654 | } | - |
655 | | - |
656 | | - |
657 | | - |
658 | | - |
659 | | - |
660 | | - |
661 | int QLabel::heightForWidth(int w) const | - |
662 | { | - |
663 | Q_D(const QLabel); | - |
664 | if (d->isTextLabel) | - |
665 | return d->sizeForWidth(w).height(); | - |
666 | return QWidget::heightForWidth(w); | - |
667 | } | - |
668 | | - |
669 | | - |
670 | | - |
671 | | - |
672 | | - |
673 | | - |
674 | | - |
675 | | - |
676 | | - |
677 | | - |
678 | | - |
679 | | - |
680 | | - |
681 | | - |
682 | | - |
683 | | - |
684 | bool QLabel::openExternalLinks() const | - |
685 | { | - |
686 | Q_D(const QLabel); | - |
687 | return d->openExternalLinks; | - |
688 | } | - |
689 | | - |
690 | void QLabel::setOpenExternalLinks(bool open) | - |
691 | { | - |
692 | Q_D(QLabel); | - |
693 | d->openExternalLinks = open; | - |
694 | if (d->control) | - |
695 | d->control->setOpenExternalLinks(open); | - |
696 | } | - |
697 | | - |
698 | | - |
699 | | - |
700 | | - |
701 | | - |
702 | | - |
703 | | - |
704 | | - |
705 | | - |
706 | | - |
707 | | - |
708 | | - |
709 | | - |
710 | void QLabel::setTextInteractionFlags(Qt::TextInteractionFlags flags) | - |
711 | { | - |
712 | Q_D(QLabel); | - |
713 | if (d->textInteractionFlags == flags) | - |
714 | return; | - |
715 | d->textInteractionFlags = flags; | - |
716 | if (flags & Qt::LinksAccessibleByKeyboard) | - |
717 | setFocusPolicy(Qt::StrongFocus); | - |
718 | else if (flags & (Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse)) | - |
719 | setFocusPolicy(Qt::ClickFocus); | - |
720 | else | - |
721 | setFocusPolicy(Qt::NoFocus); | - |
722 | | - |
723 | if (d->needTextControl()) { | - |
724 | d->ensureTextControl(); | - |
725 | } else { | - |
726 | delete d->control; | - |
727 | d->control = 0; | - |
728 | } | - |
729 | | - |
730 | if (d->control) | - |
731 | d->control->setTextInteractionFlags(d->textInteractionFlags); | - |
732 | } | - |
733 | | - |
734 | Qt::TextInteractionFlags QLabel::textInteractionFlags() const | - |
735 | { | - |
736 | Q_D(const QLabel); | - |
737 | return d->textInteractionFlags; | - |
738 | } | - |
739 | | - |
740 | | - |
741 | | - |
742 | | - |
743 | | - |
744 | | - |
745 | | - |
746 | | - |
747 | | - |
748 | | - |
749 | | - |
750 | void QLabel::setSelection(int start, int length) | - |
751 | { | - |
752 | Q_D(QLabel); | - |
753 | if (d->control) { | - |
754 | d->ensureTextPopulated(); | - |
755 | QTextCursor cursor = d->control->textCursor(); | - |
756 | cursor.setPosition(start); | - |
757 | cursor.setPosition(start + length, QTextCursor::KeepAnchor); | - |
758 | d->control->setTextCursor(cursor); | - |
759 | } | - |
760 | } | - |
761 | | - |
762 | | - |
763 | | - |
764 | | - |
765 | | - |
766 | | - |
767 | | - |
768 | | - |
769 | | - |
770 | | - |
771 | | - |
772 | | - |
773 | | - |
774 | | - |
775 | | - |
776 | | - |
777 | | - |
778 | bool QLabel::hasSelectedText() const | - |
779 | { | - |
780 | Q_D(const QLabel); | - |
781 | if (d->control) | - |
782 | return d->control->textCursor().hasSelection(); | - |
783 | return false; | - |
784 | } | - |
785 | | - |
786 | | - |
787 | | - |
788 | | - |
789 | | - |
790 | | - |
791 | | - |
792 | | - |
793 | | - |
794 | | - |
795 | | - |
796 | | - |
797 | | - |
798 | | - |
799 | | - |
800 | | - |
801 | | - |
802 | QString QLabel::selectedText() const | - |
803 | { | - |
804 | Q_D(const QLabel); | - |
805 | if (d->control) | - |
806 | return d->control->textCursor().selectedText(); | - |
807 | return QString(); | - |
808 | } | - |
809 | | - |
810 | | - |
811 | | - |
812 | | - |
813 | | - |
814 | | - |
815 | | - |
816 | | - |
817 | | - |
818 | | - |
819 | | - |
820 | | - |
821 | int QLabel::selectionStart() const | - |
822 | { | - |
823 | Q_D(const QLabel); | - |
824 | if (d->control && d->control->textCursor().hasSelection()) | - |
825 | return d->control->textCursor().selectionStart(); | - |
826 | return -1; | - |
827 | } | - |
828 | | - |
829 | | - |
830 | | - |
831 | QSize QLabel::sizeHint() const | - |
832 | { | - |
833 | Q_D(const QLabel); | - |
834 | if (!d->valid_hints) | - |
835 | (void) QLabel::minimumSizeHint(); | - |
836 | return d->sh; | - |
837 | } | - |
838 | | - |
839 | | - |
840 | | - |
841 | | - |
842 | QSize QLabel::minimumSizeHint() const | - |
843 | { | - |
844 | Q_D(const QLabel); | - |
845 | if (d->valid_hints) { | - |
846 | if (d->sizePolicy == sizePolicy()) | - |
847 | return d->msh; | - |
848 | } | - |
849 | | - |
850 | ensurePolished(); | - |
851 | d->valid_hints = true; | - |
852 | d->sh = d->sizeForWidth(-1); | - |
853 | QSize msh(-1, -1); | - |
854 | | - |
855 | if (!d->isTextLabel) { | - |
856 | msh = d->sh; | - |
857 | } else { | - |
858 | msh.rheight() = d->sizeForWidth(QWIDGETSIZE_MAX).height(); | - |
859 | msh.rwidth() = d->sizeForWidth(0).width(); | - |
860 | if (d->sh.height() < msh.height()) | - |
861 | msh.rheight() = d->sh.height(); | - |
862 | } | - |
863 | d->msh = msh; | - |
864 | d->sizePolicy = sizePolicy(); | - |
865 | return msh; | - |
866 | } | - |
867 | | - |
868 | | - |
869 | | - |
870 | void QLabel::mousePressEvent(QMouseEvent *ev) | - |
871 | { | - |
872 | Q_D(QLabel); | - |
873 | d->sendControlEvent(ev); | - |
874 | } | - |
875 | | - |
876 | | - |
877 | | - |
878 | void QLabel::mouseMoveEvent(QMouseEvent *ev) | - |
879 | { | - |
880 | Q_D(QLabel); | - |
881 | d->sendControlEvent(ev); | - |
882 | } | - |
883 | | - |
884 | | - |
885 | | - |
886 | void QLabel::mouseReleaseEvent(QMouseEvent *ev) | - |
887 | { | - |
888 | Q_D(QLabel); | - |
889 | d->sendControlEvent(ev); | - |
890 | } | - |
891 | | - |
892 | #ifndef QT_NO_CONTEXTMENU | - |
893 | | - |
894 | | - |
895 | void QLabel::contextMenuEvent(QContextMenuEvent *ev) | - |
896 | { | - |
897 | #ifdef QT_NO_CONTEXTMENU | - |
| Q_UNUSED(ev); | |
| #elseQ_D(QLabel); | |
898 | if (!d->isTextLabel) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
899 | ev->ignore(); | - |
900 | return; never executed: return; | 0 |
901 | } | - |
902 | QMenu *menu = d->createStandardContextMenu(ev->pos()); | - |
903 | if (!menu) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
904 | ev->ignore(); | - |
905 | return; never executed: return; | 0 |
906 | } | - |
907 | ev->accept(); | - |
908 | menu->setAttribute(Qt::WA_DeleteOnClose); | - |
909 | menu->popup(ev->globalPos()); | - |
| #endif never executed: end of block }never executed: end of block | |
911 | #endif | - |
912 | | - |
913 | | - |
914 | | - |
915 | | - |
916 | void QLabel::focusInEvent(QFocusEvent *ev) | - |
917 | { | - |
918 | Q_D(QLabel); | - |
919 | if (d->isTextLabel) { | - |
920 | d->ensureTextControl(); | - |
921 | d->sendControlEvent(ev); | - |
922 | } | - |
923 | QFrame::focusInEvent(ev); | - |
924 | } | - |
925 | | - |
926 | | - |
927 | | - |
928 | | - |
929 | void QLabel::focusOutEvent(QFocusEvent *ev) | - |
930 | { | - |
931 | Q_D(QLabel); | - |
932 | if (d->control) { | - |
933 | d->sendControlEvent(ev); | - |
934 | QTextCursor cursor = d->control->textCursor(); | - |
935 | Qt::FocusReason reason = ev->reason(); | - |
936 | if (reason != Qt::ActiveWindowFocusReason | - |
937 | && reason != Qt::PopupFocusReason | - |
938 | && cursor.hasSelection()) { | - |
939 | cursor.clearSelection(); | - |
940 | d->control->setTextCursor(cursor); | - |
941 | } | - |
942 | } | - |
943 | | - |
944 | QFrame::focusOutEvent(ev); | - |
945 | } | - |
946 | | - |
947 | | - |
948 | | - |
949 | bool QLabel::focusNextPrevChild(bool next) | - |
950 | { | - |
951 | Q_D(QLabel); | - |
952 | if (d->control && d->control->setFocusToNextOrPreviousAnchor(next)) | - |
953 | return true; | - |
954 | return QFrame::focusNextPrevChild(next); | - |
955 | } | - |
956 | | - |
957 | | - |
958 | | - |
959 | void QLabel::keyPressEvent(QKeyEvent *ev) | - |
960 | { | - |
961 | Q_D(QLabel); | - |
962 | d->sendControlEvent(ev); | - |
963 | } | - |
964 | | - |
965 | | - |
966 | | - |
967 | bool QLabel::event(QEvent *e) | - |
968 | { | - |
969 | Q_D(QLabel); | - |
970 | QEvent::Type type = e->type(); | - |
971 | | - |
972 | #ifndef QT_NO_SHORTCUT | - |
973 | if (type == QEvent::Shortcut) { | - |
974 | QShortcutEvent *se = static_cast<QShortcutEvent *>(e); | - |
975 | if (se->shortcutId() == d->shortcutId) { | - |
976 | QWidget * w = d->buddy; | - |
977 | QAbstractButton *button = qobject_cast<QAbstractButton *>(w); | - |
978 | if (w->focusPolicy() != Qt::NoFocus) | - |
979 | w->setFocus(Qt::ShortcutFocusReason); | - |
980 | if (button && !se->isAmbiguous()) | - |
981 | button->animateClick(); | - |
982 | else | - |
983 | window()->setAttribute(Qt::WA_KeyboardFocusChange); | - |
984 | return true; | - |
985 | } | - |
986 | } else | - |
987 | #endif | - |
988 | if (type == QEvent::Resize) { | - |
989 | if (d->control) | - |
990 | d->textLayoutDirty = true; | - |
991 | } else if (e->type() == QEvent::StyleChange | - |
992 | #ifdef Q_OS_MAC | - |
993 | || e->type() == QEvent::MacSizeChange | - |
994 | #endif | - |
995 | ) { | - |
996 | d->setLayoutItemMargins(QStyle::SE_LabelLayoutItem); | - |
997 | d->updateLabel(); | - |
998 | } | - |
999 | | - |
1000 | return QFrame::event(e); | - |
1001 | } | - |
1002 | | - |
1003 | | - |
1004 | | - |
1005 | void QLabel::paintEvent(QPaintEvent *) | - |
1006 | { | - |
1007 | Q_D(QLabel); | - |
1008 | QStyle *style = QWidget::style(); | - |
1009 | QPainter painter(this); | - |
1010 | drawFrame(&painter); | - |
1011 | QRect cr = contentsRect(); | - |
1012 | cr.adjust(d->margin, d->margin, -d->margin, -d->margin); | - |
1013 | int align = QStyle::visualAlignment(d->isTextLabel ? d->textDirection() | - |
1014 | : layoutDirection(), QFlag(d->align)); | - |
1015 | | - |
1016 | #ifndef QT_NO_MOVIE | - |
1017 | if (d->movie) { | - |
1018 | if (d->scaledcontents) | - |
1019 | style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size())); | - |
1020 | else | - |
1021 | style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap()); | - |
1022 | } | - |
1023 | else | - |
1024 | #endif | - |
1025 | if (d->isTextLabel) { | - |
1026 | QRectF lr = d->layoutRect().toAlignedRect(); | - |
1027 | QStyleOption opt; | - |
1028 | opt.initFrom(this); | - |
1029 | #ifndef QT_NO_STYLE_STYLESHEET | - |
1030 | if (QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(style)) { | - |
1031 | cssStyle->styleSheetPalette(this, &opt, &opt.palette); | - |
1032 | } | - |
1033 | #endif | - |
1034 | if (d->control) { | - |
1035 | #ifndef QT_NO_SHORTCUT | - |
1036 | const bool underline = (bool)style->styleHint(QStyle::SH_UnderlineShortcut, 0, this, 0); | - |
1037 | if (d->shortcutId != 0 | - |
1038 | && underline != d->shortcutCursor.charFormat().fontUnderline()) { | - |
1039 | QTextCharFormat fmt; | - |
1040 | fmt.setFontUnderline(underline); | - |
1041 | d->shortcutCursor.mergeCharFormat(fmt); | - |
1042 | } | - |
1043 | #endif | - |
1044 | d->ensureTextLayouted(); | - |
1045 | | - |
1046 | QAbstractTextDocumentLayout::PaintContext context; | - |
1047 | | - |
1048 | context.palette = opt.palette; | - |
1049 | | - |
1050 | if (foregroundRole() != QPalette::Text && isEnabled()) | - |
1051 | context.palette.setColor(QPalette::Text, context.palette.color(foregroundRole())); | - |
1052 | | - |
1053 | painter.save(); | - |
1054 | painter.translate(lr.topLeft()); | - |
1055 | painter.setClipRect(lr.translated(-lr.x(), -lr.y())); | - |
1056 | d->control->setPalette(context.palette); | - |
1057 | d->control->drawContents(&painter, QRectF(), this); | - |
1058 | painter.restore(); | - |
1059 | } else { | - |
1060 | int flags = align | (d->textDirection() == Qt::LeftToRight ? Qt::TextForceLeftToRight | - |
1061 | : Qt::TextForceRightToLeft); | - |
1062 | if (d->hasShortcut) { | - |
1063 | flags |= Qt::TextShowMnemonic; | - |
1064 | if (!style->styleHint(QStyle::SH_UnderlineShortcut, &opt, this)) | - |
1065 | flags |= Qt::TextHideMnemonic; | - |
1066 | } | - |
1067 | style->drawItemText(&painter, lr.toRect(), flags, opt.palette, isEnabled(), d->text, foregroundRole()); | - |
1068 | } | - |
1069 | } else | - |
1070 | #ifndef QT_NO_PICTURE | - |
1071 | if (d->picture) { | - |
1072 | QRect br = d->picture->boundingRect(); | - |
1073 | int rw = br.width(); | - |
1074 | int rh = br.height(); | - |
1075 | if (d->scaledcontents) { | - |
1076 | painter.save(); | - |
1077 | painter.translate(cr.x(), cr.y()); | - |
1078 | painter.scale((double)cr.width()/rw, (double)cr.height()/rh); | - |
1079 | painter.drawPicture(-br.x(), -br.y(), *d->picture); | - |
1080 | painter.restore(); | - |
1081 | } else { | - |
1082 | int xo = 0; | - |
1083 | int yo = 0; | - |
1084 | if (align & Qt::AlignVCenter) | - |
1085 | yo = (cr.height()-rh)/2; | - |
1086 | else if (align & Qt::AlignBottom) | - |
1087 | yo = cr.height()-rh; | - |
1088 | if (align & Qt::AlignRight) | - |
1089 | xo = cr.width()-rw; | - |
1090 | else if (align & Qt::AlignHCenter) | - |
1091 | xo = (cr.width()-rw)/2; | - |
1092 | painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture); | - |
1093 | } | - |
1094 | } else | - |
1095 | #endif | - |
1096 | if (d->pixmap && !d->pixmap->isNull()) { | - |
1097 | QPixmap pix; | - |
1098 | if (d->scaledcontents) { | - |
1099 | QSize scaledSize = cr.size() * devicePixelRatioF(); | - |
1100 | if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { | - |
1101 | if (!d->cachedimage) | - |
1102 | d->cachedimage = new QImage(d->pixmap->toImage()); | - |
1103 | delete d->scaledpixmap; | - |
1104 | QImage scaledImage = | - |
1105 | d->cachedimage->scaled(scaledSize, | - |
1106 | Qt::IgnoreAspectRatio, Qt::SmoothTransformation); | - |
1107 | d->scaledpixmap = new QPixmap(QPixmap::fromImage(scaledImage)); | - |
1108 | d->scaledpixmap->setDevicePixelRatio(devicePixelRatioF()); | - |
1109 | } | - |
1110 | pix = *d->scaledpixmap; | - |
1111 | } else | - |
1112 | pix = *d->pixmap; | - |
1113 | QStyleOption opt; | - |
1114 | opt.initFrom(this); | - |
1115 | if (!isEnabled()) | - |
1116 | pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt); | - |
1117 | style->drawItemPixmap(&painter, cr, align, pix); | - |
1118 | } | - |
1119 | } | - |
1120 | | - |
1121 | | - |
1122 | | - |
1123 | | - |
1124 | | - |
1125 | | - |
1126 | void QLabelPrivate::updateLabel() | - |
1127 | { | - |
1128 | Q_Q(QLabel); | - |
1129 | valid_hints = false; | - |
1130 | | - |
1131 | if (isTextLabel) { | - |
1132 | QSizePolicy policy = q->sizePolicy(); | - |
1133 | const bool wrap = align & Qt::TextWordWrap; | - |
1134 | policy.setHeightForWidth(wrap); | - |
1135 | if (policy != q->sizePolicy()) | - |
1136 | q->setSizePolicy(policy); | - |
1137 | textLayoutDirty = true; | - |
1138 | } | - |
1139 | q->updateGeometry(); | - |
1140 | q->update(q->contentsRect()); | - |
1141 | } | - |
1142 | | - |
1143 | #ifndef QT_NO_SHORTCUT | - |
1144 | | - |
1145 | | - |
1146 | | - |
1147 | | - |
1148 | | - |
1149 | | - |
1150 | | - |
1151 | | - |
1152 | | - |
1153 | | - |
1154 | | - |
1155 | | - |
1156 | | - |
1157 | | - |
1158 | | - |
1159 | | - |
1160 | | - |
1161 | | - |
1162 | | - |
1163 | | - |
1164 | | - |
1165 | | - |
1166 | | - |
1167 | | - |
1168 | | - |
1169 | | - |
1170 | | - |
1171 | void QLabel::setBuddy(QWidget *buddy) | - |
1172 | { | - |
1173 | Q_D(QLabel); | - |
1174 | d->buddy = buddy; | - |
1175 | if (d->isTextLabel) { | - |
1176 | if (d->shortcutId) | - |
1177 | releaseShortcut(d->shortcutId); | - |
1178 | d->shortcutId = 0; | - |
1179 | d->textDirty = true; | - |
1180 | if (buddy) | - |
1181 | d->updateShortcut(); | - |
1182 | d->updateLabel(); | - |
1183 | } | - |
1184 | } | - |
1185 | | - |
1186 | | - |
1187 | | - |
1188 | | - |
1189 | | - |
1190 | | - |
1191 | | - |
1192 | | - |
1193 | QWidget * QLabel::buddy() const | - |
1194 | { | - |
1195 | Q_D(const QLabel); | - |
1196 | return d->buddy; | - |
1197 | } | - |
1198 | | - |
1199 | void QLabelPrivate::updateShortcut() | - |
1200 | { | - |
1201 | Q_Q(QLabel); | - |
1202 | Q_ASSERT(shortcutId == 0); | - |
1203 | | - |
1204 | | - |
1205 | | - |
1206 | | - |
1207 | hasShortcut = false; | - |
1208 | | - |
1209 | if (!text.contains(QLatin1Char('&'))) | - |
1210 | return; | - |
1211 | hasShortcut = true; | - |
1212 | shortcutId = q->grabShortcut(QKeySequence::mnemonic(text)); | - |
1213 | } | - |
1214 | | - |
1215 | #endif // QT_NO_SHORTCUT | - |
1216 | | - |
1217 | #ifndef QT_NO_MOVIE | - |
1218 | void QLabelPrivate::_q_movieUpdated(const QRect& rect) | - |
1219 | { | - |
1220 | Q_Q(QLabel); | - |
1221 | if (movie && movie->isValid()) { | - |
1222 | QRect r; | - |
1223 | if (scaledcontents) { | - |
1224 | QRect cr = q->contentsRect(); | - |
1225 | QRect pixmapRect(cr.topLeft(), movie->currentPixmap().size()); | - |
1226 | if (pixmapRect.isEmpty()) | - |
1227 | return; | - |
1228 | r.setRect(cr.left(), cr.top(), | - |
1229 | (rect.width() * cr.width()) / pixmapRect.width(), | - |
1230 | (rect.height() * cr.height()) / pixmapRect.height()); | - |
1231 | } else { | - |
1232 | r = q->style()->itemPixmapRect(q->contentsRect(), align, movie->currentPixmap()); | - |
1233 | r.translate(rect.x(), rect.y()); | - |
1234 | r.setWidth(qMin(r.width(), rect.width())); | - |
1235 | r.setHeight(qMin(r.height(), rect.height())); | - |
1236 | } | - |
1237 | q->update(r); | - |
1238 | } | - |
1239 | } | - |
1240 | | - |
1241 | void QLabelPrivate::_q_movieResized(const QSize& size) | - |
1242 | { | - |
1243 | Q_Q(QLabel); | - |
1244 | q->update(); | - |
1245 | valid_hints = false; | - |
1246 | _q_movieUpdated(QRect(QPoint(0,0), size)); | - |
1247 | q->updateGeometry(); | - |
1248 | } | - |
1249 | | - |
1250 | | - |
1251 | | - |
1252 | | - |
1253 | | - |
1254 | | - |
1255 | | - |
1256 | | - |
1257 | | - |
1258 | | - |
1259 | void QLabel::setMovie(QMovie *movie) | - |
1260 | { | - |
1261 | Q_D(QLabel); | - |
1262 | d->clearContents(); | - |
1263 | | - |
1264 | if (!movie) | - |
1265 | return; | - |
1266 | | - |
1267 | d->movie = movie; | - |
1268 | connect(movie, SIGNAL(resized(QSize)), this, SLOT(_q_movieResized(QSize))); | - |
1269 | connect(movie, SIGNAL(updated(QRect)), this, SLOT(_q_movieUpdated(QRect))); | - |
1270 | | - |
1271 | | - |
1272 | | - |
1273 | if (movie->state() != QMovie::Running) | - |
1274 | d->updateLabel(); | - |
1275 | } | - |
1276 | | - |
1277 | #endif // QT_NO_MOVIE | - |
1278 | | - |
1279 | | - |
1280 | | - |
1281 | | - |
1282 | | - |
1283 | | - |
1284 | | - |
1285 | void QLabelPrivate::clearContents() | - |
1286 | { | - |
1287 | delete control; | - |
1288 | control = 0; | - |
1289 | isTextLabel = false; | - |
1290 | hasShortcut = false; | - |
1291 | | - |
1292 | #ifndef QT_NO_PICTURE | - |
1293 | delete picture; | - |
1294 | picture = 0; | - |
1295 | #endif | - |
1296 | delete scaledpixmap; | - |
1297 | scaledpixmap = 0; | - |
1298 | delete cachedimage; | - |
1299 | cachedimage = 0; | - |
1300 | delete pixmap; | - |
1301 | pixmap = 0; | - |
1302 | | - |
1303 | text.clear(); | - |
1304 | Q_Q(QLabel); | - |
1305 | #ifndef QT_NO_SHORTCUT | - |
1306 | if (shortcutId) | - |
1307 | q->releaseShortcut(shortcutId); | - |
1308 | shortcutId = 0; | - |
1309 | #endif | - |
1310 | #ifndef QT_NO_MOVIE | - |
1311 | if (movie) { | - |
1312 | QObject::disconnect(movie, SIGNAL(resized(QSize)), q, SLOT(_q_movieResized(QSize))); | - |
1313 | QObject::disconnect(movie, SIGNAL(updated(QRect)), q, SLOT(_q_movieUpdated(QRect))); | - |
1314 | } | - |
1315 | movie = 0; | - |
1316 | #endif | - |
1317 | #ifndef QT_NO_CURSOR | - |
1318 | if (onAnchor) { | - |
1319 | if (validCursor) | - |
1320 | q->setCursor(cursor); | - |
1321 | else | - |
1322 | q->unsetCursor(); | - |
1323 | } | - |
1324 | validCursor = false; | - |
1325 | onAnchor = false; | - |
1326 | #endif | - |
1327 | } | - |
1328 | | - |
1329 | | - |
1330 | #ifndef QT_NO_MOVIE | - |
1331 | | - |
1332 | | - |
1333 | | - |
1334 | | - |
1335 | | - |
1336 | | - |
1337 | | - |
1338 | | - |
1339 | QMovie *QLabel::movie() const | - |
1340 | { | - |
1341 | Q_D(const QLabel); | - |
1342 | return d->movie; | - |
1343 | } | - |
1344 | | - |
1345 | #endif // QT_NO_MOVIE | - |
1346 | | - |
1347 | | - |
1348 | | - |
1349 | | - |
1350 | | - |
1351 | | - |
1352 | | - |
1353 | | - |
1354 | | - |
1355 | | - |
1356 | | - |
1357 | | - |
1358 | | - |
1359 | Qt::TextFormat QLabel::textFormat() const | - |
1360 | { | - |
1361 | Q_D(const QLabel); | - |
1362 | return d->textformat; | - |
1363 | } | - |
1364 | | - |
1365 | void QLabel::setTextFormat(Qt::TextFormat format) | - |
1366 | { | - |
1367 | Q_D(QLabel); | - |
1368 | if (format != d->textformat) { | - |
1369 | d->textformat = format; | - |
1370 | QString t = d->text; | - |
1371 | if (!t.isNull()) { | - |
1372 | d->text.clear(); | - |
1373 | setText(t); | - |
1374 | } | - |
1375 | } | - |
1376 | } | - |
1377 | | - |
1378 | | - |
1379 | | - |
1380 | | - |
1381 | void QLabel::changeEvent(QEvent *ev) | - |
1382 | { | - |
1383 | Q_D(QLabel); | - |
1384 | if(ev->type() == QEvent::FontChange || ev->type() == QEvent::ApplicationFontChange) { | - |
1385 | if (d->isTextLabel) { | - |
1386 | if (d->control) | - |
1387 | d->control->document()->setDefaultFont(font()); | - |
1388 | d->updateLabel(); | - |
1389 | } | - |
1390 | } else if (ev->type() == QEvent::PaletteChange && d->control) { | - |
1391 | d->control->setPalette(palette()); | - |
1392 | } else if (ev->type() == QEvent::ContentsRectChange) { | - |
1393 | d->updateLabel(); | - |
1394 | } | - |
1395 | QFrame::changeEvent(ev); | - |
1396 | } | - |
1397 | | - |
1398 | | - |
1399 | | - |
1400 | | - |
1401 | | - |
1402 | | - |
1403 | | - |
1404 | | - |
1405 | | - |
1406 | | - |
1407 | | - |
1408 | bool QLabel::hasScaledContents() const | - |
1409 | { | - |
1410 | Q_D(const QLabel); | - |
1411 | return d->scaledcontents; | - |
1412 | } | - |
1413 | | - |
1414 | void QLabel::setScaledContents(bool enable) | - |
1415 | { | - |
1416 | Q_D(QLabel); | - |
1417 | if ((bool)d->scaledcontents == enable) | - |
1418 | return; | - |
1419 | d->scaledcontents = enable; | - |
1420 | if (!enable) { | - |
1421 | delete d->scaledpixmap; | - |
1422 | d->scaledpixmap = 0; | - |
1423 | delete d->cachedimage; | - |
1424 | d->cachedimage = 0; | - |
1425 | } | - |
1426 | update(contentsRect()); | - |
1427 | } | - |
1428 | | - |
1429 | Qt::LayoutDirection QLabelPrivate::textDirection() const | - |
1430 | { | - |
1431 | if (control) { | - |
1432 | QTextOption opt = control->document()->defaultTextOption(); | - |
1433 | return opt.textDirection(); | - |
1434 | } | - |
1435 | | - |
1436 | return text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight; | - |
1437 | } | - |
1438 | | - |
1439 | | - |
1440 | | - |
1441 | QRect QLabelPrivate::documentRect() const | - |
1442 | { | - |
1443 | Q_Q(const QLabel); | - |
1444 | Q_ASSERT_X(isTextLabel, "documentRect", "document rect called for label that is not a text label!"); | - |
1445 | QRect cr = q->contentsRect(); | - |
1446 | cr.adjust(margin, margin, -margin, -margin); | - |
1447 | const int align = QStyle::visualAlignment(isTextLabel ? textDirection() | - |
1448 | : q->layoutDirection(), QFlag(this->align)); | - |
1449 | int m = indent; | - |
1450 | if (m < 0 && q->frameWidth()) | - |
1451 | m = q->fontMetrics().width(QLatin1Char('x')) / 2 - margin; | - |
1452 | if (m > 0) { | - |
1453 | if (align & Qt::AlignLeft) | - |
1454 | cr.setLeft(cr.left() + m); | - |
1455 | if (align & Qt::AlignRight) | - |
1456 | cr.setRight(cr.right() - m); | - |
1457 | if (align & Qt::AlignTop) | - |
1458 | cr.setTop(cr.top() + m); | - |
1459 | if (align & Qt::AlignBottom) | - |
1460 | cr.setBottom(cr.bottom() - m); | - |
1461 | } | - |
1462 | return cr; | - |
1463 | } | - |
1464 | | - |
1465 | void QLabelPrivate::ensureTextPopulated() const | - |
1466 | { | - |
1467 | if (!textDirty) | - |
1468 | return; | - |
1469 | if (control) { | - |
1470 | QTextDocument *doc = control->document(); | - |
1471 | if (textDirty) { | - |
1472 | #ifndef QT_NO_TEXTHTMLPARSER | - |
1473 | if (isRichText) | - |
1474 | doc->setHtml(text); | - |
1475 | else | - |
1476 | doc->setPlainText(text); | - |
1477 | #else | - |
1478 | doc->setPlainText(text); | - |
1479 | #endif | - |
1480 | doc->setUndoRedoEnabled(false); | - |
1481 | | - |
1482 | #ifndef QT_NO_SHORTCUT | - |
1483 | if (hasShortcut) { | - |
1484 | | - |
1485 | int from = 0; | - |
1486 | bool found = false; | - |
1487 | QTextCursor cursor; | - |
1488 | while (!(cursor = control->document()->find((QLatin1String("&")), from)).isNull()) { | - |
1489 | cursor.deleteChar(); | - |
1490 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); | - |
1491 | from = cursor.position(); | - |
1492 | if (!found && cursor.selectedText() != QLatin1String("&")) { | - |
1493 | found = true; | - |
1494 | shortcutCursor = cursor; | - |
1495 | } | - |
1496 | } | - |
1497 | } | - |
1498 | #endif | - |
1499 | } | - |
1500 | } | - |
1501 | textDirty = false; | - |
1502 | } | - |
1503 | | - |
1504 | void QLabelPrivate::ensureTextLayouted() const | - |
1505 | { | - |
1506 | if (!textLayoutDirty) | - |
1507 | return; | - |
1508 | ensureTextPopulated(); | - |
1509 | if (control) { | - |
1510 | QTextDocument *doc = control->document(); | - |
1511 | QTextOption opt = doc->defaultTextOption(); | - |
1512 | | - |
1513 | opt.setAlignment(QFlag(this->align)); | - |
1514 | | - |
1515 | if (this->align & Qt::TextWordWrap) | - |
1516 | opt.setWrapMode(QTextOption::WordWrap); | - |
1517 | else | - |
1518 | opt.setWrapMode(QTextOption::ManualWrap); | - |
1519 | | - |
1520 | doc->setDefaultTextOption(opt); | - |
1521 | | - |
1522 | QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); | - |
1523 | fmt.setMargin(0); | - |
1524 | doc->rootFrame()->setFrameFormat(fmt); | - |
1525 | doc->setTextWidth(documentRect().width()); | - |
1526 | } | - |
1527 | textLayoutDirty = false; | - |
1528 | } | - |
1529 | | - |
1530 | void QLabelPrivate::ensureTextControl() const | - |
1531 | { | - |
1532 | Q_Q(const QLabel); | - |
1533 | if (!isTextLabel) | - |
1534 | return; | - |
1535 | if (!control) { | - |
1536 | control = new QWidgetTextControl(const_cast<QLabel *>(q)); | - |
1537 | control->document()->setUndoRedoEnabled(false); | - |
1538 | control->document()->setDefaultFont(q->font()); | - |
1539 | control->setTextInteractionFlags(textInteractionFlags); | - |
1540 | control->setOpenExternalLinks(openExternalLinks); | - |
1541 | control->setPalette(q->palette()); | - |
1542 | control->setFocus(q->hasFocus()); | - |
1543 | QObject::connect(control, SIGNAL(updateRequest(QRectF)), | - |
1544 | q, SLOT(update())); | - |
1545 | QObject::connect(control, SIGNAL(linkHovered(QString)), | - |
1546 | q, SLOT(_q_linkHovered(QString))); | - |
1547 | QObject::connect(control, SIGNAL(linkActivated(QString)), | - |
1548 | q, SIGNAL(linkActivated(QString))); | - |
1549 | textLayoutDirty = true; | - |
1550 | textDirty = true; | - |
1551 | } | - |
1552 | } | - |
1553 | | - |
1554 | void QLabelPrivate::sendControlEvent(QEvent *e) | - |
1555 | { | - |
1556 | Q_Q(QLabel); | - |
1557 | if (!isTextLabel || !control || textInteractionFlags == Qt::NoTextInteraction) { | - |
1558 | e->ignore(); | - |
1559 | return; | - |
1560 | } | - |
1561 | control->processEvent(e, -layoutRect().topLeft(), q); | - |
1562 | } | - |
1563 | | - |
1564 | void QLabelPrivate::_q_linkHovered(const QString &anchor) | - |
1565 | { | - |
1566 | Q_Q(QLabel); | - |
1567 | #ifndef QT_NO_CURSOR | - |
1568 | if (anchor.isEmpty()) { | - |
1569 | if (validCursor) | - |
1570 | q->setCursor(cursor); | - |
1571 | else | - |
1572 | q->unsetCursor(); | - |
1573 | onAnchor = false; | - |
1574 | } else if (!onAnchor) { | - |
1575 | validCursor = q->testAttribute(Qt::WA_SetCursor); | - |
1576 | if (validCursor) { | - |
1577 | cursor = q->cursor(); | - |
1578 | } | - |
1579 | q->setCursor(Qt::PointingHandCursor); | - |
1580 | onAnchor = true; | - |
1581 | } | - |
1582 | #endif | - |
1583 | emit q->linkHovered(anchor); | - |
1584 | } | - |
1585 | | - |
1586 | | - |
1587 | | - |
1588 | | - |
1589 | QRectF QLabelPrivate::layoutRect() const | - |
1590 | { | - |
1591 | QRectF cr = documentRect(); | - |
1592 | if (!control) | - |
1593 | return cr; | - |
1594 | ensureTextLayouted(); | - |
1595 | | - |
1596 | qreal rh = control->document()->documentLayout()->documentSize().height(); | - |
1597 | qreal yo = 0; | - |
1598 | if (align & Qt::AlignVCenter) | - |
1599 | yo = qMax((cr.height()-rh)/2, qreal(0)); | - |
1600 | else if (align & Qt::AlignBottom) | - |
1601 | yo = qMax(cr.height()-rh, qreal(0)); | - |
1602 | return QRectF(cr.x(), yo + cr.y(), cr.width(), cr.height()); | - |
1603 | } | - |
1604 | | - |
1605 | | - |
1606 | QPoint QLabelPrivate::layoutPoint(const QPoint& p) const | - |
1607 | { | - |
1608 | QRect lr = layoutRect().toRect(); | - |
1609 | return p - lr.topLeft(); | - |
1610 | } | - |
1611 | | - |
1612 | #ifndef QT_NO_CONTEXTMENU | - |
1613 | QMenu *QLabelPrivate::createStandardContextMenu(const QPoint &pos) | - |
1614 | { | - |
1615 | QString linkToCopy; | - |
1616 | QPoint p; | - |
1617 | if (control && isRichText) { | - |
1618 | p = layoutPoint(pos); | - |
1619 | linkToCopy = control->document()->documentLayout()->anchorAt(p); | - |
1620 | } | - |
1621 | | - |
1622 | if (linkToCopy.isEmpty() && !control) | - |
1623 | return 0; | - |
1624 | | - |
1625 | return control->createStandardContextMenu(p, q_func()); | - |
1626 | } | - |
1627 | #endif | - |
1628 | | - |
1629 | | - |
1630 | | - |
1631 | | - |
1632 | | - |
1633 | | - |
1634 | | - |
1635 | | - |
1636 | | - |
1637 | | - |
1638 | | - |
1639 | | - |
1640 | | - |
1641 | | - |
1642 | | - |
1643 | | - |
1644 | | - |
1645 | | - |
1646 | | - |
1647 | | - |
1648 | | - |
1649 | | - |
1650 | QT_END_NAMESPACE | - |
1651 | | - |
1652 | #include "moc_qlabel.cpp" | - |
| | |