Skip to content

Commit

Permalink
Fix rendering of models with uncentered icons (#11251)
Browse files Browse the repository at this point in the history
* Fix rendering of models with uncentered icons

* Cleanup

---------

Co-authored-by: Adeel Asghar <adeel.asghar@liu.se>
  • Loading branch information
MaksKBV and adeas31 committed Nov 16, 2023
1 parent ee579da commit 958380c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
62 changes: 32 additions & 30 deletions OMEdit/OMEditLIB/Element/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,10 @@ Element::Element(ModelInstance::Component *pModelComponent, bool inherited, Grap
ModelInstance::CoordinateSystem coordinateSystem = getCoOrdinateSystemNew();
qreal initialScale = coordinateSystem.getInitialScale();
QVector<QPointF> extent;
extent.append(QPointF(initialScale * boundingRect().left(), initialScale * boundingRect().top()));
extent.append(QPointF(initialScale * boundingRect().right(), initialScale * boundingRect().bottom()));
qreal xExtent = initialScale * boundingRect().width() / 2;
qreal yExtent = initialScale * boundingRect().height() / 2;
extent.append(QPointF(-xExtent, -yExtent));
extent.append(QPointF(xExtent, yExtent));
mTransformation.setExtent(extent);
mTransformation.setRotateAngle(0.0);
} else if (!placementAnnotation.isEmpty()) {
Expand Down Expand Up @@ -699,8 +701,10 @@ Element::Element(QString name, LibraryTreeItem *pLibraryTreeItem, QString annota
CoOrdinateSystem coOrdinateSystem = getCoOrdinateSystem();
qreal initialScale = coOrdinateSystem.getInitialScale();
QVector<QPointF> extent;
extent.append(QPointF(initialScale * boundingRect().left(), initialScale * boundingRect().top()));
extent.append(QPointF(initialScale * boundingRect().right(), initialScale * boundingRect().bottom()));
qreal xExtent = initialScale * boundingRect().width() / 2;
qreal yExtent = initialScale * boundingRect().height() / 2;
extent.append(QPointF(-xExtent, -yExtent));
extent.append(QPointF(xExtent, yExtent));
mTransformation.setExtent(extent);
mTransformation.setRotateAngle(0.0);
}
Expand Down Expand Up @@ -3392,7 +3396,6 @@ void Element::prepareResizeElement(ResizerItem *pResizerItem)
prepareGeometryChange();
mOldTransformation = mTransformation;
mpSelectedResizerItem = pResizerItem;
mTransform = transform();
mSceneBoundingRect = sceneBoundingRect();
QPointF topLeft = sceneBoundingRect().topLeft();
QPointF topRight = sceneBoundingRect().topRight();
Expand Down Expand Up @@ -3438,39 +3441,38 @@ void Element::resizeElement(QPointF newPosition)
yDistance = yDistance * -1;
}
//Calculate the factors by dividing the distances againts the original size of this container
mXFactor = 0;
mYFactor = 0;
mXFactor = xDistance / mSceneBoundingRect.width();
mYFactor = yDistance / mSceneBoundingRect.height();
mXFactor = 1 + mXFactor;
mYFactor = 1 + mYFactor;
qreal xFactor = 0.0;
qreal yFactor = 0.0;
xFactor = xDistance / mSceneBoundingRect.width();
yFactor = yDistance / mSceneBoundingRect.height();
xFactor = 1 + xFactor;
yFactor = 1 + yFactor;
// if preserveAspectRatio is true then resize equally
CoOrdinateSystem coOrdinateSystem = getCoOrdinateSystem();
if (coOrdinateSystem.getPreserveAspectRatio()) {
qreal factor = qMax(qFabs(mXFactor), qFabs(mYFactor));
mXFactor = mXFactor < 0 ? factor * -1 : factor;
mYFactor = mYFactor < 0 ? factor * -1 : factor;
}
// Apply the transformation to the temporary polygon using the new scaling factors
QPointF pivot = mPivotPoint - pos();
// Creates a temporaty transformation
QTransform tmpTransform = QTransform().translate(pivot.x(), pivot.y()).rotate(0)
.scale(mXFactor, mYFactor)
.translate(-pivot.x(), -pivot.y());
setTransform(mTransform * tmpTransform);
// set the final resize on component.
qreal factor = qMax(qFabs(xFactor), qFabs(yFactor));
xFactor = xFactor < 0 ? factor * -1 : factor;
yFactor = yFactor < 0 ? factor * -1 : factor;
}
PointAnnotation startOrigin = mOldTransformation.getOrigin();
ExtentAnnotation startExtent = mOldTransformation.getExtent();
QPointF startExtent1 = startExtent.at(0);
QPointF startExtent2 = startExtent.at(1);
qreal x = mPivotPoint.x() + (startOrigin.x() - mPivotPoint.x()) * xFactor;
qreal y = mPivotPoint.y() + (startOrigin.y() - mPivotPoint.y()) * yFactor;
QPointF extent1, extent2;
qreal sx, sy;
getScale(&sx, &sy);
extent1.setX(sx * boundingRect().left());
extent1.setY(sy * boundingRect().top());
extent2.setX(sx * boundingRect().right());
extent2.setY(sy * boundingRect().bottom());
mTransformation.setOrigin(scenePos());
extent1.setX(xFactor * startExtent1.x());
extent1.setY(yFactor * startExtent1.y());
extent2.setX(xFactor * startExtent2.x());
extent2.setY(yFactor * startExtent2.y());
mTransformation.setOrigin(QPointF(x, y));
QVector<QPointF> extent;
extent.append(extent1);
extent.append(extent2);
mTransformation.setExtent(extent);
if (!qFuzzyCompare(mOldTransformation.getRotateAngle(), 0.0)) {
mTransformation.setRotateAngle((xFactor < 0 ? -1 : 1) * (yFactor < 0 ? -1 : 1) * mOldTransformation.getRotateAngle());
}
setTransform(mTransformation.getTransformationMatrix());
// let connections know that component has changed.
emit transformChange(false);
Expand Down
3 changes: 0 additions & 3 deletions OMEdit/OMEditLIB/Element/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,9 @@ class Element : public QObject, public QGraphicsItem
ResizerItem *mpBottomRightResizerItem;
ResizerItem *mpSelectedResizerItem;
OriginItem *mpOriginItem;
QTransform mTransform;
QRectF mSceneBoundingRect;
QPointF mTransformationStartPosition;
QPointF mPivotPoint;
qreal mXFactor;
qreal mYFactor;
QList<Element*> mInheritedElementsList;
QList<ShapeAnnotation*> mShapesList;
QList<Element*> mElementsList;
Expand Down
19 changes: 9 additions & 10 deletions OMEdit/OMEditLIB/Element/Transformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,6 @@ QTransform Transformation::getTransformationMatrixDiagram() const
if (!mpComponent && qFuzzyCompare(sy, 0.0)) {
sy = 1;
}
/* Ticket #3032. Adjust position based on the coordinate system of the component. */
if (mpComponent) {
position = position - (QPointF(mpComponent->boundingRect().center().x() * sx, mpComponent->boundingRect().center().y() * sy));
}

// get the horizontal flip
if (extent2Diagram.x() < extent1Diagram.x()) {
Expand All @@ -343,7 +339,10 @@ QTransform Transformation::getTransformationMatrixDiagram() const
}
// return the transformations
if (mpComponent) {
QTransform transformationMatrix = QTransform::fromScale(sx, sy);
QPointF diagExtentCenter = mpComponent->boundingRect().center();

QTransform transformationMatrix = QTransform::fromTranslate(-diagExtentCenter.x(), -diagExtentCenter.y());
transformationMatrix *= QTransform::fromScale(sx, sy);
transformationMatrix *= QTransform::fromTranslate(position.x(), position.y());
transformationMatrix *= QTransform().translate(mOriginDiagram.x(), mOriginDiagram.y()).rotate(mRotateAngleDiagram).translate(-mOriginDiagram.x(), -mOriginDiagram.y());
return transformationMatrix;
Expand Down Expand Up @@ -399,10 +398,7 @@ QTransform Transformation::getTransformationMatrixIcon()
if (!mpComponent && qFuzzyCompare(sy, 0.0)) {
sy = 1;
}
/* Ticket #3032. Adjust position based on the coordinate system of the component. */
if (mpComponent) {
position = position - (QPointF(mpComponent->boundingRect().center().x() * sx, mpComponent->boundingRect().center().y() * sy));
}

// get the horizontal flip
if (extent2Icon.x() < extent1Icon.x()) {
sx = -sx;
Expand All @@ -413,7 +409,10 @@ QTransform Transformation::getTransformationMatrixIcon()
}
// return the transformations
if (mpComponent) {
QTransform transformationMatrix = QTransform::fromScale(sx, sy);
QPointF extentCenter = mpComponent->boundingRect().center();

QTransform transformationMatrix = QTransform::fromTranslate(-extentCenter.x(), -extentCenter.y());
transformationMatrix *= QTransform::fromScale(sx, sy);
transformationMatrix *= QTransform::fromTranslate(position.x(), position.y());
transformationMatrix *= QTransform().translate(mOriginIcon.x(), mOriginIcon.y()).rotate(mRotateAngleIcon).translate(-mOriginIcon.x(), -mOriginIcon.y());
return transformationMatrix;
Expand Down

0 comments on commit 958380c

Please sign in to comment.