From 8c67b8c6cf86bf3c3599f46467d7866d6c48557d Mon Sep 17 00:00:00 2001 From: Adeel Asghar Date: Wed, 18 Oct 2023 14:30:45 +0200 Subject: [PATCH] Use the scene transform of connector and do not map values to scene (#11393) Fixes #11380 Do not move to center when redrawing the model. --- .../Annotations/BitmapAnnotation.cpp | 8 ++------ .../OMEditLIB/Annotations/BitmapAnnotation.h | 2 +- .../Annotations/EllipseAnnotation.cpp | 18 ++++++----------- .../OMEditLIB/Annotations/EllipseAnnotation.h | 2 +- .../OMEditLIB/Annotations/LineAnnotation.cpp | 11 ++-------- OMEdit/OMEditLIB/Annotations/LineAnnotation.h | 2 +- .../Annotations/PolygonAnnotation.cpp | 7 +++---- .../OMEditLIB/Annotations/PolygonAnnotation.h | 2 +- .../Annotations/RectangleAnnotation.cpp | 11 +++------- .../Annotations/RectangleAnnotation.h | 2 +- .../OMEditLIB/Annotations/ShapeAnnotation.h | 2 +- .../OMEditLIB/Annotations/TextAnnotation.cpp | 7 +++---- OMEdit/OMEditLIB/Annotations/TextAnnotation.h | 2 +- OMEdit/OMEditLIB/Element/Element.cpp | 7 ++++--- OMEdit/OMEditLIB/Modeling/Commands.cpp | 12 +++++------ .../Modeling/ModelWidgetContainer.cpp | 20 +++++++++++++------ .../OMEditLIB/Modeling/ModelWidgetContainer.h | 2 +- 17 files changed, 51 insertions(+), 66 deletions(-) diff --git a/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.cpp b/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.cpp index f31a532d1a5..b3173e87e54 100644 --- a/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.cpp +++ b/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.cpp @@ -197,7 +197,7 @@ void BitmapAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem * Q_UNUSED(option); Q_UNUSED(widget); if (mVisible) { - drawAnnotation(painter, false); + drawAnnotation(painter); } } @@ -205,14 +205,10 @@ void BitmapAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem * * \brief BitmapAnnotation::drawAnnotation * Draws the bitmap. * \param painter - * \param scene */ -void BitmapAnnotation::drawAnnotation(QPainter *painter, bool scene) +void BitmapAnnotation::drawAnnotation(QPainter *painter) { QRectF rect = getBoundingRect().normalized(); - if (scene) { - rect = mapToScene(getBoundingRect()).boundingRect().normalized(); - } QImage image = mImage.scaled(rect.width(), rect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); QPointF centerPoint = rect.center() - image.rect().center(); QRectF target(centerPoint.x(), centerPoint.y(), image.width(), image.height()); diff --git a/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.h b/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.h index 95f1cdc5613..19bf46ef231 100644 --- a/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/BitmapAnnotation.h @@ -58,7 +58,7 @@ class BitmapAnnotation : public ShapeAnnotation QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; - virtual void drawAnnotation(QPainter *painter, bool scene) override; + virtual void drawAnnotation(QPainter *painter) override; QString getOMCShapeAnnotation() override; QString getOMCShapeAnnotationWithShapeName() override; QString getShapeAnnotation() override; diff --git a/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.cpp b/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.cpp index 6911652ce71..4b1d04fd2fd 100644 --- a/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.cpp +++ b/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.cpp @@ -155,7 +155,7 @@ void EllipseAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem Q_UNUSED(option); Q_UNUSED(widget); if (mVisible) { - drawAnnotation(painter, false); + drawAnnotation(painter); } } @@ -163,26 +163,20 @@ void EllipseAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem * \brief EllipseAnnotation::drawAnnotation * Draws the ellipse. * \param painter - * \param scene */ -void EllipseAnnotation::drawAnnotation(QPainter *painter, bool scene) +void EllipseAnnotation::drawAnnotation(QPainter *painter) { QRectF boundingRectangle = boundingRect(); - if (!scene) { - // first we invert the painter since we have our coordinate system inverted. - // inversion is required to draw the elliptic curves at correct angles. - painter->scale(1.0, -1.0); - painter->translate(0, ((-boundingRectangle.top()) - boundingRectangle.bottom())); - } + // first we invert the painter since we have our coordinate system inverted. + // inversion is required to draw the elliptic curves at correct angles. + painter->scale(1.0, -1.0); + painter->translate(0, ((-boundingRectangle.top()) - boundingRectangle.bottom())); applyLinePattern(painter); if (mClosure != StringHandler::ClosureNone) { applyFillPattern(painter); } boundingRectangle = getBoundingRect(); - if (scene) { - boundingRectangle = mapToScene(getBoundingRect()).boundingRect(); - } if (mClosure == StringHandler::ClosureNone) { painter->drawArc(boundingRectangle, mStartAngle*16, mEndAngle*16 - mStartAngle*16); } else if (mClosure == StringHandler::ClosureChord) { diff --git a/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.h b/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.h index 1990682e030..b36f554b67d 100644 --- a/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/EllipseAnnotation.h @@ -56,7 +56,7 @@ class EllipseAnnotation : public ShapeAnnotation QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; - virtual void drawAnnotation(QPainter *painter, bool scene) override; + virtual void drawAnnotation(QPainter *painter) override; QString getOMCShapeAnnotation() override; QString getOMCShapeAnnotationWithShapeName() override; QString getShapeAnnotation() override; diff --git a/OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp b/OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp index 2bdfbc1dc8a..755b11cf55b 100644 --- a/OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp +++ b/OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp @@ -723,7 +723,7 @@ void LineAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op painter->setOpacity(0.3); } } - drawAnnotation(painter, false); + drawAnnotation(painter); /* issue #9557 * Redraw the connectors which collides with connection. */ @@ -771,20 +771,13 @@ void LineAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op * \brief LineAnnotation::drawAnnotation * Draws the line. * \param painter - * \param scene */ -void LineAnnotation::drawAnnotation(QPainter *painter, bool scene) +void LineAnnotation::drawAnnotation(QPainter *painter) { applyLinePattern(painter); QPainterPath path = getShape(); PointArrayAnnotation points = adjustPointsForDrawing(); - if (scene) { - path = mapToScene(path); - for (int i = 0; i < points.size(); ++i) { - points.setPoint(i, mapToScene(points.at(i))); - } - } // draw highlight for connections if (mLineType == LineAnnotation::ConnectionType) { diff --git a/OMEdit/OMEditLIB/Annotations/LineAnnotation.h b/OMEdit/OMEditLIB/Annotations/LineAnnotation.h index eb54ea4099f..0eef0322488 100644 --- a/OMEdit/OMEditLIB/Annotations/LineAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/LineAnnotation.h @@ -89,7 +89,7 @@ class LineAnnotation : public ShapeAnnotation QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; - virtual void drawAnnotation(QPainter *painter, bool scene) override; + virtual void drawAnnotation(QPainter *painter) override; void drawArrow(QPainter *painter, QPointF startPos, QPointF endPos, qreal size, int arrowType) const; QPolygonF perpendicularLine(QPointF startPos, QPointF endPos, qreal size) const; QString getOMCShapeAnnotation() override; diff --git a/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.cpp b/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.cpp index 50f6bca2bfe..529e0e9f1e6 100644 --- a/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.cpp +++ b/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.cpp @@ -224,7 +224,7 @@ void PolygonAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem Q_UNUSED(option); Q_UNUSED(widget); if (mVisible) { - drawAnnotation(painter, false); + drawAnnotation(painter); } } @@ -232,13 +232,12 @@ void PolygonAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem * \brief PolygonAnnotation::drawAnnotation * Draws the polygon. * \param painter - * \param scene */ -void PolygonAnnotation::drawAnnotation(QPainter *painter, bool scene) +void PolygonAnnotation::drawAnnotation(QPainter *painter) { applyLinePattern(painter); applyFillPattern(painter); - painter->drawPath(scene ? mapToScene(getShape()) : getShape()); + painter->drawPath(getShape()); } /*! diff --git a/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.h b/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.h index a5581c94b4d..bf7a38d4fab 100644 --- a/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/PolygonAnnotation.h @@ -58,7 +58,7 @@ class PolygonAnnotation : public ShapeAnnotation QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; - virtual void drawAnnotation(QPainter *painter, bool scene) override; + virtual void drawAnnotation(QPainter *painter) override; QString getOMCShapeAnnotation() override; QString getOMCShapeAnnotationWithShapeName() override; QString getShapeAnnotation() override; diff --git a/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.cpp b/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.cpp index d50b2e03701..a8defd1a016 100644 --- a/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.cpp +++ b/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.cpp @@ -202,7 +202,7 @@ void RectangleAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsIte painter->setOpacity(0.2); } } - drawAnnotation(painter, false); + drawAnnotation(painter); } } @@ -210,17 +210,12 @@ void RectangleAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsIte * \brief RectangleAnnotation::drawAnnotation * Draws the rectangle. * \param painter - * \param scene */ -void RectangleAnnotation::drawAnnotation(QPainter *painter, bool scene) +void RectangleAnnotation::drawAnnotation(QPainter *painter) { applyLinePattern(painter); applyFillPattern(painter); - if (scene) { - painter->drawRoundedRect(mapToScene(getBoundingRect()).boundingRect().normalized(), mRadius, mRadius); - } else { - painter->drawRoundedRect(getBoundingRect().normalized(), mRadius, mRadius); - } + painter->drawRoundedRect(getBoundingRect().normalized(), mRadius, mRadius); } /*! diff --git a/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.h b/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.h index 0273fffe10e..102d8a09a8f 100644 --- a/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/RectangleAnnotation.h @@ -59,7 +59,7 @@ class RectangleAnnotation : public ShapeAnnotation QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; - virtual void drawAnnotation(QPainter *painter, bool scene) override; + virtual void drawAnnotation(QPainter *painter) override; QString getOMCShapeAnnotation() override; QString getOMCShapeAnnotationWithShapeName() override; QString getShapeAnnotation() override; diff --git a/OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h b/OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h index d741f4af053..3b66fa2695f 100644 --- a/OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h @@ -144,7 +144,7 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem virtual QString getOMCShapeAnnotation() = 0; virtual QString getOMCShapeAnnotationWithShapeName() = 0; virtual QString getShapeAnnotation() = 0; - virtual void drawAnnotation(QPainter *painter, bool scene) = 0; + virtual void drawAnnotation(QPainter *painter) = 0; QList getExtentsForInheritedShapeFromIconDiagramMap(GraphicsView *pGraphicsView, ShapeAnnotation *pReferenceShapeAnnotation); void applyTransformation(); void drawCornerItems(); diff --git a/OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp b/OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp index 152d68e1bf8..a123383c230 100644 --- a/OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp +++ b/OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp @@ -351,7 +351,7 @@ void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op painter->setOpacity(0.2); } } - drawAnnotation(painter, false); + drawAnnotation(painter); } } @@ -359,9 +359,8 @@ void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op * \brief TextAnnotation::drawAnnotation * Draws the text. * \param painter - * \param scene */ -void TextAnnotation::drawAnnotation(QPainter *painter, bool scene) +void TextAnnotation::drawAnnotation(QPainter *painter) { applyLinePattern(painter); /* Don't apply the fill patterns on Text shapes. */ @@ -397,7 +396,7 @@ void TextAnnotation::drawAnnotation(QPainter *painter, bool scene) sy = scaleY; } // map the existing bounding rect to new transformation - QRectF boundingRectangle = scene ? mapToScene(boundingRect()).boundingRect() : boundingRect(); + QRectF boundingRectangle = boundingRect(); QRectF mappedBoundingRect = QRectF(boundingRectangle.x() * sx, boundingRectangle.y() * sy, boundingRectangle.width() * sx, boundingRectangle.height() * sy); // map the existing bounding rect to new transformation but with positive width and height so that font metrics can work QRectF absMappedBoundingRect = QRectF(boundingRectangle.x() * sx, boundingRectangle.y() * sy, qAbs(boundingRectangle.width() * sx), qAbs(boundingRectangle.height() * sy)); diff --git a/OMEdit/OMEditLIB/Annotations/TextAnnotation.h b/OMEdit/OMEditLIB/Annotations/TextAnnotation.h index 6e7a7c4dacb..4a1ba41ae39 100644 --- a/OMEdit/OMEditLIB/Annotations/TextAnnotation.h +++ b/OMEdit/OMEditLIB/Annotations/TextAnnotation.h @@ -63,7 +63,7 @@ class TextAnnotation : public ShapeAnnotation QRectF boundingRect() const override; QPainterPath shape() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; - virtual void drawAnnotation(QPainter *painter, bool scene) override; + virtual void drawAnnotation(QPainter *painter) override; QString getOMCShapeAnnotation() override; QString getOMCShapeAnnotationWithShapeName() override; QString getShapeAnnotation() override; diff --git a/OMEdit/OMEditLIB/Element/Element.cpp b/OMEdit/OMEditLIB/Element/Element.cpp index 3170442cacd..c6bbaec6d75 100644 --- a/OMEdit/OMEditLIB/Element/Element.cpp +++ b/OMEdit/OMEditLIB/Element/Element.cpp @@ -2253,13 +2253,13 @@ void Element::reDrawConnector(QPainter *painter) { if (mpDefaultElementRectangle && mpDefaultElementRectangle->isVisible()) { painter->save(); - mpDefaultElementRectangle->drawAnnotation(painter, true); + mpDefaultElementRectangle->drawAnnotation(painter); painter->restore(); } if (mpDefaultElementText && mpDefaultElementText->isVisible()) { painter->save(); - mpDefaultElementText->drawAnnotation(painter, true); + mpDefaultElementText->drawAnnotation(painter); painter->restore(); } @@ -2269,7 +2269,8 @@ void Element::reDrawConnector(QPainter *painter) foreach (ShapeAnnotation *pShapeAnnotation, mShapesList) { painter->save(); - pShapeAnnotation->drawAnnotation(painter, true); + painter->setTransform(pShapeAnnotation->sceneTransform(), true); + pShapeAnnotation->drawAnnotation(painter); painter->restore(); } diff --git a/OMEdit/OMEditLIB/Modeling/Commands.cpp b/OMEdit/OMEditLIB/Modeling/Commands.cpp index d22b599e31d..573c9b5d74e 100644 --- a/OMEdit/OMEditLIB/Modeling/Commands.cpp +++ b/OMEdit/OMEditLIB/Modeling/Commands.cpp @@ -359,12 +359,12 @@ void UpdateComponentTransformationsCommand::redoInternal() mpComponent->setPos(0, 0); mpComponent->setFlag(QGraphicsItem::ItemSendsGeometryChanges, state); mpComponent->setTransform(mNewTransformation.getTransformationMatrix()); - if (mpComponent->getGraphicsView()->getViewType() == StringHandler::Diagram && mpComponent->getGraphicsView()->getModelWidget()->isNewApi()) { - mpComponent->getGraphicsView()->handleCollidingConnections(); - } mpComponent->mTransformation = mNewTransformation; mpComponent->emitTransformChange(mPositionChanged); mpComponent->emitTransformHasChanged(); + if (mpComponent->getGraphicsView()->getViewType() == StringHandler::Diagram && mpComponent->getGraphicsView()->getModelWidget()->isNewApi()) { + mpComponent->getGraphicsView()->handleCollidingConnections(); + } } /*! @@ -400,12 +400,12 @@ void UpdateComponentTransformationsCommand::undo() mpComponent->setPos(0, 0); mpComponent->setFlag(QGraphicsItem::ItemSendsGeometryChanges, state); mpComponent->setTransform(mOldTransformation.getTransformationMatrix()); - if (mpComponent->getGraphicsView()->getViewType() == StringHandler::Diagram && mpComponent->getGraphicsView()->getModelWidget()->isNewApi()) { - mpComponent->getGraphicsView()->handleCollidingConnections(); - } mpComponent->mTransformation = mOldTransformation; mpComponent->emitTransformChange(mPositionChanged); mpComponent->emitTransformHasChanged(); + if (mpComponent->getGraphicsView()->getViewType() == StringHandler::Diagram && mpComponent->getGraphicsView()->getModelWidget()->isNewApi()) { + mpComponent->getGraphicsView()->handleCollidingConnections(); + } } UpdateElementAttributesCommand::UpdateElementAttributesCommand(Element *pComponent, const ElementInfo &oldComponentInfo, const ElementInfo &newComponentInfo, UndoCommand *pParent) diff --git a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp index 6c68a3288df..1def1324fd4 100644 --- a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp +++ b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp @@ -185,9 +185,9 @@ GraphicsView::GraphicsView(StringHandler::ViewType viewType, ModelWidget *pModel if (!qFuzzyCompare(horizontal, 2) || !qFuzzyCompare(vertical, 2)) { mCoOrdinateSystem.setGrid(QPointF(horizontal, vertical)); } - setExtentRectangle(mCoOrdinateSystem.getExtentRectangle()); + setExtentRectangle(mCoOrdinateSystem.getExtentRectangle(), true); } else { // when opening a model use the default Modelica specification values - setExtentRectangle(mCoOrdinateSystem.getExtentRectangle()); + setExtentRectangle(mCoOrdinateSystem.getExtentRectangle(), true); } mMergedCoOrdinateSystem = mCoOrdinateSystem; scale(1.0, -1.0); // invert the drawing area. @@ -321,7 +321,7 @@ void GraphicsView::drawCoordinateSystem() } } - setExtentRectangle(mMergedCoOrdinateSystem.getExtentRectangle()); + setExtentRectangle(mMergedCoOrdinateSystem.getExtentRectangle(), false); resize(size()); } @@ -725,11 +725,19 @@ bool GraphicsView::isCreatingShape() isCreatingTextShape(); } -void GraphicsView::setExtentRectangle(const QRectF rectangle) +/*! + * \brief GraphicsView::setExtentRectangle + * Increases the size of the extent rectangle by 25%. + * \param rectangle + * \param moveToCenter + */ +void GraphicsView::setExtentRectangle(const QRectF rectangle, bool moveToCenter) { QRectF sceneRectangle = Utilities::adjustSceneRectangle(rectangle, 0.25); setSceneRect(sceneRectangle); - centerOn(sceneRectangle.center()); + if (moveToCenter) { + centerOn(sceneRectangle.center()); + } } void GraphicsView::setIsCreatingConnection(const bool enable) @@ -8193,7 +8201,7 @@ void ModelWidget::drawModelCoOrdinateSystem(GraphicsView *pGraphicsView) readCoOrdinateSystemFromInheritedClass(this, pGraphicsView); } - pGraphicsView->setExtentRectangle(pGraphicsView->mMergedCoOrdinateSystem.getExtentRectangle()); + pGraphicsView->setExtentRectangle(pGraphicsView->mMergedCoOrdinateSystem.getExtentRectangle(), false); pGraphicsView->resize(pGraphicsView->size()); } diff --git a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h index acbfe70892f..dc9cde900f2 100644 --- a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h +++ b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h @@ -195,7 +195,7 @@ class GraphicsView : public QGraphicsView void handleCollidingConnections(); - void setExtentRectangle(const QRectF rectangle); + void setExtentRectangle(const QRectF rectangle, bool moveToCenter); void setIsCustomScale(bool enable) {mIsCustomScale = enable;} bool isCustomScale() {return mIsCustomScale;} void setAddClassAnnotationNeeded(bool needed) {mAddClassAnnotationNeeded = needed;}