/
qlabel.cpp
1661 lines (1406 loc) · 45 KB
/
qlabel.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qpainter.h"
#include "qevent.h"
#include "qdrawutil.h"
#include "qapplication.h"
#if QT_CONFIG(abstractbutton)
#include "qabstractbutton.h"
#endif
#include "qstyle.h"
#include "qstyleoption.h"
#include <limits.h>
#include "qaction.h"
#include "qclipboard.h"
#include <qdebug.h>
#include <qurl.h>
#include "qlabel_p.h"
#include "private/qstylesheetstyle_p.h"
#include <qmath.h>
#ifndef QT_NO_ACCESSIBILITY
#include <qaccessible.h>
#endif
QT_BEGIN_NAMESPACE
QLabelPrivate::QLabelPrivate()
: QFramePrivate(),
sh(),
msh(),
text(),
pixmap(nullptr),
scaledpixmap(nullptr),
cachedimage(nullptr),
#ifndef QT_NO_PICTURE
picture(nullptr),
#endif
#if QT_CONFIG(movie)
movie(),
#endif
control(nullptr),
shortcutCursor(),
#ifndef QT_NO_CURSOR
cursor(),
#endif
#ifndef QT_NO_SHORTCUT
buddy(),
shortcutId(0),
#endif
textformat(Qt::AutoText),
textInteractionFlags(Qt::LinksAccessibleByMouse),
sizePolicy(),
margin(0),
align(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextExpandTabs),
indent(-1),
valid_hints(false),
scaledcontents(false),
textLayoutDirty(false),
textDirty(false),
isRichText(false),
isTextLabel(false),
hasShortcut(/*???*/),
#ifndef QT_NO_CURSOR
validCursor(false),
onAnchor(false),
#endif
openExternalLinks(false)
{
}
QLabelPrivate::~QLabelPrivate()
{
}
/*!
\class QLabel
\brief The QLabel widget provides a text or image display.
\ingroup basicwidgets
\inmodule QtWidgets
\image windows-label.png
QLabel is used for displaying text or an image. No user
interaction functionality is provided. The visual appearance of
the label can be configured in various ways, and it can be used
for specifying a focus mnemonic key for another widget.
A QLabel can contain any of the following content types:
\table
\header \li Content \li Setting
\row \li Plain text
\li Pass a QString to setText().
\row \li Rich text
\li Pass a QString that contains rich text to setText().
\row \li A pixmap
\li Pass a QPixmap to setPixmap().
\row \li A movie
\li Pass a QMovie to setMovie().
\row \li A number
\li Pass an \e int or a \e double to setNum(), which converts
the number to plain text.
\row \li Nothing
\li The same as an empty plain text. This is the default. Set
by clear().
\endtable
\warning When passing a QString to the constructor or calling setText(),
make sure to sanitize your input, as QLabel tries to guess whether it
displays the text as plain text or as rich text, a subset of HTML 4
markup. You may want to call
setTextFormat() explicitly, e.g. in case you expect the text to be in
plain format but cannot control the text source (for instance when
displaying data loaded from the Web).
When the content is changed using any of these functions, any
previous content is cleared.
By default, labels display \l{alignment}{left-aligned, vertically-centered}
text and images, where any tabs in the text to be displayed are
\l{Qt::TextExpandTabs}{automatically expanded}. However, the look
of a QLabel can be adjusted and fine-tuned in several ways.
The positioning of the content within the QLabel widget area can
be tuned with setAlignment() and setIndent(). Text content can
also wrap lines along word boundaries with setWordWrap(). For
example, this code sets up a sunken panel with a two-line text in
the bottom right corner (both lines being flush with the right
side of the label):
\snippet code/src_gui_widgets_qlabel.cpp 0
The properties and functions QLabel inherits from QFrame can also
be used to specify the widget frame to be used for any given label.
A QLabel is often used as a label for an interactive widget. For
this use QLabel provides a useful mechanism for adding an
mnemonic (see QKeySequence) that will set the keyboard focus to
the other widget (called the QLabel's "buddy"). For example:
\snippet code/src_gui_widgets_qlabel.cpp 1
In this example, keyboard focus is transferred to the label's
buddy (the QLineEdit) when the user presses Alt+P. If the buddy
was a button (inheriting from QAbstractButton), triggering the
mnemonic would emulate a button click.
\sa QLineEdit, QTextEdit, QPixmap, QMovie,
{fowler}{GUI Design Handbook: Label}
*/
#ifndef QT_NO_PICTURE
/*!
Returns the label's picture or nullptr if the label doesn't have a
picture.
*/
const QPicture *QLabel::picture() const
{
Q_D(const QLabel);
return d->picture;
}
#endif
/*!
Constructs an empty label.
The \a parent and widget flag \a f, arguments are passed
to the QFrame constructor.
\sa setAlignment(), setFrameStyle(), setIndent()
*/
QLabel::QLabel(QWidget *parent, Qt::WindowFlags f)
: QFrame(*new QLabelPrivate(), parent, f)
{
Q_D(QLabel);
d->init();
}
/*!
Constructs a label that displays the text, \a text.
The \a parent and widget flag \a f, arguments are passed
to the QFrame constructor.
\sa setText(), setAlignment(), setFrameStyle(), setIndent()
*/
QLabel::QLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
setText(text);
}
/*!
Destroys the label.
*/
QLabel::~QLabel()
{
Q_D(QLabel);
d->clearContents();
}
void QLabelPrivate::init()
{
Q_Q(QLabel);
q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred,
QSizePolicy::Label));
setLayoutItemMargins(QStyle::SE_LabelLayoutItem);
}
/*!
\property QLabel::text
\brief the label's text
If no text has been set this will return an empty string. Setting
the text clears any previous content.
The text will be interpreted either as plain text or as rich
text, depending on the text format setting; see setTextFormat().
The default setting is Qt::AutoText; i.e. QLabel will try to
auto-detect the format of the text set.
See \l {Supported HTML Subset} for the definition of rich text.
If a buddy has been set, the buddy mnemonic key is updated
from the new text.
Note that QLabel is well-suited to display small rich text
documents, such as small documents that get their document
specific settings (font, text color, link color) from the label's
palette and font properties. For large documents, use QTextEdit
in read-only mode instead. QTextEdit can also provide a scroll bar
when necessary.
\note This function enables mouse tracking if \a text contains rich
text.
\sa setTextFormat(), setBuddy(), alignment
*/
void QLabel::setText(const QString &text)
{
Q_D(QLabel);
if (d->text == text)
return;
QWidgetTextControl *oldControl = d->control;
d->control = nullptr;
d->clearContents();
d->text = text;
d->isTextLabel = true;
d->textDirty = true;
d->isRichText = d->textformat == Qt::RichText
|| (d->textformat == Qt::AutoText && Qt::mightBeRichText(d->text));
d->control = oldControl;
if (d->needTextControl()) {
d->ensureTextControl();
} else {
delete d->control;
d->control = nullptr;
}
if (d->isRichText) {
setMouseTracking(true);
} else {
// Note: mouse tracking not disabled intentionally
}
#ifndef QT_NO_SHORTCUT
if (d->buddy)
d->updateShortcut();
#endif
d->updateLabel();
#ifndef QT_NO_ACCESSIBILITY
if (accessibleName().isEmpty()) {
QAccessibleEvent event(this, QAccessible::NameChanged);
QAccessible::updateAccessibility(&event);
}
#endif
}
QString QLabel::text() const
{
Q_D(const QLabel);
return d->text;
}
/*!
Clears any label contents.
*/
void QLabel::clear()
{
Q_D(QLabel);
d->clearContents();
d->updateLabel();
}
/*!
\property QLabel::pixmap
\brief the label's pixmap
If no pixmap has been set this will return nullptr.
Setting the pixmap clears any previous content. The buddy
shortcut, if any, is disabled.
*/
void QLabel::setPixmap(const QPixmap &pixmap)
{
Q_D(QLabel);
if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
d->clearContents();
d->pixmap = new QPixmap(pixmap);
}
if (d->pixmap->depth() == 1 && !d->pixmap->mask())
d->pixmap->setMask(*((QBitmap *)d->pixmap));
d->updateLabel();
}
const QPixmap *QLabel::pixmap() const
{
Q_D(const QLabel);
return d->pixmap;
}
#ifndef QT_NO_PICTURE
/*!
Sets the label contents to \a picture. Any previous content is
cleared.
The buddy shortcut, if any, is disabled.
\sa picture(), setBuddy()
*/
void QLabel::setPicture(const QPicture &picture)
{
Q_D(QLabel);
d->clearContents();
d->picture = new QPicture(picture);
d->updateLabel();
}
#endif // QT_NO_PICTURE
/*!
Sets the label contents to plain text containing the textual
representation of integer \a num. Any previous content is cleared.
Does nothing if the integer's string representation is the same as
the current contents of the label.
The buddy shortcut, if any, is disabled.
\sa setText(), QString::setNum(), setBuddy()
*/
void QLabel::setNum(int num)
{
QString str;
str.setNum(num);
setText(str);
}
/*!
\overload
Sets the label contents to plain text containing the textual
representation of double \a num. Any previous content is cleared.
Does nothing if the double's string representation is the same as
the current contents of the label.
The buddy shortcut, if any, is disabled.
\sa setText(), QString::setNum(), setBuddy()
*/
void QLabel::setNum(double num)
{
QString str;
str.setNum(num);
setText(str);
}
/*!
\property QLabel::alignment
\brief the alignment of the label's contents
By default, the contents of the label are left-aligned and vertically-centered.
\sa text
*/
void QLabel::setAlignment(Qt::Alignment alignment)
{
Q_D(QLabel);
if (alignment == (d->align & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask)))
return;
d->align = (d->align & ~(Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask))
| (alignment & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask));
d->updateLabel();
}
Qt::Alignment QLabel::alignment() const
{
Q_D(const QLabel);
return QFlag(d->align & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask));
}
/*!
\property QLabel::wordWrap
\brief the label's word-wrapping policy
If this property is \c true then label text is wrapped where
necessary at word-breaks; otherwise it is not wrapped at all.
By default, word wrap is disabled.
\sa text
*/
void QLabel::setWordWrap(bool on)
{
Q_D(QLabel);
if (on)
d->align |= Qt::TextWordWrap;
else
d->align &= ~Qt::TextWordWrap;
d->updateLabel();
}
bool QLabel::wordWrap() const
{
Q_D(const QLabel);
return d->align & Qt::TextWordWrap;
}
/*!
\property QLabel::indent
\brief the label's text indent in pixels
If a label displays text, the indent applies to the left edge if
alignment() is Qt::AlignLeft, to the right edge if alignment() is
Qt::AlignRight, to the top edge if alignment() is Qt::AlignTop, and
to the bottom edge if alignment() is Qt::AlignBottom.
If indent is negative, or if no indent has been set, the label
computes the effective indent as follows: If frameWidth() is 0,
the effective indent becomes 0. If frameWidth() is greater than 0,
the effective indent becomes half the width of the "x" character
of the widget's current font().
By default, the indent is -1, meaning that an effective indent is
calculating in the manner described above.
\sa alignment, margin, frameWidth(), font()
*/
void QLabel::setIndent(int indent)
{
Q_D(QLabel);
d->indent = indent;
d->updateLabel();
}
int QLabel::indent() const
{
Q_D(const QLabel);
return d->indent;
}
/*!
\property QLabel::margin
\brief the width of the margin
The margin is the distance between the innermost pixel of the
frame and the outermost pixel of contents.
The default margin is 0.
\sa indent
*/
int QLabel::margin() const
{
Q_D(const QLabel);
return d->margin;
}
void QLabel::setMargin(int margin)
{
Q_D(QLabel);
if (d->margin == margin)
return;
d->margin = margin;
d->updateLabel();
}
/*!
Returns the size that will be used if the width of the label is \a
w. If \a w is -1, the sizeHint() is returned. If \a w is 0 minimumSizeHint() is returned
*/
QSize QLabelPrivate::sizeForWidth(int w) const
{
Q_Q(const QLabel);
if(q->minimumWidth() > 0)
w = qMax(w, q->minimumWidth());
QSize contentsMargin(leftmargin + rightmargin, topmargin + bottommargin);
QRect br;
int hextra = 2 * margin;
int vextra = hextra;
QFontMetrics fm = q->fontMetrics();
if (pixmap && !pixmap->isNull()) {
br = pixmap->rect();
br.setSize(br.size() / pixmap->devicePixelRatio());
#ifndef QT_NO_PICTURE
} else if (picture && !picture->isNull()) {
br = picture->boundingRect();
#endif
#if QT_CONFIG(movie)
} else if (movie && !movie->currentPixmap().isNull()) {
br = movie->currentPixmap().rect();
br.setSize(br.size() / movie->currentPixmap().devicePixelRatio());
#endif
} else if (isTextLabel) {
int align = QStyle::visualAlignment(textDirection(), QFlag(this->align));
// Add indentation
int m = indent;
if (m < 0 && q->frameWidth()) // no indent, but we do have a frame
m = fm.horizontalAdvance(QLatin1Char('x')) - margin*2;
if (m > 0) {
if ((align & Qt::AlignLeft) || (align & Qt::AlignRight))
hextra += m;
if ((align & Qt::AlignTop) || (align & Qt::AlignBottom))
vextra += m;
}
if (control) {
ensureTextLayouted();
const qreal oldTextWidth = control->textWidth();
// Calculate the length of document if w is the width
if (align & Qt::TextWordWrap) {
if (w >= 0) {
w = qMax(w-hextra-contentsMargin.width(), 0); // strip margin and indent
control->setTextWidth(w);
} else {
control->adjustSize();
}
} else {
control->setTextWidth(-1);
}
QSizeF controlSize = control->size();
br = QRect(QPoint(0, 0), QSize(qCeil(controlSize.width()), qCeil(controlSize.height())));
// restore state
control->setTextWidth(oldTextWidth);
} else {
// Turn off center alignment in order to avoid rounding errors for centering,
// since centering involves a division by 2. At the end, all we want is the size.
int flags = align & ~(Qt::AlignVCenter | Qt::AlignHCenter);
if (hasShortcut) {
flags |= Qt::TextShowMnemonic;
QStyleOption opt;
opt.initFrom(q);
if (!q->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, q))
flags |= Qt::TextHideMnemonic;
}
bool tryWidth = (w < 0) && (align & Qt::TextWordWrap);
if (tryWidth)
w = qMin(fm.averageCharWidth() * 80, q->maximumSize().width());
else if (w < 0)
w = 2000;
w -= (hextra + contentsMargin.width());
br = fm.boundingRect(0, 0, w ,2000, flags, text);
if (tryWidth && br.height() < 4*fm.lineSpacing() && br.width() > w/2)
br = fm.boundingRect(0, 0, w/2, 2000, flags, text);
if (tryWidth && br.height() < 2*fm.lineSpacing() && br.width() > w/4)
br = fm.boundingRect(0, 0, w/4, 2000, flags, text);
}
} else {
br = QRect(QPoint(0, 0), QSize(fm.averageCharWidth(), fm.lineSpacing()));
}
const QSize contentsSize(br.width() + hextra, br.height() + vextra);
return (contentsSize + contentsMargin).expandedTo(q->minimumSize());
}
/*!
\reimp
*/
int QLabel::heightForWidth(int w) const
{
Q_D(const QLabel);
if (d->isTextLabel)
return d->sizeForWidth(w).height();
return QWidget::heightForWidth(w);
}
/*!
\property QLabel::openExternalLinks
\since 4.2
Specifies whether QLabel should automatically open links using
QDesktopServices::openUrl() instead of emitting the
linkActivated() signal.
\b{Note:} The textInteractionFlags set on the label need to include
either LinksAccessibleByMouse or LinksAccessibleByKeyboard.
The default value is false.
\sa textInteractionFlags()
*/
bool QLabel::openExternalLinks() const
{
Q_D(const QLabel);
return d->openExternalLinks;
}
void QLabel::setOpenExternalLinks(bool open)
{
Q_D(QLabel);
d->openExternalLinks = open;
if (d->control)
d->control->setOpenExternalLinks(open);
}
/*!
\property QLabel::textInteractionFlags
\since 4.2
Specifies how the label should interact with user input if it displays text.
If the flags contain Qt::LinksAccessibleByKeyboard the focus policy is also
automatically set to Qt::StrongFocus. If Qt::TextSelectableByKeyboard is set
then the focus policy is set to Qt::ClickFocus.
The default value is Qt::LinksAccessibleByMouse.
*/
void QLabel::setTextInteractionFlags(Qt::TextInteractionFlags flags)
{
Q_D(QLabel);
if (d->textInteractionFlags == flags)
return;
d->textInteractionFlags = flags;
if (flags & Qt::LinksAccessibleByKeyboard)
setFocusPolicy(Qt::StrongFocus);
else if (flags & (Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse))
setFocusPolicy(Qt::ClickFocus);
else
setFocusPolicy(Qt::NoFocus);
if (d->needTextControl()) {
d->ensureTextControl();
} else {
delete d->control;
d->control = nullptr;
}
if (d->control)
d->control->setTextInteractionFlags(d->textInteractionFlags);
}
Qt::TextInteractionFlags QLabel::textInteractionFlags() const
{
Q_D(const QLabel);
return d->textInteractionFlags;
}
/*!
Selects text from position \a start and for \a length characters.
\sa selectedText()
\b{Note:} The textInteractionFlags set on the label need to include
either TextSelectableByMouse or TextSelectableByKeyboard.
\since 4.7
*/
void QLabel::setSelection(int start, int length)
{
Q_D(QLabel);
if (d->control) {
d->ensureTextPopulated();
QTextCursor cursor = d->control->textCursor();
cursor.setPosition(start);
cursor.setPosition(start + length, QTextCursor::KeepAnchor);
d->control->setTextCursor(cursor);
}
}
/*!
\property QLabel::hasSelectedText
\brief whether there is any text selected
hasSelectedText() returns \c true if some or all of the text has been
selected by the user; otherwise returns \c false.
By default, this property is \c false.
\sa selectedText()
\b{Note:} The textInteractionFlags set on the label need to include
either TextSelectableByMouse or TextSelectableByKeyboard.
\since 4.7
*/
bool QLabel::hasSelectedText() const
{
Q_D(const QLabel);
if (d->control)
return d->control->textCursor().hasSelection();
return false;
}
/*!
\property QLabel::selectedText
\brief the selected text
If there is no selected text this property's value is
an empty string.
By default, this property contains an empty string.
\sa hasSelectedText()
\b{Note:} The textInteractionFlags set on the label need to include
either TextSelectableByMouse or TextSelectableByKeyboard.
\since 4.7
*/
QString QLabel::selectedText() const
{
Q_D(const QLabel);
if (d->control)
return d->control->textCursor().selectedText();
return QString();
}
/*!
selectionStart() returns the index of the first selected character in the
label or -1 if no text is selected.
\sa selectedText()
\b{Note:} The textInteractionFlags set on the label need to include
either TextSelectableByMouse or TextSelectableByKeyboard.
\since 4.7
*/
int QLabel::selectionStart() const
{
Q_D(const QLabel);
if (d->control && d->control->textCursor().hasSelection())
return d->control->textCursor().selectionStart();
return -1;
}
/*!\reimp
*/
QSize QLabel::sizeHint() const
{
Q_D(const QLabel);
if (!d->valid_hints)
(void) QLabel::minimumSizeHint();
return d->sh;
}
/*!
\reimp
*/
QSize QLabel::minimumSizeHint() const
{
Q_D(const QLabel);
if (d->valid_hints) {
if (d->sizePolicy == sizePolicy())
return d->msh;
}
ensurePolished();
d->valid_hints = true;
d->sh = d->sizeForWidth(-1); // wrap ? golden ratio : min doc size
QSize msh(-1, -1);
if (!d->isTextLabel) {
msh = d->sh;
} else {
msh.rheight() = d->sizeForWidth(QWIDGETSIZE_MAX).height(); // height for one line
msh.rwidth() = d->sizeForWidth(0).width(); // wrap ? size of biggest word : min doc size
if (d->sh.height() < msh.height())
msh.rheight() = d->sh.height();
}
d->msh = msh;
d->sizePolicy = sizePolicy();
return msh;
}
/*!\reimp
*/
void QLabel::mousePressEvent(QMouseEvent *ev)
{
Q_D(QLabel);
d->sendControlEvent(ev);
}
/*!\reimp
*/
void QLabel::mouseMoveEvent(QMouseEvent *ev)
{
Q_D(QLabel);
d->sendControlEvent(ev);
}
/*!\reimp
*/
void QLabel::mouseReleaseEvent(QMouseEvent *ev)
{
Q_D(QLabel);
d->sendControlEvent(ev);
}
#ifndef QT_NO_CONTEXTMENU
/*!\reimp
*/
void QLabel::contextMenuEvent(QContextMenuEvent *ev)
{
Q_D(QLabel);
if (!d->isTextLabel) {
ev->ignore();
return;
}
QMenu *menu = d->createStandardContextMenu(ev->pos());
if (!menu) {
ev->ignore();
return;
}
ev->accept();
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->popup(ev->globalPos());
}
#endif // QT_NO_CONTEXTMENU
/*!
\reimp
*/
void QLabel::focusInEvent(QFocusEvent *ev)
{
Q_D(QLabel);
if (d->isTextLabel) {
d->ensureTextControl();
d->sendControlEvent(ev);
}
QFrame::focusInEvent(ev);
}
/*!
\reimp
*/
void QLabel::focusOutEvent(QFocusEvent *ev)
{
Q_D(QLabel);
if (d->control) {
d->sendControlEvent(ev);
QTextCursor cursor = d->control->textCursor();
Qt::FocusReason reason = ev->reason();
if (reason != Qt::ActiveWindowFocusReason
&& reason != Qt::PopupFocusReason
&& cursor.hasSelection()) {
cursor.clearSelection();
d->control->setTextCursor(cursor);
}
}
QFrame::focusOutEvent(ev);
}
/*!\reimp
*/
bool QLabel::focusNextPrevChild(bool next)
{
Q_D(QLabel);
if (d->control && d->control->setFocusToNextOrPreviousAnchor(next))
return true;
return QFrame::focusNextPrevChild(next);
}
/*!\reimp
*/
void QLabel::keyPressEvent(QKeyEvent *ev)
{
Q_D(QLabel);
d->sendControlEvent(ev);
}
/*!\reimp
*/
bool QLabel::event(QEvent *e)
{
Q_D(QLabel);
QEvent::Type type = e->type();
#ifndef QT_NO_SHORTCUT
if (type == QEvent::Shortcut) {
QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
if (se->shortcutId() == d->shortcutId) {
QWidget *w = d->buddy;
if (!w)
return QFrame::event(e);
if (w->focusPolicy() != Qt::NoFocus)
w->setFocus(Qt::ShortcutFocusReason);
#if QT_CONFIG(abstractbutton)
QAbstractButton *button = qobject_cast<QAbstractButton *>(w);
if (button && !se->isAmbiguous())
button->animateClick();
else
#endif
window()->setAttribute(Qt::WA_KeyboardFocusChange);
return true;
}
} else
#endif
if (type == QEvent::Resize) {
if (d->control)
d->textLayoutDirty = true;
} else if (e->type() == QEvent::StyleChange
#ifdef Q_OS_MAC
|| e->type() == QEvent::MacSizeChange
#endif
) {
d->setLayoutItemMargins(QStyle::SE_LabelLayoutItem);
d->updateLabel();
}
return QFrame::event(e);
}
/*!\reimp
*/
void QLabel::paintEvent(QPaintEvent *)
{