Changeset View
Changeset View
Standalone View
Standalone View
src/ksqueezedtextlabel.cpp
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Line(s) | |||||
77 | QSize KSqueezedTextLabel::sizeHint() const | 77 | QSize KSqueezedTextLabel::sizeHint() const | ||
78 | { | 78 | { | ||
79 | int maxWidth = QApplication::desktop()->screenGeometry(this).width() * 3 / 4; | 79 | int maxWidth = QApplication::desktop()->screenGeometry(this).width() * 3 / 4; | ||
80 | QFontMetrics fm(fontMetrics()); | 80 | QFontMetrics fm(fontMetrics()); | ||
81 | int textWidth = fm.width(d->fullText); | 81 | int textWidth = fm.width(d->fullText); | ||
82 | if (textWidth > maxWidth) { | 82 | if (textWidth > maxWidth) { | ||
83 | textWidth = maxWidth; | 83 | textWidth = maxWidth; | ||
84 | } | 84 | } | ||
85 | return QSize(textWidth, QLabel::sizeHint().height()); | 85 | const int chromeWidth = width() - contentsRect().width(); | ||
86 | return QSize(textWidth + chromeWidth, QLabel::sizeHint().height()); | ||||
87 | } | ||||
88 | | ||||
89 | void KSqueezedTextLabel::setIndent(int indent) | ||||
90 | { | ||||
91 | QLabel::setIndent(indent); | ||||
92 | squeezeTextToLabel(); | ||||
93 | } | ||||
94 | | ||||
95 | void KSqueezedTextLabel::setMargin(int margin) | ||||
96 | { | ||||
97 | QLabel::setMargin(margin); | ||||
98 | squeezeTextToLabel(); | ||||
99 | } | ||||
100 | | ||||
101 | void KSqueezedTextLabel::setLineWidth(int width) | ||||
102 | { | ||||
103 | QLabel::setLineWidth(width); | ||||
104 | squeezeTextToLabel(); | ||||
86 | } | 105 | } | ||
87 | 106 | | |||
88 | void KSqueezedTextLabel::setText(const QString &text) | 107 | void KSqueezedTextLabel::setText(const QString &text) | ||
89 | { | 108 | { | ||
90 | d->fullText = text; | 109 | d->fullText = text; | ||
91 | squeezeTextToLabel(); | 110 | squeezeTextToLabel(); | ||
92 | } | 111 | } | ||
93 | 112 | | |||
94 | void KSqueezedTextLabel::clear() | 113 | void KSqueezedTextLabel::clear() | ||
95 | { | 114 | { | ||
96 | d->fullText.clear(); | 115 | d->fullText.clear(); | ||
97 | QLabel::clear(); | 116 | QLabel::clear(); | ||
98 | } | 117 | } | ||
99 | 118 | | |||
100 | void KSqueezedTextLabel::squeezeTextToLabel() | 119 | void KSqueezedTextLabel::squeezeTextToLabel() | ||
101 | { | 120 | { | ||
102 | QFontMetrics fm(fontMetrics()); | 121 | QFontMetrics fm(fontMetrics()); | ||
103 | int labelWidth = size().width(); | 122 | const int labelWidth = contentsRect().width(); | ||
104 | QStringList squeezedLines; | 123 | QStringList squeezedLines; | ||
105 | bool squeezed = false; | 124 | bool squeezed = false; | ||
106 | Q_FOREACH (const QString &line, d->fullText.split(QLatin1Char('\n'))) { | 125 | Q_FOREACH (const QString &line, d->fullText.split(QLatin1Char('\n'))) { | ||
107 | int lineWidth = fm.width(line); | 126 | int lineWidth = fm.width(line); | ||
108 | if (lineWidth > labelWidth) { | 127 | if (lineWidth > labelWidth) { | ||
109 | squeezed = true; | 128 | squeezed = true; | ||
110 | squeezedLines << fm.elidedText(line, d->elideMode, labelWidth); | 129 | squeezedLines << fm.elidedText(line, d->elideMode, labelWidth); | ||
111 | } else { | 130 | } else { | ||
112 | squeezedLines << line; | 131 | squeezedLines << line; | ||
113 | } | 132 | } | ||
114 | } | 133 | } | ||
115 | 134 | | |||
116 | if (squeezed) { | 135 | if (squeezed) { | ||
117 | QLabel::setText(squeezedLines.join(QStringLiteral("\n"))); | 136 | QLabel::setText(squeezedLines.join(QStringLiteral("\n"))); | ||
118 | setToolTip(d->fullText); | 137 | setToolTip(d->fullText); | ||
119 | } else { | 138 | } else { | ||
120 | QLabel::setText(d->fullText); | 139 | QLabel::setText(d->fullText); | ||
121 | setToolTip(QString()); | 140 | setToolTip(QString()); | ||
122 | } | 141 | } | ||
123 | } | 142 | } | ||
124 | 143 | | |||
144 | QRect KSqueezedTextLabel::contentsRect() const | ||||
145 | { | ||||
146 | // copied and adapted from QLabelPrivate::documentRect() | ||||
dhaumann: Hm, this possibly is a problem: This file is licensed LGPLv2 only.
Qt is licensed under LGPLv3… | |||||
147 | | ||||
148 | QRect cr = QLabel::contentsRect(); | ||||
149 | int margin = this->margin(); | ||||
150 | cr.adjust(margin, margin, -margin, -margin); | ||||
151 | const int align = alignment(); | ||||
152 | int m = indent(); | ||||
153 | if (m < 0 && frameWidth()) // no indent, but we do have a frame | ||||
154 | m = fontMetrics().width(QLatin1Char('x')) / 2 - margin; | ||||
155 | if (m > 0) { | ||||
156 | if (align & Qt::AlignLeft) | ||||
157 | cr.setLeft(cr.left() + m); | ||||
158 | if (align & Qt::AlignRight) | ||||
159 | cr.setRight(cr.right() - m); | ||||
160 | if (align & Qt::AlignTop) | ||||
161 | cr.setTop(cr.top() + m); | ||||
162 | if (align & Qt::AlignBottom) | ||||
163 | cr.setBottom(cr.bottom() - m); | ||||
164 | } | ||||
165 | return cr; | ||||
166 | } | ||||
167 | | ||||
125 | void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) | 168 | void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) | ||
126 | { | 169 | { | ||
127 | // save fullText and restore it | 170 | // save fullText and restore it | ||
128 | QString tmpFull(d->fullText); | 171 | QString tmpFull(d->fullText); | ||
129 | QLabel::setAlignment(alignment); | 172 | QLabel::setAlignment(alignment); | ||
130 | d->fullText = tmpFull; | 173 | d->fullText = tmpFull; | ||
131 | } | 174 | } | ||
132 | 175 | | |||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |
Hm, this possibly is a problem: This file is licensed LGPLv2 only.
Qt is licensed under LGPLv3, as far as I know, so you cannot copy the code.
In an ideal world, you now would forget everything you have seen and write this code again :-)
In practice, the result often looks more or less the same.
So if you can claim the following code comes from you, then I think we are on the safe side...