SingleCameraOperationWnd.cpp 17 KB

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