SingleCameraOperationWnd.cpp 17 KB

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