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 | // copied and adapted from QLabelPrivate::documentRect() | ||||
dhaumann: Hm, this possibly is a problem: This file is licensed LGPLv2 only.
Qt is licensed under LGPLv3… | |||||
129 | | ||||
130 | QRect cr = QLabel::contentsRect(); | ||||
131 | int margin = this->margin(); | ||||
132 | cr.adjust(margin, margin, -margin, -margin); | ||||
133 | const int align = alignment(); | ||||
134 | int m = indent(); | ||||
135 | if (m < 0 && frameWidth()) // no indent, but we do have a frame | ||||
136 | m = fontMetrics().width(QLatin1Char('x')) / 2 - margin; | ||||
137 | if (m > 0) { | ||||
138 | if (align & Qt::AlignLeft) | ||||
139 | cr.setLeft(cr.left() + m); | ||||
140 | if (align & Qt::AlignRight) | ||||
141 | cr.setRight(cr.right() - m); | ||||
142 | if (align & Qt::AlignTop) | ||||
143 | cr.setTop(cr.top() + m); | ||||
144 | if (align & Qt::AlignBottom) | ||||
145 | cr.setBottom(cr.bottom() - m); | ||||
146 | } | ||||
147 | return cr; | ||||
148 | } | ||||
149 | | ||||
125 | void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) | 150 | void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) | ||
126 | { | 151 | { | ||
127 | // save fullText and restore it | 152 | // save fullText and restore it | ||
128 | QString tmpFull(d->fullText); | 153 | QString tmpFull(d->fullText); | ||
129 | QLabel::setAlignment(alignment); | 154 | QLabel::setAlignment(alignment); | ||
130 | d->fullText = tmpFull; | 155 | d->fullText = tmpFull; | ||
131 | } | 156 | } | ||
132 | 157 | | |||
▲ 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...