WaferGraphicsView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "WaferGraphicsView.h"
  2. #include <QDebug>
  3. #include <QScrollBar>
  4. WaferGraphicsView::WaferGraphicsView(QGraphicsScene* scene, QWidget* parent)
  5. : QGraphicsView(scene, parent), selecting(false), selectionRect(nullptr),
  6. scaleFactor(1.0), isDragging(false), thumbnailLabel(nullptr),
  7. thumbnailVisible(false) {
  8. setRenderHint(QPainter::Antialiasing);
  9. // setDragMode(QGraphicsView::ScrollHandDrag); // 支持拖动视图
  10. setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // 缩放时以鼠标为中心
  11. // 初始化缩略图标签
  12. thumbnailLabel = new QLabel(this);
  13. thumbnailLabel->setFixedSize(150, 150);
  14. thumbnailLabel->move(0, 0); // 默认左上角位置
  15. thumbnailLabel->setStyleSheet("background-color: white; border: 1px solid gray;");
  16. thumbnailLabel->installEventFilter(this);
  17. thumbnailLabel->hide();
  18. topLeftIndex = qMakePair(-1, -1);
  19. bottomRightIndex = qMakePair(-1, -1);
  20. }
  21. // 事件过滤器用于处理缩略图拖动
  22. bool WaferGraphicsView::eventFilter(QObject* obj, QEvent* event) {
  23. static QPoint dragStartPosition;
  24. if (obj == thumbnailLabel) {
  25. if (event->type() == QEvent::MouseButtonPress) {
  26. QMouseEvent* me = static_cast<QMouseEvent*>(event);
  27. dragStartPosition = me->pos();
  28. return true;
  29. }
  30. else if (event->type() == QEvent::MouseMove) {
  31. QMouseEvent* me = static_cast<QMouseEvent*>(event);
  32. // 计算新位置
  33. QPoint newPos = thumbnailLabel->pos() + (me->pos() - dragStartPosition);
  34. // 限制在视图范围内
  35. int maxX = this->width() - thumbnailLabel->width();
  36. int maxY = this->height() - thumbnailLabel->height();
  37. // 使用qBound限制坐标范围(0 <= x <= maxX,0 <= y <= maxY)
  38. newPos.setX(qBound(0, newPos.x(), maxX));
  39. newPos.setY(qBound(0, newPos.y(), maxY));
  40. thumbnailLabel->move(newPos);
  41. return true;
  42. }
  43. }
  44. return QGraphicsView::eventFilter(obj, event);
  45. }
  46. void WaferGraphicsView::mousePressEvent(QMouseEvent* event) {
  47. if (event->button() == Qt::LeftButton) {
  48. // 清空选中的 DieItem
  49. for (auto& item : selectedItemsMap) {
  50. DieItem* die = dynamic_cast<DieItem*>(item);
  51. if (die) {
  52. die->setSelected(false); // 取消选中状态
  53. }
  54. }
  55. selectedItemsMap.clear();
  56. if (topLeftItem && topLeftItem->scene()) {
  57. topLeftItem->setRightSelected(false);
  58. }
  59. topLeftItem.clear();
  60. if (bottomRightItem && bottomRightItem->scene()) {
  61. bottomRightItem->setRightSelected(false);
  62. }
  63. bottomRightItem.clear();
  64. topLeftIndex = qMakePair(-1, -1);
  65. bottomRightIndex = qMakePair(-1, -1);
  66. // 获取点击位置的 DieItem
  67. if (selectedItem && selectedItem->scene()) {
  68. selectedItem->setLeftSelected(false);
  69. }
  70. selectedItem.clear();
  71. QGraphicsItem* item = itemAt(event->pos());
  72. if (item) {
  73. selectedItem = dynamic_cast<DieItem*>(item);
  74. selectedItem->setLeftSelected(true);
  75. }
  76. setCursor(Qt::OpenHandCursor); // 按下时设置为小手
  77. selecting = true;
  78. lastPos = event->pos(); // 记录鼠标位置
  79. }
  80. else if (event->button() == Qt::RightButton) {
  81. // 开始框选
  82. selecting = true;
  83. selectionStart = mapToScene(event->pos());
  84. isDragging = false;
  85. if (!selectionRect) {
  86. selectionRect = new QGraphicsRectItem();
  87. selectionRect->setPen(QPen(Qt::NoPen));
  88. selectionRect->setBrush(QBrush(QColor(0, 0, 255, 50))); // 半透明蓝色
  89. scene()->addItem(selectionRect);
  90. }
  91. selectionRect->setRect(QRectF(selectionStart, QSizeF()));
  92. }
  93. QGraphicsView::mousePressEvent(event);
  94. }
  95. void WaferGraphicsView::mouseMoveEvent(QMouseEvent* event) {
  96. if (selecting && selectionRect) {
  97. QPointF currentPos = mapToScene(event->pos());
  98. selectionRect->setRect(QRectF(selectionStart, currentPos).normalized());
  99. isDragging = true;
  100. }
  101. else if (selecting) {
  102. // 计算鼠标当前位置与上次位置的差值
  103. QPointF delta = event->pos() - lastPos;
  104. // 平移视图
  105. horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
  106. verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());
  107. lastPos = event->pos(); // 更新鼠标位置
  108. }
  109. QGraphicsView::mouseMoveEvent(event);
  110. }
  111. void WaferGraphicsView::mouseReleaseEvent(QMouseEvent* event) {
  112. if (event->button() == Qt::LeftButton) {
  113. setCursor(Qt::ArrowCursor); // 松开时恢复为箭头
  114. selecting = false;
  115. }
  116. else if (event->button() == Qt::RightButton && selecting) {
  117. selecting = false;
  118. if (selectionRect && isDragging) {
  119. if (selectedItem && selectedItem->scene()) {
  120. selectedItem->setLeftSelected(false);
  121. }
  122. selectedItem.clear();
  123. if (topLeftItem && topLeftItem->scene()) {
  124. topLeftItem->setRightSelected(false);
  125. }
  126. topLeftItem.clear();
  127. if (bottomRightItem && bottomRightItem->scene()) {
  128. bottomRightItem->setRightSelected(false);
  129. }
  130. bottomRightItem.clear();
  131. topLeftIndex = qMakePair(-1, -1);
  132. bottomRightIndex = qMakePair(-1, -1);
  133. QRectF selectedArea = selectionRect->rect();
  134. scene()->removeItem(selectionRect);
  135. delete selectionRect;
  136. selectionRect = nullptr;
  137. QList<QGraphicsItem*> items = scene()->items(selectedArea, Qt::IntersectsItemShape);
  138. for (QGraphicsItem* item : items) {
  139. DieItem* die = dynamic_cast<DieItem*>(item);
  140. if (die) {
  141. // 将 DieItem 添加到 map 中
  142. selectedItemsMap.insert(qMakePair(die->getRow(), die->getCol()), die);
  143. die->setSelected(true); // 设置选中状态
  144. }
  145. }
  146. }
  147. if (selectionRect) {
  148. scene()->removeItem(selectionRect);
  149. delete selectionRect;
  150. selectionRect = nullptr;
  151. }
  152. // 如果没有进行拖动,则弹出右键菜单
  153. if (!isDragging) {
  154. QGraphicsItem* item = itemAt(event->pos());
  155. DieItem* die = dynamic_cast<DieItem*>(item);
  156. QMenu menu;
  157. QAction* showThumb = menu.addAction(thumbnailVisible ? tr("Hide thumbnails", "隐藏缩略图") : tr("Show thumbnails", "显示缩略图"));
  158. connect(showThumb, &QAction::triggered, [this] {
  159. thumbnailVisible ? hideThumbnail() : showThumbnail();
  160. });
  161. menu.addAction(tr("Send Location", "发送位置"), [this] {
  162. if (selectedItem) {
  163. qDebug() << "Row:" << selectedItem->getRow() << "Col:" << selectedItem->getCol();
  164. selectedItem->setLeftSelected(false);
  165. selectedItem = nullptr;
  166. }
  167. });
  168. if (die) {
  169. menu.addAction("移动到该位置", [this, die] {
  170. for (auto& item : selectedItemsMap) {
  171. DieItem* die = dynamic_cast<DieItem*>(item);
  172. if (die) {
  173. die->setSelected(false);
  174. }
  175. }
  176. selectedItemsMap.clear();
  177. if (topLeftItem && topLeftItem->scene()) {
  178. topLeftItem->setRightSelected(false);
  179. }
  180. topLeftItem.clear();
  181. if (bottomRightItem && bottomRightItem->scene()) {
  182. bottomRightItem->setRightSelected(false);
  183. }
  184. bottomRightItem.clear();
  185. topLeftIndex = qMakePair(-1, -1);
  186. bottomRightIndex = qMakePair(-1, -1);
  187. if (selectedItem && selectedItem->scene()) {
  188. selectedItem->setLeftSelected(false);
  189. }
  190. selectedItem.clear();
  191. selectedItem = die;
  192. selectedItem->setLeftSelected(true);
  193. });
  194. // 设置区域边界点菜单
  195. menu.addAction("设为左上点", [this, die] {
  196. if (topLeftItem && topLeftItem->scene()) {
  197. topLeftItem->setRightSelected(false);
  198. }
  199. topLeftItem.clear();
  200. topLeftItem = die;
  201. topLeftItem->setRightSelected(true);
  202. topLeftIndex = qMakePair(die->getRow(), die->getCol());
  203. if (bottomRightIndex.first >= 0) checkAndCreateRegion();
  204. });
  205. menu.addAction("设为右下点", [this, die] {
  206. if (bottomRightItem && bottomRightItem->scene()) {
  207. bottomRightItem->setRightSelected(false);
  208. }
  209. bottomRightItem.clear();
  210. bottomRightItem = die;
  211. bottomRightItem->setRightSelected(true);
  212. bottomRightIndex = qMakePair(die->getRow(), die->getCol());
  213. if (topLeftIndex.first >= 0) checkAndCreateRegion();
  214. });
  215. }
  216. menu.addAction("清除选中区域", [this] { clearRegion(); });
  217. menu.addAction("设置区域", [this] { setRegion(); });
  218. menu.exec(event->globalPos());
  219. }
  220. }
  221. QGraphicsView::mouseReleaseEvent(event);
  222. }
  223. void WaferGraphicsView::wheelEvent(QWheelEvent* event) {
  224. if (event->orientation() == Qt::Vertical) {
  225. event->ignore(); // 忽略竖直滚轮事件(即禁用滚动条滑动)
  226. return;
  227. }
  228. event->accept();
  229. }
  230. // 缩略图功能实现
  231. void WaferGraphicsView::showThumbnail() {
  232. // 本地图片路径(根据实际路径修改)
  233. QString imagePath = ":/images/test_image/image_1.png"; // 替换为本地图片路径
  234. // 加载本地图片
  235. QPixmap thumb(imagePath);
  236. if (!thumb.isNull()) {
  237. // 如果图片加载成功,设置为缩略图
  238. thumbnailLabel->setPixmap(thumb.scaled(150, 150, Qt::KeepAspectRatio));
  239. thumbnailLabel->show();
  240. thumbnailVisible = true;
  241. }
  242. else {
  243. // 如果加载图片失败,显示"图片加载失败"
  244. thumbnailLabel->setText("图片加载失败");
  245. thumbnailLabel->setAlignment(Qt::AlignCenter); // 居中显示文本
  246. thumbnailLabel->show();
  247. thumbnailVisible = true;
  248. }
  249. }
  250. void WaferGraphicsView::hideThumbnail() {
  251. thumbnailLabel->hide();
  252. thumbnailVisible = false;
  253. thumbnailLabel->move(0, 0);
  254. }
  255. void WaferGraphicsView::checkAndCreateRegion()
  256. {
  257. // 仅当两个点都有效时处理
  258. if (topLeftIndex.first < 0 || bottomRightIndex.first < 0) return;
  259. // 确定行列范围
  260. int startRow = qMin(topLeftIndex.first, bottomRightIndex.first);
  261. int endRow = qMax(topLeftIndex.first, bottomRightIndex.first);
  262. int startCol = qMin(topLeftIndex.second, bottomRightIndex.second);
  263. int endCol = qMax(topLeftIndex.second, bottomRightIndex.second);
  264. // 遍历场景中的所有项
  265. foreach(QGraphicsItem * item, scene()->items()) {
  266. if (DieItem* die = dynamic_cast<DieItem*>(item)) {
  267. int row = die->getRow();
  268. int col = die->getCol();
  269. // 判断是否在区域内
  270. if (row >= startRow && row <= endRow &&
  271. col >= startCol && col <= endCol) {
  272. // 更新选中状态
  273. die->setSelected(true);
  274. selectedItemsMap.insert(qMakePair(row, col), die);
  275. }
  276. }
  277. }
  278. // 重置索引点
  279. topLeftIndex = qMakePair(-1, -1);
  280. bottomRightIndex = qMakePair(-1, -1);
  281. }
  282. void WaferGraphicsView::clearRegion()
  283. {
  284. // 清空选中的 DieItem
  285. for (auto& item : selectedItemsMap) {
  286. DieItem* die = dynamic_cast<DieItem*>(item);
  287. if (die) {
  288. die->setSelected(false); // 取消选中状态
  289. }
  290. }
  291. selectedItemsMap.clear();
  292. if (selectedItem && selectedItem->scene()) {
  293. selectedItem->setLeftSelected(false);
  294. }
  295. selectedItem.clear();
  296. if (topLeftItem && topLeftItem->scene()) {
  297. topLeftItem->setRightSelected(false);
  298. }
  299. topLeftItem.clear();
  300. if (bottomRightItem && bottomRightItem->scene()) {
  301. bottomRightItem->setRightSelected(false);
  302. }
  303. bottomRightItem.clear();
  304. topLeftIndex = qMakePair(-1, -1);
  305. bottomRightIndex = qMakePair(-1, -1);
  306. // 清除缩略图
  307. hideThumbnail();
  308. }
  309. void WaferGraphicsView::setRegion()
  310. {
  311. for (auto it = selectedItemsMap.begin(); it != selectedItemsMap.end(); ++it) {
  312. QPair<int, int> key = it.key(); // 获取当前元素的 key
  313. qDebug() << "Row:" << key.first << ", Col:" << key.second;
  314. }
  315. // 清空选中的 DieItem
  316. for (auto& item : selectedItemsMap) {
  317. DieItem* die = dynamic_cast<DieItem*>(item);
  318. if (die) {
  319. die->setSelected(false); // 取消选中状态
  320. }
  321. }
  322. selectedItemsMap.clear();
  323. if (selectedItem && selectedItem->scene()) {
  324. selectedItem->setLeftSelected(false);
  325. }
  326. selectedItem.clear();
  327. if (topLeftItem && topLeftItem->scene()) {
  328. topLeftItem->setRightSelected(false);
  329. }
  330. topLeftItem.clear();
  331. if (bottomRightItem && bottomRightItem->scene()) {
  332. bottomRightItem->setRightSelected(false);
  333. }
  334. bottomRightItem.clear();
  335. topLeftIndex = qMakePair(-1, -1);
  336. bottomRightIndex = qMakePair(-1, -1);
  337. }