#pragma execution_character_set("utf-8") #include "widget.h" #include "ui_widget.h" #include #include #include #include #include #include #include #include #include "common.h" #include #define MAXITEMS 20 Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) , m_settingDlg(new SettingWgt(this)) , m_udpDataThread(new UdpDataThread(this)) , m_showSetting(false) { QString filePath = QApplication::applicationDirPath() + "/config/Config.json"; Common::GetInstance()->loadConfig(filePath); ui->setupUi(this); initUI(); connect(m_settingDlg, &SettingWgt::SettingFinishSig, [this](){ on_pushButton_setting_clicked(); }); connect(m_udpDataThread, &UdpDataThread::registerRet, this, &Widget::RegisterRetSlot); connect(m_udpDataThread, &UdpDataThread::connectStatus, this, &Widget::ConnectStatusSlot); } Widget::~Widget() { delete ui; } void Widget::initUI() { this->setWindowTitle(""); //this->setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint); this->setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint | Qt::CustomizeWindowHint); ui->list_log->setEditTriggers(QAbstractItemView::NoEditTriggers); QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(m_settingDlg); ui->widget_setting->setLayout(layout); ui->widget_setting->hide(); ui->lineEdit_snId->setFocus(); } void Widget::setSNLineEditFocus() { // 全选LineEdit并且焦点放在LineEdit上 ui->lineEdit_snId->selectAll(); ui->lineEdit_snId->setFocus(); } int Widget::checkSNId(const QString &snId) { if (snId.isEmpty()) { QMessageBox::warning(this, "产品SN错误", "产品SN为空! 请重新扫码!"); LWarn("Product SN id is empty!"); return SNEmptyError; } QRegExp reg("[a-z]"); if (reg.indexIn(snId) != -1) { QMessageBox::information(this,"提示",QString("产品SN(%1)包含小写! 请重新扫码!").arg(snId)); LWarn("Product SN contains lowercase!"); ui->lineEdit_snId->setText(""); return SNLowerCaseError; } if (snId.contains(" ")) { QMessageBox::information(this,"提示",QString("产品SN(%1)包含空格! 请重新扫码!").arg(snId)); LWarn("Product SN contains Spaces!"); ui->lineEdit_snId->setText(""); return SNSpaceError; } return NoError; } int Widget::startIECSProgram(const QString& workPath, const QString& snId) { QProcess *process = new QProcess(this); process->start(workPath); qDebug()<<"startIECSProgram:"<waitForStarted()) { LError("Failed to start the program with arguments:{}", process->errorString()); return ExecIECSError; } m_udpDataThread->sendSnId(snId); return NoError; } int Widget::checkForRepairProgram(const int mode, const QString& sn, bool& isFind) { auto workModeCfg = Common::GetInstance()->getWorkModeConfigs(); QDir workDir(workModeCfg[mode].workPath); workDir.cdUp(); QString dbPath = workDir.absolutePath() + "/config.db"; return databaseQuery(dbPath, sn, isFind); } int Widget::databaseQuery(const QString& databasePath, const QString& deviceSn, bool& isFind) { // 创建和打开 SQLite 数据库 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(databasePath); // 使用传入的数据库路径 if (!db.open()) { LError("Failed to open database:{}", db.lastError().text()); isFind = false; return DatabaseOpenError; } // 创建查询对象 QSqlQuery query(db); // 准备 SQL 查询 query.prepare("SELECT COUNT(1) FROM history WHERE device_sn = :device_sn;"); query.bindValue(":device_sn", deviceSn); // 执行查询 if (!query.exec()) { LError("Failed to execute query:{}", query.lastError().text()); isFind = false; db.close(); return DatabaseSelectError; } // 处理查询结果 if (query.next()) { int count = query.value(0).toInt(); isFind = (count > 0); db.close(); return NoError; } isFind = false; db.close(); return DatabaseUnknownError; } void Widget::RegisterRetSlot(bool ret) { ui->lineEdit_snId->setEnabled(true); } void Widget::ConnectStatusSlot(bool ret) { if (!ret) { ui->lineEdit_snId->setEnabled(true); } } void Widget::on_pushButton_setting_clicked() { if (ui->widget_setting->isHidden()) { ui->pushButton_setting->setIcon(QIcon(":/resources/folding.png")); m_settingDlg->loadConfig(); ui->widget_setting->show(); int currentWidth = this->width(); this->adjustSize(); this->resize(currentWidth, this->height()); // 只保持高度变化 } else { ui->pushButton_setting->setIcon(QIcon(":/resources/unfolding.png")); ui->widget_setting->hide(); int currentWidth = this->width(); this->adjustSize(); this->resize(currentWidth, this->height()); // 只保持高度变化 setSNLineEditFocus(); } } void Widget::addLogItem(const QString &text) { if (ui->list_log->count() >= MAXITEMS) { delete ui->list_log->takeItem(0); } ui->list_log->addItem(text); // 添加新条目 } int Widget::switchProgram() { return 0; } void Widget::on_lineEdit_snId_returnPressed() { QString snId = ui->lineEdit_snId->text(); int ret = 0; ret = checkSNId(snId); if (ret != 0) { LError("checkSNId error!code:{}", ret); return; } const auto selectedProducts = Common::GetInstance()->getSelectedProducts(); QString productFormat{}; for (auto& products : selectedProducts.keys()) { if (snId.contains(products)) { productFormat = products; break; } } if (productFormat.isEmpty()) { QMessageBox::information(this, "警告", "当前SN与启用设值SN不匹配,请检查或重新扫码"); return; } ui->lineEdit_snId->setEnabled(false); auto currWorkMode = Common::GetInstance()->getCurrentWorkMode(); bool isRepair = false; // 查找数据库,判断是否需要返修程序 ret = checkForRepairProgram(selectedProducts[productFormat], snId, isRepair); if (ret != NoError) { LError("checkForRepairProgram error!{}", ret); return; } auto selectMode = selectedProducts[productFormat]; auto workModeCfgs = Common::GetInstance()->getWorkModeConfigs(); if (selectMode != currWorkMode) { // 切换工作模式 if (currWorkMode != -1) { m_udpDataThread->sendClose(); } //TODO:不相等时,m_udpDataThread发送关闭信号,等待Common单例类中的标志位变成-1后,m_udpDataThread停止发送关闭信号并启动IECS程序 //同时需要去IECS程序路径中查找数据库,判断是否使用返修程序 auto workModeCfg = workModeCfgs[selectMode]; QString workPath = isRepair ? workModeCfg.reworkPath : workModeCfg.workPath; qDebug()<<"workPath:"<getCurrentWorkMode() == -1) { m_udpDataThread->stopClose(); timer->stop(); // 停止定时器 // 启动 IECS 程序 startIECSProgram(workPath, snId); } }); timer->start(1000); return; } m_udpDataThread->sendSnId(snId); } void Widget::on_pushButton_sendSnId_clicked() { on_lineEdit_snId_returnPressed(); }