Warning, file /frameworks/kwidgetsaddons/src/ksqueezedtextlabel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2000 Ronny Standtke <Ronny.Standtke@gmx.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #ifndef KSQUEEZEDTEXTLABEL_H 0009 #define KSQUEEZEDTEXTLABEL_H 0010 0011 #include <QLabel> 0012 #include <kwidgetsaddons_export.h> 0013 #include <memory> 0014 0015 /** 0016 * @class KSqueezedTextLabel ksqueezedtextlabel.h KSqueezedTextLabel 0017 * 0018 * @short A replacement for QLabel that squeezes its text into the label 0019 * 0020 * If the text is too long to fit into the label it is divided into 0021 * remaining left and right parts which are separated by three dots. 0022 * Hovering the mouse over the label shows the full text in a tooltip. 0023 * 0024 * Example: 0025 * http://www.kde.org/documentation/index.html could be squeezed to 0026 * http://www.kde...ion/index.html 0027 * 0028 * \image html ksqueezedtextlabel.png "KSqueezedTextLabel Widget" 0029 * 0030 * To change the position of the elision mark to the left or right end 0031 * of the text, use setTextElideMode(). 0032 * 0033 * @anchor non-virtual-warning 0034 * @note Several functions of KSqueezedTextLabel (indicated by a warning 0035 * in their description) reimplement non-virtual functions of QLabel. 0036 * Therefore, you may need to cast the object to KSqueezedTextLabel in 0037 * some situations: 0038 * \Example 0039 * \code 0040 * KSqueezedTextLabel* squeezed = new KSqueezedTextLabel("text", parent); 0041 * QLabel* label = squeezed; 0042 * label->setText("new text"); // this will not work 0043 * squeezed->setText("new text"); // works as expected 0044 * static_cast<KSqueezedTextLabel*>(label)->setText("new text"); // works as expected 0045 * \endcode 0046 * 0047 * @author Ronny Standtke <Ronny.Standtke@gmx.de> 0048 */ 0049 // TODO KF6: 0050 // - make more functions virtual (to benefit subclasses of KSqueezedTextLabel) 0051 // - try to eliminate need for non-virtual-warning (to benefit use as QLabel), 0052 // see https://phabricator.kde.org/D7164 for some ideas/considerations 0053 class KWIDGETSADDONS_EXPORT KSqueezedTextLabel : public QLabel 0054 { 0055 Q_OBJECT 0056 Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode) 0057 Q_PROPERTY(int indent READ indent WRITE setIndent) 0058 Q_PROPERTY(int margin READ margin WRITE setMargin) 0059 0060 public: 0061 /** 0062 * Default constructor. 0063 * @param parent the label's parent object 0064 */ 0065 explicit KSqueezedTextLabel(QWidget *parent = nullptr); 0066 0067 /** 0068 * @param text the text that will be displayed 0069 * @param parent the label's parent object 0070 */ 0071 explicit KSqueezedTextLabel(const QString &text, QWidget *parent = nullptr); 0072 0073 ~KSqueezedTextLabel() override; 0074 0075 /** 0076 * @return the label's minimum size, where the horizontal component 0077 * will be -1 to indicate the label's ability to shrink its width 0078 * by squeezing the text 0079 */ 0080 QSize minimumSizeHint() const override; 0081 0082 /** 0083 * @return the label's preferred size, which is wide enough 0084 * to display the text without squeezing it 0085 */ 0086 QSize sizeHint() const override; 0087 0088 /** 0089 * Sets the indentation of the label. 0090 * 0091 * @param indent the amount of indentation in pixels 0092 * 0093 * Reimplementation of QLabel::setIndent(). 0094 * 0095 * @warning The corresponding function in the base class is not virtual. 0096 * Therefore make sure to call this function on objects of type KSqueezedTextLabel, 0097 * as shown in the @ref non-virtual-warning "example in the class description". 0098 * 0099 * @since 5.39 0100 */ 0101 void setIndent(int indent); 0102 0103 /** 0104 * Sets the margin of the label. 0105 * 0106 * @param margin the margin size in pixels 0107 * 0108 * Reimplementation of QLabel::setMargin(). 0109 * 0110 * @warning The corresponding function in the base class is not virtual. 0111 * Therefore make sure to call this function on objects of type KSqueezedTextLabel, 0112 * as shown in the @ref non-virtual-warning "example in the class description". 0113 * 0114 * @since 5.39 0115 */ 0116 void setMargin(int margin); 0117 0118 /** 0119 * Overridden for internal reasons; the API remains unaffected. 0120 */ 0121 virtual void setAlignment(Qt::Alignment); 0122 0123 /** 0124 * @return the text elide mode 0125 */ 0126 Qt::TextElideMode textElideMode() const; 0127 0128 /** 0129 * Sets the text elide mode. 0130 * @param mode The text elide mode. 0131 */ 0132 void setTextElideMode(Qt::TextElideMode mode); 0133 0134 /** 0135 * @return the full text set via setText() 0136 * 0137 * @since 4.4 0138 */ 0139 QString fullText() const; 0140 0141 /** 0142 * @returns true if the text displayed is currently squeezed, 0143 * i.e. the original text does not fit inside the space available 0144 * and elide mode is set to a value other than Qt::ElideNone. 0145 * 0146 * @since 5.38 0147 */ 0148 bool isSqueezed() const; 0149 0150 /** 0151 * @return the rectangle to squeeze the text into 0152 * 0153 * Reimplementation of QLabel::contentsRect(). 0154 * 0155 * @warning The corresponding function in the base class is not virtual. 0156 * Therefore make sure to call this function on objects of type KSqueezedTextLabel, 0157 * as shown in the @ref non-virtual-warning "example in the class description". 0158 * 0159 * @since 5.39 0160 */ 0161 QRect contentsRect() const; 0162 0163 public Q_SLOTS: 0164 /** 0165 * Sets the text. 0166 * @param text The new text. 0167 * 0168 * Reimplementation of QLabel::setText(). 0169 * 0170 * @warning The corresponding function in the base class is not virtual. 0171 * Therefore make sure to call this function on objects of type KSqueezedTextLabel, 0172 * as shown in the @ref non-virtual-warning "example in the class description". 0173 */ 0174 void setText(const QString &text); 0175 0176 /** 0177 * Clears the text. 0178 * 0179 * Reimplementation of QLabel::clear(). 0180 * 0181 * @warning The corresponding function in the base class is not virtual. 0182 * Therefore make sure to call this function on objects of type KSqueezedTextLabel, 0183 * as shown in the @ref non-virtual-warning "example in the class description". 0184 */ 0185 void clear(); 0186 0187 protected: 0188 /** 0189 * \reimp 0190 */ 0191 void mouseReleaseEvent(QMouseEvent *) override; 0192 0193 /** 0194 * Called when widget is resized 0195 */ 0196 void resizeEvent(QResizeEvent *) override; 0197 0198 /** 0199 * \reimp 0200 */ 0201 void contextMenuEvent(QContextMenuEvent *) override; 0202 0203 /** 0204 * does the dirty work 0205 */ 0206 void squeezeTextToLabel(); 0207 0208 private: 0209 std::unique_ptr<class KSqueezedTextLabelPrivate> const d; 0210 }; 0211 0212 #endif // KSQUEEZEDTEXTLABEL_H