Skip to content
Snippets Groups Projects
Commit 60f7ef8e authored by hluk's avatar hluk
Browse files

Improve text elision performance


Signed-off-by: default avatarLukas Holecek <hluk@email.cz>
parent a14a3e3f
No related branches found
No related tags found
No related merge requests found
......@@ -470,19 +470,34 @@ bool anySessionOwnsClipboardData(const QVariantMap &data)
QString elideText(const QString &text, const QFont &font, const QString &format,
bool escapeAmpersands, int maxWidthPixels, int maxLines)
{
if ( text.isEmpty() )
return QString();
if (maxWidthPixels <= 0)
maxWidthPixels = smallIconSize() * 20;
QStringList lines = text.split('\n');
// Ignore empty lines at beginning.
static const QRegularExpression reNonEmpty(".*\\S.*");
const int firstLine = qMax(0, lines.indexOf(reNonEmpty));
const int lastLine = qMax(0, lines.lastIndexOf(reNonEmpty, firstLine + maxLines - 1));
int firstLine = -1;
int lastLine = -1;
int commonIndent = text.size();
static const QRegularExpression reNonSpace("\\S");
for (int i = 0; i < lines.size(); ++i) {
const auto &line = lines[i];
const int lineIndent = line.indexOf(reNonSpace);
if (lineIndent == -1)
continue;
// If empty lines are at beginning, prepend triple dot.
if (firstLine != 0)
lines[firstLine].prepend("...");
if (firstLine == -1)
firstLine = i;
lastLine = i;
if (lineIndent < commonIndent)
commonIndent = lineIndent;
if (firstLine - lastLine + 1 >= maxLines)
break;
}
if (lastLine == -1)
return QString("...");
......@@ -501,19 +516,9 @@ QString elideText(const QString &text, const QFont &font, const QString &format,
#endif
// Remove redundant spaces from single line text.
if (lines.size() == 1)
if (lines.size() == 1) {
lines[0] = lines[0].simplified();
// Find common indentation.
int commonIndent = lines.value(0).size();
static const QRegularExpression reNonSpace("\\S");
for (const auto &line : lines) {
const int lineIndent = line.indexOf(reNonSpace);
if (lineIndent != -1 && lineIndent < commonIndent) {
commonIndent = lineIndent;
if (commonIndent == 0)
break;
}
commonIndent = 0;
}
// Remove common indentation each line and elide text if too long.
......@@ -527,6 +532,14 @@ QString elideText(const QString &text, const QFont &font, const QString &format,
line = fm.elidedText(line, Qt::ElideMiddle, maxWidthPixels - formatWidth);
}
// If empty lines are at beginning, prepend triple dot.
if (firstLine != 0) {
if (lines.size() == 1)
lines.first().prepend("...");
else
lines.prepend("...");
}
QString result = lines.join("\n");
// Escape all ampersands.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment