gui: Begin support to also preview results in plain text files
This commit is contained in:
parent
59aa02f0cd
commit
d73674937d
@ -31,8 +31,10 @@ SOURCES += \
|
|||||||
previewgenerator.cpp \
|
previewgenerator.cpp \
|
||||||
previewgeneratormapfunctor.cpp \
|
previewgeneratormapfunctor.cpp \
|
||||||
previewgeneratorpdf.cpp \
|
previewgeneratorpdf.cpp \
|
||||||
|
previewgeneratorplaintext.cpp \
|
||||||
previewresult.cpp \
|
previewresult.cpp \
|
||||||
previewresultpdf.cpp \
|
previewresultpdf.cpp \
|
||||||
|
previewresultplaintext.cpp \
|
||||||
previewworker.cpp
|
previewworker.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
@ -44,8 +46,10 @@ HEADERS += \
|
|||||||
previewgenerator.h \
|
previewgenerator.h \
|
||||||
previewgeneratormapfunctor.h \
|
previewgeneratormapfunctor.h \
|
||||||
previewgeneratorpdf.h \
|
previewgeneratorpdf.h \
|
||||||
|
previewgeneratorplaintext.h \
|
||||||
previewresult.h \
|
previewresult.h \
|
||||||
previewresultpdf.h \
|
previewresultpdf.h \
|
||||||
|
previewresultplaintext.h \
|
||||||
previewworker.h \
|
previewworker.h \
|
||||||
renderconfig.h
|
renderconfig.h
|
||||||
|
|
||||||
|
81
gui/previewgeneratorplaintext.cpp
Normal file
81
gui/previewgeneratorplaintext.cpp
Normal 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;
|
||||||
|
}
|
12
gui/previewgeneratorplaintext.h
Normal file
12
gui/previewgeneratorplaintext.h
Normal 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
|
30
gui/previewresultplaintext.cpp
Normal file
30
gui/previewresultplaintext.cpp
Normal 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;
|
||||||
|
}
|
20
gui/previewresultplaintext.h
Normal file
20
gui/previewresultplaintext.h
Normal 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
|
Loading…
Reference in New Issue
Block a user