Skip to content

Commit

Permalink
Use the scene transform of connector and do not map values to scene (#…
Browse files Browse the repository at this point in the history
…11393)

Fixes #11380

Do not move to center when redrawing the model.
  • Loading branch information
adeas31 committed Oct 18, 2023
1 parent c1d324b commit 8c67b8c
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 66 deletions.
8 changes: 2 additions & 6 deletions OMEdit/OMEditLIB/Annotations/BitmapAnnotation.cpp
Expand Up @@ -197,22 +197,18 @@ void BitmapAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *
Q_UNUSED(option);
Q_UNUSED(widget);
if (mVisible) {
drawAnnotation(painter, false);
drawAnnotation(painter);
}
}

/*!
* \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());
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/BitmapAnnotation.h
Expand Up @@ -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;
Expand Down
18 changes: 6 additions & 12 deletions OMEdit/OMEditLIB/Annotations/EllipseAnnotation.cpp
Expand Up @@ -155,34 +155,28 @@ void EllipseAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem
Q_UNUSED(option);
Q_UNUSED(widget);
if (mVisible) {
drawAnnotation(painter, false);
drawAnnotation(painter);
}
}

/*!
* \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) {
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/EllipseAnnotation.h
Expand Up @@ -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;
Expand Down
11 changes: 2 additions & 9 deletions OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/LineAnnotation.h
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions OMEdit/OMEditLIB/Annotations/PolygonAnnotation.cpp
Expand Up @@ -224,21 +224,20 @@ void PolygonAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem
Q_UNUSED(option);
Q_UNUSED(widget);
if (mVisible) {
drawAnnotation(painter, false);
drawAnnotation(painter);
}
}

/*!
* \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());
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/PolygonAnnotation.h
Expand Up @@ -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;
Expand Down
11 changes: 3 additions & 8 deletions OMEdit/OMEditLIB/Annotations/RectangleAnnotation.cpp
Expand Up @@ -202,25 +202,20 @@ void RectangleAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsIte
painter->setOpacity(0.2);
}
}
drawAnnotation(painter, false);
drawAnnotation(painter);
}
}

/*!
* \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);
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/RectangleAnnotation.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h
Expand Up @@ -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<QPointF> getExtentsForInheritedShapeFromIconDiagramMap(GraphicsView *pGraphicsView, ShapeAnnotation *pReferenceShapeAnnotation);
void applyTransformation();
void drawCornerItems();
Expand Down
7 changes: 3 additions & 4 deletions OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp
Expand Up @@ -351,17 +351,16 @@ void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
painter->setOpacity(0.2);
}
}
drawAnnotation(painter, false);
drawAnnotation(painter);
}
}

/*!
* \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. */
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/TextAnnotation.h
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions OMEdit/OMEditLIB/Element/Element.cpp
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
12 changes: 6 additions & 6 deletions OMEdit/OMEditLIB/Modeling/Commands.cpp
Expand Up @@ -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();
}
}

/*!
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -321,7 +321,7 @@ void GraphicsView::drawCoordinateSystem()
}
}

setExtentRectangle(mMergedCoOrdinateSystem.getExtentRectangle());
setExtentRectangle(mMergedCoOrdinateSystem.getExtentRectangle(), false);
resize(size());
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -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;}
Expand Down

0 comments on commit 8c67b8c

Please sign in to comment.