2023-06-28 10:14:37 +00:00
|
|
|
#include "Manager.h"
|
2023-04-20 09:49:48 +00:00
|
|
|
#include "dialog_progress_ui.h"
|
|
|
|
#include "ui_dialog_progress_ui.h"
|
2023-04-23 01:25:52 +00:00
|
|
|
#include "lang/app_language.h"
|
2023-05-08 12:01:43 +00:00
|
|
|
#include "huagao/hgscanner_error.h"
|
2023-05-30 02:46:09 +00:00
|
|
|
#include <QKeyEvent>
|
2023-04-27 03:04:52 +00:00
|
|
|
#include <QTimer>
|
2023-05-11 13:07:30 +00:00
|
|
|
#include <thread>
|
2023-04-20 09:49:48 +00:00
|
|
|
|
2023-06-28 10:14:37 +00:00
|
|
|
extern Manager* g_manager;
|
2023-05-26 09:25:01 +00:00
|
|
|
|
2023-06-28 10:14:37 +00:00
|
|
|
Dialog_progress_ui::Dialog_progress_ui(class Manager *mgr, std::function<void (ui_result)> callback, std::function<void (int, void *, int)> *notify, QWidget *parent) :
|
2023-04-20 09:49:48 +00:00
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::Dialog_progress_ui)
|
2023-08-25 06:26:39 +00:00
|
|
|
, m_imageRecivedCount(0)
|
|
|
|
, m_isScanning(false)
|
2023-08-22 06:38:32 +00:00
|
|
|
, m_thread(nullptr)
|
2023-11-09 06:57:51 +00:00
|
|
|
, m_thread_cancelScan(nullptr)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-05-19 10:41:49 +00:00
|
|
|
|
2023-10-20 08:05:50 +00:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose, true);
|
2023-08-22 08:06:16 +00:00
|
|
|
#if defined(HG_CMP_MSC)
|
2023-05-26 09:25:01 +00:00
|
|
|
setWindowFlags(Qt::SubWindow | Qt::Popup | Qt::WindowStaysOnTopHint);
|
2023-08-22 08:06:16 +00:00
|
|
|
#else
|
|
|
|
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
|
|
|
|
#endif
|
2023-04-20 09:49:48 +00:00
|
|
|
|
2023-04-25 06:46:50 +00:00
|
|
|
ui->pbtn_cancelScan->setVisible(false);
|
|
|
|
ui->pbtn_close->setVisible(false);
|
2023-05-29 03:04:12 +00:00
|
|
|
ui->label->setText(tr("start scanning"));
|
2023-04-25 06:46:50 +00:00
|
|
|
|
2024-03-12 07:53:06 +00:00
|
|
|
ui->label_imgRecived->setText(tr("image recived:") + QString::number(m_imageRecivedCount));
|
|
|
|
ui->label_imgRecived->setAlignment(Qt::AlignCenter);
|
2023-05-26 09:25:01 +00:00
|
|
|
|
2023-06-28 10:14:37 +00:00
|
|
|
m_mgr = mgr;
|
2023-04-20 09:49:48 +00:00
|
|
|
m_callback = callback;
|
|
|
|
*notify = FuncNotify;
|
2023-05-10 12:30:07 +00:00
|
|
|
|
2023-04-27 03:04:52 +00:00
|
|
|
m_timer = new QTimer(this);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(scan_status(QString,bool)), this, SLOT(on_scan_status(QString,bool)), Qt::QueuedConnection);
|
2023-05-24 06:40:06 +00:00
|
|
|
connect(this, SIGNAL(scan_finish(int, QString)), this, SLOT(on_scan_finish(int, QString)), Qt::QueuedConnection);
|
2023-05-26 09:25:01 +00:00
|
|
|
connect(this, SIGNAL(image_recived()), this, SLOT(on_image_recived()), Qt::QueuedConnection);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dialog_progress_ui::~Dialog_progress_ui()
|
|
|
|
{
|
2023-08-22 06:38:32 +00:00
|
|
|
HGBase_CloseThread(m_thread);
|
|
|
|
m_thread = nullptr;
|
2023-11-09 06:57:51 +00:00
|
|
|
|
|
|
|
HGBase_CloseThread(m_thread_cancelScan);
|
|
|
|
m_thread_cancelScan = nullptr;
|
|
|
|
|
2023-06-28 10:14:37 +00:00
|
|
|
m_mgr->m_progressUi = nullptr;
|
2023-05-12 10:26:23 +00:00
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
delete ui;
|
2023-05-10 12:30:07 +00:00
|
|
|
}
|
2023-04-23 01:25:52 +00:00
|
|
|
|
2023-05-30 02:46:09 +00:00
|
|
|
void Dialog_progress_ui::keyPressEvent(QKeyEvent *e)
|
|
|
|
{
|
|
|
|
if (e->key() == Qt::Key_Escape)
|
|
|
|
{
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 06:38:32 +00:00
|
|
|
void Dialog_progress_ui::closeEvent(QCloseEvent* e)
|
|
|
|
{
|
|
|
|
if (e->spontaneous())
|
|
|
|
{
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
void Dialog_progress_ui::on_pbtn_cancelScan_clicked()
|
|
|
|
{
|
2023-11-10 07:28:01 +00:00
|
|
|
if (!m_isScanning)
|
|
|
|
return;
|
|
|
|
|
2023-05-24 06:40:06 +00:00
|
|
|
if (!quit_)
|
2023-11-09 06:57:51 +00:00
|
|
|
{
|
|
|
|
HGBase_OpenThread(CancelScanThreadFunc, this, &m_thread_cancelScan);
|
|
|
|
}
|
|
|
|
|
2023-05-29 03:04:12 +00:00
|
|
|
ui->pbtn_cancelScan->setVisible(false);
|
|
|
|
ui->label->setText(tr("stop scanning"));
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 06:46:50 +00:00
|
|
|
void Dialog_progress_ui::on_pbtn_close_clicked()
|
|
|
|
{
|
2023-04-27 03:04:52 +00:00
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked()));
|
2023-08-22 06:38:32 +00:00
|
|
|
|
2023-05-24 06:40:06 +00:00
|
|
|
if (!quit_)
|
|
|
|
{
|
2023-08-22 06:38:32 +00:00
|
|
|
HGBase_OpenThread(ThreadFunc, this, &m_thread);
|
2023-05-24 06:40:06 +00:00
|
|
|
}
|
2023-08-22 06:38:32 +00:00
|
|
|
|
2023-04-25 06:46:50 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2023-04-27 03:04:52 +00:00
|
|
|
void Dialog_progress_ui::on_scan_status(QString finishInfo, bool isScanning)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
m_isScanning = isScanning;
|
2023-05-26 09:25:01 +00:00
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
ui->label->setText(finishInfo);
|
2023-04-27 03:04:52 +00:00
|
|
|
ui->pbtn_cancelScan->setVisible(isScanning);
|
|
|
|
ui->pbtn_close->setVisible(!isScanning);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 06:40:06 +00:00
|
|
|
void Dialog_progress_ui::on_scan_finish(int flag, QString finishInfo)
|
2023-04-25 06:46:50 +00:00
|
|
|
{
|
2023-05-24 06:40:06 +00:00
|
|
|
on_scan_status(finishInfo, false);
|
2023-05-26 09:25:01 +00:00
|
|
|
|
2024-03-12 07:53:06 +00:00
|
|
|
QString info = tr("Total scanned images: %1").arg(QString::number(m_imageRecivedCount));
|
|
|
|
ui->label_imgRecived->setText(info);
|
|
|
|
ui->label_imgRecived->repaint();
|
|
|
|
this->repaint();
|
2023-08-25 06:26:39 +00:00
|
|
|
|
|
|
|
m_imageRecivedCount = 0;
|
2023-05-26 09:25:01 +00:00
|
|
|
|
2023-05-08 14:27:56 +00:00
|
|
|
if (SCANNER_ERR_OK == flag || SCANNER_ERR_USER_CANCELED == flag)
|
2023-05-08 12:01:43 +00:00
|
|
|
{
|
2023-06-01 06:57:39 +00:00
|
|
|
m_timer->start(1000);
|
2023-05-08 12:01:43 +00:00
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked()), Qt::QueuedConnection);
|
|
|
|
}
|
2023-04-25 06:46:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 09:25:01 +00:00
|
|
|
void Dialog_progress_ui::on_image_recived()
|
|
|
|
{
|
2023-08-25 06:26:39 +00:00
|
|
|
m_imageRecivedCount++;
|
2024-03-12 07:53:06 +00:00
|
|
|
ui->label_imgRecived->setText(tr("image recived:") + QString::number(m_imageRecivedCount));
|
|
|
|
ui->label_imgRecived->repaint();
|
|
|
|
this->repaint();
|
2023-05-26 09:25:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 06:38:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-09 06:57:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
void Dialog_progress_ui::FuncNotify(int event, void *msg, int flag)
|
|
|
|
{
|
2023-06-28 10:14:37 +00:00
|
|
|
Dialog_progress_ui *p = (Dialog_progress_ui *)g_manager->m_progressUi;
|
2023-05-10 12:30:07 +00:00
|
|
|
if (nullptr == p)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-04-20 09:49:48 +00:00
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
2023-04-25 06:46:50 +00:00
|
|
|
case SANE_EVENT_WORKING:
|
|
|
|
{
|
2023-08-26 07:07:18 +00:00
|
|
|
QString finishInfo;
|
2023-10-28 08:31:21 +00:00
|
|
|
if (0 != flag)
|
2023-08-26 07:07:18 +00:00
|
|
|
{
|
|
|
|
finishInfo = (char*)msg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finishInfo = tr("Start scan...");
|
|
|
|
}
|
|
|
|
|
2023-04-27 03:04:52 +00:00
|
|
|
emit p->scan_status(finishInfo, true);
|
2023-04-25 06:46:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
case SANE_EVENT_SCAN_FINISHED:
|
|
|
|
{
|
2023-08-26 07:07:18 +00:00
|
|
|
QString finishInfo;
|
2023-10-28 08:31:21 +00:00
|
|
|
if (0 != flag)
|
2023-08-26 07:07:18 +00:00
|
|
|
{
|
|
|
|
finishInfo = (char*)msg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finishInfo = tr("Scan completed");
|
|
|
|
}
|
|
|
|
|
2023-05-24 06:40:06 +00:00
|
|
|
emit p->scan_finish(flag, finishInfo);
|
2023-05-26 09:25:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SANE_EVENT_IMAGE_OK:
|
|
|
|
{
|
|
|
|
emit p->image_recived();
|
|
|
|
}
|
|
|
|
break;
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
}
|