diff --git a/autotests/ksqueezedtextlabelautotest.cpp b/autotests/ksqueezedtextlabelautotest.cpp --- a/autotests/ksqueezedtextlabelautotest.cpp +++ b/autotests/ksqueezedtextlabelautotest.cpp @@ -175,7 +175,6 @@ label->adjustSize(); QVERIFY(!label->isSqueezed()); - QEXPECT_FAIL("", "To fix: Respect chrome in sizeHint()", Abort); QCOMPARE(label->size().width(), labelWidth + indent); QCOMPARE(label->minimumSizeHint().width(), -1); QCOMPARE(label->sizeHint().width(), labelWidth + indent); @@ -227,7 +226,6 @@ label->setProperty(attribute.toLatin1().data(), amount); QTest::qWaitForWindowExposed(label.data()); - QEXPECT_FAIL("", "To fix: Respect chrome in squeezeTextToLabel()", Abort); QVERIFY(label->isSqueezed()); label->adjustSize(); diff --git a/src/ksqueezedtextlabel.h b/src/ksqueezedtextlabel.h --- a/src/ksqueezedtextlabel.h +++ b/src/ksqueezedtextlabel.h @@ -59,6 +59,11 @@ * @author Ronny Standtke */ +// TODO KF6: +// - make more functions virtual (to benefit subclasses of KSqueezedTextLabel) +// - try to eliminate need for non-virtual-warning (to benefit use as QLabel), +// see https://phabricator.kde.org/D7164 for some ideas/considerations + /* * QLabel */ @@ -127,6 +132,19 @@ */ bool isSqueezed() const; + /** + * @return the rectangle to squeeze the text into + * + * Reimplementation of QLabel::contentsRect(). + * + * @warning The corresponding function in the base class is not virtual. + * Therefore make sure to call this function on objects of type KSqueezedTextLabel, + * as shown in the @ref non-virtual-warning "example in the class description". + * + * @since 5.39 + */ + QRect contentsRect() const; + public Q_SLOTS: /** * Sets the text. diff --git a/src/ksqueezedtextlabel.cpp b/src/ksqueezedtextlabel.cpp --- a/src/ksqueezedtextlabel.cpp +++ b/src/ksqueezedtextlabel.cpp @@ -82,7 +82,8 @@ if (textWidth > maxWidth) { textWidth = maxWidth; } - return QSize(textWidth, QLabel::sizeHint().height()); + const int chromeWidth = width() - contentsRect().width(); + return QSize(textWidth + chromeWidth, QLabel::sizeHint().height()); } void KSqueezedTextLabel::setText(const QString &text) @@ -100,7 +101,7 @@ void KSqueezedTextLabel::squeezeTextToLabel() { QFontMetrics fm(fontMetrics()); - int labelWidth = size().width(); + const int labelWidth = contentsRect().width(); QStringList squeezedLines; bool squeezed = false; Q_FOREACH (const QString &line, d->fullText.split(QLatin1Char('\n'))) { @@ -122,6 +123,40 @@ } } +QRect KSqueezedTextLabel::contentsRect() const +{ + // calculation according to API docs for QLabel::indent + const int margin = this->margin(); + int indent = this->indent(); + if (indent < 0) { + if (frameWidth() == 0) { + indent = 0; + } else { + indent = fontMetrics().width(QLatin1Char('x')) / 2 - margin; + } + } + + QRect result = QLabel::contentsRect(); + if (indent > 0) { + const int alignment = this->alignment(); + if (alignment & Qt::AlignLeft) { + result.setLeft(result.left() + indent); + } + if (alignment & Qt::AlignTop) { + result.setTop(result.top() + indent); + } + if (alignment & Qt::AlignRight) { + result.setRight(result.right() - indent); + } + if (alignment & Qt::AlignBottom) { + result.setBottom(result.bottom() - indent); + } + } + + result.adjust(margin, margin, -margin, -margin); + return result; +} + void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) { // save fullText and restore it