code_app/modules/twainui/dialog_progress_ui.cpp

173 lines
4.7 KiB
C++

#include "dialog_progress_ui.h"
#include "ui_dialog_progress_ui.h"
#include "lang/app_language.h"
#include "huagao/hgscanner_error.h"
#include <QKeyEvent>
#include <QTimer>
#include <thread>
Dialog_progress_ui *Dialog_progress_ui::m_param = nullptr;
static int imageRecivedCount = 0;
static int imageUploadedCount = 0;
Dialog_progress_ui::Dialog_progress_ui(std::function<void (ui_result)> callback, std::function<void (int, void *, int)> *notify, QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_progress_ui)
, m_isScanning(false)
{
setAttribute(Qt::WA_DeleteOnClose, true);
ui->setupUi(this);
setWindowFlags(Qt::SubWindow | Qt::Popup | Qt::WindowStaysOnTopHint);
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(imageRecivedCount));
ui->label_imgUploaded->setVisible(false);
ui->lineEdit_imgUploaded->setVisible(false);
ui->label_totalImgUploaded->setVisible(false);
ui->lineEdit_imgUploaded->setText(QString::number(imageUploadedCount));
m_callback = callback;
*notify = FuncNotify;
m_param = this;
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_uploaded()), this, SLOT(on_image_uploaded()), Qt::QueuedConnection);
connect(this, SIGNAL(image_recived()), this, SLOT(on_image_recived()), Qt::QueuedConnection);
}
Dialog_progress_ui::~Dialog_progress_ui()
{
m_param = nullptr;
imageRecivedCount = 0;
imageUploadedCount = 0;
delete ui;
}
void Dialog_progress_ui::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape)
{
e->ignore();
}
}
void Dialog_progress_ui::on_pbtn_cancelScan_clicked()
{
if (!quit_)
m_callback(UI_RESULT_CLOSE_CANCEL);
ui->pbtn_cancelScan->setVisible(false);
ui->label->setText(tr("stop scanning"));
}
void notify_close(std::function<void (ui_result)> callback)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (callback)
callback(UI_RESULT_CLOSE_NORMAL);
}
void Dialog_progress_ui::on_pbtn_close_clicked()
{
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked()));
//CloseHandle(CreateThread(NULL, 0, notify_close, (void*)m_callback, 0, 0));
if (!quit_)
{
std::thread th_close(notify_close, m_callback);
th_close.detach();
}
// m_callback(UI_RESULT_CLOSE_NORMAL);
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_imgUploaded->setVisible(isScanning);
ui->lineEdit_imgUploaded->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_totalImgUploaded->setVisible(true);
ui->label_totalImgUploaded->setText(tr("Total scanned images: %1").arg(QString::number(imageRecivedCount)));
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()
{
ui->lineEdit_imgRecived->setText(QString::number(++imageRecivedCount));
}
void Dialog_progress_ui::on_image_uploaded()
{
ui->lineEdit_imgUploaded->setText(QString::number(++imageUploadedCount));
}
void Dialog_progress_ui::FuncNotify(int event, void *msg, int flag)
{
Dialog_progress_ui *p = (Dialog_progress_ui *)m_param;
if (nullptr == p)
{
return;
}
switch (event)
{
case SANE_EVENT_WORKING:
{
QString finishInfo((char*)msg);
emit p->scan_status(finishInfo, true);
}
break;
case SANE_EVENT_SCAN_FINISHED:
{
QString finishInfo((char*)msg);
emit p->scan_finish(flag, finishInfo);
}
break;
case SANE_EVENT_IMAGE_OK:
{
emit p->image_recived();
}
break;
case SANE_EVENT_IMG_UPLOADED:
{
emit p->image_uploaded();
}
break;
}
}