| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "OcrWindow.h" |
| 21 | |
| 22 | OcrWindow::OcrWindow(const QPixmap &pixmap, const QString &title, const QSharedPointer<IPluginManager> &pluginManager) : |
| 23 | mTextEdit(new QTextEdit(this)), |
| 24 | mProcessIndicator(new ProcessIndicator(this)), |
| 25 | mLayout(new QGridLayout(this)) |
| 26 | { |
| 27 | setWindowTitle(title); |
| 28 | setGeometry(geometry().x(), geometry().y(), pixmap.size().width(), pixmap.size().height()); |
| 29 | |
| 30 | mLayout->addWidget(mTextEdit, 0, 0); |
| 31 | mLayout->addWidget(mProcessIndicator, 0, 0, Qt::AlignCenter); |
| 32 | |
| 33 | setProcessingVisible(true); |
| 34 | |
| 35 | auto ocrProcessingFuture = QtConcurrent::run([=]() { return this->process(pixmap, pluginManager); }); |
| 36 | |
| 37 | connect(&mOcrProcessFutureWatcher, &QFutureWatcher<QString>::finished, this, &OcrWindow::processingFinished); |
| 38 | mOcrProcessFutureWatcher.setFuture(ocrProcessingFuture); |
| 39 | } |
| 40 | |
| 41 | void OcrWindow::closeEvent(QCloseEvent *event) |
| 42 | { |
| 43 | emit closeRequest(); |
| 44 | QDialog::closeEvent(event); |
| 45 | } |
| 46 | |
| 47 | QString OcrWindow::process(const QPixmap &pixmap, const QSharedPointer<IPluginManager> &pluginManager) |
| 48 | { |
| 49 | auto ocrPlugin = pluginManager->get(PluginType::Ocr).objectCast<IPluginOcr>(); |
| 50 | return ocrPlugin->recognize(pixmap); |
| 51 | } |
| 52 | |
| 53 | void OcrWindow::setProcessingVisible(bool isVisible) |
| 54 | { |
| 55 | if(isVisible) { |
| 56 | mProcessIndicator->start(); |
| 57 | } else { |
| 58 | mProcessIndicator->stop(); |
| 59 | } |
| 60 | |
| 61 | mTextEdit->setVisible(!isVisible); |
| 62 | mProcessIndicator->setVisible(isVisible); |
| 63 | } |
| 64 | |
| 65 | void OcrWindow::processingFinished() |
| 66 | { |
| 67 | setProcessingVisible(false); |
| 68 | |
| 69 | auto text = mOcrProcessFutureWatcher.future().result(); |
| 70 | mTextEdit->setText(text); |
| 71 | } |