ControlOperationPage.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "ControlOperationPage.h"
  2. #include "ui_ControlOperationPage.h"
  3. #include "JoystickPage.h"
  4. ControlOperationPage::ControlOperationPage(QWidget* parent)
  5. : QWidget(parent)
  6. , ui(new Ui::ControlOperationPage)
  7. {
  8. ui->setupUi(this);
  9. InitWnd();
  10. }
  11. ControlOperationPage::~ControlOperationPage()
  12. {
  13. delete ui;
  14. }
  15. void ControlOperationPage::updateOperateWidget(const QPixmap& pixmap) {
  16. QSize size = ui->Operatewidget->size();
  17. QPixmap scaledPixmap = pixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  18. clearLayout();
  19. ui->Operatewidget->setPixmap(scaledPixmap);
  20. m_currentPixmap = scaledPixmap;
  21. m_scaleFactor = 1.0;
  22. m_previousScaleFactor = 1.0;
  23. ui->label_Percentage->setText("100%");
  24. m_currentMode = ModeImage;
  25. }
  26. void ControlOperationPage::setDataSources(const QStringList& textList) {
  27. ui->DataSources->clear();
  28. ui->DataSources->addItems(textList);
  29. }
  30. // 清除大窗口上当前的布局
  31. void ControlOperationPage::clearLayout() {
  32. // 获取当前布局
  33. QLayout* layout = ui->Operatewidget->layout();
  34. if (layout) {
  35. QLayoutItem* child;
  36. while ((child = layout->takeAt(0)) != nullptr) {
  37. if (child->widget() != nullptr) {
  38. delete child->widget(); // 删除控件
  39. }
  40. delete child; // 删除布局项
  41. }
  42. delete layout; // 删除布局本身
  43. }
  44. }
  45. void ControlOperationPage::on_ZoomUpButton_clicked()
  46. {
  47. m_mousePos = ui->Operatewidget->geometry().center();
  48. updateScale(m_scaleFactor * 1.1);
  49. }
  50. void ControlOperationPage::on_ZoomOutButton_clicked()
  51. {
  52. m_mousePos = ui->Operatewidget->geometry().center();
  53. updateScale(m_scaleFactor * 0.9);
  54. }
  55. // 更新缩放比例
  56. void ControlOperationPage::updateScale(double newScaleFactor)
  57. {
  58. if (newScaleFactor >= 1.0)
  59. {
  60. m_scaleFactor = newScaleFactor;
  61. }
  62. else {
  63. m_scaleFactor = 1.0; // 最小缩放比例为 1.0
  64. }
  65. applyScale(); // 应用缩放
  66. }
  67. // 应用缩放
  68. void ControlOperationPage::applyScale()
  69. {
  70. if (m_currentMode == ModeImage)
  71. {
  72. // 图片模式:缩放图片
  73. int newWidth = m_currentPixmap.width() * m_scaleFactor;
  74. int newHeight = m_currentPixmap.height() * m_scaleFactor;
  75. QPixmap scaledImage = m_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  76. ui->Operatewidget->setPixmapAndPoint(scaledImage, m_previousScaleFactor, m_scaleFactor, m_mousePos);
  77. m_previousScaleFactor = m_scaleFactor;
  78. }
  79. else if (m_currentMode == ModeView && m_currentView)
  80. {
  81. // View 模式:缩放 view
  82. QTransform transform;
  83. transform.scale(m_scaleFactor, m_scaleFactor);
  84. m_currentView->setTransform(transform);
  85. }
  86. // 更新百分比显示
  87. double percentage = m_scaleFactor * 100;
  88. QString percentageStr = QString::number((int)percentage);
  89. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  90. }
  91. void ControlOperationPage::handleDoubleClick() {
  92. if (m_currentMode == ModeImage)
  93. {
  94. QPixmap scaledImage = m_currentPixmap.scaled(m_currentPixmap.width(), m_currentPixmap.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  95. ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
  96. }
  97. else if (m_currentMode == ModeView && m_currentView)
  98. {
  99. QTransform transform;
  100. transform.scale(1, 1);
  101. m_currentView->setTransform(transform);
  102. }
  103. m_scaleFactor = 1.0;
  104. m_previousScaleFactor = 1.0;
  105. ui->label_Percentage->setText("100%");
  106. }
  107. void ControlOperationPage::updateMaterialWidget(kinds materialWndType)
  108. {
  109. clearLayout();
  110. switch (materialWndType)
  111. {
  112. case wafer_kind: KindsofWidget(wafer_kind); break;
  113. case waffle_kind: KindsofWidget(waffle_kind); break;
  114. case materialbox_kind: KindsofWidget(materialbox_kind); break;
  115. case bond_kind:KindsofWidget(bond_kind); break;
  116. }
  117. }
  118. void ControlOperationPage::KindsofWidget(kinds kind)
  119. {
  120. bool isRun = false;
  121. ui->Operatewidget->clearPixmap();
  122. QVBoxLayout* layout = new QVBoxLayout(ui->Operatewidget);
  123. layout->setContentsMargins(0, 0, 0, 0);
  124. m_currentMode = ModeView;
  125. if (kind == wafer_kind)
  126. {
  127. if (m_wafer)
  128. {
  129. isRun = true;
  130. m_wafer->initFrom(ui->Operatewidget);
  131. layout->addWidget(m_wafer->view);
  132. m_currentView = m_wafer->view;
  133. }
  134. }
  135. else if (kind == waffle_kind)
  136. {
  137. if (m_waffle)
  138. {
  139. isRun = true;
  140. m_waffle->initFrom(ui->Operatewidget);
  141. layout->addWidget(m_waffle->view);
  142. m_currentView = m_waffle->view;
  143. }
  144. }
  145. else if (kind == materialbox_kind)
  146. {
  147. if (m_materialbox)
  148. {
  149. isRun = true;
  150. m_materialbox->initFrom(ui->Operatewidget);
  151. layout->addWidget(m_materialbox->view);
  152. m_currentView = m_materialbox->view;
  153. }
  154. }
  155. else if (kind == bond_kind)
  156. {
  157. if (m_bond)
  158. {
  159. isRun = true;
  160. m_bond->initFrom(ui->Operatewidget);
  161. layout->addWidget(m_bond->view);
  162. m_currentView = m_bond->view;
  163. }
  164. }
  165. if (isRun)
  166. {
  167. ui->Operatewidget->setLayout(layout);
  168. m_currentMode = ModeView;
  169. m_scaleFactor = 1.0;
  170. applyScale();
  171. }
  172. }
  173. void ControlOperationPage::setWafer(Wafer* wafer) {
  174. m_wafer = wafer;
  175. updateMaterialWidget(wafer_kind);
  176. }
  177. void ControlOperationPage::setWaffle(Waffle* waffle) {
  178. m_waffle = waffle;
  179. updateMaterialWidget(waffle_kind);
  180. }
  181. void ControlOperationPage::setMaterialBox(MaterialBox* materialbox) {
  182. m_materialbox = materialbox;
  183. updateMaterialWidget(materialbox_kind);
  184. }
  185. void ControlOperationPage::setBond(Bond* bond) {
  186. m_bond = bond;
  187. updateMaterialWidget(bond_kind);
  188. }
  189. void ControlOperationPage::initForm()
  190. {
  191. connect(ui->Operatewidget, &ImageWidget::sendDoubleClicksignal, this, &ControlOperationPage::handleDoubleClick);
  192. ui->Operatewidget->setMouseTracking(true);
  193. }
  194. ImageWidget* ControlOperationPage::getOperatewidget()
  195. {
  196. return ui->Operatewidget;
  197. }
  198. void ControlOperationPage::resizeSingleUI(bool bMax /*= false*/)
  199. {
  200. ui->DataSources->setGeometry(QRect(20, 20, 400, 32));
  201. ui->Operatewidget->setGeometry(QRect(5, 5, 786, 786));
  202. //ui->line_2->setGeometry(QRect(826, 20, 1, 953));
  203. ui->LiveButton->setGeometry(QRect(436, 20, 60, 32));
  204. ui->horizontalLayout_2->setGeometry(QRect(12, 882, 786, 32));
  205. ui->layoutWidget->setGeometry(QRect(10, 882, 790, 31));
  206. ui->ZoomUpButton->setGeometry(QRect(25, 882, 120, 32));
  207. ui->ZoomOutButton->setGeometry(QRect(155, 882, 100, 32));
  208. ui->label_Percentage->setGeometry(QRect(265, 882, 120, 32));
  209. ui->RulerButton->setGeometry(QRect(400, 882, 110, 32));
  210. ui->PenButton->setGeometry(QRect(530, 882, 100, 32));
  211. // ui->switchJoystickBut
  212. ui->horizontalLayout->setGeometry(QRect(12, 882, 786, 32));
  213. ui->BackGround->setGeometry(QRect(16, 68, 794, 794));
  214. if (bMax)
  215. {
  216. ui->Operatewidget->m_nSingleCameraOperationWnd = true;
  217. }
  218. }
  219. void ControlOperationPage::resizeChartsAndCamerasUI() {
  220. }
  221. qreal ControlOperationPage::getScaleFactorValue() {
  222. return m_scaleFactor;
  223. }
  224. void ControlOperationPage::setScaleFactorSize(QPixmap scaledImage) {
  225. int newWidth = scaledImage.width() * m_scaleFactor;
  226. int newHeight = scaledImage.height() * m_scaleFactor;
  227. QPixmap curr_scaledImage = scaledImage.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  228. ui->Operatewidget->setPixmapAndPoint(curr_scaledImage, m_previousScaleFactor, m_scaleFactor, m_mousePos);
  229. ui->Operatewidget->setPixmap(curr_scaledImage);
  230. }
  231. void ControlOperationPage::UpDateCameraBind(CameraBind* pCameraBind)
  232. {
  233. m_pCameraBindCopy = pCameraBind;
  234. m_isAdd = true;
  235. // 有指针了在去刷新
  236. if (m_pCameraBindCopy != nullptr)
  237. {
  238. DeduplicationBox(ui->moduleTypeComboBox, m_pCameraBindCopy->m_vecCAxis, 0);
  239. DeduplicationBox(ui->axisTypeComboBox, m_pCameraBindCopy->m_vecCAxis, 1);
  240. }
  241. m_isAdd = false;
  242. setSwitchJoystickButEnable(true);
  243. }
  244. void ControlOperationPage::setSwitchJoystickButEnable(bool isEnable)
  245. {
  246. ui->switchJoystickBut->setEnabled(isEnable);
  247. if (isEnable == false)
  248. {
  249. ResetIdleTimer(false);
  250. m_isUserOnclick = false;
  251. }
  252. }
  253. void ControlOperationPage::on_switchJoystickBut_clicked()
  254. {
  255. m_isUserOnclick = true;
  256. ResetIdleTimer(true);
  257. UpdataModuleType("aaa", 3);
  258. }
  259. void ControlOperationPage::MouseMovedSlots(const QPoint& delta)
  260. {
  261. qDebug() << "MouseMovedSlots:" << delta;
  262. if (m_pCameraBindCopy)
  263. {
  264. m_pCameraBindCopy->JCameraMove(0, delta.x(), delta.y());
  265. }
  266. }
  267. void ControlOperationPage::RequestCursorMoveSlots(const QPoint& pos)
  268. {
  269. QCursor::setPos(pos);
  270. }
  271. void ControlOperationPage::on_moduleTypeComboBox_currentIndexChanged(int index)
  272. {
  273. m_isAdd = true;
  274. QString strMod = ui->moduleTypeComboBox->itemText(index);
  275. ui->axisTypeComboBox->clear();
  276. for (auto a: m_pCameraBindCopy->m_vecCAxis)
  277. {
  278. // 如果2个相等说明当前模组,与轴匹配
  279. if (strMod == a->GetModuleType().c_str())
  280. {
  281. ui->axisTypeComboBox->addItem(a->GetStringAxisType().c_str());
  282. }
  283. }
  284. m_isAdd = false;
  285. UpdataModuleType(strMod, 1);
  286. }
  287. void ControlOperationPage::on_axisTypeComboBox_currentIndexChanged(int index)
  288. {
  289. if (!m_isAdd)
  290. {
  291. UpdataModuleType(ui->axisTypeComboBox->itemText(index), 2);
  292. }
  293. }
  294. void ControlOperationPage::timerEvent(QTimerEvent* event)
  295. {
  296. int nID = event->timerId();
  297. if (nID == m_idleTimer)
  298. {
  299. ResetIdleTimer(false);
  300. m_isUserOnclick = false;
  301. }
  302. }
  303. void ControlOperationPage::mousePressEvent(QMouseEvent* event)
  304. {
  305. if (event->button() == Qt::LeftButton)
  306. {
  307. LockMouse(true);
  308. }
  309. else if (event->button() == Qt::RightButton)
  310. {
  311. LockMouse(false);
  312. }
  313. }
  314. void ControlOperationPage::mouseMoveEvent(QMouseEvent* event)
  315. {
  316. int a = 10;
  317. }
  318. bool ControlOperationPage::eventFilter(QObject* obj, QEvent* event)
  319. {
  320. switch (event->type())
  321. {
  322. case QEvent::KeyPress:
  323. case QEvent::MouseMove:
  324. case QEvent::MouseButtonPress:
  325. case QEvent::MouseButtonDblClick:
  326. case QEvent::Wheel:
  327. {
  328. if (m_isUserOnclick)
  329. {
  330. ResetIdleTimer(false);
  331. ResetIdleTimer(true);
  332. }
  333. }
  334. break;
  335. default:
  336. break;
  337. }
  338. return QWidget::eventFilter(obj, event);
  339. }
  340. void ControlOperationPage::wheelEvent(QWheelEvent* event) {
  341. if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) {
  342. m_mousePos = ui->Operatewidget->mapFromGlobal(event->globalPos());
  343. if (event->angleDelta().y() > 0) {
  344. updateScale(m_scaleFactor * 1.1); // 放大
  345. }
  346. else {
  347. updateScale(m_scaleFactor * 0.9); // 缩小
  348. }
  349. }
  350. }
  351. void ControlOperationPage::HideLayout(bool bShow)
  352. {
  353. for (int i = 0; i < ui->horizontalLayout->count(); ++i)
  354. {
  355. QWidget* widget = ui->horizontalLayout->itemAt(i)->widget();
  356. if (widget)
  357. {
  358. if (bShow)
  359. {
  360. widget->show();
  361. }
  362. else
  363. {
  364. widget->hide();
  365. }
  366. }
  367. }
  368. }
  369. void ControlOperationPage::ResetIdleTimer(bool bStart /*= false*/)
  370. {
  371. if (bStart)
  372. {
  373. if (isActiveWindow())
  374. {
  375. m_idleTimer = startTimer(g_unnSuspensionWaitingTime);
  376. }
  377. }
  378. else
  379. {
  380. killTimer(m_idleTimer);
  381. m_idleTimer = -1;
  382. }
  383. HideLayout(bStart);
  384. }
  385. void ControlOperationPage::InitWnd()
  386. {
  387. HideLayout(false);
  388. /* connect(ui->widgetPage, &JoystickPage::PositionChangedSignals, [&](qreal x, qreal y) {
  389. ui->showLabel->setText(QString("摇杆位置: (%1, %2)").arg(x, 0, 'f', 2).arg(y, 0, 'f', 2));
  390. });*/
  391. this->setMouseTracking(true);
  392. this->installEventFilter(this);
  393. }
  394. void ControlOperationPage::CreateMouseMonitor(bool isStart)
  395. {
  396. if (isStart)
  397. {
  398. m_pMousethread = new JMouseMonitorThread(this);
  399. connect(m_pMousethread, &JMouseMonitorThread::MouseMovedSlg, this, &ControlOperationPage::MouseMovedSlots);
  400. connect(m_pMousethread, &JMouseMonitorThread::RequestCursorMoveSlg, this, &ControlOperationPage::RequestCursorMoveSlots);
  401. m_pMousethread->start();
  402. }
  403. else
  404. {
  405. m_pMousethread->stop();
  406. m_pMousethread->wait();
  407. delete m_pMousethread;
  408. m_pMousethread = nullptr;
  409. }
  410. }
  411. void ControlOperationPage::LockMouse(bool islockMouse)
  412. {
  413. if (m_pCameraBindCopy)
  414. {
  415. if (islockMouse)
  416. {
  417. if (!m_bMouseRun)
  418. {
  419. CreateMouseMonitor(true);
  420. QPoint center = rect().center();
  421. QPoint globalCenter = mapToGlobal(center);
  422. m_pMousethread->setLockCenter(globalCenter);
  423. QCursor::setPos(globalCenter);
  424. setCursor(Qt::BlankCursor);
  425. grabMouse();
  426. m_bMouseRun = true;
  427. }
  428. }
  429. else
  430. {
  431. if (m_bMouseRun)
  432. {
  433. if (m_pMousethread)
  434. {
  435. m_pMousethread->unlock();
  436. releaseMouse();
  437. unsetCursor();
  438. QCursor::setPos(mapToGlobal(rect().center()));
  439. CreateMouseMonitor(false);
  440. }
  441. m_bMouseRun = false;
  442. }
  443. }
  444. }
  445. }
  446. void ControlOperationPage::UpdataModuleType(const QString& strMode, int nIndex)
  447. {
  448. if (nIndex == 1)
  449. {
  450. m_currentSelectRunAxis.ModuleType = strMode.toStdString();
  451. }
  452. else if (nIndex == 2)
  453. {
  454. m_currentSelectRunAxis.AxisType = strMode.toStdString();
  455. }
  456. if (m_isAdd == false)
  457. {
  458. emit SendModuleTypeSignals(m_currentSelectRunAxis);
  459. }
  460. }
  461. template<class T>
  462. void ControlOperationPage::DeduplicationBox(QComboBox* pCom, const T& veTemp, int nIndex)
  463. {
  464. for (auto& a : veTemp)
  465. {
  466. QString strName;
  467. if (nIndex == 0)
  468. {
  469. strName = a->GetModuleType().c_str();
  470. }
  471. else if (nIndex == 1)
  472. {
  473. strName = a->GetStringAxisType().c_str();
  474. }
  475. QStringList items;
  476. for (int i = 0; i < pCom->count(); ++i)
  477. {
  478. items << pCom->itemText(i);
  479. }
  480. bool bMa = false; // 是否匹配
  481. for (auto b : items)
  482. {
  483. if (b == strName)
  484. {
  485. bMa = true;
  486. break;
  487. }
  488. }
  489. if (!bMa)
  490. {
  491. pCom->addItem(strName);
  492. }
  493. }
  494. }
  495. void ControlOperationPage::setEnableControls(bool enable) {
  496. ui->LiveButton->setEnabled(enable);
  497. ui->DataSources->setEnabled(enable);
  498. if (enable == true) {
  499. ui->LiveButton->setStyleSheet(
  500. "QPushButton:hover {"
  501. " background-color: #45a049;"
  502. "}"
  503. "QPushButton:pressed {"
  504. " background-color: #3e8e41;"
  505. "}"
  506. );
  507. }
  508. else {
  509. ui->LiveButton->setStyleSheet("background-color: lightgray;");
  510. }
  511. }
  512. void ControlOperationPage::setBlueBord() {
  513. ui->BackGround->setStyleSheet(QString::fromUtf8("border: 4px solid blue;"));
  514. }