ControlOperationPage.cpp 12 KB

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