SingleCameraOperationWnd.cpp 17 KB

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