mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-25 15:34:13 +01:00
Fix element editor Mirror and Flip to reflect the selection in place
Mirror (M) and Flip (F) in the element editor reflected the selected parts across the element origin: every part's mirror() and flip() negated the scene x or y coordinate. A part drawn to the right of the origin landed the same distance to the left, so the selection jumped to the other side of the canvas instead of turning around. MirrorElementsCommand and FlipElementsCommand now compute the united scene bounding rectangle of the selected items and reflect across the vertical or horizontal line through its center. The center is snapped to the nearest half of the diagram grid, so points that were on the grid stay on the grid after the reflection. Terminals in particular keep their grid alignment. Each part's mirror() and flip() takes the axis coordinate as a parameter with a default of 0, so the previous behavior remains available to any other caller. The command stores the axis when it is created and undo reapplies the same reflection, which is its own inverse, so the existing undo path is unchanged. Parts covered: PartArc, PartDynamicTextField, PartEllipse, PartLine, PartPolygon, PartRectangle, PartTerminal, PartText. Fixes #812
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "editorcommands.h"
|
||||
#include "../diagram.h"
|
||||
|
||||
/**
|
||||
@brief ElementEditionCommand::ElementEditionCommand
|
||||
@@ -666,10 +667,32 @@ void RotateFineElementsCommand::redo()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@brief selectionCenter
|
||||
@param items
|
||||
@return the center of the united scene bounding rect of items,
|
||||
snapped to the nearest half of the diagram grid. Mirroring across a
|
||||
half-grid line keeps points that were on the grid on the grid.
|
||||
*/
|
||||
static QPointF selectionCenter(const QList<QGraphicsItem *> &items)
|
||||
{
|
||||
QRectF bounding;
|
||||
for (auto *item : items) {
|
||||
bounding = bounding.united(item->sceneBoundingRect());
|
||||
}
|
||||
QPointF center = bounding.center();
|
||||
const qreal half_x = Diagram::xGrid / 2.0;
|
||||
const qreal half_y = Diagram::yGrid / 2.0;
|
||||
center.setX(qRound(center.x() / half_x) * half_x);
|
||||
center.setY(qRound(center.y() / half_y) * half_y);
|
||||
return center;
|
||||
}
|
||||
|
||||
MirrorElementsCommand::MirrorElementsCommand(ElementScene *scene, QUndoCommand *parent) :
|
||||
ElementEditionCommand(QObject::tr("Miroir de sélection", "undo caption"), scene, nullptr, parent)
|
||||
{
|
||||
m_items = scene->selectedItems();
|
||||
m_axis_x = selectionCenter(m_items).x();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -680,28 +703,28 @@ void MirrorElementsCommand::redo()
|
||||
foreach (auto *item, m_items) {
|
||||
if (item->type() == PartText::Type) {
|
||||
PartText* staticText = qgraphicsitem_cast<PartText*>(item);
|
||||
staticText->mirror();
|
||||
staticText->mirror(m_axis_x);
|
||||
} else if (item->type() == PartDynamicTextField::Type) {
|
||||
PartDynamicTextField* dyntext = qgraphicsitem_cast<PartDynamicTextField*>(item);
|
||||
dyntext->mirror();
|
||||
dyntext->mirror(m_axis_x);
|
||||
} else if (item->type() == PartArc::Type) {
|
||||
PartArc* arc = qgraphicsitem_cast<PartArc*>(item);
|
||||
arc->mirror();
|
||||
arc->mirror(m_axis_x);
|
||||
} else if (item->type() == PartEllipse::Type) {
|
||||
PartEllipse* ellipse = qgraphicsitem_cast<PartEllipse*>(item);
|
||||
ellipse->mirror();
|
||||
ellipse->mirror(m_axis_x);
|
||||
} else if (item->type() == PartLine::Type) {
|
||||
PartLine* line = qgraphicsitem_cast<PartLine*>(item);
|
||||
line->mirror();
|
||||
line->mirror(m_axis_x);
|
||||
} else if (item->type() == PartPolygon::Type) {
|
||||
PartPolygon* poly = qgraphicsitem_cast<PartPolygon*>(item);
|
||||
poly->mirror();
|
||||
poly->mirror(m_axis_x);
|
||||
} else if (item->type() == PartRectangle::Type) {
|
||||
PartRectangle* rect = qgraphicsitem_cast<PartRectangle*>(item);
|
||||
rect->mirror();
|
||||
rect->mirror(m_axis_x);
|
||||
} else if (item->type() == PartTerminal::Type) {
|
||||
PartTerminal* term = qgraphicsitem_cast<PartTerminal*>(item);
|
||||
term->mirror();
|
||||
term->mirror(m_axis_x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -718,6 +741,7 @@ FlipElementsCommand::FlipElementsCommand(ElementScene *scene, QUndoCommand *pare
|
||||
ElementEditionCommand(QObject::tr("Retourner la sélection", "undo caption"), scene, nullptr, parent)
|
||||
{
|
||||
m_items = scene->selectedItems();
|
||||
m_axis_y = selectionCenter(m_items).y();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -728,28 +752,28 @@ void FlipElementsCommand::redo()
|
||||
foreach (auto *item, m_items) {
|
||||
if (item->type() == PartText::Type) {
|
||||
PartText* staticText = qgraphicsitem_cast<PartText*>(item);
|
||||
staticText->flip();
|
||||
staticText->flip(m_axis_y);
|
||||
} else if (item->type() == PartDynamicTextField::Type) {
|
||||
PartDynamicTextField* dyntext = qgraphicsitem_cast<PartDynamicTextField*>(item);
|
||||
dyntext->flip();
|
||||
dyntext->flip(m_axis_y);
|
||||
} else if (item->type() == PartArc::Type) {
|
||||
PartArc* arc = qgraphicsitem_cast<PartArc*>(item);
|
||||
arc->flip();
|
||||
arc->flip(m_axis_y);
|
||||
} else if (item->type() == PartEllipse::Type) {
|
||||
PartEllipse* ellipse = qgraphicsitem_cast<PartEllipse*>(item);
|
||||
ellipse->flip();
|
||||
ellipse->flip(m_axis_y);
|
||||
} else if (item->type() == PartLine::Type) {
|
||||
PartLine* line = qgraphicsitem_cast<PartLine*>(item);
|
||||
line->flip();
|
||||
line->flip(m_axis_y);
|
||||
} else if (item->type() == PartPolygon::Type) {
|
||||
PartPolygon* poly = qgraphicsitem_cast<PartPolygon*>(item);
|
||||
poly->flip();
|
||||
poly->flip(m_axis_y);
|
||||
} else if (item->type() == PartRectangle::Type) {
|
||||
PartRectangle* rect = qgraphicsitem_cast<PartRectangle*>(item);
|
||||
rect->flip();
|
||||
rect->flip(m_axis_y);
|
||||
} else if (item->type() == PartTerminal::Type) {
|
||||
PartTerminal* term = qgraphicsitem_cast<PartTerminal*>(item);
|
||||
term->flip();
|
||||
term->flip(m_axis_y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user