123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #include "MaterialBoxGraphicsView.h"
- MaterialBoxGraphicsView::MaterialBoxGraphicsView(QGraphicsScene* scene, QWidget* parent)
- : QGraphicsView(scene, parent){
- setRenderHint(QPainter::Antialiasing);
- setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // 缩放时以鼠标为中心
- };
- void MaterialBoxGraphicsView::mousePressEvent(QMouseEvent* event){
- if(event->button() == Qt::LeftButton){
- QGraphicsItem* item = itemAt(event->pos());
- if (item && dynamic_cast<MaterialBoxDie*>(item)) {
- clearSeleted();
- selectedLeftItem = dynamic_cast<MaterialBoxDie*>(item);
- selectedLeftItem->setLeftSelected(true);
- }else{
- clearSeleted();
- return;
- }
- setCursor(Qt::OpenHandCursor); // 按下时设置为小手
- }else if(event->button() == Qt::RightButton){
- QGraphicsItem* item = itemAt(event->pos());
- if (item && dynamic_cast<MaterialBoxDie*>(item)) {
- clearSeleted();
- selectedRightItem = dynamic_cast<MaterialBoxDie*>(item);
- selectedRightItem->setRightSelected(true);
- qDebug()<<selectedRightItem->getRow();
- emit layerRightClicked(selectedRightItem->getRow()+1);
- }else{
- return;
- }
- }
- QGraphicsView::mousePressEvent(event);
- }
- void MaterialBoxGraphicsView::mouseReleaseEvent(QMouseEvent* event){
- if (event->button() == Qt::LeftButton) {
- setCursor(Qt::ArrowCursor); // 松开时恢复为箭头
- }
- }
- void MaterialBoxGraphicsView::wheelEvent(QWheelEvent* event) {
- if (event->orientation() == Qt::Vertical) {
- event->ignore(); // 忽略竖直滚轮事件(即禁用滚动条滑动)
- return;
- }
- event->accept();
- }
- void MaterialBoxGraphicsView::clearSeleted(){
- if(selectedLeftItem){
- selectedLeftItem->setLeftSelected(false);
- }
- if(selectedRightItem){
- selectedRightItem->setRightSelected(false);
- }
- }
- void BackgroundRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
- // 绘制灰色背景框
- painter->setPen(Qt::black);
- painter->setBrush(Qt::lightGray);
- painter->drawRect(m_containerWidthdp, m_containerHeightdp, m_containerWidth, m_containerHeight);
- }
- MaterialBoxDie::MaterialBoxDie(int row, int col, int status, qreal weight,qreal height, QGraphicsItem* parent)
- : QGraphicsRectItem(parent), row(row), col(col), status(status){
- setRect(0, 0, weight, height); // 设置单元格大小
- setBrush(getColorByStatus(status));
- // 设置边框(Pen)
- setPen(QPen(QColor(0, 0, 0), 0.5)); // 黑色边框,宽度为0.5
- }
- void MaterialBoxDie::setLeftSelected(bool selected){
- if (selected) {
- setPen(QPen(Qt::blue, 2));
- setZValue(1);
- } else {
- setPen(QPen(Qt::black, 0.5)); // 未选中时恢复为黑色边框
- setZValue(0);
- }
- }
- void MaterialBoxDie::setRightSelected(bool selected){
- if (selected) {
- setPen(QPen(Qt::green, 2));
- setZValue(1);
- } else {
- setPen(QPen(Qt::black, 0.5)); // 未选中时恢复为黑色边框
- setZValue(0);
- }
- }
- QColor MaterialBoxDie::getColorByStatus(int status){
- switch (status) {
- case 0: return QColor(255, 0, 0); // 红色
- case 1: return QColor(40, 129, 5); // 绿色
- default: return QColor(0, 0, 0);
- }
- }
- int MaterialBoxDie::getRow(){
- return row;
- }
|