WaffleGraphicsView.cpp 19 KB

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