BondGraphicsView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include "BondGraphicsView.h"
  2. #include <QMouseEvent>
  3. #include <QGraphicsRectItem>
  4. #include <QDebug>
  5. #include <QScrollBar>
  6. #include <QMenu>
  7. BondItem::BondItem(ns_mat::POINT_INFO_STRUCT point, QGraphicsItem* parent)
  8. : QGraphicsRectItem(parent), point(point) {
  9. setBrush(getColorByStatus());
  10. setPen(QPen(QColor(0, 0, 0), 1));
  11. }
  12. void BondItem::setSelected(bool selected) {
  13. if (selected) {
  14. setPen(QPen(Qt::red, 1));
  15. }
  16. else {
  17. setPen(QPen(Qt::black, 1)); // 未选中时恢复为黑色边框
  18. }
  19. }
  20. void BondItem::setLeftSelected(bool selected) {
  21. if (selected) {
  22. setPen(QPen(Qt::green, 1));
  23. }
  24. else {
  25. setPen(QPen(Qt::black, 1)); // 未选中时恢复为黑色边框
  26. }
  27. }
  28. int BondItem::getDieIndex() {
  29. return dieIndex;
  30. }
  31. void BondItem::hoverEnterEvent(QGraphicsSceneHoverEvent*) {
  32. setZValue(1); // 悬停时提升Z值
  33. update();
  34. }
  35. void BondItem::hoverLeaveEvent(QGraphicsSceneHoverEvent*) {
  36. setZValue(0);
  37. update();
  38. }
  39. QColor BondItem::getColorByStatus() {
  40. dieIndex = m_info.stIndex.iIndex;
  41. if (m_info.stBondStatus.bDieStatus == ns_mat::DIE_STATUS::BOND_DEL) {
  42. return QColor(255, 50, 50);
  43. }else{
  44. if (m_info.stBondStatus.bIsCheck == true) {
  45. return QColor(255, 100, 0);
  46. }
  47. else {
  48. if (m_info.stBondStatus.bDieStatus != ns_mat::DIE_STATUS::NO_BOND) {
  49. switch (m_info.stBondStatus.bDieStatus) {
  50. case ns_mat::DIE_STATUS::TRANSFER_PICK_DONE: return QColor(255, 165, 0);
  51. case ns_mat::DIE_STATUS::LOOKUP_CALIB_DONE: return QColor(0, 150, 255);
  52. case ns_mat::DIE_STATUS::BOND_DONE: return QColor(144, 238, 144);
  53. default: return Qt::gray;
  54. }
  55. }
  56. else {
  57. if (m_info.stBondStatus.bAlnStatus == true) {
  58. return QColor(255, 100, 255);
  59. }
  60. else {
  61. switch (m_info.stBondStatus.bPickStatus) {
  62. case ns_mat::PICK_STATUS::NO_PICK: return QColor(200, 200, 200);
  63. case ns_mat::PICK_STATUS::WAF_PICK_DONE: return QColor(100, 200, 230);
  64. case ns_mat::PICK_STATUS::TRANSFER_BOND_DONE: return QColor(255, 255, 0);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. BondGraphicsView::BondGraphicsView(QGraphicsScene* scene, QWidget* parent)
  72. : QGraphicsView(scene, parent), selecting(false), selectionRect(nullptr),
  73. scaleFactor(1.0), isDragging(false), thumbnailLabel(nullptr),
  74. thumbnailVisible(false) {
  75. setRenderHint(QPainter::Antialiasing);
  76. // setDragMode(QGraphicsView::ScrollHandDrag); // 支持拖动视图
  77. setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // 缩放时以鼠标为中心
  78. // 初始化缩略图标签
  79. thumbnailLabel = new QLabel(this);
  80. thumbnailLabel->setFixedSize(150, 150);
  81. thumbnailLabel->move(0, 0); // 默认左上角位置
  82. thumbnailLabel->setStyleSheet("background-color: white; border: 1px solid gray;");
  83. thumbnailLabel->installEventFilter(this);
  84. thumbnailLabel->hide();
  85. }
  86. // 事件过滤器用于处理缩略图拖动
  87. bool BondGraphicsView::eventFilter(QObject* obj, QEvent* event) {
  88. static QPoint dragStartPosition;
  89. if (obj == thumbnailLabel) {
  90. if (event->type() == QEvent::MouseButtonPress) {
  91. QMouseEvent* me = static_cast<QMouseEvent*>(event);
  92. dragStartPosition = me->pos();
  93. return true;
  94. }
  95. else if (event->type() == QEvent::MouseMove) {
  96. QMouseEvent* me = static_cast<QMouseEvent*>(event);
  97. // 计算新位置
  98. QPoint newPos = thumbnailLabel->pos() + (me->pos() - dragStartPosition);
  99. // 限制在视图范围内
  100. int maxX = this->width() - thumbnailLabel->width();
  101. int maxY = this->height() - thumbnailLabel->height();
  102. // 使用qBound限制坐标范围(0 <= x <= maxX,0 <= y <= maxY)
  103. newPos.setX(qBound(0, newPos.x(), maxX));
  104. newPos.setY(qBound(0, newPos.y(), maxY));
  105. thumbnailLabel->move(newPos);
  106. return true;
  107. }
  108. }
  109. return QGraphicsView::eventFilter(obj, event);
  110. }
  111. void BondGraphicsView::mousePressEvent(QMouseEvent* event) {
  112. if (event->button() == Qt::LeftButton) {
  113. // 清空选中的 BondItem
  114. for (auto& item : selectedItemsMap) {
  115. BondItem* die = dynamic_cast<BondItem*>(item);
  116. if (die) {
  117. die->setSelected(false); // 取消选中状态
  118. }
  119. }
  120. selectedItemsMap.clear();
  121. // 获取点击位置的 BondItem
  122. if (selectedItem && selectedItem->scene()) {
  123. selectedItem->setLeftSelected(false);
  124. }
  125. selectedItem.clear();
  126. QGraphicsItem* item = itemAt(event->pos());
  127. // if (BondItem* bondItem = dynamic_cast<BondItem*>(item)) {
  128. // selectedItem = bondItem;
  129. // selectedItem->setLeftSelected(true);
  130. // }
  131. BondItem* bondItem = nullptr;
  132. while (item && !bondItem) {
  133. bondItem = dynamic_cast<BondItem*>(item);
  134. item = item->parentItem();
  135. }
  136. if (bondItem) {
  137. selectedItem = bondItem;
  138. selectedItem->setLeftSelected(true);
  139. }
  140. setCursor(Qt::OpenHandCursor); // 按下时设置为小手
  141. selecting = true;
  142. lastPos = event->pos(); // 记录鼠标位置
  143. }
  144. else if (event->button() == Qt::RightButton) {
  145. // 开始框选
  146. selecting = true;
  147. selectionStart = mapToScene(event->pos());
  148. isDragging = false;
  149. if (!selectionRect) {
  150. selectionRect = new QGraphicsRectItem();
  151. selectionRect->setPen(QPen(Qt::NoPen));
  152. selectionRect->setBrush(QBrush(QColor(0, 0, 255, 50))); // 半透明蓝色
  153. scene()->addItem(selectionRect);
  154. }
  155. selectionRect->setRect(QRectF(selectionStart, QSizeF()));
  156. }
  157. QGraphicsView::mousePressEvent(event);
  158. }
  159. void BondGraphicsView::mouseMoveEvent(QMouseEvent* event) {
  160. if (selecting && selectionRect) {
  161. QPointF currentPos = mapToScene(event->pos());
  162. selectionRect->setRect(QRectF(selectionStart, currentPos).normalized());
  163. isDragging = true;
  164. }
  165. else if (selecting) {
  166. // 计算鼠标当前位置与上次位置的差值
  167. QPointF delta = event->pos() - lastPos;
  168. // 平移视图
  169. horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
  170. verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());
  171. lastPos = event->pos(); // 更新鼠标位置
  172. }
  173. QGraphicsView::mouseMoveEvent(event);
  174. }
  175. void BondGraphicsView::mouseReleaseEvent(QMouseEvent* event) {
  176. if (event->button() == Qt::LeftButton) {
  177. setCursor(Qt::ArrowCursor); // 松开时恢复为箭头
  178. selecting = false;
  179. }
  180. else if (event->button() == Qt::RightButton && selecting) {
  181. selecting = false;
  182. if (selectionRect && isDragging) {
  183. if (selectedItem && selectedItem->scene()) {
  184. selectedItem->setLeftSelected(false);
  185. }
  186. selectedItem.clear();
  187. QRectF selectedArea = selectionRect->rect();
  188. scene()->removeItem(selectionRect);
  189. delete selectionRect;
  190. selectionRect = nullptr;
  191. QList<QGraphicsItem*> items = scene()->items(selectedArea, Qt::IntersectsItemShape);
  192. for (QGraphicsItem* item : items) {
  193. if (BondItem* die = dynamic_cast<BondItem*>(item)) {
  194. selectedItemsMap.insert(die->point.stIndex.iIndex, die);
  195. die->setSelected(true);
  196. }
  197. }
  198. }
  199. if (selectionRect) {
  200. scene()->removeItem(selectionRect);
  201. delete selectionRect;
  202. selectionRect = nullptr;
  203. }
  204. // 如果没有进行拖动,则弹出右键菜单
  205. if (!isDragging) {
  206. QGraphicsItem* item = itemAt(event->pos());
  207. QMenu menu;
  208. QAction* showThumb = menu.addAction(thumbnailVisible ? tr("Hide thumbnails", "隐藏缩略图") : tr("Show thumbnails", "显示缩略图"));
  209. connect(showThumb, &QAction::triggered, [this] {
  210. thumbnailVisible ? hideThumbnail() : showThumbnail();
  211. });
  212. //menu.addAction(tr("Send Location", "发送位置"), [this] {
  213. // if (selectedItem) {
  214. // qDebug() << selectedItem->point.stIndex.iIndex;
  215. // selectedItem->setLeftSelected(false);
  216. // selectedItem = nullptr;
  217. // }
  218. // });
  219. BondItem* die = nullptr;
  220. QList<QGraphicsItem*> items = scene()->items(mapToScene(event->pos()));
  221. foreach(QGraphicsItem * item, items) {
  222. if (typeid(*item) == typeid(BondItem)) {
  223. // 找到了你需要的 item
  224. die = dynamic_cast<BondItem*>(item);
  225. break;
  226. }
  227. }
  228. if (die) {
  229. menu.addAction(tr("move to current location","移动到该位置"), [this, die] {
  230. for (auto& item : selectedItemsMap) {
  231. BondItem* die = dynamic_cast<BondItem*>(item);
  232. if (die) {
  233. die->setSelected(false);
  234. }
  235. }
  236. selectedItemsMap.clear();
  237. if (selectedItem && selectedItem->scene()) {
  238. selectedItem->setLeftSelected(false);
  239. }
  240. selectedItem.clear();
  241. selectedItem = die;
  242. selectedItem->setLeftSelected(true);
  243. m_pCViewInterface->GetViewMatrix()->MoveBondToPoint(die->getDieIndex());
  244. qDebug() << "move to current location" << die->getDieIndex();
  245. });
  246. // 设置区域边界点菜单
  247. menu.addAction(tr("set Top left point", "设为左上点"), [this, die] {
  248. //if (topLeftItem && topLeftItem->scene()) {
  249. // topLeftItem->setRightSelected(false);
  250. //}
  251. //topLeftItem.clear();
  252. //topLeftItem = die;
  253. //topLeftItem->setRightSelected(true);
  254. //topLeftIndex = qMakePair(die->getRow(), die->getCol());
  255. //if (bottomRightIndex.first >= 0) checkAndCreateRegion();
  256. });
  257. menu.addAction(tr("set bottom right point", "设为右下点"), [this, die] {
  258. //if (bottomRightItem && bottomRightItem->scene()) {
  259. // bottomRightItem->setRightSelected(false);
  260. //}
  261. //bottomRightItem.clear();
  262. //bottomRightItem = die;
  263. //bottomRightItem->setRightSelected(true);
  264. //bottomRightIndex = qMakePair(die->getRow(), die->getCol());
  265. //if (topLeftIndex.first >= 0) checkAndCreateRegion();
  266. });
  267. }
  268. menu.addAction(tr("clear the selected area","清除选中区域"), [this] { clearRegion(); });
  269. menu.addAction(tr("set area","设置区域"), [this] { setRegion(); });
  270. menu.exec(event->globalPos());
  271. }
  272. }
  273. QGraphicsView::mouseReleaseEvent(event);
  274. }
  275. void BondGraphicsView::wheelEvent(QWheelEvent* event) {
  276. if (event->orientation() == Qt::Vertical) {
  277. event->ignore(); // 忽略竖直滚轮事件(即禁用滚动条滑动)
  278. return;
  279. }
  280. event->accept();
  281. }
  282. // 缩略图功能实现
  283. void BondGraphicsView::showThumbnail() {
  284. ImageInfo image;
  285. int dieIndex;
  286. m_pCViewInterface->GetViewMatrix()->GetBondRefImage(dieIndex,image);
  287. QPixmap thumb = convertToPixmap(image);
  288. if (!thumb.isNull()) {
  289. // 如果图片加载成功,设置为缩略图
  290. thumbnailLabel->setPixmap(thumb.scaled(150, 150, Qt::KeepAspectRatio));
  291. thumbnailLabel->show();
  292. thumbnailVisible = true;
  293. }
  294. else {
  295. // 如果加载图片失败,显示"图片加载失败"
  296. thumbnailLabel->setText("图片加载失败");
  297. thumbnailLabel->setAlignment(Qt::AlignCenter); // 居中显示文本
  298. thumbnailLabel->show();
  299. thumbnailVisible = true;
  300. }
  301. }
  302. void BondGraphicsView::hideThumbnail() {
  303. thumbnailLabel->hide();
  304. thumbnailVisible = false;
  305. thumbnailLabel->move(0, 0);
  306. }
  307. void BondGraphicsView::clearRegion()
  308. {
  309. // 清空选中的 BondItem
  310. for (auto& item : selectedItemsMap) {
  311. if (BondItem* die = dynamic_cast<BondItem*>(item)) {
  312. die->setSelected(false);
  313. }
  314. }
  315. selectedItemsMap.clear();
  316. if (selectedItem && selectedItem->scene()) {
  317. selectedItem->setLeftSelected(false);
  318. }
  319. selectedItem.clear();
  320. // 清除缩略图
  321. hideThumbnail();
  322. }
  323. void BondGraphicsView::setRegion()
  324. {
  325. for (auto it = selectedItemsMap.begin(); it != selectedItemsMap.end(); ++it) {
  326. int key = it.key(); // 获取当前元素的 key
  327. qDebug() << key;
  328. }
  329. // 清空选中的 BondItem
  330. for (auto& item : selectedItemsMap) {
  331. if (BondItem* die = dynamic_cast<BondItem*>(item)) {
  332. die->setSelected(false);
  333. }
  334. }
  335. selectedItemsMap.clear();
  336. if (selectedItem && selectedItem->scene()) {
  337. selectedItem->setLeftSelected(false);
  338. }
  339. selectedItem.clear();
  340. }
  341. void BondGraphicsView::yuv422_to_rgb888(const unsigned char* src, unsigned char* dst, int width, int height)
  342. {
  343. for (int i = 0; i < width * height; i += 2) {
  344. unsigned char y0 = src[0];
  345. unsigned char u = src[1];
  346. unsigned char y1 = src[2];
  347. unsigned char v = src[1];
  348. // 简单反色度插值,适用于 U/V 在相邻像素间共享的情况
  349. int r, g, b;
  350. // YUV to RGB 转换公式
  351. #define CLIP(x) qBound(0, int(x), 255)
  352. // Pixel 0
  353. r = CLIP(y0 + 1.402 * (v - 128));
  354. g = CLIP(y0 - 0.344 * (u - 128) - 0.714 * (v - 128));
  355. b = CLIP(y0 + 1.772 * (u - 128));
  356. *dst++ = r;
  357. *dst++ = g;
  358. *dst++ = b;
  359. // Pixel 1
  360. r = CLIP(y1 + 1.402 * (v - 128));
  361. g = CLIP(y1 - 0.344 * (u - 128) - 0.714 * (v - 128));
  362. b = CLIP(y1 + 1.772 * (u - 128));
  363. *dst++ = r;
  364. *dst++ = g;
  365. *dst++ = b;
  366. src += 4;
  367. }
  368. }
  369. QPixmap BondGraphicsView::convertToPixmap(const ImageInfo& imgData)
  370. {
  371. QImage::Format qFormat = QImage::Format_Invalid;
  372. switch (imgData.format) {
  373. case ImageFormat::GRAY8:
  374. qFormat = QImage::Format_Grayscale8;
  375. break;
  376. case ImageFormat::RGB888:
  377. qFormat = QImage::Format_RGB888;
  378. break;
  379. case ImageFormat::ARGB32:
  380. qFormat = QImage::Format_ARGB32;
  381. break;
  382. case ImageFormat::RGB32:
  383. qFormat = QImage::Format_RGB32;
  384. break;
  385. case ImageFormat::YUV422: {
  386. // 需要先转换为 RGB888
  387. int byteCount = imgData.width * imgData.height * 3;
  388. unsigned char* rgbData = new unsigned char[byteCount];
  389. yuv422_to_rgb888(imgData.data, rgbData, imgData.width, imgData.height);
  390. QImage tmp(rgbData, imgData.width, imgData.height, QImage::Format_RGB888);
  391. QPixmap pixmap = QPixmap::fromImage(tmp);
  392. delete[] rgbData;
  393. return pixmap;
  394. }
  395. default:
  396. qDebug() << "Unsupported image format!";
  397. return QPixmap();
  398. }
  399. QImage qImg(imgData.data, imgData.width, imgData.height,
  400. imgData.width * imgData.channel, qFormat);
  401. return QPixmap::fromImage(qImg);
  402. }
  403. void BondGraphicsView::setCViewInterface(ns_module::CViewInterface* CViewInterface) {
  404. m_pCViewInterface = CViewInterface;
  405. }
  406. void BondGraphicsView::checkAndCreateRegion()
  407. {
  408. /*
  409. // 仅当两个点都有效时处理
  410. if (topLeftIndex.first < 0 || bottomRightIndex.first < 0) return;
  411. // 确定行列范围
  412. int startRow = qMin(topLeftIndex.first, bottomRightIndex.first);
  413. int endRow = qMax(topLeftIndex.first, bottomRightIndex.first);
  414. int startCol = qMin(topLeftIndex.second, bottomRightIndex.second);
  415. int endCol = qMax(topLeftIndex.second, bottomRightIndex.second);
  416. // 遍历场景中的所有项
  417. foreach(QGraphicsItem * item, scene()->items()) {
  418. if (DieItem* die = dynamic_cast<DieItem*>(item)) {
  419. int row = die->getRow();
  420. int col = die->getCol();
  421. // 判断是否在区域内
  422. if (row >= startRow && row <= endRow &&
  423. col >= startCol && col <= endCol) {
  424. // 更新选中状态
  425. die->setSelected(true);
  426. selectedItemsMap.insert(qMakePair(row, col), die);
  427. }
  428. }
  429. }
  430. // 重置索引点
  431. topLeftIndex = qMakePair(-1, -1);
  432. bottomRightIndex = qMakePair(-1, -1);
  433. */
  434. }