From ec9af5ca6f92a618f81b3d8b3f45a8716c31888e Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 15 Apr 2016 15:26:58 +0300 Subject: [PATCH] Wrap tool tip text by word in Widget action --- src/ModuleBase/ModuleBase_Tools.cpp | 52 +++++++++++++++++++--- src/ModuleBase/ModuleBase_Tools.h | 6 +++ src/ModuleBase/ModuleBase_WidgetAction.cpp | 3 +- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/ModuleBase/ModuleBase_Tools.cpp b/src/ModuleBase/ModuleBase_Tools.cpp index baaea0da0..e444d4a35 100755 --- a/src/ModuleBase/ModuleBase_Tools.cpp +++ b/src/ModuleBase/ModuleBase_Tools.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -158,17 +159,17 @@ QPixmap lighter(const QString& theIcon, const int theLighterValue) return QPixmap(); QImage aResult(theIcon); - for ( int i = 0; i < anIcon.width(); i++ ) + for (int i = 0; i < anIcon.width(); i++) { - for ( int j = 0; j < anIcon.height(); j++ ) + for (int j = 0; j < anIcon.height(); j++) { - QRgb anRgb = anIcon.pixel( i, j ); + QRgb anRgb = anIcon.pixel(i, j); QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb), - qAlpha( aResult.pixel( i, j ) )); + qAlpha(aResult.pixel(i, j))); QColor aLighterColor = aPixelColor.lighter(theLighterValue); - aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(), - aLighterColor.blue(), aLighterColor.alpha() ) ); + aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(), + aLighterColor.blue(), aLighterColor.alpha())); } } return QPixmap::fromImage(aResult); @@ -543,6 +544,45 @@ void blockUpdateViewer(const bool theValue) Events_Loop::loop()->send(aMsg); } +QString ModuleBase_Tools::wrapTextByWords(const QString& theValue, QWidget* theWidget, + int theMaxLineInPixels) +{ + static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font()); + static qreal phi = 2.618; + + QRect aBounds = tfm.boundingRect(theValue); + if(aBounds.width() <= theMaxLineInPixels) + return theValue; + + qreal s = aBounds.width() * aBounds.height(); + qreal aGoldWidth = sqrt(s*phi); + + QStringList aWords = theValue.split(" ", QString::SkipEmptyParts); + QStringList aLines; + int n = aWords.count(); + QString aLine; + for (int i = 0; i < n; i++) { + QString aLineExt = aLine + " " + aWords[i]; + qreal anWidthNonExt = tfm.boundingRect(aLine).width(); + qreal anWidthExt = tfm.boundingRect(aLineExt).width(); + qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth); + qreal aDeltaExt = fabs(anWidthExt-aGoldWidth); + if(aDeltaNonExt < aDeltaExt) { + // new line + aLines.append(aLine); + aLine = aWords[i]; + } + else + aLine = aLineExt; + } + + if(!aLine.isEmpty()) + aLines.append(aLine); + + QString aResult = aLines.join("\n"); + return aResult; +} + } // namespace ModuleBase_Tools diff --git a/src/ModuleBase/ModuleBase_Tools.h b/src/ModuleBase/ModuleBase_Tools.h index 610136420..938b30e5c 100755 --- a/src/ModuleBase/ModuleBase_Tools.h +++ b/src/ModuleBase/ModuleBase_Tools.h @@ -207,6 +207,12 @@ MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject); /// \param theValue a boolean value MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue); +/// Generates a wrapped string to be less than value with '\n' separators +/// \param theValue a boolean value +/// \param theWidget a widget to know the font +/// \param theMaxLineInPixels a maximum line width in pixels +MODULEBASE_EXPORT QString wrapToolTipByWords(const QString& theValue, QWidget* theWidget, + int theMaxLineInPixels = 150); } #endif diff --git a/src/ModuleBase/ModuleBase_WidgetAction.cpp b/src/ModuleBase/ModuleBase_WidgetAction.cpp index 2b0dd3915..f63455d18 100755 --- a/src/ModuleBase/ModuleBase_WidgetAction.cpp +++ b/src/ModuleBase/ModuleBase_WidgetAction.cpp @@ -24,7 +24,8 @@ ModuleBase_WidgetAction::ModuleBase_WidgetAction(QWidget* theParent, setAttributeID(""); QString aText = QString::fromStdString(theData->widgetLabel()); - QString aToolTip = QString::fromStdString(theData->widgetTooltip()); + QString aToolTip = ModuleBase_Tools::wrapToolTipByWords( + QString::fromStdString(theData->widgetTooltip()), myButton); myButton = new QToolButton(this); myButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); -- 2.39.2