gui: PreviewGeneratorPlainText: Truncate dirtily to avoid lags

It was possible the text was getting too big. The GUI
was lagging for previews of some text files. The first
assumption was that we would only have a couple of hits,
which is unreasonable for large .txt files and common
words.

We only ever see a handful of previews, it makes no sense
to get all snippets. So just allow 7 snippets, that's it.

Also, just cut after 1000 chars no matter what.
This commit is contained in:
Albert S. 2022-05-30 18:35:54 +02:00
rodzic 4aa850d5ed
commit bb1e653690

Wyświetl plik

@ -23,12 +23,14 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
int lastWordPos = 0;
QHash<QString, int> countmap;
const unsigned int maxSnippets = 7;
unsigned int currentSnippets = 0;
for(QString &word : config.wordsToHighlight)
{
int lastPos = 0;
int index = content.indexOf(word, lastPos, Qt::CaseInsensitive);
while(index != -1)
while(index != -1 && currentSnippets < maxSnippets)
{
countmap[word] = countmap.value(word, 0) + 1;
@ -52,6 +54,7 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
lastPos = index;
index = content.indexOf(word, lastPos + 1, Qt::CaseInsensitive);
++currentSnippets;
}
lastWordPos = lastPos;
}
@ -75,8 +78,13 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
{
header += word + ": " + QString::number(countmap[word]) + " ";
}
if(currentSnippets == maxSnippets)
{
header += "(truncated)";
}
header += "<hr>";
result->setText(header + resulText.replace("\n", "<br>"));
result->setText(header + resulText.replace("\n", "<br>").mid(0, 1000));
return QSharedPointer<PreviewResultPlainText>(result);
}