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;
44 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 46 int width = metrics.width(ellipsis);
58 text = text.left(characters) + ellipsis;
62 text = ellipsis + text.right(characters);
67 text = text.left(characters/2 + 1) + ellipsis
68 + text.right(characters - (characters/2 + 1));
70 text = text.left(characters) + ellipsis;
84 if (maxLength <= ellipsis.length()) maxLength = ellipsis.length() + 1;
85 if (text.length() <= maxLength + fl)
return text;
87 int truncated = maxLength - ellipsis.length();
93 const QFontMetrics &metrics,
int &maxWidth,
94 Policy policy, QString ellipsis)
98 int tw = metrics.width(text);
100 if (tw <= maxWidth) {
105 int truncated = text.length();
106 QString original = text;
108 while (tw > maxWidth && truncated > 1) {
112 if (truncated > ellipsis.length()) {
113 text =
abbreviateTo(original, truncated, policy, ellipsis);
118 tw = metrics.width(text);
127 Policy policy,
bool fuzzy, QString ellipsis)
134 if (maxLength <= ellipsis.length()) maxLength = ellipsis.length() + 1;
136 int maxOrigLength = 0;
137 for (
int i = 0; i < texts.size(); ++i) {
138 int len = texts[i].length();
139 if (len > maxOrigLength) maxOrigLength = len;
141 if (maxOrigLength <= maxLength + fl)
return texts;
144 (texts, maxOrigLength - maxLength, ellipsis),
145 maxLength,
ElideEnd, fuzzy, ellipsis);
149 for (
int i = 0; i < texts.size(); ++i) {
151 (
abbreviate(texts[i], maxLength, policy, fuzzy, ellipsis));
158 int &maxWidth,
Policy policy, QString ellipsis)
165 int maxOrigWidth = 0;
166 for (
int i = 0; i < texts.size(); ++i) {
167 int w = metrics.width(texts[i]);
168 if (w > maxOrigWidth) maxOrigWidth = w;
172 maxOrigWidth - maxWidth, ellipsis),
173 metrics, maxWidth,
ElideEnd, ellipsis);
177 int maxAbbrWidth = 0;
178 for (
int i = 0; i < texts.size(); ++i) {
179 int width = maxWidth;
180 QString abbr =
abbreviate(texts[i], metrics, width, policy, ellipsis);
181 if (width > maxAbbrWidth) maxAbbrWidth = width;
182 results.push_back(abbr);
184 maxWidth = maxAbbrWidth;
193 if (texts.empty())
return texts;
196 if (plen < fl)
return texts;
198 QString prefix = texts[0].left(plen);
199 int truncated = plen;
200 if (plen >= targetReduction + fl) {
201 truncated = plen - targetReduction;
208 for (
int i = 0; i < texts.size(); ++i) {
210 (prefix + texts[i].right(texts[i].length() - plen));
217 const QFontMetrics &metrics,
218 int targetWidthReduction,
221 if (texts.empty())
return texts;
224 if (plen < fl)
return texts;
226 QString prefix = texts[0].left(plen);
227 int pwid = metrics.width(prefix);
228 int twid = pwid - targetWidthReduction;
229 if (twid < metrics.width(ellipsis) * 2) twid = metrics.width(ellipsis) * 2;
233 for (
int i = 0; i < texts.size(); ++i) {
235 (prefix + texts[i].right(texts[i].length() - plen));
243 for (
int i = 1; i < texts.size(); ++i) {
244 if (!texts[i].startsWith(prefix))
return false;
252 QString reference = texts[0];
254 if (reference ==
"" ||
havePrefix(reference, texts)) {
255 return reference.length();
258 int candidate = reference.length();
259 QString splitChars(
";:,./#-!()$_+=[]{}\\");
261 while (--candidate > 1) {
262 if (splitChars.contains(reference[candidate])) {
263 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)