Moving line in canvas
This commit is contained in:
47
Canvas.cpp
47
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*)
|
||||
|
||||
Reference in New Issue
Block a user