gui: PreviewGenerator*: Fallback to partial highlighting if no whole word match

This commit is contained in:
Albert S. 2022-08-28 12:44:42 +02:00
parent 5a47f5949f
commit 2df273dee3
2 changed files with 22 additions and 3 deletions

View File

@ -49,6 +49,10 @@ QSharedPointer<PreviewResult> PreviewGeneratorPdf::generate(RenderConfig config,
{
QList<QRectF> rects =
pdfPage->search(word, Poppler::Page::SearchFlag::IgnoreCase | Poppler::Page::SearchFlag::WholeWords);
if(rects.empty())
{
rects = pdfPage->search(word, Poppler::Page::SearchFlag::IgnoreCase);
}
for(QRectF &rect : rects)
{
QPainter painter(&img);

View File

@ -103,10 +103,25 @@ QString PreviewGeneratorPlainText::generateLineBasedPreviewText(QTextStream &in,
for(QString &word : config.wordsToHighlight)
{
QRegularExpression searchRegex("\\b" + word + "\\b");
if(line.contains(searchRegex))
bool containsRegex = line.contains(searchRegex);
bool contains = false;
if(!containsRegex)
{
contains = line.contains(word, Qt::CaseInsensitive);
}
if(containsRegex || contains)
{
currentSnippet.wordCountMap[word] = currentSnippet.wordCountMap.value(word, 0) + 1;
line.replace(searchRegex, "<span style=\"background-color: yellow;\">" + word + "</span>");
QString replacementString = "<span style=\"background-color: yellow;\">" + word + "</span>";
if(containsRegex)
{
line.replace(searchRegex, replacementString);
}
else
{
line.replace(word, replacementString, Qt::CaseInsensitive);
}
++foundWordsCount;
}
}
@ -143,7 +158,7 @@ QString PreviewGeneratorPlainText::generateLineBasedPreviewText(QTextStream &in,
}
for(QString &word : config.wordsToHighlight)
{
if(currentLine.contains(QRegularExpression("\\b" + word + "\\b")))
if(currentLine.contains(word, Qt::CaseInsensitive))
{
matched = true;
break;