DieItem.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "DieItem.h"
  2. DieItem::DieItem(int row, int col, PICK_DIE_STATUS status, qreal size, QGraphicsItem* parent)
  3. : QGraphicsRectItem(parent), row(row), col(col), status(status) {
  4. setRect(0, 0, size, size); // 设置单元格大小
  5. setBrush(getColorByStatus(status));
  6. // 设置边框(Pen)
  7. setPen(QPen(QColor(0, 0, 0), 0.5)); // 黑色边框,宽度为0.5
  8. }
  9. void DieItem::setSelected(bool selected) {
  10. if (selected) {
  11. setPen(QPen(Qt::green, 1)); // 选中时边框变为绿色
  12. } else {
  13. setPen(QPen(Qt::black, 0.5)); // 未选中时恢复为黑色边框
  14. }
  15. qDebug() << "DieItem clicked: Row:" << row << "Col:" << col;
  16. }
  17. // void DieItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  18. // if (event->button() == Qt::LeftButton) {
  19. // // isSelected = !isSelected; // 切换选中状态
  20. // // setSelected(isSelected); // 更新边框颜色
  21. // // 获取当前图形项的包围矩形
  22. // QRectF itemRect = boundingRect();
  23. // QPointF center = itemRect.center(); // 矩形的中心点
  24. // // 计算新的矩形尺寸(长宽的 95%)
  25. // qreal newWidth = itemRect.width() * 0.95;
  26. // qreal newHeight = itemRect.height() * 0.95;
  27. // // 计算新的矩形的左上角位置,使其保持原中心位置
  28. // qreal newX = center.x() - newWidth / 2;
  29. // qreal newY = center.y() - newHeight / 2;
  30. // // 创建新的矩形,作为响应区域
  31. // QRectF validArea(newX, newY, newWidth, newHeight);
  32. // // 获取鼠标点击位置
  33. // QPointF clickPos = event->pos();
  34. // // 判断点击位置是否在新的矩形区域内
  35. // if (validArea.contains(clickPos)) {
  36. // // 如果点击在有效区域内,则切换选中状态
  37. // isSelected = !isSelected; // 切换选中状态
  38. // setSelected(isSelected); // 更新边框颜色
  39. // }
  40. // }
  41. // QGraphicsRectItem::mousePressEvent(event); // 调用基类的事件处理
  42. // }
  43. QColor DieItem::getColorByStatus(PICK_DIE_STATUS status) {
  44. switch (status) {
  45. case DIE_EXIST: return QColor(0, 102, 255); // 蓝色
  46. case NO_EXIST: return QColor(200, 200, 200); // 浅灰
  47. case PICK_ING: return QColor(255, 255, 0); // 黄色
  48. case SKIP_DIE: return QColor(128, 128, 128); // 深灰
  49. case EDGE_DIE: return QColor(255, 165, 0); // 橙色
  50. default: return QColor(0, 0, 0);
  51. }
  52. }
  53. int DieItem::getRow() const {
  54. return row;
  55. }
  56. int DieItem::getCol() const {
  57. return col;
  58. }
  59. // void DieItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
  60. // // 创建右键菜单
  61. // QMenu menu;
  62. // QAction *action1 = menu.addAction("菜单项 1");
  63. // QAction *action2 = menu.addAction("菜单项 2");
  64. // // 连接菜单项的信号和槽
  65. // connect(action1, &QAction::triggered, this, &DieItem::handleAction1);
  66. // connect(action2, &QAction::triggered, this, &DieItem::handleAction2);
  67. // // 显示菜单
  68. // menu.exec(event->screenPos());
  69. // }
  70. // void DieItem::handleAction1() {
  71. // // 处理菜单项 1 的逻辑
  72. // qDebug() << "菜单项 1 被点击";
  73. // }
  74. // void DieItem::handleAction2() {
  75. // // 处理菜单项 2 的逻辑
  76. // qDebug() << "菜单项 2 被点击";
  77. // }