MaterialBoxGraphicsView.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "MaterialBoxGraphicsView.h"
  2. MaterialBoxGraphicsView::MaterialBoxGraphicsView(QGraphicsScene* scene, QWidget* parent)
  3. : QGraphicsView(scene, parent){
  4. setRenderHint(QPainter::Antialiasing);
  5. setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // 缩放时以鼠标为中心
  6. };
  7. void MaterialBoxGraphicsView::mousePressEvent(QMouseEvent* event){
  8. if(event->button() == Qt::LeftButton){
  9. QGraphicsItem* item = itemAt(event->pos());
  10. if (item && dynamic_cast<MaterialBoxDie*>(item)) {
  11. clearSeleted();
  12. selectedLeftItem = dynamic_cast<MaterialBoxDie*>(item);
  13. selectedLeftItem->setLeftSelected(true);
  14. }else{
  15. clearSeleted();
  16. return;
  17. }
  18. setCursor(Qt::OpenHandCursor); // 按下时设置为小手
  19. }else if(event->button() == Qt::RightButton){
  20. QGraphicsItem* item = itemAt(event->pos());
  21. if (item && dynamic_cast<MaterialBoxDie*>(item)) {
  22. clearSeleted();
  23. selectedRightItem = dynamic_cast<MaterialBoxDie*>(item);
  24. selectedRightItem->setRightSelected(true);
  25. qDebug()<<selectedRightItem->getRow();
  26. emit layerRightClicked(selectedRightItem->getRow()+1);
  27. }else{
  28. return;
  29. }
  30. }
  31. QGraphicsView::mousePressEvent(event);
  32. }
  33. void MaterialBoxGraphicsView::mouseReleaseEvent(QMouseEvent* event){
  34. if (event->button() == Qt::LeftButton) {
  35. setCursor(Qt::ArrowCursor); // 松开时恢复为箭头
  36. }
  37. }
  38. void MaterialBoxGraphicsView::wheelEvent(QWheelEvent* event) {
  39. if (event->orientation() == Qt::Vertical) {
  40. event->ignore(); // 忽略竖直滚轮事件(即禁用滚动条滑动)
  41. return;
  42. }
  43. event->accept();
  44. }
  45. void MaterialBoxGraphicsView::clearSeleted(){
  46. if(selectedLeftItem){
  47. selectedLeftItem->setLeftSelected(false);
  48. }
  49. if(selectedRightItem){
  50. selectedRightItem->setRightSelected(false);
  51. }
  52. }
  53. void BackgroundRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
  54. // 绘制灰色背景框
  55. painter->setPen(Qt::black);
  56. painter->setBrush(Qt::lightGray);
  57. painter->drawRect(m_containerWidthdp, m_containerHeightdp, m_containerWidth, m_containerHeight);
  58. }
  59. MaterialBoxDie::MaterialBoxDie(int row, int col, int status, qreal weight,qreal height, QGraphicsItem* parent)
  60. : QGraphicsRectItem(parent), row(row), col(col), status(status){
  61. setRect(0, 0, weight, height); // 设置单元格大小
  62. setBrush(getColorByStatus(status));
  63. // 设置边框(Pen)
  64. setPen(QPen(QColor(0, 0, 0), 0.5)); // 黑色边框,宽度为0.5
  65. }
  66. void MaterialBoxDie::setLeftSelected(bool selected){
  67. if (selected) {
  68. setPen(QPen(Qt::blue, 2));
  69. setZValue(1);
  70. } else {
  71. setPen(QPen(Qt::black, 0.5)); // 未选中时恢复为黑色边框
  72. setZValue(0);
  73. }
  74. }
  75. void MaterialBoxDie::setRightSelected(bool selected){
  76. if (selected) {
  77. setPen(QPen(Qt::green, 2));
  78. setZValue(1);
  79. } else {
  80. setPen(QPen(Qt::black, 0.5)); // 未选中时恢复为黑色边框
  81. setZValue(0);
  82. }
  83. }
  84. QColor MaterialBoxDie::getColorByStatus(int status){
  85. switch (status) {
  86. case 0: return QColor(255, 0, 0); // 红色
  87. case 1: return QColor(40, 129, 5); // 绿色
  88. default: return QColor(0, 0, 0);
  89. }
  90. }
  91. int MaterialBoxDie::getRow(){
  92. return row;
  93. }