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()); | ||||
86 | } | 87 | } | ||
87 | 88 | | |||
88 | void KSqueezedTextLabel::setText(const QString &text) | 89 | void KSqueezedTextLabel::setText(const QString &text) | ||
89 | { | 90 | { | ||
90 | d->fullText = text; | 91 | d->fullText = text; | ||
91 | squeezeTextToLabel(); | 92 | squeezeTextToLabel(); | ||
92 | } | 93 | } | ||
93 | 94 | | |||
94 | void KSqueezedTextLabel::clear() | 95 | void KSqueezedTextLabel::clear() | ||
95 | { | 96 | { | ||
96 | d->fullText.clear(); | 97 | d->fullText.clear(); | ||
97 | QLabel::clear(); | 98 | QLabel::clear(); | ||
98 | } | 99 | } | ||
99 | 100 | | |||
100 | void KSqueezedTextLabel::squeezeTextToLabel() | 101 | void KSqueezedTextLabel::squeezeTextToLabel() | ||
101 | { | 102 | { | ||
102 | QFontMetrics fm(fontMetrics()); | 103 | QFontMetrics fm(fontMetrics()); | ||
103 | int labelWidth = size().width(); | 104 | const int labelWidth = contentsRect().width(); | ||
104 | QStringList squeezedLines; | 105 | QStringList squeezedLines; | ||
105 | bool squeezed = false; | 106 | bool squeezed = false; | ||
106 | Q_FOREACH (const QString &line, d->fullText.split(QLatin1Char('\n'))) { | 107 | Q_FOREACH (const QString &line, d->fullText.split(QLatin1Char('\n'))) { | ||
107 | int lineWidth = fm.width(line); | 108 | int lineWidth = fm.width(line); | ||
108 | if (lineWidth > labelWidth) { | 109 | if (lineWidth > labelWidth) { | ||
109 | squeezed = true; | 110 | squeezed = true; | ||
110 | squeezedLines << fm.elidedText(line, d->elideMode, labelWidth); | 111 | squeezedLines << fm.elidedText(line, d->elideMode, labelWidth); | ||
111 | } else { | 112 | } else { | ||
112 | squeezedLines << line; | 113 | squeezedLines << line; | ||
113 | } | 114 | } | ||
114 | } | 115 | } | ||
115 | 116 | | |||
116 | if (squeezed) { | 117 | if (squeezed) { | ||
117 | QLabel::setText(squeezedLines.join(QStringLiteral("\n"))); | 118 | QLabel::setText(squeezedLines.join(QStringLiteral("\n"))); | ||
118 | setToolTip(d->fullText); | 119 | setToolTip(d->fullText); | ||
119 | } else { | 120 | } else { | ||
120 | QLabel::setText(d->fullText); | 121 | QLabel::setText(d->fullText); | ||
121 | setToolTip(QString()); | 122 | setToolTip(QString()); | ||
122 | } | 123 | } | ||
123 | } | 124 | } | ||
124 | 125 | | |||
126 | QRect KSqueezedTextLabel::contentsRect() const | ||||
127 | { | ||||
128 | // calculation according to API docs for QLabel::indent | ||||
dhaumann: Hm, this possibly is a problem: This file is licensed LGPLv2 only.
Qt is licensed under LGPLv3… | |||||
129 | const int margin = this->margin(); | ||||
130 | int indent = this->indent(); | ||||
131 | if (indent < 0) { | ||||
132 | if (frameWidth() == 0) { | ||||
133 | indent = 0; | ||||
134 | } else { | ||||
135 | indent = fontMetrics().width(QLatin1Char('x')) / 2 - margin; | ||||
136 | } | ||||
137 | } | ||||
138 | | ||||
139 | QRect result = QLabel::contentsRect(); | ||||
140 | if (indent > 0) { | ||||
141 | const int alignment = this->alignment(); | ||||
142 | if (alignment & Qt::AlignLeft) { | ||||
143 | result.setLeft(result.left() + indent); | ||||
144 | } | ||||
145 | if (alignment & Qt::AlignTop) { | ||||
146 | result.setTop(result.top() + indent); | ||||
147 | } | ||||
148 | if (alignment & Qt::AlignRight) { | ||||
149 | result.setRight(result.right() - indent); | ||||
150 | } | ||||
151 | if (alignment & Qt::AlignBottom) { | ||||
152 | result.setBottom(result.bottom() - indent); | ||||
153 | } | ||||
154 | } | ||||
155 | | ||||
156 | result.adjust(margin, margin, -margin, -margin); | ||||
157 | return result; | ||||
158 | } | ||||
159 | | ||||
125 | void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) | 160 | void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) | ||
126 | { | 161 | { | ||
127 | // save fullText and restore it | 162 | // save fullText and restore it | ||
128 | QString tmpFull(d->fullText); | 163 | QString tmpFull(d->fullText); | ||
129 | QLabel::setAlignment(alignment); | 164 | QLabel::setAlignment(alignment); | ||
130 | d->fullText = tmpFull; | 165 | d->fullText = tmpFull; | ||
131 | } | 166 | } | ||
132 | 167 | | |||
▲ 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...