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.
这个提交包含在:
Albert S. 2022-05-30 18:35:54 +02:00
父节点 4aa850d5ed
当前提交 bb1e653690

查看文件

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