From a7ba79553c364df9ab55c5c177b15606191cd568 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 27 Jan 2019 21:04:25 +0100 Subject: QCommonStyle: factor out elided text calculation Factor out the calculation of the elided text from QCommonStylePrivate::viewItemDrawText() so it can be used by other painting functions. Change-Id: I28e6bfd2fe4d7c552848446fa9913df78589d15b Reviewed-by: Christian Andersen Reviewed-by: Richard Moe Gustavsen --- src/widgets/styles/qcommonstyle.cpp | 142 ++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 61 deletions(-) (limited to 'src/widgets/styles/qcommonstyle.cpp') diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index 3d626a57fa..867e91ab3e 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -843,8 +843,6 @@ static void drawArrow(const QStyle *style, const QStyleOptionToolButton *toolbut } #endif // QT_CONFIG(toolbutton) -#if QT_CONFIG(itemviews) - static QSizeF viewItemTextLayout(QTextLayout &textLayout, int lineWidth) { qreal height = 0; @@ -863,6 +861,80 @@ static QSizeF viewItemTextLayout(QTextLayout &textLayout, int lineWidth) return QSizeF(widthUsed, height); } +QString QCommonStylePrivate::calculateElidedText(const QString &text, const QTextOption &textOption, + const QFont &font, const QRect &textRect, const Qt::Alignment valign, + Qt::TextElideMode textElideMode, int flags, + bool lastVisibleLineShouldBeElided, QPointF *paintStartPosition) const +{ + QTextLayout textLayout(text, font); + textLayout.setTextOption(textOption); + + viewItemTextLayout(textLayout, textRect.width()); + + const QRectF boundingRect = textLayout.boundingRect(); + // don't care about LTR/RTL here, only need the height + const QRect layoutRect = QStyle::alignedRect(Qt::LayoutDirectionAuto, valign, + boundingRect.size().toSize(), textRect); + + if (paintStartPosition) + *paintStartPosition = QPointF(textRect.x(), layoutRect.top()); + + QString ret; + qreal height = 0; + const int lineCount = textLayout.lineCount(); + for (int i = 0; i < lineCount; ++i) { + const QTextLine line = textLayout.lineAt(i); + height += line.height(); + + // above visible rect + if (height + layoutRect.top() <= textRect.top()) { + if (paintStartPosition) + paintStartPosition->ry() += line.height(); + continue; + } + + const int start = line.textStart(); + const int length = line.textLength(); + const bool drawElided = line.naturalTextWidth() > textRect.width(); + bool elideLastVisibleLine = false; + if (!drawElided && i + 1 < lineCount && lastVisibleLineShouldBeElided) { + const QTextLine nextLine = textLayout.lineAt(i + 1); + const int nextHeight = height + nextLine.height() / 2; + // elide when less than the next half line is visible + if (nextHeight + layoutRect.top() > textRect.height() + textRect.top()) + elideLastVisibleLine = true; + } + + QString text = textLayout.text().mid(start, length); + if (drawElided || elideLastVisibleLine) { + if (elideLastVisibleLine) { + if (text.endsWith(QChar::LineSeparator)) + text.chop(1); + text += QChar(0x2026); + } + const QStackTextEngine engine(text, font); + ret += engine.elidedText(textElideMode, textRect.width(), flags); + + // no newline for the last line (last visible or real) + // sometimes drawElided is true but no eliding is done so the text ends + // with QChar::LineSeparator - don't add another one. This happened with + // arabic text in the testcase for QTBUG-72805 + if (i < lineCount - 1 && + !ret.endsWith(QChar::LineSeparator)) + ret += QChar::LineSeparator; + } else { + ret += text; + } + + // below visible text, can stop + if (height + layoutRect.top() >= textRect.bottom()) + break; + } + return ret; +} + +#if QT_CONFIG(itemviews) + QSize QCommonStylePrivate::viewItemSize(const QStyleOptionViewItem *option, int role) const { const QWidget *widget = option->widget; @@ -935,67 +1007,15 @@ void QCommonStylePrivate::viewItemDrawText(QPainter *p, const QStyleOptionViewIt textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap); textOption.setTextDirection(option->direction); textOption.setAlignment(QStyle::visualAlignment(option->direction, option->displayAlignment)); - QTextLayout textLayout(option->text, option->font); - textLayout.setTextOption(textOption); - - viewItemTextLayout(textLayout, textRect.width()); - - const QRectF boundingRect = textLayout.boundingRect(); - const QRect layoutRect = QStyle::alignedRect(option->direction, option->displayAlignment, - boundingRect.size().toSize(), textRect); - QPointF paintPosition = QPointF(textRect.x(), layoutRect.top()); - - QString newText; - qreal height = 0; - const int lineCount = textLayout.lineCount(); - for (int i = 0; i < lineCount; ++i) { - const QTextLine line = textLayout.lineAt(i); - height += line.height(); - - // above visible rect - if (height + layoutRect.top() <= textRect.top()) { - paintPosition.ry() += line.height(); - continue; - } - - const int start = line.textStart(); - const int length = line.textLength(); - - const bool drawElided = line.naturalTextWidth() > textRect.width(); - bool elideLastVisibleLine = false; - if (!drawElided && i + 1 < lineCount) { - const QTextLine nextLine = textLayout.lineAt(i + 1); - const int nextHeight = height + nextLine.height() / 2; - // elide when less than the next half line is visible - if (nextHeight + layoutRect.top() > textRect.height() + textRect.top()) - elideLastVisibleLine = true; - } - - QString text = textLayout.text().mid(start, length); - if (drawElided || elideLastVisibleLine) { - if (elideLastVisibleLine) { - if (text.endsWith(QChar::LineSeparator)) - text.chop(1); - text += QChar(0x2026); - } - const QStackTextEngine engine(text, option->font); - newText += engine.elidedText(option->textElideMode, textRect.width()); - // sometimes drawElided is true but no eliding is done so the text ends - // with QChar::LineSeparator - don't add another one. This happened with - // arabic text in the testcase for QTBUG-72805 - if (i < lineCount - 1 && - !newText.endsWith(QChar::LineSeparator)) - newText += QChar::LineSeparator; - } else { - newText += text; - } - // below visible text, can stop - if (height + layoutRect.top() >= textRect.bottom()) - break; - } + QPointF paintPosition; + const QString newText = calculateElidedText(option->text, textOption, + option->font, textRect, option->displayAlignment, + option->textElideMode, 0, + true, &paintPosition); - textLayout.setText(newText); + QTextLayout textLayout(newText, option->font); + textLayout.setTextOption(textOption); viewItemTextLayout(textLayout, textRect.width()); textLayout.draw(p, paintPosition); } -- cgit v1.2.3 From 7024090e1dc7043e7a2a692ede105bcb7952781d Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 27 Jan 2019 21:15:52 +0100 Subject: QToolButton: fix handling multi-line texts The patch to elide the QToolButton text when there is not enough space introduced a regression with multi-line text. Fix it by using the newly introduced common function to elide multi-line text. Fixes: QTBUG-72226 Change-Id: I066ebbd2f360add93406cc29bb4bbbebf599ba42 Reviewed-by: Samuel Gaist Reviewed-by: Richard Moe Gustavsen --- src/widgets/styles/qcommonstyle.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/widgets/styles/qcommonstyle.cpp') diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index 867e91ab3e..79e338a6e7 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -1157,6 +1157,25 @@ void QCommonStylePrivate::viewItemLayout(const QStyleOptionViewItem *opt, QRect } #endif // QT_CONFIG(itemviews) +#if QT_CONFIG(toolbutton) +QString QCommonStylePrivate::toolButtonElideText(const QStyleOptionToolButton *option, + const QRect &textRect, int flags) const +{ + if (option->fontMetrics.horizontalAdvance(option->text) <= textRect.width()) + return option->text; + + QString text = option->text; + text.replace('\n', QChar::LineSeparator); + QTextOption textOption; + textOption.setWrapMode(QTextOption::ManualWrap); + textOption.setTextDirection(option->direction); + + return calculateElidedText(text, textOption, + option->font, textRect, Qt::AlignTop, + Qt::ElideMiddle, flags, + false, nullptr); +} +#endif // QT_CONFIG(toolbutton) #if QT_CONFIG(tabbar) /*! \internal @@ -1705,8 +1724,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, alignment |= Qt::AlignLeft | Qt::AlignVCenter; } tr.translate(shiftX, shiftY); - const QString text = toolbutton->fontMetrics.elidedText(toolbutton->text, Qt::ElideMiddle, - tr.width(), alignment); + const QString text = d->toolButtonElideText(toolbutton, tr, alignment); proxy()->drawItemText(p, QStyle::visualRect(opt->direction, rect, tr), alignment, toolbutton->palette, toolbutton->state & State_Enabled, text, QPalette::ButtonText); -- cgit v1.2.3