From fe8da94951f0f91bde1b2bd3b5cc2a0bd0ee7c43 Mon Sep 17 00:00:00 2001 From: Albert Schwarzkopf Date: Tue, 6 Dec 2022 11:55:15 +0100 Subject: [PATCH] gui: Introduce PreviewCoordinator Move some preview generation logic to PreviewCoordinator --- gui/gui.pro | 2 + gui/previewcoordinator.cpp | 97 ++++++++++++++++++++++++++++++++++++++ gui/previewcoordinator.h | 48 +++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 gui/previewcoordinator.cpp create mode 100644 gui/previewcoordinator.h diff --git a/gui/gui.pro b/gui/gui.pro index cb515f7..0b890c5 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -34,6 +34,7 @@ SOURCES += \ main.cpp \ mainwindow.cpp \ clicklabel.cpp \ + previewcoordinator.cpp \ previewgenerator.cpp \ previewgeneratormapfunctor.cpp \ previewgeneratorodt.cpp \ @@ -54,6 +55,7 @@ HEADERS += \ ipcserver.h \ mainwindow.h \ clicklabel.h \ + previewcoordinator.h \ previewgenerator.h \ previewgeneratormapfunctor.h \ previewgeneratorodt.h \ diff --git a/gui/previewcoordinator.cpp b/gui/previewcoordinator.cpp new file mode 100644 index 0000000..0bc9843 --- /dev/null +++ b/gui/previewcoordinator.cpp @@ -0,0 +1,97 @@ +#include "previewcoordinator.h" +#include + +PreviewCoordinator::PreviewCoordinator() +{ + this->ipcPreviewClient.moveToThread(&this->ipcClientThread); + + connect(&ipcPreviewClient, &IPCPreviewClient::previewReceived, this, &PreviewCoordinator::handleReceivedPreview, + Qt::QueuedConnection); + connect(&ipcPreviewClient, &IPCPreviewClient::finished, this, [&] { emit completedGeneration(); }); + connect(this, &PreviewCoordinator::ipcStartGeneration, &ipcPreviewClient, &IPCPreviewClient::startGeneration, + Qt::QueuedConnection); + + this->ipcClientThread.start(); +} + +void PreviewCoordinator::init(const QVector &searchResults) +{ + this->previewableSearchResults.clear(); + for(const SearchResult &result : searchResults) + { + if(result.wasContentSearch) + { + QString path = result.fileData.absPath; + // HACK until we can preview them properly + if(path.endsWith(".html") || path.endsWith(".htm")) + { + continue; + } + QFileInfo info{path}; + if(info.exists()) + { + this->previewableSearchResults.append(result); + } + } + } +} + +void PreviewCoordinator::setSocketPath(QString socketPath) +{ + this->socketPath = socketPath; + this->ipcPreviewClient.setSocketPath(socketPath); +} + +int PreviewCoordinator::previewableCount() const +{ + return this->previewableSearchResults.count(); +} + +QSharedPointer PreviewCoordinator::resultAt(int index) +{ + if(this->previewResults.size() > index) + { + return {this->previewResults[index]}; + } + return {nullptr}; +} + +const QVector &PreviewCoordinator::getPreviewableSearchResults() const +{ + return this->previewableSearchResults; +} + +void PreviewCoordinator::handleReceivedPreview(QSharedPointer preview, unsigned int previewGeneration) +{ + if(previewGeneration < this->currentPreviewGeneration) + { + return; + } + if(!preview.isNull() && preview->hasPreview()) + { + QString docPath = preview->getDocumentPath(); + auto previewPage = preview->getPage(); + int pos = previewOrder[docPath + QString::number(previewPage)]; + this->previewResults[pos] = preview; + emit previewReady(); + } +} + +void PreviewCoordinator::startGeneration(RenderConfig config, const QVector &targets) +{ + ++this->currentPreviewGeneration; + + this->previewOrder.clear(); + this->previewResults.clear(); + + this->previewResults.resize(targets.size()); + this->previewResults.fill(nullptr); + + int i = 0; + for(const RenderTarget &target : targets) + { + this->previewOrder[target.path + QString::number(target.page)] = i++; + } + + emit ipcStartGeneration(config, targets); +} diff --git a/gui/previewcoordinator.h b/gui/previewcoordinator.h new file mode 100644 index 0000000..9e82e4d --- /dev/null +++ b/gui/previewcoordinator.h @@ -0,0 +1,48 @@ +#ifndef PREVIEWCOORDINATOR_H +#define PREVIEWCOORDINATOR_H +#include +#include +#include +#include "searchresult.h" +#include "previewresult.h" +#include "ipcpreviewclient.h" +#include "rendertarget.h" +class PreviewCoordinator : public QObject +{ + Q_OBJECT + private: + QThread ipcClientThread; + IPCPreviewClient ipcPreviewClient; + QString socketPath; + + QVector> previewResults; + QVector previewableSearchResults; + + unsigned int currentPreviewGeneration = 1; + + /* Quick lookup table for the order a preview should have */ + QHash previewOrder; + + public: + PreviewCoordinator(); + + void init(const QVector &searchResults); + + int previewableCount() const; + const QVector &getPreviewableSearchResults() const; + + QSharedPointer resultAt(int index); + + void setSocketPath(QString socketPath); + public slots: + void startGeneration(RenderConfig config, const QVector &targets); + void handleReceivedPreview(QSharedPointer preview, unsigned int previewGeneration); + + signals: + void previewReady(); + void completedGeneration(); + void error(QString); + void ipcStartGeneration(RenderConfig config, const QVector &targets); +}; + +#endif // PREVIEWCOORDINATOR_H