ControlOperationPage.cpp 14 KB

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