ControlOperationPage.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. void ControlOperationPage::UpDateCameraBind(CameraBind* pCameraBind)
  222. {
  223. m_pCameraBindCopy = pCameraBind;
  224. m_isAdd = true;
  225. // 有指针了在去刷新
  226. if (m_pCameraBindCopy != nullptr)
  227. {
  228. DeduplicationBox(ui->moduleTypeComboBox, m_pCameraBindCopy->m_vecCAxis, 0);
  229. DeduplicationBox(ui->axisTypeComboBox, m_pCameraBindCopy->m_vecCAxis, 1);
  230. }
  231. m_isAdd = false;
  232. setSwitchJoystickButEnable(true);
  233. }
  234. void ControlOperationPage::setSwitchJoystickButEnable(bool isEnable)
  235. {
  236. ui->switchJoystickBut->setEnabled(isEnable);
  237. if (isEnable == false)
  238. {
  239. ResetIdleTimer(false);
  240. m_isUserOnclick = false;
  241. }
  242. }
  243. void ControlOperationPage::on_switchJoystickBut_clicked()
  244. {
  245. m_isUserOnclick = true;
  246. ResetIdleTimer(true);
  247. UpdataModuleType("aaa", 3);
  248. }
  249. void ControlOperationPage::MouseMovedSlots(const QPoint& delta)
  250. {
  251. qDebug() << "MouseMovedSlots:" << delta;
  252. if (m_pCameraBindCopy)
  253. {
  254. m_pCameraBindCopy->JCameraMove(0, delta.x(), delta.y());
  255. }
  256. }
  257. void ControlOperationPage::RequestCursorMoveSlots(const QPoint& pos)
  258. {
  259. QCursor::setPos(pos);
  260. }
  261. void ControlOperationPage::on_moduleTypeComboBox_currentIndexChanged(int index)
  262. {
  263. m_isAdd = true;
  264. QString strMod = ui->moduleTypeComboBox->itemText(index);
  265. ui->axisTypeComboBox->clear();
  266. for (auto a: m_pCameraBindCopy->m_vecCAxis)
  267. {
  268. // 如果2个相等说明当前模组,与轴匹配
  269. if (strMod == a->GetModuleType().c_str())
  270. {
  271. ui->axisTypeComboBox->addItem(a->GetStringAxisType().c_str());
  272. }
  273. }
  274. UpdataModuleType(strMod, 1);
  275. m_isAdd = false;
  276. }
  277. void ControlOperationPage::on_axisTypeComboBox_currentIndexChanged(int index)
  278. {
  279. if (!m_isAdd)
  280. {
  281. UpdataModuleType(ui->axisTypeComboBox->itemText(index), 2);
  282. }
  283. }
  284. void ControlOperationPage::timerEvent(QTimerEvent* event)
  285. {
  286. int nID = event->timerId();
  287. if (nID == m_idleTimer)
  288. {
  289. ResetIdleTimer(false);
  290. m_isUserOnclick = false;
  291. }
  292. }
  293. void ControlOperationPage::mousePressEvent(QMouseEvent* event)
  294. {
  295. if (event->button() == Qt::LeftButton)
  296. {
  297. LockMouse(true);
  298. }
  299. else if (event->button() == Qt::RightButton)
  300. {
  301. LockMouse(false);
  302. }
  303. }
  304. void ControlOperationPage::mouseMoveEvent(QMouseEvent* event)
  305. {
  306. int a = 10;
  307. }
  308. bool ControlOperationPage::eventFilter(QObject* obj, QEvent* event)
  309. {
  310. switch (event->type())
  311. {
  312. case QEvent::KeyPress:
  313. case QEvent::MouseMove:
  314. case QEvent::MouseButtonPress:
  315. case QEvent::MouseButtonDblClick:
  316. case QEvent::Wheel:
  317. {
  318. if (m_isUserOnclick)
  319. {
  320. ResetIdleTimer(false);
  321. ResetIdleTimer(true);
  322. }
  323. }
  324. break;
  325. default:
  326. break;
  327. }
  328. return QWidget::eventFilter(obj, event);
  329. }
  330. void ControlOperationPage::wheelEvent(QWheelEvent* event) {
  331. if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) {
  332. QPoint mousePos;
  333. mousePos = ui->Operatewidget->mapFromGlobal(event->globalPos());
  334. if (event->angleDelta().y() > 0) {
  335. updateScale(m_scaleFactor * 1.1); // 放大
  336. }
  337. else {
  338. updateScale(m_scaleFactor * 0.9); // 缩小
  339. }
  340. }
  341. }
  342. void ControlOperationPage::HideLayout(bool bShow)
  343. {
  344. for (int i = 0; i < ui->horizontalLayout->count(); ++i)
  345. {
  346. QWidget* widget = ui->horizontalLayout->itemAt(i)->widget();
  347. if (widget)
  348. {
  349. if (bShow)
  350. {
  351. widget->show();
  352. }
  353. else
  354. {
  355. widget->hide();
  356. }
  357. }
  358. }
  359. }
  360. void ControlOperationPage::ResetIdleTimer(bool bStart /*= false*/)
  361. {
  362. if (bStart)
  363. {
  364. if (isActiveWindow())
  365. {
  366. m_idleTimer = startTimer(g_unnSuspensionWaitingTime);
  367. }
  368. }
  369. else
  370. {
  371. killTimer(m_idleTimer);
  372. m_idleTimer = -1;
  373. }
  374. HideLayout(bStart);
  375. }
  376. void ControlOperationPage::InitWnd()
  377. {
  378. HideLayout(false);
  379. /* connect(ui->widgetPage, &JoystickPage::PositionChangedSignals, [&](qreal x, qreal y) {
  380. ui->showLabel->setText(QString("摇杆位置: (%1, %2)").arg(x, 0, 'f', 2).arg(y, 0, 'f', 2));
  381. });*/
  382. this->setMouseTracking(true);
  383. this->installEventFilter(this);
  384. }
  385. void ControlOperationPage::CreateMouseMonitor(bool isStart)
  386. {
  387. if (isStart)
  388. {
  389. m_pMousethread = new JMouseMonitorThread(this);
  390. connect(m_pMousethread, &JMouseMonitorThread::MouseMovedSlg, this, &ControlOperationPage::MouseMovedSlots);
  391. connect(m_pMousethread, &JMouseMonitorThread::RequestCursorMoveSlg, this, &ControlOperationPage::RequestCursorMoveSlots);
  392. m_pMousethread->start();
  393. }
  394. else
  395. {
  396. m_pMousethread->stop();
  397. m_pMousethread->wait();
  398. delete m_pMousethread;
  399. m_pMousethread = nullptr;
  400. }
  401. }
  402. void ControlOperationPage::LockMouse(bool islockMouse)
  403. {
  404. if (m_pCameraBindCopy)
  405. {
  406. if (islockMouse)
  407. {
  408. if (!m_bMouseRun)
  409. {
  410. CreateMouseMonitor(true);
  411. QPoint center = rect().center();
  412. QPoint globalCenter = mapToGlobal(center);
  413. m_pMousethread->setLockCenter(globalCenter);
  414. QCursor::setPos(globalCenter);
  415. setCursor(Qt::BlankCursor);
  416. grabMouse();
  417. m_bMouseRun = true;
  418. }
  419. }
  420. else
  421. {
  422. if (m_bMouseRun)
  423. {
  424. if (m_pMousethread)
  425. {
  426. m_pMousethread->unlock();
  427. releaseMouse();
  428. unsetCursor();
  429. QCursor::setPos(mapToGlobal(rect().center()));
  430. CreateMouseMonitor(false);
  431. }
  432. m_bMouseRun = false;
  433. }
  434. }
  435. }
  436. }
  437. void ControlOperationPage::UpdataModuleType(const QString& strMode, int nIndex)
  438. {
  439. if (nIndex == 1)
  440. {
  441. m_currentSelectRunAxis.ModuleType = strMode.toStdString();
  442. }
  443. else if (nIndex == 2)
  444. {
  445. m_currentSelectRunAxis.AxisType = strMode.toStdString();
  446. }
  447. if (m_isAdd == false)
  448. {
  449. emit SendModuleTypeSignals(m_currentSelectRunAxis);
  450. }
  451. }
  452. template<class T>
  453. void ControlOperationPage::DeduplicationBox(QComboBox* pCom, const T& veTemp, int nIndex)
  454. {
  455. for (auto& a : veTemp)
  456. {
  457. QString strName;
  458. if (nIndex == 0)
  459. {
  460. strName = a->GetModuleType().c_str();
  461. }
  462. else if (nIndex == 1)
  463. {
  464. strName = a->GetStringAxisType().c_str();
  465. }
  466. QStringList items;
  467. for (int i = 0; i < pCom->count(); ++i)
  468. {
  469. items << pCom->itemText(i);
  470. }
  471. bool bMa = false; // 是否匹配
  472. for (auto b : items)
  473. {
  474. if (b == strName)
  475. {
  476. bMa = true;
  477. break;
  478. }
  479. }
  480. if (!bMa)
  481. {
  482. pCom->addItem(strName);
  483. }
  484. }
  485. }
  486. void ControlOperationPage::setEnableControls(bool enable) {
  487. ui->LiveButton->setEnabled(enable);
  488. ui->DataSources->setEnabled(enable);
  489. if (enable == true) {
  490. ui->LiveButton->setStyleSheet(
  491. "QPushButton:hover {"
  492. " background-color: #45a049;"
  493. "}"
  494. "QPushButton:pressed {"
  495. " background-color: #3e8e41;"
  496. "}"
  497. );
  498. }
  499. else {
  500. ui->LiveButton->setStyleSheet("background-color: lightgray;");
  501. }
  502. }
  503. void ControlOperationPage::setBlueBord() {
  504. ui->BackGround->setStyleSheet(QString::fromUtf8("border: 4px solid blue;"));
  505. }