#include "MainAndSecondaryCamerasWnd.h" #include "ui_MainAndSecondaryCamerasWnd.h" #include #include #include #include #include #include #include "CameraDistribute.h" #include "Src/common/JMessageTip.h" MainAndSecondaryCamerasWnd::MainAndSecondaryCamerasWnd(QWidget *parent) : JOriginalMainWnd(parent) , ui(new Ui::MainAndSecondaryCamerasWnd) { ui->setupUi(this); QString buildDirPath = QCoreApplication::applicationDirPath(); QDir buildDirectory(buildDirPath); QStringList files = getImageslist(buildDirectory.filePath("photos")); for (const QString& file : files) { qDebug() << file; } ui->control_r->HideOrShowPage(false); ui->control_L->HideOrShowPage(false); ui->viewwidgetgroup_L->setBlueBord(); qApp->installEventFilter(this); m_veCurrentSelectGroup_R.isInitialed = false; m_veCurrentSelectGroup_L.isInitialed = false; m_veCurrentSelectGroup_R.isBond = false; m_veCurrentSelectGroup_L.isBond = false; } MainAndSecondaryCamerasWnd::~MainAndSecondaryCamerasWnd() { UnCameraBind(); delete ui; } void MainAndSecondaryCamerasWnd::initFrom() { { // 界面刷新 InitPage(); //STR_TEMPLATE strFun = {}; //strFun.pView = ui->scrollArea; //strFun.veSignalFun.emplace_back(std::bind(&MainAndSecondaryCamerasWnd::UpdateCameraDisplay0, this, std::placeholders::_1, std::placeholders::_2)); //strFun.veSignalFun.emplace_back(std::bind(&MainAndSecondaryCamerasWnd::UpdateCameraDisplay1, this, std::placeholders::_1, std::placeholders::_2)); ////strFun.veSlotFun.emplace_back([this]() { CheckIs(); }); //strFun.veSlotFun.emplace_back(std::bind(&MainAndSecondaryCamerasWnd::CheckIs, this)); //InitAllCamerPage(strFun, this); //CameraImageHandlera A(this, strFun); //SwitchToConfigAAAAA(); } //ui->LeftOperatewidget->setMouseTracking(true); //ui->RightOperatewidget->setMouseTracking(true); } void MainAndSecondaryCamerasWnd::InitPage() { QWidget* viewport = ui->scrollArea->viewport(); QWidget* container = new QWidget(viewport); QHBoxLayout* mainLayout = new QHBoxLayout(container); mainLayout->setSpacing(28); mainLayout->setMargin(0); int nSize = 0; if (m_pMainCameraBind != nullptr) { nSize = m_pMainCameraBind->m_vecCamera.size(); if (nSize == 0) // 针对获取不到的情况 { nSize = 5; //JMessageTip::Message_warning("a"); } QList numbers; for (int i = 1; i <= nSize; i++) { numbers.push_back(i); } for (int i = 0; i < numbers.size(); ++i) { int num = numbers[i]; CameraImageHandler* manager = new CameraImageHandler(num); Group* widget = manager->getGroup(); CameraConnectUpdateImageFun(i, widget); connect(widget, &Group::sendUpdateGroupState, this, &MainAndSecondaryCamerasWnd::checkSettings); if (widget != nullptr) { mainLayout->addWidget(widget); m_allGroup.append(widget); } if (manager->getWafer()) { m_mapWaferMap.insert(num, manager->getWafer()); } if (manager->getWaffle()) { m_mapWaffleMap.insert(num, manager->getWaffle()); } if (manager->getMaterialBox()) { m_mapMaterialBoxMap.insert(num, manager->getMaterialBox()); } if (manager->getBond()) { m_mapBondMap.insert(num, manager->getBond()); } delete manager; } // 设置控件的最小高度和最大宽度 int minHeight = 162; int maxWidth = 244; int num = -28; for(Group* w : m_allGroup) { w->setMinimumHeight(minHeight); w->setMaximumWidth(maxWidth); num = num + 272; } container->setMinimumSize(num, 162); container->setLayout(mainLayout); ui->scrollArea->setWidget(container); ui->scrollArea->resize(1062, 177); initLeftAndRightWidget(); //// 只有一个相机的情况,直接绑定 //if (numbers.size() == 1) //{ // Group* pGroup = m_allGroup[0]; // connect(pGroup, &Group::SetUpDataImageShowSig, // this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0); // ST_CURRENT_SELECT_GROUP _curr; // _curr.isBond = true; // _curr.pSelectGroup = pGroup; // _curr.nCurrentCameraId = pGroup->m_nGroupId; // _curr.nSelectLeft1 = 1; // m_veCurrentSelectGroup.push_back(_curr); // UpdataLightJoystickSwitchPage(); //} } } void MainAndSecondaryCamerasWnd::CameraConnectUpdateImageFun(int nIndex, Group* widget, bool isUnCameraBind /*= false*/) { auto BinCamerasImage = [&](void (MainAndSecondaryCamerasWnd::* pCamerasImage)(const QPixmap& imageData)) { if (isUnCameraBind) { //disconnect(widget, &Group::SetCurrentSelectSig, this, &MainAndSecondaryCamerasWnd::GetCurrentSelectSlots); disconnect(this, pCamerasImage, widget, &Group::UpDataImageShowSlots); } else { //共用函数 // connect(widget, &Group::SetCurrentSelectSig, this, &MainAndSecondaryCamerasWnd::GetCurrentSelectSlots); connect(this, pCamerasImage, widget, &Group::UpDataImageShowSlots); } }; if (nIndex == 0) { BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals0); } else if (nIndex == 1) { BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals1); } else if (nIndex == 2) { BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals2); } else if (nIndex == 3) { BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals3); } else if (nIndex == 4) { BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals4); } } void MainAndSecondaryCamerasWnd::initLeftAndRightWidget() { //初始化当前页面的信息 QSettings settings("YourOrganization", "YourApplication"); lastLastIndex = settings.value("DoubleLastLastIndex", 2).toInt(); lastLastGroupId = settings.value("DoubleLastLastGroupId", 1).toInt(); lastIndex = settings.value("DoubleLastIndex", 1).toInt(); lastGroupId = settings.value("DoubleLastGroupId", 1).toInt(); //处理各种情况的初始化 if (m_pMainCameraBind == nullptr || m_allGroup.size() == 0) { return; } if (m_allGroup.size() < lastGroupId) { //重置记录 lastGroupId = 1; lastIndex = 1; lastLastGroupId = 1; lastLastGroupId = 2; } m_veCurrentSelectGroup_R.isInitialed = true; m_veCurrentSelectGroup_L.isInitialed = true; if (m_allGroup.size() == 1) { if (lastIndex != lastLastIndex) { m_veCurrentSelectGroup_L.pSelectGroup = m_allGroup.at(lastGroupId - 1); m_veCurrentSelectGroup_R.pSelectGroup = m_allGroup.at(lastGroupId - 1); if (lastIndex == 1) { BindImageOrMaterial(m_nCurrentRight, m_nImageIndex); BindImageOrMaterial(m_nLastLeft, m_nMaterialIndex); } else { BindImageOrMaterial(m_nCurrentRight, m_nMaterialIndex); BindImageOrMaterial(m_nLastLeft, m_nImageIndex); } } else { BindImageOrMaterial(m_nCurrentRight, m_nMaterialIndex); BindImageOrMaterial(m_nLastLeft, m_nImageIndex); } } else { if (lastLastGroupId > m_allGroup.size() || lastGroupId > m_allGroup.size()) { //重置记录 lastGroupId = 1; lastIndex = 1; lastLastGroupId = 1; lastLastGroupId = 2; } m_veCurrentSelectGroup_L.pSelectGroup = m_allGroup.at(lastLastGroupId - 1); m_veCurrentSelectGroup_R.pSelectGroup = m_allGroup.at(lastGroupId - 1); if (lastLastIndex == 1) { BindImageOrMaterial(m_nLastLeft, m_nImageIndex); } else { BindImageOrMaterial(m_nLastLeft, m_nMaterialIndex); } if (lastIndex == 1) { BindImageOrMaterial(m_nCurrentRight, m_nImageIndex); } else { BindImageOrMaterial(m_nCurrentRight, m_nMaterialIndex); } } } void MainAndSecondaryCamerasWnd::BindImageOrMaterial(int location, int index) { if (location == m_nLastLeft) { if (index == m_nImageIndex) { m_veCurrentSelectGroup_L.isBond = true; connect(m_veCurrentSelectGroup_L.pSelectGroup, &Group::SetUpDataImageShowSig, this, &MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_L); UpdataLightJoystickSwitchPage(m_veCurrentSelectGroup_L.pSelectGroup->m_nGroupId, 1, true); }else { //左边更新物料窗口 QSettings settings("YourOrganization", "YourApplication"); settings.beginGroup(QString::number(m_veCurrentSelectGroup_L.pSelectGroup->m_nGroupId)); int lastMaterialWndType = settings.value("MaterialWndType").toInt(); settings.endGroup(); m_veCurrentSelectGroup_L.isBond = false; UpdataLightJoystickSwitchPage(m_veCurrentSelectGroup_L.pSelectGroup->m_nGroupId, 1, false); updateMaterialWidget(m_nLastLeft, lastMaterialWndType, m_veCurrentSelectGroup_L.pSelectGroup->m_nGroupId); } }else { if (index == m_nImageIndex) { m_veCurrentSelectGroup_R.isBond = true; connect(m_veCurrentSelectGroup_R.pSelectGroup, &Group::SetUpDataImageShowSig, this, &MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_R); UpdataLightJoystickSwitchPage(m_veCurrentSelectGroup_R.pSelectGroup->m_nGroupId, 2, true); } else { //右边更新物料窗口 QSettings settings("YourOrganization", "YourApplication"); settings.beginGroup(QString::number(m_veCurrentSelectGroup_R.pSelectGroup->m_nGroupId)); int materialWndType = settings.value("MaterialWndType").toInt(); settings.endGroup(); m_veCurrentSelectGroup_R.isBond = false; UpdataLightJoystickSwitchPage(m_veCurrentSelectGroup_R.pSelectGroup->m_nGroupId, 2, false); updateMaterialWidget(m_nCurrentRight, materialWndType, m_veCurrentSelectGroup_R.pSelectGroup->m_nGroupId); } } } void MainAndSecondaryCamerasWnd::checkSettings(int groupId,int index) { if (groupId != lastGroupId || index != lastIndex) { GetCurrentSelectSlots(groupId, index); lastLastGroupId = lastGroupId; lastLastIndex = lastIndex; lastGroupId = groupId; lastIndex = index; saveInfoOfLast(); } } void MainAndSecondaryCamerasWnd::saveInfoOfLast() { QSettings settings("YourOrganization", "YourApplication"); settings.setValue("DoubleLastIndex", lastIndex); settings.setValue("DoubleLastGroupId", lastGroupId); settings.setValue("DoubleLastLastIndex", lastLastIndex); settings.setValue("DoubleLastLastGroupId", lastLastGroupId); } void MainAndSecondaryCamerasWnd::updateMaterialWidget(int side,int materialWndType, int groupId) { if (side == m_nLastLeft) { switch (materialWndType) { case 1: ui->viewwidgetgroup_L->setWafer(m_mapWaferMap.value(groupId)); break; case 2: ui->viewwidgetgroup_L->setWaffle(m_mapWaffleMap.value(groupId)); break; case 3: ui->viewwidgetgroup_L->setMaterialBox(m_mapMaterialBoxMap.value(groupId)); break; case 4: ui->viewwidgetgroup_L->setBond(m_mapBondMap.value(groupId)); break; } } else if (side == m_nCurrentRight) { switch (materialWndType) { case 1: ui->viewwidgetgroup_R->setWafer(m_mapWaferMap.value(groupId)); break; case 2: ui->viewwidgetgroup_R->setWaffle(m_mapWaffleMap.value(groupId)); break; case 3: ui->viewwidgetgroup_R->setMaterialBox(m_mapMaterialBoxMap.value(groupId)); break; case 4: ui->viewwidgetgroup_R->setBond(m_mapBondMap.value(groupId)); break; } } } void MainAndSecondaryCamerasWnd::InitMainCameraBind(CameraBind* pCameraBind) { m_pMainCameraBind = pCameraBind; initFrom(); } void MainAndSecondaryCamerasWnd::UnCameraBind() { for (size_t i = 0; i < m_allGroup.size(); i++) { CameraConnectUpdateImageFun(i, m_allGroup[i], true); } //判断更新前的左窗口是否是绑定相机 if (m_veCurrentSelectGroup_L.isBond == true) { disconnect(m_veCurrentSelectGroup_L.pSelectGroup, &Group::SetUpDataImageShowSig, this, &MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_L); } //判断更新前的右窗口是否是绑定相机 if (m_veCurrentSelectGroup_R.isBond == true) { //解绑 disconnect(m_veCurrentSelectGroup_R.pSelectGroup, &Group::SetUpDataImageShowSig, this, &MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_R); } } void MainAndSecondaryCamerasWnd::UpdateCameraDisplay0(int iCameraId, JVision::ImageInfo imageData) { // TODO: 这里需要改动 QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8); ui->viewwidgetgroup_L->getOperatewidget()->setGeometry(ui->viewwidgetgroup_L->getOperatewidget()->pos().x(), ui->viewwidgetgroup_L->getOperatewidget()->pos().y(), ui->viewwidgetgroup_L->getOperatewidget()->width(), ui->viewwidgetgroup_L->getOperatewidget()->height()); //ui->LeftOperatewidget->setPixmap(QPixmap::fromImage(image)); emit UpDataImageShowSignals0(QPixmap::fromImage(image)); } void MainAndSecondaryCamerasWnd::UpdateCameraDisplay1(int iCameraId, JVision::ImageInfo imageData) { QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8); ui->viewwidgetgroup_R->getOperatewidget()->setGeometry(ui->viewwidgetgroup_R->getOperatewidget()->pos().x(), ui->viewwidgetgroup_R->getOperatewidget()->pos().y(), ui->viewwidgetgroup_R->getOperatewidget()->width(), ui->viewwidgetgroup_R->getOperatewidget()->height()); //ui->RightOperatewidget->setPixmap(QPixmap::fromImage(image)); emit UpDataImageShowSignals1(QPixmap::fromImage(image)); } void MainAndSecondaryCamerasWnd::UpdateCameraDisplay2(int iCameraId, JVision::ImageInfo imageData) { QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8); emit UpDataImageShowSignals2(QPixmap::fromImage(image)); } void MainAndSecondaryCamerasWnd::UpdateCameraDisplay3(int iCameraId, JVision::ImageInfo imageData) { QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8); emit UpDataImageShowSignals3(QPixmap::fromImage(image)); } void MainAndSecondaryCamerasWnd::UpdateCameraDisplay4(int iCameraId, JVision::ImageInfo imageData) { QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8); ui->viewwidgetgroup_R->getOperatewidget()->setGeometry(ui->viewwidgetgroup_R->getOperatewidget()->pos().x(), ui->viewwidgetgroup_R->getOperatewidget()->pos().y(), ui->viewwidgetgroup_R->getOperatewidget()->width(), ui->viewwidgetgroup_R->getOperatewidget()->height()); //ui->RightOperatewidget->setPixmap(QPixmap::fromImage(image)); emit UpDataImageShowSignals4(QPixmap::fromImage(image)); } void MainAndSecondaryCamerasWnd::HideLayout(QHBoxLayout* layout, bool isHide) { for (int i = 0; i < layout->count(); ++i) { QWidget* widget = layout->itemAt(i)->widget(); if (widget) { if (isHide) { widget->hide(); } else { widget->show(); } } } } void MainAndSecondaryCamerasWnd::UpdataLightJoystickSwitchPage(int groupId, int index, bool isShow) { auto Fun = [&](Group* pGroup,LightJoystickSwitchPage* p, ControlOperationPage* pContPage, bool bShow) { p->UpdatemPageGroup(pGroup); p->InitMainCameraBind(m_pMainCameraBind, bShow); p->HideOrShowPage(bShow); if (bShow) { pContPage->UpDateCameraBind(m_pMainCameraBind); } else { pContPage->setSwitchJoystickButEnable(bShow); } }; if (index == 1) { disconnect(ui->viewwidgetgroup_L, &ControlOperationPage::SendModuleTypeSignals, ui->control_L, &LightJoystickSwitchPage::GetModuleTypeSlots); Group* currGroup = m_allGroup.at(groupId-1); Fun(currGroup, ui->control_L, ui->viewwidgetgroup_L, isShow); connect(ui->viewwidgetgroup_L, &ControlOperationPage::SendModuleTypeSignals, ui->control_L,&LightJoystickSwitchPage::GetModuleTypeSlots); } else if(index == 2) { disconnect(ui->viewwidgetgroup_R, &ControlOperationPage::SendModuleTypeSignals, ui->control_r, &LightJoystickSwitchPage::GetModuleTypeSlots); Group* currGroup = m_allGroup.at(groupId-1); Fun(currGroup, ui->control_r, ui->viewwidgetgroup_R, isShow); connect(ui->viewwidgetgroup_R, &ControlOperationPage::SendModuleTypeSignals, ui->control_r, &LightJoystickSwitchPage::GetModuleTypeSlots); } } void MainAndSecondaryCamerasWnd::timerEvent(QTimerEvent* event) { int nID = event->timerId(); } void MainAndSecondaryCamerasWnd::GetCurrentSelectSlots(int groupId, int nIndex) { int nOnClickGroupId = groupId - 1; // 这里new 有些是0,有些是1 暂时选择 if (m_pMainCameraBind == nullptr || m_allGroup.size() < groupId) { return; } //判断更新前的左窗口是否是绑定相机 if (m_veCurrentSelectGroup_L.isBond == true) { disconnect(m_veCurrentSelectGroup_L.pSelectGroup, &Group::SetUpDataImageShowSig, this, &MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_L); } //判断更新前的右窗口是否是绑定相机 if (m_veCurrentSelectGroup_R.isBond == true) { //解绑 disconnect(m_veCurrentSelectGroup_R.pSelectGroup, &Group::SetUpDataImageShowSig, this, &MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_R); } m_veCurrentSelectGroup_L = m_veCurrentSelectGroup_R; //判断更新前的右窗口的index是相机还是物料 if (lastIndex == 1) { //左窗口绑定 BindImageOrMaterial(m_nLastLeft, m_nImageIndex); } else { BindImageOrMaterial(m_nLastLeft, m_nMaterialIndex); } //右边更新物料窗口 m_veCurrentSelectGroup_R.pSelectGroup = m_allGroup.at(nOnClickGroupId); //判断当前index是相机还是物料 if (nIndex == 2) { BindImageOrMaterial(m_nCurrentRight, m_nMaterialIndex); } else if (nIndex == 1) {//当前切换时相机序号 BindImageOrMaterial(m_nCurrentRight, m_nImageIndex); } } void MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_L(const QPixmap& imageData) { //lastIndex = 1; QSize size_left = ui->viewwidgetgroup_L->getOperatewidget()->size(); QPixmap scaledPixmap_left = m_lastRightPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation); //ui->viewwidgetgroup_L->getOperatewidget()->setPixmap(scaledPixmap_left); ui->viewwidgetgroup_L->setScaleFactorSize(scaledPixmap_left); m_lastRightPixmap = imageData; //UpdataGroupSettings(imageData); } void MainAndSecondaryCamerasWnd::GetGroupImageShowSignalsTo_R(const QPixmap& imageData) { QSize size_left = ui->viewwidgetgroup_R->getOperatewidget()->size(); QPixmap scaledPixmap_right = imageData.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation); //ui->viewwidgetgroup_R->getOperatewidget()->setPixmap(scaledPixmap_left); ui->viewwidgetgroup_R->setScaleFactorSize(scaledPixmap_right); m_lastRightPixmap = imageData; } void MainAndSecondaryCamerasWnd::wheelEvent(QWheelEvent *event) { //if (ui->viewwidgetgroup_L->getOperatewidget()->rect().contains(ui->viewwidgetgroup_L->getOperatewidget()->mapFromGlobal(event->globalPos()))) { // mousePos = ui->viewwidgetgroup_L->getOperatewidget()->mapFromGlobal(event->globalPos()); // if (event->angleDelta().y() > 0) { // ui->viewwidgetgroup_L->updateScale(Left_scaleFactor * 1.1); // 放大 // } else { // ui->viewwidgetgroup_L->updateScale(Left_scaleFactor * 0.9); // 缩小 // } //} //if (ui->viewwidgetgroup_R->getOperatewidget()->rect().contains(ui->viewwidgetgroup_R->getOperatewidget()->mapFromGlobal(event->globalPos()))) { // mousePos = ui->viewwidgetgroup_R->getOperatewidget()->mapFromGlobal(event->globalPos()); // if (event->angleDelta().y() > 0) { // ui->viewwidgetgroup_R->updateScale(Right_scaleFactor * 1.1); // 放大 // } else { // ui->viewwidgetgroup_R->updateScale(Right_scaleFactor * 0.9); // 缩小 // } //} //if (ui->scrollArea->rect().contains(ui->scrollArea->mapFromGlobal(event->globalPos()))) { // // 获取 QScrollArea 的横向滚动条 // QScrollBar *scrollBar = ui->scrollArea->horizontalScrollBar(); // // 根据滚轮滚动方向改变滚动条的值 // if (event->angleDelta().y() > 0) { // scrollBar->setValue(scrollBar->value() - 50); // } else { // scrollBar->setValue(scrollBar->value() + 50); // } //} QMainWindow::wheelEvent(event); } void MainAndSecondaryCamerasWnd::showEvent(QShowEvent *event) { isUpdatingSettings = false; QMainWindow::showEvent(event); isUpdatingSettings = false; } void MainAndSecondaryCamerasWnd::hideEvent(QHideEvent *event) { isUpdatingSettings = false; QMainWindow::hideEvent(event); } QStringList MainAndSecondaryCamerasWnd::getImageslist(const QString& path) { // 创建 QDir 对象 QDir dir(path); // 检查文件夹是否存在 if (!dir.exists()) { qWarning() << "Folder does not exist:" ; } // 获取文件夹中的所有图片文件 QStringList filters; filters << "*.png" << "*.jpg" << "*.jpeg" << "*.bmp" << "*.gif"; // 添加你需要的图片格式 QStringList fileList = dir.entryList(filters, QDir::Files); return fileList; }