SingleCameraOperationWnd.cpp 18 KB

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