diff --git a/YACReader/render.cpp b/YACReader/render.cpp index ef9370887..d4cb29be7 100644 --- a/YACReader/render.cpp +++ b/YACReader/render.cpp @@ -741,22 +741,10 @@ void Render::loadComic(const QString &path, int atPage) void Render::startLoad() { - QThread *thread = nullptr; - - thread = new QThread(); - - comic->moveToThread(thread); - - connect(comic, SIGNAL(errorOpening()), thread, SLOT(quit()), Qt::QueuedConnection); - connect(comic, SIGNAL(errorOpening(QString)), thread, SLOT(quit()), Qt::QueuedConnection); - connect(comic, SIGNAL(imagesLoaded()), thread, SLOT(quit()), Qt::QueuedConnection); - connect(comic, SIGNAL(destroyed()), thread, SLOT(quit()), Qt::QueuedConnection); - connect(comic, SIGNAL(invalidated()), thread, SLOT(quit()), Qt::QueuedConnection); - connect(thread, SIGNAL(started()), comic, SLOT(process())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - - if (thread != nullptr) - thread->start(); + const auto thread = new QThread(); + comic->moveAndConnectToThread(thread); + connect(comic, &QObject::destroyed, thread, &QThread::quit, Qt::QueuedConnection); + thread->start(); invalidate(); loadedComic = true; diff --git a/YACReaderLibrary/server/controllers/v1/comiccontroller.cpp b/YACReaderLibrary/server/controllers/v1/comiccontroller.cpp index 91d6e1e40..63df86d37 100644 --- a/YACReaderLibrary/server/controllers/v1/comiccontroller.cpp +++ b/YACReaderLibrary/server/controllers/v1/comiccontroller.cpp @@ -55,22 +55,10 @@ void ComicController::service(HttpRequest &request, HttpResponse &response) Comic *comicFile = FactoryComic::newComic(libraries.getPath(libraryId) + comic.path); if (comicFile != nullptr) { - QThread *thread = nullptr; - - thread = new QThread(); - - comicFile->moveToThread(thread); - - connect(comicFile, SIGNAL(errorOpening()), thread, SLOT(quit())); - connect(comicFile, SIGNAL(errorOpening(QString)), thread, SLOT(quit())); - connect(comicFile, SIGNAL(imagesLoaded()), thread, SLOT(quit())); - connect(thread, SIGNAL(started()), comicFile, SLOT(process())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - + const auto thread = new QThread(); + comicFile->moveAndConnectToThread(thread); comicFile->load(libraries.getPath(libraryId) + comic.path); - - if (thread != nullptr) - thread->start(); + thread->start(); if (remoteComic) { QLOG_TRACE() << "remote comic requested"; diff --git a/YACReaderLibrary/server/controllers/v2/comiccontroller_v2.cpp b/YACReaderLibrary/server/controllers/v2/comiccontroller_v2.cpp index 273d275d5..5c2fa4039 100644 --- a/YACReaderLibrary/server/controllers/v2/comiccontroller_v2.cpp +++ b/YACReaderLibrary/server/controllers/v2/comiccontroller_v2.cpp @@ -57,22 +57,10 @@ void ComicControllerV2::service(HttpRequest &request, HttpResponse &response) Comic *comicFile = FactoryComic::newComic(libraries.getPath(libraryId) + comic.path); if (comicFile != nullptr) { - QThread *thread = nullptr; - - thread = new QThread(); - - comicFile->moveToThread(thread); - - connect(comicFile, SIGNAL(errorOpening()), thread, SLOT(quit())); - connect(comicFile, SIGNAL(errorOpening(QString)), thread, SLOT(quit())); - connect(comicFile, SIGNAL(imagesLoaded()), thread, SLOT(quit())); - connect(thread, SIGNAL(started()), comicFile, SLOT(process())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - + const auto thread = new QThread(); + comicFile->moveAndConnectToThread(thread); comicFile->load(libraries.getPath(libraryId) + comic.path); - - if (thread != nullptr) - thread->start(); + thread->start(); if (remoteComic) { QLOG_TRACE() << "remote comic requested"; diff --git a/common/comic.cpp b/common/comic.cpp index 4bc1fd5c7..4f8841bb9 100644 --- a/common/comic.cpp +++ b/common/comic.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -120,7 +121,6 @@ Comic::Comic(const QString &pathFile, int atPage) //----------------------------------------------------------------------------- Comic::~Comic() { - emit destroyed(); delete bm; } //----------------------------------------------------------------------------- @@ -340,6 +340,23 @@ QList Comic::findValidComicFilesInFolder(const QString &path) return validComicFiles; } +void Comic::moveAndConnectToThread(QThread *thread) +{ + Q_ASSERT(thread); + moveToThread(thread); + + void (Comic::*errorOpening0)() = &Comic::errorOpening; + void (Comic::*errorOpening1)(QString) = &Comic::errorOpening; + + connect(this, errorOpening0, thread, &QThread::quit, Qt::QueuedConnection); + connect(this, errorOpening1, thread, &QThread::quit, Qt::QueuedConnection); + connect(this, &Comic::imagesLoaded, thread, &QThread::quit, Qt::QueuedConnection); + connect(this, &Comic::invalidated, thread, &QThread::quit, Qt::QueuedConnection); + + connect(thread, &QThread::started, this, &Comic::process); + connect(thread, &QThread::finished, thread, &QObject::deleteLater); +} + //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -652,10 +669,6 @@ FolderComic::FolderComic(const QString &path, int atPage) load(path, atPage); } -FolderComic::~FolderComic() -{ -} - bool FolderComic::load(const QString &path, int atPage) { _path = path; @@ -748,10 +761,6 @@ PDFComic::PDFComic(const QString &path, int atPage) load(path, atPage); } -PDFComic::~PDFComic() -{ -} - bool PDFComic::load(const QString &path, int atPage) { QFileInfo fi(path); diff --git a/common/comic.h b/common/comic.h index 3174746af..e1f6979eb 100644 --- a/common/comic.h +++ b/common/comic.h @@ -12,6 +12,7 @@ #include "pdf_comic.h" #endif //NO_PDF class ComicDB; +class QThread; //#define EXTENSIONS << "*.jpg" << "*.jpeg" << "*.png" << "*.gif" << "*.tiff" << "*.tif" << "*.bmp" Comic::getSupportedImageFormats() //#define EXTENSIONS_LITERAL << ".jpg" << ".jpeg" << ".png" << ".gif" << ".tiff" << ".tif" << ".bmp" //Comic::getSupportedImageLiteralFormats() class Comic : public QObject @@ -52,7 +53,7 @@ class Comic : public QObject //Constructors Comic(); Comic(const QString &pathFile, int atPage = -1); - ~Comic(); + ~Comic() override; void setup(); //Load pages from file virtual bool load(const QString &path, int atPage = -1) = 0; @@ -83,6 +84,8 @@ class Comic : public QObject static QList findValidComicFiles(const QList &list); static QList findValidComicFilesInFolder(const QString &path); + void moveAndConnectToThread(QThread *thread); + public slots: void loadFinished(); void setBookmark(); @@ -93,9 +96,10 @@ public slots: void setPageLoaded(int page); void invalidate(); + virtual void process() = 0; + signals: void invalidated(); - void destroyed(); void imagesLoaded(); void imageLoaded(int index); void imageLoaded(int index, const QByteArray &image); @@ -121,39 +125,34 @@ class FileComic : public Comic, public ExtractDelegate public: FileComic(); FileComic(const QString &path, int atPage = -1); - ~FileComic(); - virtual bool load(const QString &path, int atPage = -1); - virtual bool load(const QString &path, const ComicDB &comic); + ~FileComic() override; + bool load(const QString &path, int atPage = -1) final; + bool load(const QString &path, const ComicDB &comic) final; static QList filter(const QList &src); //ExtractDelegate - void fileExtracted(int index, const QByteArray &rawData); - void crcError(int index); - void unknownError(int index); - bool isCancelled(); + void fileExtracted(int index, const QByteArray &rawData) override; + void crcError(int index) override; + void unknownError(int index) override; + bool isCancelled() override; public slots: - void process(); + void process() override; }; class FolderComic : public Comic { Q_OBJECT -private: - //void run(); - public: FolderComic(); FolderComic(const QString &path, int atPage = -1); - ~FolderComic(); - virtual bool load(const QString &path, int atPage = -1); + bool load(const QString &path, int atPage = -1) final; public slots: - - void process(); + void process() override; }; #ifndef NO_PDF @@ -171,19 +170,16 @@ class PDFComic : public Comic Poppler::Document *pdfComic; #endif void renderPage(int page); - //void run(); public: PDFComic(); PDFComic(const QString &path, int atPage = -1); - ~PDFComic(); - virtual bool load(const QString &path, int atPage = -1); - virtual bool load(const QString &path, const ComicDB &comic); + bool load(const QString &path, int atPage = -1) final; + bool load(const QString &path, const ComicDB &comic) final; public slots: - - void process(); + void process() override; }; #endif //NO_PDF class FactoryComic