looqs/gui/ipcpreviewworker.cpp

59 linhas
1.5 KiB
C++
Original Visão normal Histórico

2022-05-27 09:26:03 +02:00
#include <QtConcurrent>
#include "ipcpreviewworker.h"
#include "previewgeneratormapfunctor.h"
IPCPreviewWorker::IPCPreviewWorker(QLocalSocket *peer)
2022-05-27 09:26:03 +02:00
{
this->peer = peer;
this->connect(&previewWorkerWatcher, &QFutureWatcher<QByteArray>::resultReadyAt, this,
[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
}
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();
}