comparison 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
comparison
equal deleted inserted replaced
52:384420567575 53:3c46b2ac45d3
84 } 84 }
85 85
86 QString 86 QString
87 TextAbbrev::abbreviate(QString text, 87 TextAbbrev::abbreviate(QString text,
88 const QFontMetrics &metrics, int &maxWidth, 88 const QFontMetrics &metrics, int &maxWidth,
89 Policy policy, QString ellipsis) 89 Policy policy, QString ellipsis, int wrapLines)
90 { 90 {
91 if (ellipsis == "") ellipsis = getDefaultEllipsis(); 91 if (ellipsis == "") ellipsis = getDefaultEllipsis();
92 92
93 int tw = metrics.width(text); 93 int tw = metrics.width(text);
94 94
98 } 98 }
99 99
100 int truncated = text.length(); 100 int truncated = text.length();
101 QString original = text; 101 QString original = text;
102 102
103 while (tw > maxWidth && truncated > 1) { 103 if (wrapLines < 2) {
104 104
105 truncated--; 105 while (tw > maxWidth && truncated > 1) {
106 106
107 if (truncated > ellipsis.length()) { 107 truncated--;
108 text = abbreviateTo(original, truncated, policy, ellipsis); 108
109 } else { 109 if (truncated > ellipsis.length()) {
110 break; 110 text = abbreviateTo(original, truncated, policy, ellipsis);
111 } 111 } else {
112 112 break;
113 tw = metrics.width(text); 113 }
114 } 114
115 115 tw = metrics.width(text);
116 maxWidth = tw; 116 }
117 return text; 117
118 maxWidth = tw;
119 return text;
120
121 } else {
122
123 QStringList words = text.split(' ', QString::SkipEmptyParts);
124 text = "";
125
126 tw = 0;
127 int i = 0;
128 QString good = "";
129 int lastUsed = 0;
130 while (tw < maxWidth && i < words.size()) {
131 if (text != "") text += " ";
132 text += words[i++];
133 tw = metrics.width(text);
134 if (tw < maxWidth) {
135 good = text;
136 lastUsed = i;
137 }
138 }
139
140 if (tw < maxWidth) {
141 maxWidth = tw;
142 return text;
143 }
144
145 text = good;
146
147 QString remainder;
148 while (lastUsed < words.size()) {
149 if (remainder != "") remainder += " ";
150 remainder += words[lastUsed++];
151 }
152 remainder = abbreviate(remainder, metrics, maxWidth,
153 policy, ellipsis, wrapLines - 1);
154
155 maxWidth = std::max(maxWidth, tw);
156 text = text + '\n' + remainder;
157 return text;
158 }
118 } 159 }
119 160
120 QStringList 161 QStringList
121 TextAbbrev::abbreviate(const QStringList &texts, int maxLength, 162 TextAbbrev::abbreviate(const QStringList &texts, int maxLength,
122 Policy policy, bool fuzzy, QString ellipsis) 163 Policy policy, bool fuzzy, QString ellipsis)