ControlOperationPage.cpp 11 KB

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