WaffleGraphicsView.cpp 19 KB

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