diff --git a/autotests/ksqueezedtextlabelautotest.cpp b/autotests/ksqueezedtextlabelautotest.cpp --- a/autotests/ksqueezedtextlabelautotest.cpp +++ b/autotests/ksqueezedtextlabelautotest.cpp @@ -172,7 +172,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); @@ -242,7 +241,6 @@ (label->*attributeFn)(amount); - 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 @@ -82,6 +82,35 @@ QSize sizeHint() const Q_DECL_OVERRIDE; /** + * Sets the indentation of the label. + * Reimplementation of QLabel::setIndent(), but see setText() for additional remark. + * @param indent the amount of indentation in pixels + * + * @since 5.38 + */ + void setIndent(int indent); + + /** + * Sets the margin of the label. + * Reimplementation of QLabel::setMargin(), but see setText() for additional remark. + * @param margin the margin size in pixels + * + * @since 5.38 + */ + void setMargin(int margin); + + /** + * Sets the frame width of the label. + * Reimplementation of QLabel::setLineWidth(), but see setText() for additional remark. + * @param width the frame width in pixels + * + * @see QFrame::setFrameStyle() + * + * @since 5.38 + */ + void setLineWidth(int width); + + /** * Overridden for internal reasons; the API remains unaffected. */ virtual void setAlignment(Qt::Alignment); @@ -157,6 +186,15 @@ */ void squeezeTextToLabel(); + /** + * @return the rectangle to squeeze the text into + * + * Reimplementation of QLabel::contentsRect(), but see setText() for additional remark. + * + * @since 5.38 + */ + QRect contentsRect() const; + private: Q_PRIVATE_SLOT(d, void _k_copyFullText()) KSqueezedTextLabelPrivate *const d; diff --git a/src/ksqueezedtextlabel.cpp b/src/ksqueezedtextlabel.cpp --- a/src/ksqueezedtextlabel.cpp +++ b/src/ksqueezedtextlabel.cpp @@ -82,7 +82,26 @@ 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::setIndent(int indent) +{ + QLabel::setIndent(indent); + squeezeTextToLabel(); +} + +void KSqueezedTextLabel::setMargin(int margin) +{ + QLabel::setMargin(margin); + squeezeTextToLabel(); +} + +void KSqueezedTextLabel::setLineWidth(int width) +{ + QLabel::setLineWidth(width); + squeezeTextToLabel(); } void KSqueezedTextLabel::setText(const QString &text) @@ -100,7 +119,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 +141,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