1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QLABEL_H
41 #define QLABEL_H
42 
43 #include <QtWidgets/qtwidgetsglobal.h>
44 #include <QtWidgets/qframe.h>
45 
46 QT_REQUIRE_CONFIG(label);
47 
48 QT_BEGIN_NAMESPACE
49 
50 
51 class QLabelPrivate;
52 
53 class Q_WIDGETS_EXPORT QLabel : public QFrame
54 {
55     Q_OBJECT
56     Q_PROPERTY(QString text READ text WRITE setText)
57     Q_PROPERTY(Qt::TextFormat textFormat READ textFormat WRITE setTextFormat)
58     Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
59     Q_PROPERTY(bool scaledContents READ hasScaledContents WRITE setScaledContents)
60     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
61     Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap)
62     Q_PROPERTY(int margin READ margin WRITE setMargin)
63     Q_PROPERTY(int indent READ indent WRITE setIndent)
64     Q_PROPERTY(bool openExternalLinks READ openExternalLinks WRITE setOpenExternalLinks)
65     Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
66     Q_PROPERTY(bool hasSelectedText READ hasSelectedText)
67     Q_PROPERTY(QString selectedText READ selectedText)
68 
69 public:
70     explicit QLabel(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
71     explicit QLabel(const QString &text, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
72     ~QLabel();
73 
74     QString text() const;
75 
76 #if QT_DEPRECATED_SINCE(5,15)
77     QT_DEPRECATED_VERSION_X(5, 15, "Use the other overload which returns QPixmap by-value")
78     const QPixmap *pixmap() const; // ### Qt 7: Remove function
79 
80     QPixmap pixmap(Qt::ReturnByValueConstant) const;
81 #else
82     QPixmap pixmap(Qt::ReturnByValueConstant = Qt::ReturnByValue) const; // ### Qt 7: Remove arg
83 #endif // QT_DEPRECATED_SINCE(5,15)
84 
85 #ifndef QT_NO_PICTURE
86 #  if QT_DEPRECATED_SINCE(5,15)
87     QT_DEPRECATED_VERSION_X(5, 15, "Use the other overload which returns QPicture by-value")
88     const QPicture *picture() const; // ### Qt 7: Remove function
89 
90     QPicture picture(Qt::ReturnByValueConstant) const;
91 #  else
92     QPicture picture(Qt::ReturnByValueConstant = Qt::ReturnByValue) const; // ### Qt 7: Remove arg
93 #  endif // QT_DEPRECATED_SINCE(5,15)
94 #endif
95 #if QT_CONFIG(movie)
96     QMovie *movie() const;
97 #endif
98 
99     Qt::TextFormat textFormat() const;
100     void setTextFormat(Qt::TextFormat);
101 
102     Qt::Alignment alignment() const;
103     void setAlignment(Qt::Alignment);
104 
105     void setWordWrap(bool on);
106     bool wordWrap() const;
107 
108     int indent() const;
109     void setIndent(int);
110 
111     int margin() const;
112     void setMargin(int);
113 
114     bool hasScaledContents() const;
115     void setScaledContents(bool);
116     QSize sizeHint() const override;
117     QSize minimumSizeHint() const override;
118 #ifndef QT_NO_SHORTCUT
119     void setBuddy(QWidget *);
120     QWidget *buddy() const;
121 #endif
122     int heightForWidth(int) const override;
123 
124     bool openExternalLinks() const;
125     void setOpenExternalLinks(bool open);
126 
127     void setTextInteractionFlags(Qt::TextInteractionFlags flags);
128     Qt::TextInteractionFlags textInteractionFlags() const;
129 
130     void setSelection(int, int);
131     bool hasSelectedText() const;
132     QString selectedText() const;
133     int selectionStart() const;
134 
135 public Q_SLOTS:
136     void setText(const QString &);
137     void setPixmap(const QPixmap &);
138 #ifndef QT_NO_PICTURE
139     void setPicture(const QPicture &);
140 #endif
141 #if QT_CONFIG(movie)
142     void setMovie(QMovie *movie);
143 #endif
144     void setNum(int);
145     void setNum(double);
146     void clear();
147 
148 Q_SIGNALS:
149     void linkActivated(const QString& link);
150     void linkHovered(const QString& link);
151 
152 protected:
153     bool event(QEvent *e) override;
154     void keyPressEvent(QKeyEvent *ev) override;
155     void paintEvent(QPaintEvent *) override;
156     void changeEvent(QEvent *) override;
157     void mousePressEvent(QMouseEvent *ev) override;
158     void mouseMoveEvent(QMouseEvent *ev) override;
159     void mouseReleaseEvent(QMouseEvent *ev) override;
160 #ifndef QT_NO_CONTEXTMENU
161     void contextMenuEvent(QContextMenuEvent *ev) override;
162 #endif // QT_NO_CONTEXTMENU
163     void focusInEvent(QFocusEvent *ev) override;
164     void focusOutEvent(QFocusEvent *ev) override;
165     bool focusNextPrevChild(bool next) override;
166 
167 
168 private:
169     Q_DISABLE_COPY(QLabel)
170     Q_DECLARE_PRIVATE(QLabel)
171 #if QT_CONFIG(movie)
172     Q_PRIVATE_SLOT(d_func(), void _q_movieUpdated(const QRect&))
173     Q_PRIVATE_SLOT(d_func(), void _q_movieResized(const QSize&))
174 #endif
175     Q_PRIVATE_SLOT(d_func(), void _q_linkHovered(const QString &))
176 
177 #ifndef QT_NO_SHORTCUT
178     Q_PRIVATE_SLOT(d_func(), void _q_buddyDeleted())
179 #endif
180     friend class QTipLabel;
181     friend class QMessageBoxPrivate;
182     friend class QBalloonTip;
183 };
184 
185 QT_END_NAMESPACE
186 
187 #endif // QLABEL_H
188