#include "Manager.h" #include "dialog_progress_ui.h" #include "ui_dialog_progress_ui.h" #include "lang/app_language.h" #include "huagao/hgscanner_error.h" #include #include #include extern Manager* g_manager; Dialog_progress_ui::Dialog_progress_ui(class Manager *mgr, std::function callback, std::function *notify, QWidget *parent) : QDialog(parent), ui(new Ui::Dialog_progress_ui) , m_imageRecivedCount(0) , m_isScanning(false) , m_thread(nullptr) , m_thread_cancelScan(nullptr) { ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose, true); #if defined(HG_CMP_MSC) setWindowFlags(Qt::SubWindow | Qt::Popup | Qt::WindowStaysOnTopHint); #else setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint); #endif ui->pbtn_cancelScan->setVisible(false); ui->pbtn_close->setVisible(false); ui->label->setText(tr("start scanning")); ui->label_imgRecived->setVisible(false); ui->lineEdit_imgRecived->setVisible(false); ui->label_totalImgRecived->setVisible(false); ui->lineEdit_imgRecived->setText(QString::number(m_imageRecivedCount)); m_mgr = mgr; m_callback = callback; *notify = FuncNotify; m_timer = new QTimer(this); connect(this, SIGNAL(scan_status(QString,bool)), this, SLOT(on_scan_status(QString,bool)), Qt::QueuedConnection); connect(this, SIGNAL(scan_finish(int, QString)), this, SLOT(on_scan_finish(int, QString)), Qt::QueuedConnection); connect(this, SIGNAL(image_recived()), this, SLOT(on_image_recived()), Qt::QueuedConnection); } Dialog_progress_ui::~Dialog_progress_ui() { HGBase_CloseThread(m_thread); m_thread = nullptr; HGBase_CloseThread(m_thread_cancelScan); m_thread_cancelScan = nullptr; m_mgr->m_progressUi = nullptr; delete ui; } void Dialog_progress_ui::keyPressEvent(QKeyEvent *e) { if (e->key() == Qt::Key_Escape) { e->ignore(); } } void Dialog_progress_ui::closeEvent(QCloseEvent* e) { if (e->spontaneous()) { e->ignore(); } } void Dialog_progress_ui::on_pbtn_cancelScan_clicked() { if (!m_isScanning) return; if (!quit_) { HGBase_OpenThread(CancelScanThreadFunc, this, &m_thread_cancelScan); } ui->pbtn_cancelScan->setVisible(false); ui->label->setText(tr("stop scanning")); } void Dialog_progress_ui::on_pbtn_close_clicked() { disconnect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked())); if (!quit_) { HGBase_OpenThread(ThreadFunc, this, &m_thread); } close(); } void Dialog_progress_ui::on_scan_status(QString finishInfo, bool isScanning) { m_isScanning = isScanning; ui->label_imgRecived->setVisible(isScanning); ui->lineEdit_imgRecived->setVisible(isScanning); ui->label->setText(finishInfo); ui->pbtn_cancelScan->setVisible(isScanning); ui->pbtn_close->setVisible(!isScanning); } void Dialog_progress_ui::on_scan_finish(int flag, QString finishInfo) { on_scan_status(finishInfo, false); ui->label_totalImgRecived->setVisible(true); ui->label_totalImgRecived->setText(tr("Total scanned images: %1").arg(QString::number(m_imageRecivedCount))); m_imageRecivedCount = 0; if (SCANNER_ERR_OK == flag || SCANNER_ERR_USER_CANCELED == flag) { m_timer->start(1000); connect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked()), Qt::QueuedConnection); } } void Dialog_progress_ui::on_image_recived() { m_imageRecivedCount++; ui->lineEdit_imgRecived->setText(QString::number(m_imageRecivedCount)); } void HGAPI Dialog_progress_ui::ThreadFunc(HGThread thread, HGPointer param) { #if defined(HG_CMP_MSC) Sleep(500); #else usleep(500 * 1000); #endif Dialog_progress_ui* p = (Dialog_progress_ui*)param; if (p->m_callback) p->m_callback(UI_RESULT_CLOSE_NORMAL); } void HGAPI Dialog_progress_ui::CancelScanThreadFunc(HGThread thread, HGPointer param) { Dialog_progress_ui* p = (Dialog_progress_ui*)param; if (p->m_callback) p->m_callback(UI_RESULT_CLOSE_CANCEL); } void Dialog_progress_ui::FuncNotify(int event, void *msg, int flag) { Dialog_progress_ui *p = (Dialog_progress_ui *)g_manager->m_progressUi; if (nullptr == p) { return; } switch (event) { case SANE_EVENT_WORKING: { QString finishInfo; if (0 != flag) { finishInfo = (char*)msg; } else { finishInfo = tr("Start scan..."); } emit p->scan_status(finishInfo, true); } break; case SANE_EVENT_SCAN_FINISHED: { QString finishInfo; if (0 != flag) { finishInfo = (char*)msg; } else { finishInfo = tr("Scan completed"); } emit p->scan_finish(flag, finishInfo); } break; case SANE_EVENT_IMAGE_OK: { emit p->image_recived(); } break; } }