SingleCameraOperationWnd.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #include "SingleCameraOperationWnd.h"
  2. #include "ui_SingleCameraOperationWnd.h"
  3. #include <QSettings>
  4. #include <QCloseEvent>
  5. #include <QVBoxLayout>
  6. #include <QDebug>
  7. #include <QTimer>
  8. #include "CameraMaterialGroupWnd/CameraImage/CameraImageHandler.h"
  9. SingleCameraOperationWnd::SingleCameraOperationWnd(QWidget* parent)
  10. : JOriginalMainWnd(parent)
  11. , ui(new Ui::SingleCameraOperationWnd), scaleFactor(1.0)
  12. {
  13. ui->setupUi(this);
  14. ui->viewwidgetgroup->resizeSingleUI(true);
  15. ui->lightJoystickSwitchPage->resizeSingleUI();
  16. isShow = true;
  17. connect(ui->viewwidgetgroup, &ControlOperationPage::send_ComboBox_singal, this, &SingleCameraOperationWnd::HandleComboxchange);
  18. initFrom();
  19. }
  20. SingleCameraOperationWnd::~SingleCameraOperationWnd()
  21. {
  22. UnCameraBind();
  23. delete ui;
  24. }
  25. void SingleCameraOperationWnd::initFrom()
  26. {
  27. // 设置右上部分
  28. QWidget* viewport = ui->scrollArea->viewport();
  29. QWidget* container = new QWidget(viewport);
  30. QVBoxLayout* layout = new QVBoxLayout(container);
  31. layout->setSpacing(16); // 设置Group之间的间隔距离
  32. layout->setMargin(0);
  33. int nSize = 0;
  34. if (m_pMainCameraBind != nullptr)
  35. {
  36. //QList<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
  37. nSize = m_pMainCameraBind->m_vecCamera.size();
  38. if (nSize == 0) // 针对获取不到的情况
  39. {
  40. nSize = 5;
  41. //JMessageTip::Message_warning("a");
  42. }
  43. QList<int> numbers;
  44. for (int i = 1; i <= nSize; i++)
  45. {
  46. numbers.push_back(i);
  47. }
  48. for (int i = 0; i < numbers.size(); ++i)
  49. {
  50. int num = numbers[i];
  51. CameraImageHandler* manager = nullptr;
  52. if (nSize == 5) // 测试数据
  53. {
  54. CameraInfo test;
  55. test.iCameraId = i;
  56. test.name = ("camera" + QString::number(i)).toStdString();
  57. test.eType = MATERIAL_BOX;
  58. manager = new CameraImageHandler(num, test);
  59. }
  60. else
  61. {
  62. m_pMainCameraBind->m_vecCamera[i].eType = MATERIAL_BOX;
  63. manager = new CameraImageHandler(num, m_pMainCameraBind->m_vecCamera[i]);
  64. }
  65. Group* widget = manager->getGroup();
  66. if (m_pMainCameraBind->m_vecCamera.size() > 0)
  67. {
  68. widget->SetCameaInfo(m_pMainCameraBind->m_vecCamera[i]);
  69. }
  70. if (widget != nullptr)
  71. {
  72. CameraConnectUpdateImageFun(i, widget);
  73. connect(widget, &Group::sendUpdateGroupState,
  74. this, &SingleCameraOperationWnd::checkSettings);
  75. connect(widget, &Group::send_ComboBox_singal,
  76. this, &SingleCameraOperationWnd::GetGroupComboxChanged);
  77. layout->addWidget(widget);
  78. m_allGroup.append(widget);
  79. groupMap[num] = widget;
  80. }
  81. if (manager->getWafer())
  82. {
  83. waferMap.insert(num, manager->getWafer());
  84. ui->viewwidgetgroup->setWafer(waferMap.value(i + 1));
  85. m_allGroup[i]->setWaferWidget(ui->viewwidgetgroup->getWafer()->getGlobalPixmap());
  86. }
  87. if (manager->getWaffle())
  88. {
  89. waffleMap.insert(num, manager->getWaffle());
  90. ui->viewwidgetgroup->setWaffle(waffleMap.value(i + 1));
  91. m_allGroup[i]->setWaffleWidget(ui->viewwidgetgroup->getWaffle()->getGlobalPixmap());
  92. }
  93. if (manager->getMaterialBox())
  94. {
  95. materialBoxMap.insert(num, manager->getMaterialBox());
  96. }
  97. if (manager->getBond())
  98. {
  99. bondMap.insert(num, manager->getBond());
  100. ui->viewwidgetgroup->setBond(bondMap.value(i + 1));
  101. m_allGroup[i]->setBondWidget(ui->viewwidgetgroup->getBond()->getGlobalPixmap());
  102. }
  103. if (!manager->getFileList().isEmpty())
  104. {
  105. m_mapFileListMap.insert(num, manager->getFileList());
  106. }
  107. delete manager;
  108. }
  109. //清理ui->viewwidgetgroup
  110. int width = ui->viewwidgetgroup->width();
  111. int height = ui->viewwidgetgroup->height();
  112. QPixmap pixmap(width, height);
  113. pixmap.fill(Qt::white); // 白色背景
  114. GetGroupImageShowSignals(pixmap);
  115. // 设置控件的最小高度和最大宽度
  116. int minHeight = 162;
  117. int maxWidth = 244;
  118. for (Group* w : m_allGroup) {
  119. w->setMinimumHeight(minHeight);
  120. w->setMaximumWidth(maxWidth);
  121. }
  122. container->setLayout(layout);
  123. ui->scrollArea->setWidget(container);
  124. ui->scrollArea->resize(261, 700);
  125. initWidget();
  126. }
  127. }
  128. void SingleCameraOperationWnd::closeEvent(QCloseEvent* event) {
  129. // 保存滑块状态
  130. // saveSliderStates();
  131. // 确保调用基类的 closeEvent 处理其他关闭逻辑
  132. QMainWindow::closeEvent(event);
  133. }
  134. void SingleCameraOperationWnd::checkSettings(int groupId, int index) {
  135. if (groupId != lastGroupId || index != lastIndex) {
  136. GetCurrentSelectSlots(groupId, index);
  137. lastGroupId = groupId;
  138. lastIndex = index;
  139. saveInfoOfLast();
  140. }
  141. }
  142. void SingleCameraOperationWnd::updateMaterialWidget(int materialWndType, int groupId)
  143. {
  144. switch (materialWndType)
  145. {
  146. case 1: ui->viewwidgetgroup->setWafer(waferMap.value(groupId)); break;
  147. case 2: ui->viewwidgetgroup->setWaffle(waffleMap.value(groupId)); break;
  148. case 3: ui->viewwidgetgroup->setMaterialBox(materialBoxMap.value(groupId)); break;
  149. case 4: ui->viewwidgetgroup->setBond(bondMap.value(groupId)); break;
  150. }
  151. }
  152. void SingleCameraOperationWnd::InitMainCameraBind(CameraBind* pCameraBind)
  153. {
  154. m_pMainCameraBind = pCameraBind;
  155. initFrom();
  156. }
  157. void SingleCameraOperationWnd::UnCameraBind()
  158. {
  159. for (size_t i = 0; i < m_allGroup.size(); i++)
  160. {
  161. CameraConnectUpdateImageFun(i, m_allGroup[i], true);
  162. }
  163. //判断退出前的窗口是否是绑定相机
  164. if (m_veCurrentSelectGroup.isBond == true) {
  165. //解绑
  166. disconnect(m_veCurrentSelectGroup.pSelectGroup, &Group::SetUpDataImageShowSig,
  167. this, &SingleCameraOperationWnd::GetGroupImageShowSignals);
  168. }
  169. }
  170. void SingleCameraOperationWnd::CameraConnectUpdateImageFun(int nIndex, Group* widget, bool isUnCameraBind /*= false*/)
  171. {
  172. auto BinCamerasImage = [&](void (SingleCameraOperationWnd::* pCamerasImage)(const QPixmap& imageData))
  173. {
  174. if (isUnCameraBind)
  175. {
  176. // disconnect(widget, &Group::SetCurrentSelectSig, this, &SingleCameraOperationWnd::checkSettings);
  177. disconnect(this, pCamerasImage, widget, &Group::UpDataImageShowSlots);
  178. }
  179. else
  180. {
  181. //共用函数
  182. //connect(widget, &Group::sendUpdateGroupState, this, &SingleCameraOperationWnd::checkSettings);
  183. connect(this, pCamerasImage, widget, &Group::UpDataImageShowSlots);
  184. }
  185. };
  186. if (nIndex == 0)
  187. {
  188. BinCamerasImage(&SingleCameraOperationWnd::UpDataImageShowSignals0);
  189. }
  190. else if (nIndex == 1)
  191. {
  192. BinCamerasImage(&SingleCameraOperationWnd::UpDataImageShowSignals1);
  193. }
  194. else if (nIndex == 2)
  195. {
  196. BinCamerasImage(&SingleCameraOperationWnd::UpDataImageShowSignals2);
  197. }
  198. else if (nIndex == 3)
  199. {
  200. BinCamerasImage(&SingleCameraOperationWnd::UpDataImageShowSignals3);
  201. }
  202. else if (nIndex == 4)
  203. {
  204. BinCamerasImage(&SingleCameraOperationWnd::UpDataImageShowSignals4);
  205. }
  206. }
  207. void SingleCameraOperationWnd::UpdateCameraDisplay0(int iCameraId, JVision::ImageInfo imageData)
  208. {
  209. QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
  210. emit UpDataImageShowSignals0(QPixmap::fromImage(image));
  211. }
  212. void SingleCameraOperationWnd::UpdateCameraDisplay1(int iCameraId, JVision::ImageInfo imageData)
  213. {
  214. QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
  215. emit UpDataImageShowSignals1(QPixmap::fromImage(image));
  216. }
  217. void SingleCameraOperationWnd::UpdateCameraDisplay2(int iCameraId, JVision::ImageInfo imageData)
  218. {
  219. QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
  220. emit UpDataImageShowSignals2(QPixmap::fromImage(image));
  221. }
  222. void SingleCameraOperationWnd::UpdateCameraDisplay3(int iCameraId, JVision::ImageInfo imageData)
  223. {
  224. QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
  225. emit UpDataImageShowSignals3(QPixmap::fromImage(image));
  226. }
  227. void SingleCameraOperationWnd::UpdateCameraDisplay4(int iCameraId, JVision::ImageInfo imageData)
  228. {
  229. QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
  230. emit UpDataImageShowSignals4(QPixmap::fromImage(image));
  231. }
  232. void SingleCameraOperationWnd::wheelEvent(QWheelEvent* event)
  233. {
  234. //mousePos = ui->viewwidgetgroup->getOperatewidget()->mapFromGlobal(event->globalPos());
  235. //if (ui->viewwidgetgroup->getOperatewidget()->rect().contains(ui->viewwidgetgroup->getOperatewidget()->mapFromGlobal(event->globalPos()))) {
  236. // if (event->angleDelta().y() > 0) {
  237. // ui->viewwidgetgroup->updateScale(scaleFactor * 1.1); // 放大
  238. // } else {
  239. // ui->viewwidgetgroup->updateScale(scaleFactor * 0.9); // 缩小
  240. // }
  241. //}
  242. //if (ui->scrollArea->rect().contains(ui->scrollArea->mapFromGlobal(event->globalPos()))) {
  243. // // 获取 QScrollArea 的横向滚动条
  244. // QScrollBar *scrollBar = ui->scrollArea->horizontalScrollBar();
  245. // // 根据滚轮滚动方向改变滚动条的值
  246. // if (event->angleDelta().y() > 0) {
  247. // scrollBar->setValue(scrollBar->value() - 50);
  248. // } else {
  249. // scrollBar->setValue(scrollBar->value() + 50);
  250. // }
  251. //}
  252. QMainWindow::wheelEvent(event);
  253. }
  254. QString SingleCameraOperationWnd::getImagePathFromIndex(int index) {
  255. QStringList imagePaths = {
  256. ":/images/test_image/image.png",
  257. ":/images/test_image/image_2.png",
  258. };
  259. if (index >= 0 && index < imagePaths.size()) {
  260. return imagePaths[index];
  261. }
  262. return ":/images/test_image/image.png";
  263. }
  264. void SingleCameraOperationWnd::showEvent(QShowEvent* event) {
  265. QMainWindow::showEvent(event);
  266. }
  267. void SingleCameraOperationWnd::hideEvent(QHideEvent* event) {
  268. QMainWindow::hideEvent(event);
  269. }
  270. #if 0
  271. void SingleCameraOperationWnd::on_DatacomboBox_currentIndexChanged(int index) {
  272. QSettings settings("YourCompany", "YourApplication_");
  273. int groupId = settings.value("GroupId", 0).toInt();
  274. if (groupMap.contains(groupId)) {
  275. Group* group = groupMap[groupId];
  276. // 修改属性
  277. group->setDatacomboBox(index);
  278. }
  279. else {
  280. qDebug() << "Group with id" << groupId << "not found.";
  281. }
  282. }
  283. #endif
  284. void SingleCameraOperationWnd::updateImage(const QImage& image) {
  285. QPixmap pixmap = QPixmap::fromImage(image);
  286. QSize size = ui->viewwidgetgroup->getOperatewidget()->size();
  287. // QPixmap scaledPixmap = pixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  288. QPixmap scaledPixmap = pixmap.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  289. ui->viewwidgetgroup->getOperatewidget()->setPixmap(scaledPixmap);
  290. }
  291. void SingleCameraOperationWnd::GetGroupImageShowSignals(const QPixmap& imageData) {
  292. QSize size = ui->viewwidgetgroup->getOperatewidget()->size();
  293. QPixmap scaledPixmap = imageData.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  294. ui->viewwidgetgroup->setScaleFactorSize(scaledPixmap);
  295. // m_lastRightPixmap = imageData;
  296. }
  297. void SingleCameraOperationWnd::BindImageOrMaterial(int index) {
  298. if (index == m_nImageIndex) {
  299. m_veCurrentSelectGroup.isBond = true;
  300. connect(m_veCurrentSelectGroup.pSelectGroup, &Group::SetUpDataImageShowSig,
  301. this, &SingleCameraOperationWnd::GetGroupImageShowSignals);
  302. UpdataLightJoystickSwitchPage(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId, true);
  303. }
  304. else {
  305. QSettings settings("YourOrganization", "YourApplication");
  306. settings.beginGroup(QString::number(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId));
  307. int materialWndType = settings.value("MaterialWndType").toInt();
  308. settings.endGroup();
  309. m_veCurrentSelectGroup.isBond = false;
  310. UpdataLightJoystickSwitchPage(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId, false);
  311. updateMaterialWidget(materialWndType, m_veCurrentSelectGroup.pSelectGroup->m_nGroupId);
  312. }
  313. }
  314. void SingleCameraOperationWnd::initWidget() {
  315. //初始化当前页面的信息
  316. QSettings settings("YourOrganization", "YourApplication");
  317. lastIndex = settings.value("SingleLastIndex", 1).toInt();
  318. lastGroupId = settings.value("SingleLastGroupId", 1).toInt();
  319. //处理各种情况的初始化
  320. if (m_pMainCameraBind == nullptr || m_allGroup.size() == 0)
  321. {
  322. return;
  323. }
  324. //当lastGroupId大于m_allGroup.size()时,初始化lastGroupId为1
  325. if (m_allGroup.size() < lastGroupId) {
  326. lastGroupId = 1;
  327. lastIndex = 1;
  328. m_veCurrentSelectGroup.isInitialed = true;
  329. m_veCurrentSelectGroup.pSelectGroup = m_allGroup.at(lastGroupId - 1);
  330. m_veCurrentSelectGroup.pSelectGroup->initBorderStyle(lastIndex, 1);
  331. //初始化下拉框内容同步小窗口索引
  332. int currentComboBoxIndex = m_veCurrentSelectGroup.pSelectGroup->getCurrentComboBoxIndex();
  333. ui->viewwidgetgroup->setEnableControls(true);
  334. ui->viewwidgetgroup->setComboBox(m_mapFileListMap[m_veCurrentSelectGroup.pSelectGroup->m_nGroupId], currentComboBoxIndex);
  335. BindImageOrMaterial(m_nImageIndex);
  336. }
  337. else {
  338. }
  339. m_veCurrentSelectGroup.isInitialed = true;
  340. m_veCurrentSelectGroup.pSelectGroup = m_allGroup.at(lastGroupId - 1);
  341. m_veCurrentSelectGroup.pSelectGroup->initBorderStyle(lastIndex, 1);
  342. //初始化下拉框内容同步小窗口索引
  343. int currentComboBoxIndex = m_veCurrentSelectGroup.pSelectGroup->getCurrentComboBoxIndex();
  344. ui->viewwidgetgroup->setComboBox(m_mapFileListMap[m_veCurrentSelectGroup.pSelectGroup->m_nGroupId], currentComboBoxIndex);
  345. BindImageOrMaterial(m_nImageIndex);
  346. if (lastIndex == 1) {
  347. ui->viewwidgetgroup->setEnableControls(true);
  348. BindImageOrMaterial(m_nImageIndex);
  349. }
  350. else {
  351. ui->viewwidgetgroup->setEnableControls(false);
  352. BindImageOrMaterial(m_nMaterialIndex);
  353. }
  354. }
  355. void SingleCameraOperationWnd::saveInfoOfLast() {
  356. QSettings settings("YourOrganization", "YourApplication");
  357. settings.setValue("SingleLastIndex", lastIndex);
  358. settings.setValue("SingleLastGroupId", lastGroupId);
  359. }
  360. void SingleCameraOperationWnd::GetCurrentSelectSlots(int groupId, int nIndex) {
  361. int nOnClickGroupId = groupId - 1;
  362. if (m_pMainCameraBind == nullptr || m_allGroup.size() < groupId)
  363. {
  364. return;
  365. }
  366. //判断更新前的窗口是否是绑定相机
  367. if (m_veCurrentSelectGroup.isBond == true) {
  368. //解绑
  369. disconnect(m_veCurrentSelectGroup.pSelectGroup, &Group::SetUpDataImageShowSig,
  370. this, &SingleCameraOperationWnd::GetGroupImageShowSignals);
  371. }
  372. m_veCurrentSelectGroup.pSelectGroup = m_allGroup.at(nOnClickGroupId);
  373. //判断index是相机还是物料
  374. //更新下拉框内容同步小窗口索引
  375. int currentComboBoxIndex = m_veCurrentSelectGroup.pSelectGroup->getCurrentComboBoxIndex();
  376. ui->viewwidgetgroup->setComboBox(m_mapFileListMap[m_veCurrentSelectGroup.pSelectGroup->m_nGroupId], currentComboBoxIndex);
  377. if (nIndex == 2) {
  378. BindImageOrMaterial(m_nMaterialIndex);
  379. ui->viewwidgetgroup->setEnableControls(false);
  380. }
  381. else if (nIndex == 1) {
  382. ui->viewwidgetgroup->setEnableControls(true);
  383. BindImageOrMaterial(m_nImageIndex);
  384. QPixmap pixmap = ui->viewwidgetgroup->getCurrentComboBoxPixmap(currentComboBoxIndex);
  385. GetGroupImageShowSignals(pixmap);
  386. }
  387. }
  388. void SingleCameraOperationWnd::UpdataLightJoystickSwitchPage(int groupId, bool isShow)
  389. {
  390. auto Fun = [&](Group* pGroup, LightJoystickSwitchPage* p, ControlOperationPage* pContPage, bool bShow)
  391. {
  392. p->UpdatemPageGroup(pGroup);
  393. p->InitMainCameraBind(m_pMainCameraBind, bShow);
  394. p->HideOrShowPage(bShow);
  395. if (bShow)
  396. {
  397. pContPage->UpDateCameraBind(m_pMainCameraBind);
  398. }
  399. else
  400. {
  401. pContPage->setSwitchJoystickButEnable(bShow);
  402. }
  403. };
  404. disconnect(ui->viewwidgetgroup, &ControlOperationPage::SendModuleTypeSignals,
  405. ui->lightJoystickSwitchPage, &LightJoystickSwitchPage::GetModuleTypeSlots);
  406. Group* currGroup = m_allGroup.at(groupId - 1);
  407. Fun(currGroup, ui->lightJoystickSwitchPage, ui->viewwidgetgroup, isShow);
  408. connect(ui->viewwidgetgroup, &ControlOperationPage::SendModuleTypeSignals,
  409. ui->lightJoystickSwitchPage, &LightJoystickSwitchPage::GetModuleTypeSlots);
  410. }
  411. void SingleCameraOperationWnd::GetGroupComboxChanged(int groupId, int comboxIndex) {
  412. m_allGroup.at(groupId - 1);
  413. if (comboxIndex == 0) {
  414. CameraConnectUpdateImageFun(groupId - 1, m_allGroup.at(groupId - 1), false);
  415. }
  416. else {
  417. CameraConnectUpdateImageFun(groupId - 1, m_allGroup.at(groupId - 1), true);
  418. }
  419. m_allGroup.at(groupId - 1)->setCurrentCombox(comboxIndex);
  420. if (groupId == m_veCurrentSelectGroup.pSelectGroup->m_nGroupId && lastIndex == 1) {
  421. if (comboxIndex != 0) {//图片
  422. QPixmap updatePixmap = ui->viewwidgetgroup->getCurrentComboBoxPixmap(comboxIndex);
  423. m_veCurrentSelectGroup.pSelectGroup->UpDataImageShowSlots(updatePixmap);
  424. }
  425. }
  426. }
  427. void SingleCameraOperationWnd::HandleComboxchange(int index) {
  428. if (index == 0) {
  429. CameraConnectUpdateImageFun(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId - 1, m_allGroup.at(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId - 1), false);
  430. }
  431. else {
  432. CameraConnectUpdateImageFun(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId - 1, m_allGroup.at(m_veCurrentSelectGroup.pSelectGroup->m_nGroupId - 1), true);
  433. QPixmap updatePixmap = ui->viewwidgetgroup->getCurrentComboBoxPixmap(index);
  434. m_veCurrentSelectGroup.pSelectGroup->UpDataImageShowSlots(updatePixmap);
  435. }
  436. }