123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #include "WaferGraphicsView.h"
- #include <QDebug>
- #include <QScrollBar>
- WaferGraphicsView::WaferGraphicsView(QGraphicsScene* scene, QWidget* parent)
- : QGraphicsView(scene, parent), selecting(false), selectionRect(nullptr), scaleFactor(1.0) {
- setRenderHint(QPainter::Antialiasing);
-
- setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
- }
- void WaferGraphicsView::mousePressEvent(QMouseEvent* event) {
- if (event->button() == Qt::LeftButton) {
-
- for (auto& item : selectedItemsMap) {
- DieItem* die = dynamic_cast<DieItem*>(item);
- if (die) {
- die->setSelected(false);
- }
- }
- selectedItemsMap.clear();
-
- QGraphicsItem* item = itemAt(event->pos());
- DieItem* die = dynamic_cast<DieItem*>(item);
- if (die) {
-
- selectedItemsMap.insert(qMakePair(die->getRow(), die->getCol()), die);
- die->setSelected(true);
- }
- setCursor(Qt::OpenHandCursor);
- selecting = true;
- lastPos = event->pos();
- } else if (event->button() == Qt::RightButton) {
-
- selecting = true;
- selectionStart = mapToScene(event->pos());
- isDragging = false;
- if (!selectionRect) {
- selectionRect = new QGraphicsRectItem();
- selectionRect->setPen(QPen(Qt::NoPen));
- selectionRect->setBrush(QBrush(QColor(0, 0, 255, 50)));
- scene()->addItem(selectionRect);
- }
- selectionRect->setRect(QRectF(selectionStart, QSizeF()));
- }
- QGraphicsView::mousePressEvent(event);
- }
- void WaferGraphicsView::mouseMoveEvent(QMouseEvent* event) {
- if (selecting && selectionRect) {
- QPointF currentPos = mapToScene(event->pos());
- selectionRect->setRect(QRectF(selectionStart, currentPos).normalized());
- isDragging = true;
- } else if (selecting) {
-
- QPointF delta = event->pos() - lastPos;
-
- horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
- verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());
- lastPos = event->pos();
- }
- QGraphicsView::mouseMoveEvent(event);
- }
- void WaferGraphicsView::mouseReleaseEvent(QMouseEvent* event) {
- if (event->button() == Qt::LeftButton) {
- setCursor(Qt::ArrowCursor);
- selecting = false;
- } else if (event->button() == Qt::RightButton && selecting) {
- selecting = false;
- if (selectionRect) {
- QRectF selectedArea = selectionRect->rect();
- scene()->removeItem(selectionRect);
- delete selectionRect;
- selectionRect = nullptr;
- QList<QGraphicsItem*> items = scene()->items(selectedArea, Qt::IntersectsItemShape);
- for (QGraphicsItem* item : items) {
- DieItem* die = dynamic_cast<DieItem*>(item);
- if (die) {
-
- selectedItemsMap.insert(qMakePair(die->getRow(), die->getCol()), die);
- die->setSelected(true);
- }
- }
- }
-
- if (!isDragging) {
- QMenu menu;
- QAction* action1 = menu.addAction("菜单项 1");
- QAction* action2 = menu.addAction("菜单项 2");
- connect(action1, &QAction::triggered, this, &WaferGraphicsView::handleAction1);
- connect(action2, &QAction::triggered, this, &WaferGraphicsView::handleAction2);
- menu.exec(event->globalPos());
- }
- }
- QGraphicsView::mouseReleaseEvent(event);
- }
- void WaferGraphicsView::wheelEvent(QWheelEvent* event) {
- if (event->orientation() == Qt::Vertical) {
- event->ignore();
- return;
- }
- event->accept();
- }
- void WaferGraphicsView::handleAction1() {
- qDebug() << "菜单项 1 被点击";
- }
- void WaferGraphicsView::handleAction2() {
- qDebug() << "菜单项 2 被点击";
- }
|