From c447a1301740cd1dc0528c4f79492a549f105bf9 Mon Sep 17 00:00:00 2001 From: yangjiaxuan <171295266@qq.com> Date: Mon, 26 Dec 2022 18:38:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=95=8C=E9=9D=A2=E6=B5=81?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/HGProductionTool/form_maininterface.cpp | 23 ++++++- app/HGProductionTool/form_maininterface.h | 8 ++- app/HGProductionTool/hgscanner.cpp | 5 +- app/HGProductionTool/hgscanner.h | 6 +- app/HGProductionTool/mainwindow.cpp | 76 ++++++++++++++++----- app/HGProductionTool/mainwindow.h | 5 +- app/HGProductionTool/ui_helper.h | 11 +-- 7 files changed, 102 insertions(+), 32 deletions(-) diff --git a/app/HGProductionTool/form_maininterface.cpp b/app/HGProductionTool/form_maininterface.cpp index 22f042a..c1d018c 100644 --- a/app/HGProductionTool/form_maininterface.cpp +++ b/app/HGProductionTool/form_maininterface.cpp @@ -55,12 +55,12 @@ static const RELA Rela[] = { {HGPDTTOOLDB_TITLE_CLEAR_ROLLER_COUNT, HGPDTTOOLDB_ENTRYNAME_CLEAR_ROLLER_COUNT} }; -Form_mainInterface::Form_mainInterface(class MainWindow *mainwnd, ui_helper *uiHelper, SANE_Handle handle, HGPdtToolDbUserMgr pdtToolDbuserMgr, HGPdtToolDbDevice pdtToolDbDevice, +Form_mainInterface::Form_mainInterface(class MainWindow *mainwnd, SANE_Handle handle, HGPdtToolDbUserMgr pdtToolDbuserMgr, HGPdtToolDbDevice pdtToolDbDevice, const QString &serialNum, const QString &devType, const QString &devName, const QString &devFwNum) : QWidget(mainwnd) , ui(new Ui::Form_mainInterface) , m_mainwnd(mainwnd) - , m_uiHhelper(uiHelper) + , m_hg(nullptr) , m_handle(handle) , m_pdtToolDbuserMgr(pdtToolDbuserMgr) , m_pdtToolDbDevice(pdtToolDbDevice) @@ -91,12 +91,19 @@ Form_mainInterface::Form_mainInterface(class MainWindow *mainwnd, ui_helper *uiH HGPdtToolDb_SetDeviceType(m_pdtToolDbDevice, m_devType.toStdString().c_str()); HGPdtToolDb_SetDeviceVersion(m_pdtToolDbDevice, m_devFwNum.toStdString().c_str()); HGPdtToolDb_SetDeviceName(m_pdtToolDbDevice, m_devName.toStdString().c_str()); + + m_hg = new hgscanner(this, m_handle); } Form_mainInterface::~Form_mainInterface() { + delete m_hg; + m_hg = nullptr; + sane_close(m_handle); + m_handle = nullptr; HGPdtToolDb_CloseDevice(m_pdtToolDbDevice); + m_pdtToolDbDevice = nullptr; delete ui; } @@ -111,6 +118,16 @@ QString Form_mainInterface::getDevName() return m_devName; } +SANE_Handle Form_mainInterface::getDevHandle() +{ + return m_handle; +} + +hgscanner *Form_mainInterface::getScanner() +{ + return m_hg; +} + void Form_mainInterface::paintEvent(QPaintEvent *event) { (void)event; @@ -359,5 +376,5 @@ void Form_mainInterface::on_pbtn_start_clicked() int row = ui->tableWidget->currentRow(); QTableWidgetItem *item = ui->tableWidget->item(row, 1); QString title = item->text(); - func_test_go(title.toStdWString().c_str(), L"null", m_uiHhelper); + func_test_go(title.toStdWString().c_str(), L"null", m_hg); } diff --git a/app/HGProductionTool/form_maininterface.h b/app/HGProductionTool/form_maininterface.h index 0cb89c0..07b21ac 100644 --- a/app/HGProductionTool/form_maininterface.h +++ b/app/HGProductionTool/form_maininterface.h @@ -9,7 +9,7 @@ #include "analysisjson.h" #include "sane/sane_ex.h" #include "HGPdtToolDb.h" -#include "ui_helper.h" +#include "hgscanner.h" namespace Ui { class Form_mainInterface; @@ -20,12 +20,14 @@ class Form_mainInterface : public QWidget Q_OBJECT public: - explicit Form_mainInterface(class MainWindow *mainwnd, ui_helper *uiHelper, SANE_Handle handle, HGPdtToolDbUserMgr pdtToolDbuserMgr, HGPdtToolDbDevice pdtToolDbDevice, + explicit Form_mainInterface(class MainWindow *mainwnd, SANE_Handle handle, HGPdtToolDbUserMgr pdtToolDbuserMgr, HGPdtToolDbDevice pdtToolDbDevice, const QString &serialNum, const QString &devType, const QString &devName, const QString &devFwNum); ~Form_mainInterface(); QString getSn(); QString getDevName(); + SANE_Handle getDevHandle(); + hgscanner *getScanner(); protected: void paintEvent(QPaintEvent *event) override; @@ -60,7 +62,7 @@ private: HGImgView *m_view; class MainWindow *m_mainwnd; - ui_helper *m_uiHhelper; + hgscanner *m_hg; SANE_Handle m_handle; HGPdtToolDbUserMgr m_pdtToolDbuserMgr; HGPdtToolDbDevice m_pdtToolDbDevice; diff --git a/app/HGProductionTool/hgscanner.cpp b/app/HGProductionTool/hgscanner.cpp index 8a48c39..3969952 100644 --- a/app/HGProductionTool/hgscanner.cpp +++ b/app/HGProductionTool/hgscanner.cpp @@ -1,7 +1,10 @@ #include "hgscanner.h" #include +#include "form_maininterface.h" -hgscanner::hgscanner(SANE_Handle h):devHandle_(h) +hgscanner::hgscanner(Form_mainInterface *form, SANE_Handle h) + : m_interface(form) + , devHandle_(h) { } diff --git a/app/HGProductionTool/hgscanner.h b/app/HGProductionTool/hgscanner.h index 0805024..51203a1 100644 --- a/app/HGProductionTool/hgscanner.h +++ b/app/HGProductionTool/hgscanner.h @@ -3,10 +3,12 @@ #include "ui_helper.h" #include "test_base.h" +class Form_mainInterface; + class hgscanner :public ui_helper { public: - hgscanner(SANE_Handle h); + hgscanner(Form_mainInterface *form, SANE_Handle h); ~hgscanner(); virtual parameter* get_user_input(data_from from, value_type type, const wchar_t* title, const wchar_t* desc = NULL); virtual void test_callback(const wchar_t* name/*test name*/, test_event ev, void* data, size_t flag); @@ -15,7 +17,7 @@ public: virtual int io_control(unsigned long code, void* data, unsigned* len); sane_callback cb_; private: + class Form_mainInterface *m_interface; SANE_Handle devHandle_; - }; diff --git a/app/HGProductionTool/mainwindow.cpp b/app/HGProductionTool/mainwindow.cpp index e13f69a..ff4b43c 100644 --- a/app/HGProductionTool/mainwindow.cpp +++ b/app/HGProductionTool/mainwindow.cpp @@ -14,6 +14,7 @@ MainWindow::MainWindow(HGPdtToolDbUserMgr pdtToolDbuserMgr, QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_pdtToolDbuserMgr(pdtToolDbuserMgr) + , m_lock(nullptr) , m_top_splitter(nullptr) , m_bot_splitter(nullptr) , m_isLogOut(false) @@ -21,6 +22,8 @@ MainWindow::MainWindow(HGPdtToolDbUserMgr pdtToolDbuserMgr, QWidget *parent) ui->setupUi(this); setWindowIcon(QIcon(":image/image_rsc/logo/logo.ico")); + HGBase_CreateLock(&m_lock); + connect(this, SIGNAL(sane_dev_arrive(QString)), this, SLOT(on_sane_dev_arrive(QString)), Qt::QueuedConnection); connect(this, SIGNAL(sane_dev_remove(QString)), this, SLOT(on_sane_dev_remove(QString)), Qt::QueuedConnection); @@ -62,23 +65,26 @@ MainWindow::MainWindow(HGPdtToolDbUserMgr pdtToolDbuserMgr, QWidget *parent) MainWindow::~MainWindow() { - for (int i = 0; i < m_top_splitter->count(); ++i) + HGBase_EnterLock(m_lock); + while (0 != m_top_splitter->count()) { - Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(i); + Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(0); delete interface; } - for (int i = 0; i < m_bot_splitter->count(); ++i) + while (0 != m_bot_splitter->count()) { - Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(i); + Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(0); delete interface; } + HGBase_LeaveLock(m_lock); sane_exit(); HGPdtToolDb_DestroyUserMgr(m_pdtToolDbuserMgr); m_pdtToolDbuserMgr = NULL; + HGBase_DestroyLock(m_lock); delete ui; } @@ -157,10 +163,40 @@ Form_mainInterface *MainWindow::FindInterfaceByDevName(const QString &devName) return nullptr; } +Form_mainInterface* MainWindow::FindInterface(SANE_Handle handle) +{ + Form_mainInterface *form = nullptr; + for (int i = 0; i < m_top_splitter->count(); ++i) + { + Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(i); + if (handle == interface->getDevHandle()) + { + form = interface; + break; + } + } + + if (nullptr == form) + { + for (int i = 0; i < m_bot_splitter->count(); ++i) + { + Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(i); + if (handle == interface->getDevHandle()) + { + form = interface; + break; + } + } + } + + return form; +} + bool MainWindow::AddInterface(Form_mainInterface *interface) { assert(nullptr != interface); + HGBase_EnterLock(m_lock); bool ret = false; if (m_top_splitter->count() < 2) { @@ -172,6 +208,7 @@ bool MainWindow::AddInterface(Form_mainInterface *interface) m_bot_splitter->addWidget(interface); ret = true; } + HGBase_LeaveLock(m_lock); updateSplitter(); return ret; @@ -180,7 +217,9 @@ bool MainWindow::AddInterface(Form_mainInterface *interface) bool MainWindow::RemoveInterface(Form_mainInterface *interface) { assert(nullptr != interface); + HGBase_EnterLock(m_lock); delete interface; + HGBase_LeaveLock(m_lock); updateSplitter(); return true; } @@ -188,20 +227,18 @@ bool MainWindow::RemoveInterface(Form_mainInterface *interface) int MainWindow::sane_ex_callback(SANE_Handle hdev, int code, void *data, unsigned int *len, void *param) { MainWindow* p = (MainWindow*)param; - switch (code) - { - case SANE_EVENT_DEVICE_ARRIVED: + + if (SANE_EVENT_DEVICE_ARRIVED == code) { SANE_Device_Ex* sane_dev = (SANE_Device_Ex*)data; emit p->sane_dev_arrive(sane_dev->name); + return 0; } - break; - case SANE_EVENT_DEVICE_LEFT: + else if (SANE_EVENT_DEVICE_LEFT == code) { SANE_Device* sane_dev = (SANE_Device*)data; emit p->sane_dev_remove(sane_dev->name); - } - break; + return 0; } if (!hdev) @@ -209,7 +246,15 @@ int MainWindow::sane_ex_callback(SANE_Handle hdev, int code, void *data, unsigne return -1; } - p->m_hg->cb_(hdev, code, data, len, p->m_uiHelper); + HGBase_EnterLock(p->m_lock); + Form_mainInterface* interface = p->FindInterface(hdev); + if (nullptr != interface) + { + if (nullptr != interface->getScanner() && nullptr != interface->getScanner()->cb_) + interface->getScanner()->cb_(hdev, code, data, len, interface->getScanner()); + } + HGBase_LeaveLock(p->m_lock); + return 0; } @@ -290,10 +335,7 @@ void MainWindow::on_sane_dev_arrive(QString devName) HGPdtToolDbDevice pdtToolDbDevice = nullptr; HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, dlg.GetSn().toStdString().c_str(), &pdtToolDbDevice); - m_hg = new hgscanner(devHandle); - m_uiHelper = dynamic_cast(m_hg); - - Form_mainInterface *mainInterface = new Form_mainInterface(this, m_uiHelper, devHandle, m_pdtToolDbuserMgr, pdtToolDbDevice, + Form_mainInterface *mainInterface = new Form_mainInterface(this, devHandle, m_pdtToolDbuserMgr, pdtToolDbDevice, dlg.GetSn(), dlg.GetDevType(), devName, getDevFwNum(devHandle)); if (!AddInterface(mainInterface)) { @@ -355,7 +397,7 @@ void MainWindow::on_act_newDevice_triggered() HGPdtToolDbDevice pdtToolDbDevice = nullptr; HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, dlg.GetSn().toStdString().c_str(), &pdtToolDbDevice); - Form_mainInterface *mainInterface = new Form_mainInterface(this, nullptr, nullptr, m_pdtToolDbuserMgr, pdtToolDbDevice, dlg.GetSn(), dlg.GetDevType(), nullptr, nullptr); + Form_mainInterface *mainInterface = new Form_mainInterface(this, nullptr, m_pdtToolDbuserMgr, pdtToolDbDevice, dlg.GetSn(), dlg.GetDevType(), nullptr, nullptr); if (!AddInterface(mainInterface)) { QMessageBox::information(this, tr("tips"), tr("cannot create more table")); diff --git a/app/HGProductionTool/mainwindow.h b/app/HGProductionTool/mainwindow.h index 78e4ef1..e71165a 100644 --- a/app/HGProductionTool/mainwindow.h +++ b/app/HGProductionTool/mainwindow.h @@ -7,6 +7,7 @@ #include #include "HGPdtToolDb.h" #include "sane/sane_ex.h" +#include "base/HGLock.h" #include "hgscanner.h" QT_BEGIN_NAMESPACE @@ -26,6 +27,7 @@ public: void updateSplitter(); Form_mainInterface* FindInterface(const QString &sn); Form_mainInterface* FindInterfaceByDevName(const QString &devName); + Form_mainInterface* FindInterface(SANE_Handle handle); bool AddInterface(Form_mainInterface *interface); bool RemoveInterface(Form_mainInterface *interface); @@ -70,7 +72,6 @@ private: private: Ui::MainWindow *ui; HGPdtToolDbUserMgr m_pdtToolDbuserMgr; - hgscanner *m_hg; - ui_helper *m_uiHelper; + HGLock m_lock; }; #endif // MAINWINDOW_H diff --git a/app/HGProductionTool/ui_helper.h b/app/HGProductionTool/ui_helper.h index 15b1b61..59cd789 100644 --- a/app/HGProductionTool/ui_helper.h +++ b/app/HGProductionTool/ui_helper.h @@ -79,10 +79,13 @@ public: ) =0; enum test_event - { - TEST_EVENT_TIPS = 0, // messages in testing process, data is (wchar_t*), flag is unused - TEST_EVENT_xxx, // should be complemented ... - TEST_EVENT_RESULT, // test result, data is (wchar_t*)description, flag is (bool)result, true - test pass + { + TEST_EVENT_TIPS = 0, // messages in testing process, data is (wchar_t*), flag is unused + TEST_EVENT_xxx, // should be complemented ... + TEST_EVENT_NOT_FIND_TEST, // dll. not find test ,data is (wchar_t*)description, flag is (false) + TEST_EVENT_IO_FAIL, // IO. err flag is SCANNER_ERR_DEVICE_NOT_SUPPORT or io err,data is null + TEST_EVENT_MANUAL_CONFIRMATION, // After send cmd ,need manual confirmation. + TEST_EVENT_RESULT, // test result, data is (wchar_t*)description, flag is (bool)result, true - test pass }; virtual void test_callback(const wchar_t* name/*test name*/, test_event ev, void* data, size_t flag)=0 ;