File indexing completed on 2025-01-19 03:52:37

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2022-08-26
0007  * Description : Text Converter threads manager
0008  *
0009  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2022      by Quoc Hung Tran <quochungtran1999 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "textconverterthread.h"
0017 
0018 // Local includes
0019 
0020 #include "digikam_debug.h"
0021 #include "textconvertertask.h"
0022 
0023 using namespace Digikam;
0024 
0025 namespace DigikamGenericTextConverterPlugin
0026 {
0027 
0028 class TextConverterActionThread::Private
0029 {
0030 public:
0031 
0032     Private()
0033     {
0034     }
0035 
0036     OcrOptions opt;
0037 };
0038 
0039 TextConverterActionThread::TextConverterActionThread(QObject* const parent)
0040     : ActionThreadBase(parent),
0041       d               (new Private)
0042 {
0043     qRegisterMetaType<TextConverterActionData>();
0044 }
0045 
0046 TextConverterActionThread::~TextConverterActionThread()
0047 {
0048     // cancel the thread
0049 
0050     cancel();
0051 
0052     // wait for the thread to finish
0053 
0054     wait();
0055 
0056     delete d;
0057 }
0058 
0059 void TextConverterActionThread::setOcrOptions(const OcrOptions& opt)
0060 {
0061     d->opt = opt;
0062 
0063     if (!opt.multicores)
0064     {
0065         setMaximumNumberOfThreads(1);
0066     }
0067 }
0068 
0069 OcrOptions TextConverterActionThread::ocrOptions() const
0070 {
0071     return d->opt;
0072 }
0073 
0074 void TextConverterActionThread::ocrProcessFile(const QUrl& url)
0075 {
0076     QList<QUrl> oneFile;
0077     oneFile.append(url);
0078     ocrProcessFiles(oneFile);
0079 }
0080 
0081 void TextConverterActionThread::ocrProcessFiles(const QList<QUrl>& urlList)
0082 {
0083     ActionJobCollection collection;
0084 
0085     for (QList<QUrl>::const_iterator it = urlList.constBegin() ; it != urlList.constEnd() ; ++it)
0086     {
0087         TextConverterTask* const t = new TextConverterTask(this, *it, PROCESS);
0088         t->setOcrOptions(d->opt);
0089 
0090         connect(t, SIGNAL(signalStarting(DigikamGenericTextConverterPlugin::TextConverterActionData)),
0091                 this, SIGNAL(signalStarting(DigikamGenericTextConverterPlugin::TextConverterActionData)));
0092 
0093         connect(t, SIGNAL(signalFinished(DigikamGenericTextConverterPlugin::TextConverterActionData)),
0094                 this, SIGNAL(signalFinished(DigikamGenericTextConverterPlugin::TextConverterActionData)));
0095 
0096         connect(this, SIGNAL(signalCancelTextConverterTask()),
0097                 t, SLOT(slotCancel()),
0098                 Qt::QueuedConnection);
0099 
0100         collection.insert(t, 0);
0101     }
0102 
0103     appendJobs(collection);
0104 }
0105 
0106 void TextConverterActionThread::cancel()
0107 {
0108     if (isRunning())
0109     {
0110         Q_EMIT signalCancelTextConverterTask();
0111     }
0112 
0113     ActionThreadBase::cancel();
0114 }
0115 
0116 }  // namespace DigikamGenericTextConverterPlugin
0117 
0118 #include "moc_textconverterthread.cpp"