From 5a1f6fdba864b1898ab3f97cfef2442e5f5dd2e6 Mon Sep 17 00:00:00 2001 From: baldurk Date: Thu, 10 Jun 2021 12:44:07 +0100 Subject: [PATCH] Elide tool button text if the button isn't large enough --- .../RDTweakedNativeStyle.cpp | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/qrenderdoc/Styles/RDTweakedNativeStyle/RDTweakedNativeStyle.cpp b/qrenderdoc/Styles/RDTweakedNativeStyle/RDTweakedNativeStyle.cpp index 4b5358b4d..9adab3bb7 100644 --- a/qrenderdoc/Styles/RDTweakedNativeStyle/RDTweakedNativeStyle.cpp +++ b/qrenderdoc/Styles/RDTweakedNativeStyle/RDTweakedNativeStyle.cpp @@ -482,9 +482,28 @@ void RDTweakedNativeStyle::drawControl(ControlElement control, const QStyleOptio proxy()->drawItemPixmap(p, QStyle::visualRect(opt->direction, rect, iconRect), Qt::AlignCenter, pixmap); - proxy()->drawItemText(p, QStyle::visualRect(opt->direction, rect, textRect), textFlags, - toolopt->palette, toolopt->state & State_Enabled, toolopt->text, - QPalette::ButtonText); + // elide text from the right if there's not enough space + QFontMetrics metrics(toolopt->font); + + int space = metrics.horizontalAdvance(QLatin1Char(' ')); + textRect = QStyle::visualRect(opt->direction, rect, textRect); + + if(toolopt->toolButtonStyle == Qt::ToolButtonTextOnly) + { + textRect.adjust(3 + space, 0, -3 - space, 0); + } + + const QString elidedText = metrics.elidedText(toolopt->text, Qt::ElideRight, textRect.width()); + + // if we elided, align left now + if(elidedText.length() < toolopt->text.length()) + { + textFlags &= ~Qt::AlignCenter; + textFlags |= Qt::AlignLeft | Qt::AlignVCenter; + } + + proxy()->drawItemText(p, textRect, textFlags, toolopt->palette, + toolopt->state & State_Enabled, elidedText, QPalette::ButtonText); } return;