diff textabbrev.cpp @ 53:3c46b2ac45d3

* Put proper labels &c in changeset items; colour branches and users; etc
author Chris Cannam
date Fri, 12 Nov 2010 16:48:18 +0000
parents c76782c14371
children f583e44d9d31
line wrap: on
line diff
--- a/textabbrev.cpp	Fri Nov 12 11:32:01 2010 +0000
+++ b/textabbrev.cpp	Fri Nov 12 16:48:18 2010 +0000
@@ -86,7 +86,7 @@
 QString
 TextAbbrev::abbreviate(QString text,
                        const QFontMetrics &metrics, int &maxWidth,
-                       Policy policy, QString ellipsis)
+                       Policy policy, QString ellipsis, int wrapLines)
 {
     if (ellipsis == "") ellipsis = getDefaultEllipsis();
 
@@ -100,21 +100,62 @@
     int truncated = text.length();
     QString original = text;
 
-    while (tw > maxWidth && truncated > 1) {
+    if (wrapLines < 2) {
 
-        truncated--;
+        while (tw > maxWidth && truncated > 1) {
 
-        if (truncated > ellipsis.length()) {
-            text = abbreviateTo(original, truncated, policy, ellipsis);
-        } else {
-            break;
+            truncated--;
+            
+            if (truncated > ellipsis.length()) {
+                text = abbreviateTo(original, truncated, policy, ellipsis);
+            } else {
+                break;
+            }
+            
+            tw = metrics.width(text);
+        }
+        
+        maxWidth = tw;
+        return text;
+
+    } else {
+        
+        QStringList words = text.split(' ', QString::SkipEmptyParts);
+        text = "";
+
+        tw = 0;
+        int i = 0;
+        QString good = "";
+        int lastUsed = 0;
+        while (tw < maxWidth && i < words.size()) {
+            if (text != "") text += " ";
+            text += words[i++];
+            tw = metrics.width(text);
+            if (tw < maxWidth) {
+                good = text;
+                lastUsed = i;
+            }
+        }
+            
+        if (tw < maxWidth) {
+            maxWidth = tw;
+            return text;
         }
 
-        tw = metrics.width(text);
+        text = good;
+
+        QString remainder;
+        while (lastUsed < words.size()) {
+            if (remainder != "") remainder += " ";
+            remainder += words[lastUsed++];
+        }
+        remainder = abbreviate(remainder, metrics, maxWidth,
+                               policy, ellipsis, wrapLines - 1);
+
+        maxWidth = std::max(maxWidth, tw);
+        text = text + '\n' + remainder;
+        return text;
     }
-
-    maxWidth = tw;
-    return text;
 }
 
 QStringList