looqs/gui/ipcpreviewworker.cpp

59 líneas
1.5 KiB
C++

2022-05-27 09:26:03 +02:00
#include <QtConcurrent>
#include "ipcpreviewworker.h"
#include "previewgeneratormapfunctor.h"
2022-08-27 10:21:58 +02:00
IPCPreviewWorker::IPCPreviewWorker(QLocalSocket *peer)
2022-05-27 09:26:03 +02:00
{
2022-08-27 10:21:58 +02:00
this->peer = peer;
this->connect(&previewWorkerWatcher, &QFutureWatcher<QByteArray>::resultReadyAt, this,
2022-08-27 10:21:58 +02:00
[this](int index)
{
if(this->peer != nullptr)
{
QDataStream stream{this->peer};
stream << previewWorkerWatcher.resultAt(index);
this->peer->flush();
}
});
connect(&previewWorkerWatcher, &QFutureWatcher<QByteArray>::finished, this, &IPCPreviewWorker::shutdownSocket);
connect(this->peer, &QLocalSocket::disconnected, this, &IPCPreviewWorker::shutdownSocket);
2022-05-27 09:26:03 +02:00
}
2022-08-27 10:21:58 +02:00
void IPCPreviewWorker::shutdownSocket()
{
if(cleaned)
{
return;
}
cleaned = true;
if(this->peer != nullptr)
{
if(this->peer->state() == QLocalSocket::ConnectedState)
{
this->peer->flush();
this->peer->waitForBytesWritten();
this->peer->disconnectFromServer();
if(this->peer->state() != QLocalSocket::UnconnectedState)
{
this->peer->waitForDisconnected();
}
}
delete this->peer;
this->peer = nullptr;
}
emit finished();
}
void IPCPreviewWorker::start(RenderConfig config, const QVector<RenderTarget> &targets)
2022-05-27 09:26:03 +02:00
{
auto mapFunctor = PreviewGeneratorMapFunctor();
mapFunctor.setRenderConfig(config);
2022-05-27 09:26:03 +02:00
previewWorkerWatcher.setFuture(QtConcurrent::mapped(targets, mapFunctor));
2022-05-27 09:26:03 +02:00
}
void IPCPreviewWorker::stop()
{
previewWorkerWatcher.cancel();
previewWorkerWatcher.waitForFinished();
}