gui: PreviewGeneratorPlainText: Move snippet gen to own function for reuse
This commit is contained in:
parent
ac4d7dd0a5
commit
fdbf3a7358
@ -3,19 +3,10 @@
|
|||||||
#include "previewgeneratorplaintext.h"
|
#include "previewgeneratorplaintext.h"
|
||||||
#include "previewresultplaintext.h"
|
#include "previewresultplaintext.h"
|
||||||
|
|
||||||
QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig config, QString documentPath,
|
QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderConfig config, QString fileName)
|
||||||
unsigned int page)
|
|
||||||
{
|
{
|
||||||
PreviewResultPlainText *result = new PreviewResultPlainText(documentPath, page);
|
|
||||||
QFile file(documentPath);
|
|
||||||
if(!file.open(QFile::ReadOnly | QFile::Text))
|
|
||||||
{
|
|
||||||
return QSharedPointer<PreviewResultPlainText>(result);
|
|
||||||
}
|
|
||||||
QTextStream in(&file);
|
|
||||||
|
|
||||||
QString resulText = "";
|
QString resulText = "";
|
||||||
QString content = in.readAll();
|
|
||||||
QMap<int, QString> snippet;
|
QMap<int, QString> snippet;
|
||||||
|
|
||||||
int coveredRange = 0;
|
int coveredRange = 0;
|
||||||
@ -71,9 +62,7 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
|
|||||||
resulText.replace(word, "<span style=\"background-color: yellow;\">" + word + "</span>", Qt::CaseInsensitive);
|
resulText.replace(word, "<span style=\"background-color: yellow;\">" + word + "</span>", Qt::CaseInsensitive);
|
||||||
}
|
}
|
||||||
|
|
||||||
QFileInfo info{documentPath};
|
QString header = "<b>" + fileName + "</b> ";
|
||||||
|
|
||||||
QString header = "<b>" + info.fileName() + "</b> ";
|
|
||||||
for(QString &word : config.wordsToHighlight)
|
for(QString &word : config.wordsToHighlight)
|
||||||
{
|
{
|
||||||
header += word + ": " + QString::number(countmap[word]) + " ";
|
header += word + ": " + QString::number(countmap[word]) + " ";
|
||||||
@ -85,6 +74,22 @@ QSharedPointer<PreviewResult> PreviewGeneratorPlainText::generate(RenderConfig c
|
|||||||
|
|
||||||
header += "<hr>";
|
header += "<hr>";
|
||||||
|
|
||||||
result->setText(header + resulText.replace("\n", "<br>").mid(0, 1000));
|
return header + resulText.replace("\n", "<br>").mid(0, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
QSharedPointer<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 QSharedPointer<PreviewResultPlainText>(result);
|
||||||
|
}
|
||||||
|
QTextStream in(&file);
|
||||||
|
|
||||||
|
QString content = in.readAll();
|
||||||
|
QFileInfo info{documentPath};
|
||||||
|
result->setText(generatePreviewText(content, config, info.fileName()));
|
||||||
return QSharedPointer<PreviewResultPlainText>(result);
|
return QSharedPointer<PreviewResultPlainText>(result);
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ class PreviewGeneratorPlainText : public PreviewGenerator
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using PreviewGenerator::PreviewGenerator;
|
using PreviewGenerator::PreviewGenerator;
|
||||||
|
QString generatePreviewText(QString content, RenderConfig config, QString fileName);
|
||||||
QSharedPointer<PreviewResult> generate(RenderConfig config, QString documentPath, unsigned int page);
|
QSharedPointer<PreviewResult> generate(RenderConfig config, QString documentPath, unsigned int page);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user