From d73674937d4e311a51f7713f863e1717fa8a55d7 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 24 Apr 2022 12:30:31 +0200 Subject: [PATCH] gui: Begin support to also preview results in plain text files --- gui/gui.pro | 4 ++ gui/previewgeneratorplaintext.cpp | 81 +++++++++++++++++++++++++++++++ gui/previewgeneratorplaintext.h | 12 +++++ gui/previewresultplaintext.cpp | 30 ++++++++++++ gui/previewresultplaintext.h | 20 ++++++++ 5 files changed, 147 insertions(+) create mode 100644 gui/previewgeneratorplaintext.cpp create mode 100644 gui/previewgeneratorplaintext.h create mode 100644 gui/previewresultplaintext.cpp create mode 100644 gui/previewresultplaintext.h diff --git a/gui/gui.pro b/gui/gui.pro index 6e84438..5e1e37f 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -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 diff --git a/gui/previewgeneratorplaintext.cpp b/gui/previewgeneratorplaintext.cpp new file mode 100644 index 0000000..a0e4656 --- /dev/null +++ b/gui/previewgeneratorplaintext.cpp @@ -0,0 +1,81 @@ +#include + +#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 snippet; + + int coveredRange = 0; + + int lastWordPos = 0; + QHash 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] = "...
" + content.mid(begin, after) + "...
"; + 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, "" + word + "", Qt::CaseInsensitive); + } + + QFileInfo info{documentPath}; + + QString header = "" + info.fileName() + " "; + for(QString &word : config.wordsToHighlight) + { + header += word + ": " + QString::number(countmap[word]) + " "; + } + header += "
"; + + result->setText(header + resulText.replace("\n", "
")); + return result; +} diff --git a/gui/previewgeneratorplaintext.h b/gui/previewgeneratorplaintext.h new file mode 100644 index 0000000..be7cc00 --- /dev/null +++ b/gui/previewgeneratorplaintext.h @@ -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 diff --git a/gui/previewresultplaintext.cpp b/gui/previewresultplaintext.cpp new file mode 100644 index 0000000..3e2a916 --- /dev/null +++ b/gui/previewresultplaintext.cpp @@ -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; +} diff --git a/gui/previewresultplaintext.h b/gui/previewresultplaintext.h new file mode 100644 index 0000000..786f201 --- /dev/null +++ b/gui/previewresultplaintext.h @@ -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