gui: Begin support to also preview results in plain text files

This commit is contained in:
Albert S. 2022-04-24 12:30:31 +02:00
parent 59aa02f0cd
commit d73674937d
5 changed files with 147 additions and 0 deletions

View File

@ -31,8 +31,10 @@ SOURCES += \
previewgenerator.cpp \
previewgeneratormapfunctor.cpp \
previewgeneratorpdf.cpp \
previewgeneratorplaintext.cpp \
previewresult.cpp \
previewresultpdf.cpp \
previewresultplaintext.cpp \
previewworker.cpp
HEADERS += \
@ -44,8 +46,10 @@ HEADERS += \
previewgenerator.h \
previewgeneratormapfunctor.h \
previewgeneratorpdf.h \
previewgeneratorplaintext.h \
previewresult.h \
previewresultpdf.h \
previewresultplaintext.h \
previewworker.h \
renderconfig.h

View File

@ -0,0 +1,81 @@
#include <QTextStream>
#include "previewgeneratorplaintext.h"
#include "previewresultplaintext.h"
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 result;
}
QTextStream in(&file);
QString resulText = "";
QString content = in.readAll();
QMap<int, QString> snippet;
int coveredRange = 0;
int lastWordPos = 0;
QHash<QString, int> countmap;
for(QString &word : config.wordsToHighlight)
{
int lastPos = 0;
int index = content.indexOf(word, lastPos, Qt::CaseInsensitive);
while(index != -1)
{
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);
}
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]) + " ";
}
header += "<hr>";
result->setText(header + resulText.replace("\n", "<br>"));
return result;
}

View File

@ -0,0 +1,12 @@
#ifndef PREVIEWGENERATORPLAINTEXT_H
#define PREVIEWGENERATORPLAINTEXT_H
#include "previewgenerator.h"
class PreviewGeneratorPlainText : public PreviewGenerator
{
public:
using PreviewGenerator::PreviewGenerator;
PreviewResult *generate(RenderConfig config, QString documentPath, unsigned int page);
};
#endif // PREVIEWGENERATORPLAINTEXT_H

View File

@ -0,0 +1,30 @@
#include "previewresultplaintext.h"
PreviewResultPlainText::PreviewResultPlainText(const PreviewResult &o)
{
this->documentPath = o.getDocumentPath();
this->page = o.getPage();
}
QWidget *PreviewResultPlainText::createPreviewWidget()
{
ClickLabel *label = new ClickLabel();
label->setText(this->text);
label->setToolTip(getDocumentPath());
label->setStyleSheet("border: 1px solid black");
label->setMaximumWidth(768);
label->setMaximumHeight(512);
label->setTextFormat(Qt::RichText);
return label;
}
bool PreviewResultPlainText::hasPreview()
{
return !text.isEmpty();
}
void PreviewResultPlainText::setText(QString text)
{
this->text = text;
}

View File

@ -0,0 +1,20 @@
#ifndef PREVIEWRESULTPLAINTEXT_H
#define PREVIEWRESULTPLAINTEXT_H
#include "previewresult.h"
class PreviewResultPlainText : public PreviewResult
{
private:
QString text;
public:
using PreviewResult::PreviewResult;
PreviewResultPlainText(const PreviewResult &o);
QWidget *createPreviewWidget() override;
bool hasPreview() override;
void setText(QString text);
};
#endif // PREVIEWRESULTPLAINTEXT_H