Albert S
bb1e653690
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.
91 satır
2.1 KiB
C++
91 satır
2.1 KiB
C++
#include <QTextStream>
|
|
|
|
#include "previewgeneratorplaintext.h"
|
|
#include "previewresultplaintext.h"
|
|
|
|
QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath,
|
|
unsigned int page)
|
|
{
|
|
PreviewResultPlainText *result = new PreviewResultPlainText(documentPath, page);
|
|
QFile file(documentPath);
|
|
if(!file.open(QFile::ReadOnly | QFile::Text))
|
|
{
|
|
return QSharedPointer<PreviewResultPlainText>(result);
|
|
}
|
|
QTextStream in(&file);
|
|
|
|
QString resulText = "";
|
|
QString content = in.readAll();
|
|
QMap<int, QString> snippet;
|
|
|
|
int coveredRange = 0;
|
|
|
|
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 && currentSnippets < maxSnippets)
|
|
{
|
|
countmap[word] = countmap.value(word, 0) + 1;
|
|
|
|
if(index >= lastWordPos && index <= coveredRange)
|
|
{
|
|
break;
|
|
}
|
|
int begin = index - 50;
|
|
if(begin < 0)
|
|
{
|
|
begin = 0;
|
|
}
|
|
int after = index + 50;
|
|
if(after > content.size())
|
|
{
|
|
after = content.size();
|
|
}
|
|
|
|
snippet[index] = "...<br>" + content.mid(begin, after) + "...<br>";
|
|
coveredRange = after;
|
|
lastPos = index;
|
|
|
|
index = content.indexOf(word, lastPos + 1, Qt::CaseInsensitive);
|
|
++currentSnippets;
|
|
}
|
|
lastWordPos = lastPos;
|
|
}
|
|
|
|
auto i = snippet.constBegin();
|
|
while(i != snippet.constEnd())
|
|
{
|
|
resulText.append(i.value());
|
|
++i;
|
|
}
|
|
|
|
for(QString &word : config.wordsToHighlight)
|
|
{
|
|
resulText.replace(word, "<span style=\"background-color: yellow;\">" + word + "</span>", Qt::CaseInsensitive);
|
|
}
|
|
|
|
QFileInfo info{documentPath};
|
|
|
|
QString header = "<b>" + info.fileName() + "</b> ";
|
|
for(QString &word : config.wordsToHighlight)
|
|
{
|
|
header += word + ": " + QString::number(countmap[word]) + " ";
|
|
}
|
|
if(currentSnippets == maxSnippets)
|
|
{
|
|
header += "(truncated)";
|
|
}
|
|
|
|
header += "<hr>";
|
|
|
|
result->setText(header + resulText.replace("\n", "<br>").mid(0, 1000));
|
|
return QSharedPointer<PreviewResultPlainText>(result);
|
|
}
|