Salome HOME
Wrap tool tip text by word in Widget action
authornds <nds@opencascade.com>
Fri, 15 Apr 2016 12:26:58 +0000 (15:26 +0300)
committernds <nds@opencascade.com>
Fri, 15 Apr 2016 12:26:58 +0000 (15:26 +0300)
src/ModuleBase/ModuleBase_Tools.cpp
src/ModuleBase/ModuleBase_Tools.h
src/ModuleBase/ModuleBase_WidgetAction.cpp

index baaea0da067c4fdaf64501194818160af6f1a555..e444d4a350861c91e3636b2e511027c7a89abec0 100755 (executable)
@@ -43,6 +43,7 @@
 #include <QDoubleSpinBox>
 #include <QGraphicsDropShadowEffect>
 #include <QColor>
+#include <QApplication>
 
 #include <sstream>
 #include <string>
@@ -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
 
 
index 6101364200a1494b6aba5e21ad80a8fd5ff90ff1..938b30e5c18d8bb90b577ad23d8b7a0b8de90e30 100755 (executable)
@@ -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
index 2b0dd39151156044a7a95ccb990f1432fcf00e06..f63455d18824431da9640463496ae2211e7239f9 100755 (executable)
@@ -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);