WaffleGraphicsView.cpp 14 KB

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