diff --git a/Canvas.cpp b/Canvas.cpp index 18ec9d8..793a1ef 100644 --- a/Canvas.cpp +++ b/Canvas.cpp @@ -73,6 +73,18 @@ void Canvas::mousePressEvent(QMouseEvent* event) if (p) { draggedPoint = p; dragOffset = scene - QPointF(*p->x, *p->y); + return; + } + + Line* found = findAt(scene); + if (found) { + draggedLine = found; + QPointF lineCenter( + (*found->p1.x + *found->p2.x) / 2.0, + (*found->p1.y + *found->p2.y) / 2.0 + ); + dragOffset = scene - lineCenter; + return; } } @@ -268,6 +280,36 @@ void Canvas::mouseMoveEvent(QMouseEvent* event) *draggedPoint->y = pos.y(); update(); } + + else if (draggedLine) { + QPointF newCenter = event->pos() - dragOffset; + QPointF oldCenter( + (*draggedLine->p1.x + *draggedLine->p2.x) / 2.0, + (*draggedLine->p1.y + *draggedLine->p2.y) / 2.0 + ); + + // Смещение для перемещения + double dx = newCenter.x() - oldCenter.x(); + double dy = newCenter.y() - oldCenter.y(); + + Point* linePoints[2] = { draggedLine->start_ref, draggedLine->end_ref }; + + for (int i = 0; i < 2; i++) { + Point* currentPoint = linePoints[i]; + + *currentPoint->x += dx; + *currentPoint->y += dy; + + for (Point* pair : points) { + if (pair != currentPoint && areCoincident(currentPoint, pair)) { + *pair->x = *currentPoint->x; + *pair->y = *currentPoint->y; + } + } + } + + update(); + } } void Canvas::mouseReleaseEvent(QMouseEvent* event) @@ -276,6 +318,11 @@ void Canvas::mouseReleaseEvent(QMouseEvent* event) draggedPoint = nullptr; update(); } + + if (draggedLine) { + draggedLine = nullptr; + update(); + } } void Canvas::paintEvent(QPaintEvent*) diff --git a/Canvas.h b/Canvas.h index 9c2cf16..c129e6a 100644 --- a/Canvas.h +++ b/Canvas.h @@ -59,6 +59,7 @@ private: // ====================== Перемещение ====================== Point* draggedPoint{ nullptr }; + Line* draggedLine{ nullptr }; QPointF dragOffset; // ====================== Данные сцены ======================