18 #include <QFontMetrics> 19 #include <QApplication> 32 int len = ellipsis.length();
33 if (len < 3)
return len + 3;
34 else if (len > 5)
return len + 5;
41 int width = metrics.width(ellipsis);
53 text = text.left(characters) + ellipsis;
57 text = ellipsis + text.right(characters);
62 text = text.left(characters/2 + 1) + ellipsis
63 + text.right(characters - (characters/2 + 1));
65 text = text.left(characters) + ellipsis;
79 if (maxLength <= ellipsis.length()) maxLength = ellipsis.length() + 1;
80 if (text.length() <= maxLength + fl)
return text;
82 int truncated = maxLength - ellipsis.length();
88 const QFontMetrics &metrics,
int &maxWidth,
89 Policy policy, QString ellipsis)
93 int tw = metrics.width(text);
100 int truncated = text.length();
101 QString original = text;
103 while (tw > maxWidth && truncated > 1) {
107 if (truncated > ellipsis.length()) {
108 text =
abbreviateTo(original, truncated, policy, ellipsis);
113 tw = metrics.width(text);
122 Policy policy,
bool fuzzy, QString ellipsis)
129 if (maxLength <= ellipsis.length()) maxLength = ellipsis.length() + 1;
131 int maxOrigLength = 0;
132 for (
int i = 0; i < texts.size(); ++i) {
133 int len = texts[i].length();
134 if (len > maxOrigLength) maxOrigLength = len;
136 if (maxOrigLength <= maxLength + fl)
return texts;
139 (texts, maxOrigLength - maxLength, ellipsis),
140 maxLength,
ElideEnd, fuzzy, ellipsis);
144 for (
int i = 0; i < texts.size(); ++i) {
146 (
abbreviate(texts[i], maxLength, policy, fuzzy, ellipsis));
153 int &maxWidth,
Policy policy, QString ellipsis)
160 int maxOrigWidth = 0;
161 for (
int i = 0; i < texts.size(); ++i) {
162 int w = metrics.width(texts[i]);
163 if (w > maxOrigWidth) maxOrigWidth = w;
167 maxOrigWidth - maxWidth, ellipsis),
168 metrics, maxWidth,
ElideEnd, ellipsis);
172 int maxAbbrWidth = 0;
173 for (
int i = 0; i < texts.size(); ++i) {
174 int width = maxWidth;
175 QString abbr =
abbreviate(texts[i], metrics, width, policy, ellipsis);
176 if (width > maxAbbrWidth) maxAbbrWidth = width;
177 results.push_back(abbr);
179 maxWidth = maxAbbrWidth;
188 if (texts.empty())
return texts;
191 if (plen < fl)
return texts;
193 QString prefix = texts[0].left(plen);
194 int truncated = plen;
195 if (plen >= targetReduction + fl) {
196 truncated = plen - targetReduction;
203 for (
int i = 0; i < texts.size(); ++i) {
205 (prefix + texts[i].right(texts[i].length() - plen));
212 const QFontMetrics &metrics,
213 int targetWidthReduction,
216 if (texts.empty())
return texts;
219 if (plen < fl)
return texts;
221 QString prefix = texts[0].left(plen);
222 int pwid = metrics.width(prefix);
223 int twid = pwid - targetWidthReduction;
224 if (twid < metrics.width(ellipsis) * 2) twid = metrics.width(ellipsis) * 2;
228 for (
int i = 0; i < texts.size(); ++i) {
230 (prefix + texts[i].right(texts[i].length() - plen));
238 for (
int i = 1; i < texts.size(); ++i) {
239 if (!texts[i].startsWith(prefix))
return false;
247 QString reference = texts[0];
249 if (reference ==
"" ||
havePrefix(reference, texts)) {
250 return reference.length();
253 int candidate = reference.length();
254 QString splitChars(
";:,./#-!()$_+=[]{}\\");
256 while (--candidate > 1) {
257 if (splitChars.contains(reference[candidate])) {
258 if (
havePrefix(reference.left(candidate), texts)) {
static int getFuzzLength(QString ellipsis)
static QString getDefaultEllipsis()
static QString abbreviateTo(QString text, int characters, Policy policy, QString ellipsis)
static QString abbreviate(QString text, int maxLength, Policy policy=ElideEnd, bool fuzzy=true, QString ellipsis="")
Abbreviate the given text to the given maximum length (including ellipsis), using the given abbreviat...
static QStringList elidePrefixes(const QStringList &texts, int targetReduction, QString ellipsis)
static int getPrefixLength(const QStringList &texts)
static bool havePrefix(QString prefix, const QStringList &texts)
static int getFuzzWidth(const QFontMetrics &metrics, QString ellipsis)