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 thread 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 "textconvertertask.h" 0017 0018 // Qt includes 0019 0020 #include <QPointer> 0021 0022 // Local includes 0023 0024 #include "digikam_debug.h" 0025 #include "ocrtesseractengine.h" 0026 0027 using namespace Digikam; 0028 0029 namespace DigikamGenericTextConverterPlugin 0030 { 0031 0032 class TextConverterTask::Private 0033 0034 { 0035 public: 0036 0037 Private() 0038 : action (TextConverterAction()) 0039 { 0040 } 0041 0042 OcrOptions opt; 0043 0044 QUrl url; 0045 TextConverterAction action; 0046 0047 QPointer<OcrTesseractEngine> ocrEngine; 0048 }; 0049 0050 TextConverterTask::TextConverterTask(QObject* const parent, 0051 const QUrl& fileUrl, 0052 const TextConverterAction& action) 0053 : ActionJob(parent), 0054 d (new Private) 0055 { 0056 d->url = fileUrl; 0057 d->action = action; 0058 } 0059 0060 TextConverterTask::~TextConverterTask() 0061 { 0062 slotCancel(); 0063 0064 delete d->ocrEngine; 0065 delete d; 0066 } 0067 0068 void TextConverterTask::setOcrOptions(const OcrOptions& opt) 0069 { 0070 d->opt = opt; 0071 } 0072 0073 OcrOptions TextConverterTask::ocrOptions() const 0074 { 0075 return d->opt; 0076 } 0077 0078 void TextConverterTask::run() 0079 { 0080 if (m_cancel) 0081 { 0082 return; 0083 } 0084 0085 switch (d->action) 0086 { 0087 case PROCESS: 0088 { 0089 TextConverterActionData ad1; 0090 ad1.action = PROCESS; 0091 ad1.fileUrl = d->url; 0092 ad1.starting = true; 0093 0094 Q_EMIT signalStarting(ad1); 0095 0096 d->ocrEngine = new OcrTesseractEngine; 0097 d->ocrEngine->setInputFile(d->url.toLocalFile()); 0098 d->ocrEngine->setOcrOptions(d->opt); 0099 int ret = d->ocrEngine->runOcrProcess(); 0100 0101 TextConverterActionData ad2; 0102 ad2.action = PROCESS; 0103 ad2.fileUrl = d->url; 0104 ad2.destPath = d->ocrEngine->outputFile(); 0105 ad2.result = ret; 0106 ad2.outputText = d->ocrEngine->outputText(); 0107 0108 Q_EMIT signalFinished(ad2); 0109 0110 break; 0111 } 0112 0113 default: 0114 { 0115 qCritical(DIGIKAM_GENERAL_LOG) << "Unknown action specified"; 0116 break; 0117 } 0118 } 0119 0120 Q_EMIT signalDone(); 0121 } 0122 0123 void TextConverterTask::slotCancel() 0124 { 0125 if (d->ocrEngine) 0126 { 0127 d->ocrEngine->cancelOcrProcess(); 0128 } 0129 } 0130 0131 } // namespace DigikamGenericTextConverterPlugin 0132 0133 #include "moc_textconvertertask.cpp"