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) {
|
if (p) {
|
||||||
draggedPoint = p;
|
draggedPoint = p;
|
||||||
dragOffset = scene - QPointF(*p->x, *p->y);
|
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();
|
*draggedPoint->y = pos.y();
|
||||||
update();
|
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)
|
void Canvas::mouseReleaseEvent(QMouseEvent* event)
|
||||||
@@ -276,6 +318,11 @@ void Canvas::mouseReleaseEvent(QMouseEvent* event)
|
|||||||
draggedPoint = nullptr;
|
draggedPoint = nullptr;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (draggedLine) {
|
||||||
|
draggedLine = nullptr;
|
||||||
|
update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Canvas::paintEvent(QPaintEvent*)
|
void Canvas::paintEvent(QPaintEvent*)
|
||||||
|
|||||||
1
Canvas.h
1
Canvas.h
@@ -59,6 +59,7 @@ private:
|
|||||||
|
|
||||||
// ====================== Перемещение ======================
|
// ====================== Перемещение ======================
|
||||||
Point* draggedPoint{ nullptr };
|
Point* draggedPoint{ nullptr };
|
||||||
|
Line* draggedLine{ nullptr };
|
||||||
QPointF dragOffset;
|
QPointF dragOffset;
|
||||||
|
|
||||||
// ====================== Данные сцены ======================
|
// ====================== Данные сцены ======================
|
||||||
|
|||||||
Reference in New Issue
Block a user