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,30 @@ } } +QRect KSqueezedTextLabel::contentsRect() const +{ + // copied and adapted from QLabelPrivate::documentRect() + + QRect cr = QLabel::contentsRect(); + int margin = this->margin(); + cr.adjust(margin, margin, -margin, -margin); + const int align = alignment(); + int m = indent(); + if (m < 0 && frameWidth()) // no indent, but we do have a frame + m = fontMetrics().width(QLatin1Char('x')) / 2 - margin; + if (m > 0) { + if (align & Qt::AlignLeft) + cr.setLeft(cr.left() + m); + if (align & Qt::AlignRight) + cr.setRight(cr.right() - m); + if (align & Qt::AlignTop) + cr.setTop(cr.top() + m); + if (align & Qt::AlignBottom) + cr.setBottom(cr.bottom() - m); + } + return cr; +} + void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) { // save fullText and restore it