void Canvas::mouseMoveEvent(QMouseEvent *event) { if(!enableFill) { if(!isMoved) { AbstractShape * item = currentShape(); QPointF p = event->pos(); QRectF r = item->boundingRect(); QPointF tl = mapFromScene(r.topLeft()); QPointF br = mapFromScene(r.bottomRight()); QPointF tr = mapFromScene(r.topRight()); QPointF bl = mapFromScene(r.bottomLeft()); checkIfProperRect(tl, br, tr, bl); if(enableResize && shapeDrawn) { //checkIfProperRect(tl, br); if(item != nullptr && item->type() != AbstractShape::AbstractType::Type + 1) { if(belongToFirstCorners(p, tl, br)) { setCursor(Qt::SizeBDiagCursor); } /*else if(belongToSecondCorners(p, tl, br)) { setCursor(Qt::SizeFDiagCursor); }*/ else if(((p.x() <= tl.x() && p.x() > tl.x() - 3) && p.y() > tl.y() - 3 && p.y() < br.y()) || ((p.x() > br.x() - 3 && p.x() <= br.x()) && p.y() > tl.y() - 3 && p.y() < br.y())) { setCursor(Qt::SizeHorCursor); } else if(((p.y() > br.y() - 3 && p.y() <= br.y()) && p.x() > tl.x() - 3 && p.x() < br.x()) || (p.y() <= tl.y() && p.y() > tl.y() - 3) && p.x() > tl.x() - 3 && p.x() < br.x()) { setCursor(Qt::SizeVerCursor); } else if(p.x() >= tl.x() && p.x() <= br.x() && p.y() >= tl.y() && p.y() <= br.y()) { setCursor(Qt::SizeAllCursor); } else { setCursor(Qt::CrossCursor); } if(event->buttons() & Qt::LeftButton) { QPointF tl = mapFromScene(r.topLeft()); QPointF br = mapFromScene(r.bottomRight()); QPointF point = mapToScene(event->pos()); switch(_direction) { case Left : { if(sceneLocker(point)) { if(tl.x() > br.x()) { _endX = point.x(); } else { _startX = point.x(); } } break; } case Right : { if(sceneLocker(point)) { if(tl.x() > br.x()) { _startX = point.x(); } else { _endX = point.x(); } } break; } case Top: { if(sceneLocker(point)) { if(tl.y() > br.y()) { _endY = point.y(); } else { _startY = point.y(); } } break; } case Bottom : { if(sceneLocker(point)) { if(tl.y() > br.y()) { _startY = point.y(); } else { _endY = point.y(); } } break; } } item->draw(startX(), startY(), endX(), endY()); currentScene->edge()->draw(startX(), startY(), endX(), endY()); currentScene->update(); switch(_normalize) { case NormalizeLeft: { _startX = point.x(); _startY = point.y(); item->normalize(point.x(), point.y(), point.x() - rect().topLeft().x()); QRectF tmp = item->boundingRect(); currentScene->edge()->draw(tmp.topLeft().x(), tmp.topLeft().y(), tmp.bottomRight().x(), tmp.bottomRight().y()); _startX = tmp.topLeft().x(); _startY = tmp.topLeft().y(); _endX = tmp.bottomRight().x(); _endY = tmp.bottomRight().y(); if(item->type() == Triangle::TriangleType::Type) { ((Triangle*)item)->normalizeFlag = true; _startX = point.x(); _startY = point.y(); } break; } } } } } QPointF point = mapToScene(event->pos()); if(event->buttons() & Qt::LeftButton && _direction == None && _normalize == NormalizeNone) { preventOverBoundingOnDraw(point, _endX, _endY); if(shapeSet) { currentScene->currentShape()->setPen(currentPen); currentScene->currentShape()->draw(startX(), startY(), endX(), endY()); currentScene->update(); edgeLocker = true; } } } else if((event->buttons() & Qt::LeftButton) && isMoved && currentScene->currentShape()->type() != LineSegment::LineSegmentType::Type && (!currentScene->shapes.isEmpty() || !currentScene->lines.isEmpty())) { QRectF r = currentShape()->boundingRect(); QPoint tmp = QPoint(event->pos().x() - coordinatesIterationMove.x(), event->pos().y() - coordinatesIterationMove.y()); preventOverBoundingOnMove(r, tmp); currentScene->currentShape()->setPen(currentPen); if(currentShape()->type() == Triangle::TriangleType::Type && ((Triangle *)currentShape())->normalizeFlag) { currentScene->currentShape()->normalize(startX(), startY(), startX() - rect().topLeft().x()); } else { currentScene->currentShape()->draw(startX(), startY(), endX(), endY()); } if(!linesEnds.isEmpty()) { for(auto i: linePointIndexes) { preventOverBoundingOnDraw(linesEnds[linePointIndexes.indexOf(i)], linesEnds[linePointIndexes.indexOf(i)].rx(), linesEnds[linePointIndexes.indexOf(i)].ry()); linesEnds[linePointIndexes.indexOf(i)] += tmp; currentScene->lines.at(i)->draw(linesStarts[linePointIndexes.indexOf(i)].x(), linesStarts[linePointIndexes.indexOf(i)].y(), linesEnds[linePointIndexes.indexOf(i)].x(), linesEnds[linePointIndexes.indexOf(i)].y()); } } currentScene->edge()->hide(); currentScene->update(); coordinatesIterationMove = event->pos(); } } else { AbstractShape * tmp = currentShape(); if(tmp->isUnderMouse()) { setCursor(Qt::PointingHandCursor); } else { setCursor(Qt::CrossCursor); } } }
void Canvas::mousePressEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton) { if(enableFill) { AbstractShape * tmp = currentShape(); if(tmp->isUnderMouse()) { tmp->setBrush(QBrush(color())); currentScene->update(); } } else { QPointF point = mapToScene(event->pos()); _startX = point.x(); _startY = point.y(); QGraphicsItem * item = currentShape(); QPointF p = event->pos(); QRectF r = item->boundingRect(); QPointF tl = mapFromScene(r.topLeft()); QPointF br = mapFromScene(r.bottomRight()); QPointF tr = mapFromScene(r.topRight()); QPointF bl = mapFromScene(r.bottomLeft()); checkIfProperRect(tl, br, tr, bl); if(belongToFirstCorners(p, tl, br)) { _direction = None; _normalize = NormalizeLeft; } /*else if(belongToSecondCorners(p, tl, br)) { _direction = None; _normalize = NormalizeRight; }*/ else if((p.x() <= tl.x() && p.x() > tl.x() - 3) && p.y() > tl.y() - 3 && p.y() < br.y()) { _direction = Left; _startY = r.topLeft().y(); } else if((p.x() > br.x() - 3 && p.x() <= br.x()) && p.y() > tl.y() - 3 && p.y() < br.y()) { _direction = Right; _startX = r.topLeft().x(); _startY = r.topLeft().y(); _endY = r.bottomRight().y(); } else if((p.y() <= tl.y() && p.y() > tl.y() - 3) && p.x() > tl.x() - 3 && p.x() < br.x()) { _direction = Top; _startX = r.topLeft().x(); } else if((p.y() > br.y() - 3 && p.y() <= br.y()) && p.x() > tl.x() - 3 && p.x() < br.x()) { _direction = Bottom; _startX = r.topLeft().x(); _startY = r.topLeft().y(); _endX = r.bottomRight().x(); } else if(p.x() >= tl.x() && p.x() <= br.x() && p.y() >= tl.y() && p.y() <= br.y()) { _direction = None; isMoved = true; _startX = item->boundingRect().topLeft().x(); _startY = item->boundingRect().topLeft().y(); _endX = item->boundingRect().bottomRight().x(); _endY = item->boundingRect().bottomRight().y(); coordinatesIterationMove = event->pos(); linesEnds.clear(); linesStarts.clear(); linePointIndexes.clear(); for(auto & i : currentScene->lines) { if(i->boundingRect().intersects(currentShape()->boundingRect())) { QPointF tl = i->boundingRect().topLeft(); QPointF br = i->boundingRect().bottomRight(); if(currentShape()->boundingRect().contains(br) && !currentShape()->boundingRect().contains(tl)) { linesEnds.append(br.toPoint()); linesStarts.append(tl.toPoint()); linePointIndexes.append(currentScene->lines.indexOf(i)); } else if(currentShape()->boundingRect().contains(tl) && !currentShape()->boundingRect().contains(br)) { linesEnds.append(tl.toPoint()); linesStarts.append(br.toPoint()); linePointIndexes.append(currentScene->lines.indexOf(i)); } } } } else { _direction = None; currentScene->edge()->hide(); if(point.x() > sceneRect().topLeft().x() && point.y() > sceneRect().topLeft().y() && point.x() < sceneRect().bottomRight().x() && point.y() < sceneRect().bottomRight().y()) { enableResize = false; buttonPressed = true; if(shapeDrawn) { notifyObservers(); } } } } } }