1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #ifndef MATERIALBOXGRAPHICSVIEW_H
- #define MATERIALBOXGRAPHICSVIEW_H
- #include <QGraphicsView>
- #include <QGraphicsScene>
- #include <QGraphicsRectItem>
- #include <QMouseEvent>
- #include <QLabel>
- #include <QPointer>
- #include <QGraphicsItem>
- #include <QPainter>
- #include <QColor>
- #include <QGraphicsSceneEvent>
- #include <QDebug>
- class BackgroundRect : public QGraphicsItem {
- public:
- BackgroundRect(int containerWidthdp, int containerHeightdp, int containerWidth, int containerHeight)
- : m_containerWidthdp(containerWidthdp), m_containerHeightdp(containerHeightdp), m_containerWidth(containerWidth), m_containerHeight(containerHeight) {
- setFlag(QGraphicsItem::ItemIsSelectable);
- setFlag(QGraphicsItem::ItemIsMovable, false);
- setFlag(QGraphicsItem::ItemIsFocusable, false);
- setAcceptedMouseButtons(Qt::NoButton); // 禁用所有鼠标按键
- setAcceptHoverEvents(false); // 禁用悬停事件
- }
- QRectF boundingRect() const override {
- return QRectF(m_containerWidthdp, m_containerHeightdp, m_containerWidth, m_containerHeight);
- }
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override ;
- private:
- int m_containerWidthdp;
- int m_containerHeightdp;
- int m_containerWidth;
- int m_containerHeight;
- // 可选:显式忽略所有鼠标事件
- void mousePressEvent(QGraphicsSceneMouseEvent* event) override {
- event->ignore(); // 允许事件穿透
- }
- };
- class MaterialBoxDie:public QObject, public QGraphicsRectItem{
- Q_OBJECT
- public:
- MaterialBoxDie(int row, int col, int status, qreal weight,qreal height, QGraphicsItem* parent = nullptr);
- void setLeftSelected(bool selected);
- void setRightSelected(bool selected);
- int getRow();
- private:
- int row, col;
- int status;
- bool isSelected = false;
- // 获取单元格状态颜色
- QColor getColorByStatus(int status);
- };
- class MaterialBoxGraphicsView : public QGraphicsView {
- Q_OBJECT
- public:
- MaterialBoxGraphicsView(QGraphicsScene* scene, QWidget* parent = nullptr);
- signals:
- void layerRightClicked(int layer);
- protected:
- void mousePressEvent(QMouseEvent* event) override;
- // void mouseMoveEvent(QMouseEvent* event) override;
- void mouseReleaseEvent(QMouseEvent* event) override;
- void wheelEvent(QWheelEvent* event) override;
- // bool eventFilter(QObject *obj, QEvent *event) override;
- void clearSeleted();
- private:
- double scaleFactor; // 当前缩放比例
- QPointer<MaterialBoxDie> selectedLeftItem;
- QPointer<MaterialBoxDie> selectedRightItem;
- };
- #endif // MATERIALBOXGRAPHICSVIEW_H
|