#include "Widget.h" #include "ui_Widget.h" #include #include #include #include /// \~chinese 用于限制显示帧率的时间戳 \~english Time stamp to limit the rate of display QDateTime gLastDisplay; Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); mCtrlThread = new DeviceControlThread(this); connect(mCtrlThread, &DeviceControlThread::signalControlFinish, this, &Widget::onControlFinish); connect(mCtrlThread, &DeviceControlThread::signalSelectGrabConfig, this, &Widget::onSelectGrabConfig, Qt::BlockingQueuedConnection); /// \~chinese 连接采图回调到主线程,需要等待显示完成后再释放缓冲区数据。主线程的耗时操作影响界面刷新时,会对采集速度造成影响 connect(mCtrlThread, &DeviceControlThread::signalImageGrabbed, this, &Widget::onImageGrabbed, Qt::BlockingQueuedConnection); } Widget::~Widget() { delete ui; } /// \~chinese 扫描相机按钮的槽函数 \~english Slot function of probe push button void Widget::on_pushButton_probe_clicked() { mCtrlThread->StartProbe(); } /// \~chinese 打开/关闭相机按钮的槽函数 \~english Slot function of open / close camera push button void Widget::on_pushButton_openCamera_clicked() { if (gCamera) { mCtrlThread->StartClose(); } else { int index = ui->comboBox_cameraList->currentIndex(); mCtrlThread->StartOpen(index); } } /// \~chinese 开始/停止采集按钮的槽函数 \~english Slot function of start / stop acquisitio push button void Widget::on_pushButton_startGrab_clicked() { if (nullptr == gCamera) { QMessageBox::critical(this, "Critical", "Open camera first!"); return; } if (gCamera->g_bGrabbing) { mCtrlThread->StartStopGrab(); } else { mCtrlThread->StartGrab(); } } void Widget::onControlFinish(ITK_DEVICE_CONTROL_TYPE nType, bool bSuccess) { switch (nType) { case ITK_DEVICE_CONTROL_PROBE: { if (!bSuccess) { QMessageBox::critical(this, "Critical", "Probe failed!"); return; } QStringList camList = mCtrlThread->GetDeviceList(); ui->comboBox_cameraList->clear(); ui->comboBox_cameraList->addItems(camList); } break; case ITK_DEVICE_CONTROL_OPEN: if (!bSuccess) { QMessageBox::critical(this, "Critical", "Failed to open camera!"); return; } ui->pushButton_openCamera->setText("Close"); ui->pushButton_probe->setEnabled(false); ui->comboBox_cameraList->setEnabled(false); break; case ITK_DEVICE_CONTROL_CLOSE: if (gCamera) ui->pushButton_startGrab->setText(gCamera->g_bGrabbing ? "Stop" : "Grab"); if (!bSuccess) { QMessageBox::critical(this, "Critical", "Failed to close camera!"); return; } ui->pushButton_openCamera->setText("Open"); ui->pushButton_probe->setEnabled(true); ui->comboBox_cameraList->setEnabled(true); ui->pushButton_startGrab->setText("Grab"); break; case ITK_DEVICE_CONTROL_GRAB: if (!bSuccess) { QMessageBox::critical(this, "Critical", "Failed to start grab!"); return; } ui->pushButton_startGrab->setText("Stop"); break; case ITK_DEVICE_CONTROL_STOP: if (!bSuccess) { QMessageBox::critical(this, "Critical", "Failed to stop grab!"); return; } ui->pushButton_startGrab->setText("Grab"); break; } } QString Widget::onSelectGrabConfig() { QString fileName = QFileDialog::getOpenFileName(this, tr("Select Vlcf"), "", "*.vlcf"); if (fileName.isEmpty()) { QMessageBox::information(this, "Info", tr("No config file selected, camera will grab with default paramter")); } return fileName; } void Widget::onImageGrabbed(const QImage &img) { QDateTime now = QDateTime::currentDateTime(); /// \~chinese 控制刷新帧率小于5 \~english Limit the rate of diaply less than 5 if (gLastDisplay.isValid() && gLastDisplay.msecsTo(now) < 200) return; gLastDisplay = now; ui->labe_img->setPixmap(QPixmap::fromImage(img).scaled(ui->labe_img->size())); }