ControlOperationPage.cpp 17 KB

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