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

这个提交包含在:
Albert S. 2022-08-28 12:44:42 +02:00
父节点 5a47f5949f
当前提交 2df273dee3
共有 2 个文件被更改,包括 22 次插入3 次删除

查看文件

@ -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);

查看文件

@ -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;