From 4fa24ee1178ccdc45d574c86c2644263e5aa7822 Mon Sep 17 00:00:00 2001 From: Adeel Asghar Date: Fri, 14 Nov 2014 05:00:30 +0000 Subject: [PATCH] #2506 - Keep the connecting lines manhattanized. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23357 f25d12d1-65f4-0310-ae8a-bbce733d8d8e --- .../OMEditGUI/Annotations/LineAnnotation.cpp | 489 +++++++----------- OMEdit/OMEditGUI/Annotations/LineAnnotation.h | 4 +- .../Annotations/PolygonAnnotation.cpp | 15 +- .../OMEditGUI/Annotations/PolygonAnnotation.h | 2 + .../OMEditGUI/Annotations/ShapeAnnotation.cpp | 182 +++++-- .../OMEditGUI/Annotations/ShapeAnnotation.h | 10 +- .../Annotations/ShapePropertiesDialog.cpp | 14 +- OMEdit/OMEditGUI/Component/Component.cpp | 17 +- OMEdit/OMEditGUI/Component/CornerItem.cpp | 25 +- OMEdit/OMEditGUI/Component/CornerItem.h | 2 + .../Modeling/ModelWidgetContainer.cpp | 48 +- 11 files changed, 397 insertions(+), 411 deletions(-) diff --git a/OMEdit/OMEditGUI/Annotations/LineAnnotation.cpp b/OMEdit/OMEditGUI/Annotations/LineAnnotation.cpp index 32e35690b19..8b31611f962 100644 --- a/OMEdit/OMEditGUI/Annotations/LineAnnotation.cpp +++ b/OMEdit/OMEditGUI/Annotations/LineAnnotation.cpp @@ -42,8 +42,8 @@ LineAnnotation::LineAnnotation(QString annotation, Component *pParent) : ShapeAnnotation(pParent) { mLineType = LineAnnotation::ComponentType; - mpStartComponent = 0; - mpEndComponent = 0; + setStartComponent(0); + setEndComponent(0); // set the default values GraphicItem::setDefaults(); ShapeAnnotation::setDefaults(); @@ -57,8 +57,8 @@ LineAnnotation::LineAnnotation(QString annotation, bool inheritedShape, Graphics { setFlag(QGraphicsItem::ItemIsSelectable); mLineType = LineAnnotation::ShapeType; - mpStartComponent = 0; - mpEndComponent = 0; + setStartComponent(0); + setEndComponent(0); // set the default values GraphicItem::setDefaults(); ShapeAnnotation::setDefaults(); @@ -66,8 +66,9 @@ LineAnnotation::LineAnnotation(QString annotation, bool inheritedShape, Graphics ShapeAnnotation::setUserDefaults(); parseShapeAnnotation(annotation); /* Only set the ItemIsMovable flag on shape if the class is not a system library class OR shape is not an inherited shape. */ - if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && !isInheritedShape()) + if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && !isInheritedShape()) { setFlag(QGraphicsItem::ItemIsMovable); + } mpGraphicsView->addShapeObject(this); mpGraphicsView->scene()->addItem(this); connect(this, SIGNAL(updateClassAnnotation()), mpGraphicsView, SLOT(addClassAnnotation())); @@ -83,8 +84,7 @@ LineAnnotation::LineAnnotation(Component *pStartComponent, GraphicsView *pGraphi GraphicItem::setDefaults(); ShapeAnnotation::setDefaults(); // use the linecolor of start component for the connection line. - if (pStartComponent->getShapesList().size() > 0) - { + if (pStartComponent->getShapesList().size() > 0) { ShapeAnnotation *pShapeAnnotation = pStartComponent->getShapesList().at(0); mLineColor = pShapeAnnotation->getLineColor(); } @@ -92,6 +92,7 @@ LineAnnotation::LineAnnotation(Component *pStartComponent, GraphicsView *pGraphi mpGraphicsView->scene()->addItem(this); // set the start component setStartComponent(pStartComponent); + setEndComponent(0); } LineAnnotation::LineAnnotation(QString annotation, bool inheritedShape, Component *pStartComponent, Component *pEndComponent, @@ -111,8 +112,7 @@ LineAnnotation::LineAnnotation(QString annotation, bool inheritedShape, Componen parseShapeAnnotation(annotation); /* make the points relative to origin */ QList points; - for (int i = 0 ; i < mPoints.size() ; i++) - { + for (int i = 0 ; i < mPoints.size() ; i++) { QPointF point = mOrigin + mPoints[i]; points.append(point); } @@ -127,24 +127,20 @@ void LineAnnotation::parseShapeAnnotation(QString annotation) GraphicItem::parseShapeAnnotation(annotation); // parse the shape to get the list of attributes of Line. QStringList list = StringHandler::getStrings(annotation); - if (list.size() < 10) - { + if (list.size() < 10) { return; } // 4th item of list contains the points. QStringList pointsList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(3))); - foreach (QString point, pointsList) - { + foreach (QString point, pointsList) { QStringList linePoints = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(point)); - if (linePoints.size() >= 2) - { + if (linePoints.size() >= 2) { addPoint(QPointF(linePoints.at(0).toFloat(), linePoints.at(1).toFloat())); } } // 5th item of list contains the color. QStringList colorList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(4))); - if (colorList.size() >= 3) - { + if (colorList.size() >= 3) { int red, green, blue = 0; red = colorList.at(0).toInt(); green = colorList.at(1).toInt(); @@ -157,8 +153,7 @@ void LineAnnotation::parseShapeAnnotation(QString annotation) mLineThickness = list.at(6).toFloat(); // 8th item of list contains the Line Arrows. QStringList arrowList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(7))); - if (arrowList.size() >= 2) - { + if (arrowList.size() >= 2) { mArrow.replace(0, StringHandler::getArrowType(arrowList.at(0))); mArrow.replace(1, StringHandler::getArrowType(arrowList.at(1))); } @@ -171,21 +166,15 @@ void LineAnnotation::parseShapeAnnotation(QString annotation) QPainterPath LineAnnotation::getShape() const { QPainterPath path; - if (mPoints.size() > 0) - { + if (mPoints.size() > 0) { // mPoints.size() is at least 1 path.moveTo(mPoints.at(0)); - if (mSmooth) - { - if (mPoints.size() == 2) - { + if (mSmooth) { + if (mPoints.size() == 2) { // if points are only two then spline acts as simple line path.lineTo(mPoints.at(1)); - } - else - { - for (int i = 2 ; i < mPoints.size() ; i++) - { + } else { + for (int i = 2 ; i < mPoints.size() ; i++) { QPointF point3 = mPoints.at(i); // calculate middle points for bezier curves QPointF point2 = mPoints.at(i - 1); @@ -195,17 +184,13 @@ QPainterPath LineAnnotation::getShape() const path.lineTo(point12); path.cubicTo(point12, point2, point23); // if its the last point - if (i == mPoints.size() - 1) - { + if (i == mPoints.size() - 1) { path.lineTo(point3); } } } - } - else - { - for (int i = 1 ; i < mPoints.size() ; i++) - { + } else { + for (int i = 1 ; i < mPoints.size() ; i++) { path.lineTo(mPoints.at(i)); } } @@ -228,8 +213,7 @@ void LineAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op { Q_UNUSED(option); Q_UNUSED(widget); - if (mVisible) - { + if (mVisible) { drawLineAnnotaion(painter); } } @@ -238,33 +222,25 @@ void LineAnnotation::drawLineAnnotaion(QPainter *painter) { applyLinePattern(painter); // draw start arrow - if (mPoints.size() > 1) - { - if (mArrow.at(0) == StringHandler::ArrowFilled) - { + if (mPoints.size() > 1) { + if (mArrow.at(0) == StringHandler::ArrowFilled) { painter->save(); painter->setBrush(QBrush(mLineColor, Qt::SolidPattern)); painter->drawPolygon(drawArrow(mPoints.at(0), mPoints.at(1), mArrowSize, mArrow.at(0))); painter->restore(); - } - else - { + } else { painter->drawPolygon(drawArrow(mPoints.at(0), mPoints.at(1), mArrowSize, mArrow.at(0))); } } painter->drawPath(getShape()); // draw end arrow - if (mPoints.size() > 1) - { - if (mArrow.at(1) == StringHandler::ArrowFilled) - { + if (mPoints.size() > 1) { + if (mArrow.at(1) == StringHandler::ArrowFilled) { painter->save(); painter->setBrush(QBrush(mLineColor, Qt::SolidPattern)); painter->drawPolygon(drawArrow(mPoints.at(mPoints.size() - 1), mPoints.at(mPoints.size() - 2), mArrowSize, mArrow.at(1))); painter->restore(); - } - else - { + } else { painter->drawPolygon(drawArrow(mPoints.at(mPoints.size() - 1), mPoints.at(mPoints.size() - 2), mArrowSize, mArrow.at(1))); } } @@ -276,8 +252,7 @@ QPolygonF LineAnnotation::drawArrow(QPointF startPos, QPointF endPos, qreal size double yA = size * sqrt(3) / 2; double xB = -xA; double yB = yA; - switch (arrowType) - { + switch (arrowType) { case StringHandler::ArrowFilled: break; case StringHandler::ArrowHalf: @@ -289,22 +264,15 @@ QPolygonF LineAnnotation::drawArrow(QPointF startPos, QPointF endPos, qreal size break; } double angle = 0.0f; - if (endPos.x() - startPos.x() == 0) - { - if (endPos.y() - startPos.y() >= 0) - { + if (endPos.x() - startPos.x() == 0) { + if (endPos.y() - startPos.y() >= 0) { angle = 0; - } - else - { + } else { angle = M_PI; } - } - else - { + } else { angle = -(M_PI / 2 - (atan((endPos.y() - startPos.y())/(endPos.x() - startPos.x())))); - if(startPos.x() > endPos.x()) - { + if(startPos.x() > endPos.x()) { angle += M_PI; } } @@ -337,27 +305,22 @@ QString LineAnnotation::getShapeAnnotation() annotationString.append(GraphicItem::getShapeAnnotation()); // get points QString pointsString; - if (mPoints.size() > 0) - { + if (mPoints.size() > 0) { pointsString.append("points={"); } - for (int i = 0 ; i < mPoints.size() ; i++) - { + for (int i = 0 ; i < mPoints.size() ; i++) { pointsString.append("{").append(QString::number(mPoints[i].x())).append(","); pointsString.append(QString::number(mPoints[i].y())).append("}"); - if (i < mPoints.size() - 1) - { + if (i < mPoints.size() - 1) { pointsString.append(","); } } - if (mPoints.size() > 0) - { + if (mPoints.size() > 0) { pointsString.append("}"); annotationString.append(pointsString); } // get the line color - if (mLineColor != Qt::black) - { + if (mLineColor != Qt::black) { QString colorString; colorString.append("color={"); colorString.append(QString::number(mLineColor.red())).append(","); @@ -367,18 +330,15 @@ QString LineAnnotation::getShapeAnnotation() annotationString.append(colorString); } // get the line pattern - if (mLinePattern != StringHandler::LineSolid) - { + if (mLinePattern != StringHandler::LineSolid) { annotationString.append(QString("pattern=").append(StringHandler::getLinePatternString(mLinePattern))); } // get the thickness - if (mLineThickness != 0.25) - { + if (mLineThickness != 0.25) { annotationString.append(QString("thickness=").append(QString::number(mLineThickness))); } // get the start and end arrow - if ((mArrow.at(0) != StringHandler::ArrowNone) || (mArrow.at(1) != StringHandler::ArrowNone)) - { + if ((mArrow.at(0) != StringHandler::ArrowNone) || (mArrow.at(1) != StringHandler::ArrowNone)) { QString arrowString; arrowString.append("arrow="); arrowString.append("{").append(StringHandler::getArrowString(mArrow.at(0))).append(","); @@ -386,13 +346,11 @@ QString LineAnnotation::getShapeAnnotation() annotationString.append(arrowString); } // get the arrow size - if (mArrowSize != 3) - { + if (mArrowSize != 3) { annotationString.append(QString("arrowSize=").append(QString::number(mArrowSize))); } // get the smooth - if (mSmooth != StringHandler::SmoothNone) - { + if (mSmooth != StringHandler::SmoothNone) { annotationString.append(QString("smooth=").append(StringHandler::getSmoothString(mSmooth))); } return QString("Line(").append(annotationString.join(",")).append(")"); @@ -421,140 +379,134 @@ Component* LineAnnotation::getEndComponent() void LineAnnotation::addPoint(QPointF point) { mPoints.append(point); - qreal startAngle = 0; - switch (mLineType) - { - case LineAnnotation::ConnectionType: - if (mpStartComponent) - { - startAngle = StringHandler::getNormalizedAngle(mpStartComponent->getRootParentComponent()->getTransformation()->getRotateAngle()); - } - if(mPoints.size() <= 2) - { - if(startAngle >= 0 && startAngle < 90) - { - mGeometries.push_back(LineAnnotation::Horizontal); - } - else if(startAngle >= 90 && startAngle < 180) - { - mGeometries.push_back(LineAnnotation::Vertical); - } - else if(startAngle >= 180 && startAngle < 270) - { - mGeometries.push_back(LineAnnotation::Horizontal); - } - else if(startAngle >= 270 && startAngle < 360) - { - mGeometries.push_back(LineAnnotation::Vertical); - } - else - { - mGeometries.push_back(LineAnnotation::Horizontal); - } + if (mPoints.size() > 1) { + if (mGeometries.size() == 0) { + QPointF currentPoint = mPoints[mPoints.size() - 1]; + QPointF previousPoint = mPoints[mPoints.size() - 2]; + mGeometries.append(findLineGeometryType(previousPoint, currentPoint)); + } else { + if (mGeometries.back() == ShapeAnnotation::HorizontalLine) { + mGeometries.push_back(ShapeAnnotation::VerticalLine); + } else if (mGeometries.back() == ShapeAnnotation::VerticalLine) { + mGeometries.push_back(ShapeAnnotation::HorizontalLine); } - else - { - if(mGeometries.back() == LineAnnotation::Horizontal) - { - mGeometries.push_back(LineAnnotation::Vertical); - } - else if(mGeometries.back() == LineAnnotation::Vertical) - { - mGeometries.push_back(LineAnnotation::Horizontal); - } - else if(mGeometries.back() == LineAnnotation::Diagonal) - { - mGeometries.push_back(LineAnnotation::Diagonal); - //Give new line correct angle! + } + } +} + +void LineAnnotation::removePoint(int index) +{ + if (mPoints.size() > index) { + mPoints.removeAt(index); + } + if (mGeometries.size() > index - 1) { + mGeometries.removeAt(index -1); + // adjust the remaining geometries accordingly + for (int i = index - 1 ; i < mGeometries.size() ; i++) { + if (mGeometries.size() > i - 1) { + if (mGeometries[i - 1] == ShapeAnnotation::HorizontalLine) { + mGeometries[i] = ShapeAnnotation::VerticalLine; + } else if (mGeometries[i - 1] == ShapeAnnotation::VerticalLine) { + mGeometries[i] = ShapeAnnotation::HorizontalLine; } } - break; - default: - break; + } } } -//! Updates the first point of the connection, and adjusts the second point accordingly depending on the geometry list. -//! @param point is the new start point. -//! @see updateEndPoint(QPointF point) +/*! + Clears the points list. + */ +void LineAnnotation::clearPoints() +{ + mPoints.clear(); + mGeometries.clear(); +} + +/*! + Updates the first point of the connection, and adjusts the second point accordingly depending on the geometry list. + \param point - is the new start point. + \sa updateEndPoint(QPointF point) + */ void LineAnnotation::updateStartPoint(QPointF point) { - if (mPoints.size() == 0) - { - mPoints.push_back(point); + qreal dx = point.x() - mPoints[0].x(); + qreal dy = point.y() - mPoints[0].y(); + // if connection points are just two we need to add extra points + if (mPoints.size() == 2) { + // just check if additional points are really needed or not. + if ((mGeometries[0] == ShapeAnnotation::HorizontalLine && mPoints[0].y() != point.y()) || + (mGeometries[0] == ShapeAnnotation::VerticalLine && mPoints[0].x() != point.x())) { + insertPointsGeometriesAndCornerItems(1); + setCornerItemsPassive(); + } } - else - { + /* update the 1st point */ + if (mPoints.size() > 0) { mPoints[0] = point; - } - /* update the 1st CornerItem */ - if (mCornerItemsList.size() > 0) - { - mCornerItemsList[0]->setPos(point); + updateCornerItem(0); } /* update the 2nd point */ - if (mPoints.size() > 2) - { - if (mGeometries[0] == LineAnnotation::Horizontal) - { - mPoints[1] = QPointF(mPoints[1].x(),mPoints[0].y()); - } - else if (mGeometries[0] == LineAnnotation::Vertical) - { - mPoints[1] = QPointF(mPoints[0].x(),mPoints[1].y()); - } - /* updated the 2nd CornerItem */ - if (mCornerItemsList.size() > 1) - { - mCornerItemsList[1]->setPos(mPoints[1]); + if (mPoints.size() > 1) { + if (mGeometries[0] == ShapeAnnotation::HorizontalLine) { + mPoints[1] = QPointF(mPoints[1].x(), mPoints[1].y() + dy); + } else if (mGeometries[0] == ShapeAnnotation::VerticalLine) { + mPoints[1] = QPointF(mPoints[1].x() + dx, mPoints[1].y()); } + updateCornerItem(1); } } +/*! + Updates the end point of the connection, and adjusts the second last point accordingly depending on the geometry list. + \param point - is the new end point. + \sa updateStartPoint(QPointF point) + */ void LineAnnotation::updateEndPoint(QPointF point) { - mPoints.back() = point; - switch (mLineType) - { - case LineAnnotation::ConnectionType: - /* updated the last CornerItem */ - if (mCornerItemsList.size() > (mPoints.size() - 1)) - { - mCornerItemsList[mPoints.size() - 1]->setPos(point); + if (mLineType == LineAnnotation::ConnectionType) { + int lastIndex = mPoints.size() - 1; + int secondLastIndex = mPoints.size() - 2; + qreal dx = point.x() - mPoints[lastIndex].x(); + qreal dy = point.y() - mPoints[lastIndex].y(); + /* + if connection points are just two we need to add extra points + This function is also called when creating a component so for that we don't need to add extra points. In order to avoid this we check + for the mpEndComponent since mpEndComponent will only be set when the connection is complete. + */ + if (mPoints.size() == 2 && mpEndComponent) { + // just check if additional points are really needed or not. + if ((mGeometries[secondLastIndex] == ShapeAnnotation::HorizontalLine && mPoints[lastIndex].y() != point.y()) || + (mGeometries[secondLastIndex] == ShapeAnnotation::VerticalLine && mPoints[lastIndex].x() != point.x())) { + insertPointsGeometriesAndCornerItems(lastIndex); + setCornerItemsPassive(); + lastIndex = mPoints.size() - 1; + secondLastIndex = mPoints.size() - 2; } - if (mPoints.size() > 2) - { - /* update the 2nd last point */ - if (mGeometries.back() == LineAnnotation::Horizontal) - { - mPoints[mPoints.size() - 2] = QPointF(mPoints[mPoints.size() - 2].x(),point.y()); - } - else if (mGeometries.back() == LineAnnotation::Vertical) - { - mPoints[mPoints.size() - 2] = QPointF(point.x(),mPoints[mPoints.size() - 2].y()); - } - /* updated the 2nd last CornerItem */ - if (mCornerItemsList.size() > (mPoints.size() - 2)) - { - mCornerItemsList[mPoints.size() - 2]->setPos(mPoints[mPoints.size() - 2]); - } + } + /* update the last point */ + if (mPoints.size() > 1) { + mPoints.back() = point; + updateCornerItem(lastIndex); + /* update the 2nd point */ + if (mGeometries[secondLastIndex] == ShapeAnnotation::HorizontalLine) { + mPoints[secondLastIndex] = QPointF(mPoints[secondLastIndex].x(), mPoints[secondLastIndex].y() + dy); + } else if (mGeometries[secondLastIndex] == ShapeAnnotation::VerticalLine) { + mPoints[secondLastIndex] = QPointF(mPoints[secondLastIndex].x() + dx, mPoints[secondLastIndex].y()); } - break; - default: - break; + updateCornerItem(secondLastIndex); + } + } else { + mPoints.back() = point; } } void LineAnnotation::moveAllPoints(qreal offsetX, qreal offsetY) { - for(int i = 0 ; i < mPoints.size() ; i++) - { + for(int i = 0 ; i < mPoints.size() ; i++) { mPoints[i] = QPointF(mPoints[i].x()+offsetX, mPoints[i].y()+offsetY); - /* updated the corresponding the CornerItem */ - if (mCornerItemsList.size() > i) - { - mCornerItemsList[i]->setPos(mPoints[i]); - } + /* updated the corresponding CornerItem */ + updateCornerItem(i); } } @@ -585,44 +537,30 @@ QString LineAnnotation::getEndComponentName() void LineAnnotation::handleComponentMoved() { - if (mPoints.size() < 2) - { + if (mPoints.size() < 2) { return; } // if both the components are moved then move the whole connection - if (mpStartComponent && mpEndComponent) - { - if (mpStartComponent->getRootParentComponent()->isSelected() && mpEndComponent->getRootParentComponent()->isSelected()) - { + if (mpStartComponent && mpEndComponent) { + if (mpStartComponent->getRootParentComponent()->isSelected() && mpEndComponent->getRootParentComponent()->isSelected()) { moveAllPoints(mpStartComponent->mapToScene(mpStartComponent->boundingRect().center()).x() - mPoints[0].x(), mpStartComponent->mapToScene(mpStartComponent->boundingRect().center()).y() - mPoints[0].y()); - } - else - { + } else { Component *pComponent = qobject_cast(sender()); - if (pComponent == mpStartComponent->getRootParentComponent()) - { + if (pComponent == mpStartComponent->getRootParentComponent()) { updateStartPoint(mpStartComponent->mapToScene(mpStartComponent->boundingRect().center())); - } - else if (pComponent == mpEndComponent->getRootParentComponent()) - { + } else if (pComponent == mpEndComponent->getRootParentComponent()) { updateEndPoint(mpEndComponent->mapToScene(mpEndComponent->boundingRect().center())); } } - } - else if (mpStartComponent) - { + } else if (mpStartComponent) { Component *pComponent = qobject_cast(sender()); - if (pComponent == mpStartComponent->getRootParentComponent()) - { + if (pComponent == mpStartComponent->getRootParentComponent()) { updateStartPoint(mpStartComponent->mapToScene(mpStartComponent->boundingRect().center())); } - } - else if (mpEndComponent) - { + } else if (mpEndComponent) { Component *pComponent = qobject_cast(sender()); - if (pComponent == mpEndComponent->getRootParentComponent()) - { + if (pComponent == mpEndComponent->getRootParentComponent()) { updateEndPoint(mpEndComponent->mapToScene(mpEndComponent->boundingRect().center())); } } @@ -631,23 +569,18 @@ void LineAnnotation::handleComponentMoved() void LineAnnotation::handleComponentRotation() { - if (mPoints.size() < 2) - { + if (mPoints.size() < 2) { return; } - if (mpStartComponent) - { + if (mpStartComponent) { Component *pComponent = qobject_cast(sender()); - if (pComponent == mpStartComponent->getRootParentComponent()) - { + if (pComponent == mpStartComponent->getRootParentComponent()) { updateStartPoint(mpStartComponent->mapToScene(mpStartComponent->boundingRect().center())); } } - if (mpEndComponent) - { + if (mpEndComponent) { Component *pComponent = qobject_cast(sender()); - if (pComponent == mpEndComponent->getRootParentComponent()) - { + if (pComponent == mpEndComponent->getRootParentComponent()) { updateEndPoint(mpEndComponent->mapToScene(mpEndComponent->boundingRect().center())); } } @@ -681,8 +614,10 @@ void LineAnnotation::duplicate() pLineAnnotation->setEndArrow(getEndArrow()); pLineAnnotation->setArrowSize(getArrowSize()); pLineAnnotation->setSmooth(getSmooth()); - pLineAnnotation->setPoints(getPoints()); - pLineAnnotation->addPoint(QPoint(0, 0)); + QList points = getPoints(); + for (int i = 0 ; i < points.size() ; i++) { + pLineAnnotation->addPoint(points[i]); + } pLineAnnotation->drawCornerItems(); pLineAnnotation->setCornerItemsPassive(); pLineAnnotation->update(); @@ -698,8 +633,7 @@ void LineAnnotation::duplicate() */ void LineAnnotation::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if ((mLineType == LineAnnotation::ConnectionType || mLineType == LineAnnotation::ShapeType) && mpGraphicsView) - { + if ((mLineType == LineAnnotation::ConnectionType || mLineType == LineAnnotation::ShapeType) && mpGraphicsView) { if (mpGraphicsView->isCreatingConnection()) { setFlag(QGraphicsItem::ItemIsSelectable, false); setFlag(QGraphicsItem::ItemIsMovable, false); @@ -708,9 +642,8 @@ void LineAnnotation::mousePressEvent(QGraphicsSceneMouseEvent *event) /* Only set the ItemIsMovable flag on Line if the class is not a system library class AND Line is not an inherited Line AND Line type is not ConnectionType. */ - if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && - !isInheritedShape() && - mLineType != LineAnnotation::ConnectionType) { + bool isSystemLibrary = mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary(); + if (!isSystemLibrary && !isInheritedShape() && mLineType != LineAnnotation::ConnectionType) { setFlag(QGraphicsItem::ItemIsMovable, true); } } @@ -735,36 +668,26 @@ ConnectionArray::ConnectionArray(GraphicsView *pGraphicsView, LineAnnotation *pC // Description text QString indexString = tr("[index]"); QString startComponentDescription; - if (pConnectionLineAnnotation->getStartComponent()->getParentComponent()) - { + if (pConnectionLineAnnotation->getStartComponent()->getParentComponent()) { startComponentDescription = QString("").append(pConnectionLineAnnotation->getStartComponent()->getParentComponent()->getName()) .append(".").append(pConnectionLineAnnotation->getStartComponent()->getName()).append(""); - } - else - { + } else { startComponentDescription = QString("").append(pConnectionLineAnnotation->getStartComponent()->getName()).append(""); } - if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()) - { - if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()->isArray()) - { + if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()) { + if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()->isArray()) { startComponentDescription.append(indexString); } } QString endComponentDescription; - if (pConnectionLineAnnotation->getEndComponent()->getParentComponent()) - { + if (pConnectionLineAnnotation->getEndComponent()->getParentComponent()) { endComponentDescription = QString("").append(pConnectionLineAnnotation->getEndComponent()->getParentComponent()->getName()) .append(".").append(pConnectionLineAnnotation->getEndComponent()->getName()).append(""); - } - else - { + } else { endComponentDescription = QString("").append(pConnectionLineAnnotation->getEndComponent()->getName()).append(""); } - if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()) - { - if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()->isArray()) - { + if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()) { + if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()->isArray()) { endComponentDescription.append(indexString); } } @@ -772,25 +695,19 @@ ConnectionArray::ConnectionArray(GraphicsView *pGraphicsView, LineAnnotation *pC // start component QIntValidator *pIntValidator = new QIntValidator(this); pIntValidator->setBottom(0); - if (pConnectionLineAnnotation->getStartComponent()->getParentComponent()) - { + if (pConnectionLineAnnotation->getStartComponent()->getParentComponent()) { mpStartComponentLabel = new Label(tr("Enter index value for ").append(pConnectionLineAnnotation->getStartComponent()->getParentComponent()->getName()) .append(".").append(pConnectionLineAnnotation->getStartComponent()->getName()).append("")); - } - else - { + } else { mpStartComponentLabel = new Label(tr("Enter index value for ").append(pConnectionLineAnnotation->getStartComponent()->getName()).append("")); } mpStartComponentTextBox = new QLineEdit; mpStartComponentTextBox->setValidator(pIntValidator); // end component - if (pConnectionLineAnnotation->getEndComponent()->getParentComponent()) - { + if (pConnectionLineAnnotation->getEndComponent()->getParentComponent()) { mpEndComponentLabel = new Label(tr("Enter index value for ").append(pConnectionLineAnnotation->getEndComponent()->getParentComponent()->getName()) .append(".").append(pConnectionLineAnnotation->getEndComponent()->getName()).append("")); - } - else - { + } else { mpEndComponentLabel = new Label(tr("Enter index value for ").append(pConnectionLineAnnotation->getEndComponent()->getName()).append("")); } mpEndComponentTextBox = new QLineEdit; @@ -812,19 +729,15 @@ ConnectionArray::ConnectionArray(GraphicsView *pGraphicsView, LineAnnotation *pC mainLayout->addWidget(mpHorizontalLine, 1, 0); mainLayout->addWidget(mpDescriptionLabel, 2, 0); int i = 3; - if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()) - { - if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()->isArray()) - { + if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()) { + if (pConnectionLineAnnotation->getStartComponent()->getComponentInfo()->isArray()) { mainLayout->addWidget(mpStartComponentLabel, i, 0); mainLayout->addWidget(mpStartComponentTextBox, i+1, 0); i = i + 2; } } - if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()) - { - if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()->isArray()) - { + if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()) { + if (pConnectionLineAnnotation->getEndComponent()->getComponentInfo()->isArray()) { mainLayout->addWidget(mpEndComponentLabel, i, 0); mainLayout->addWidget(mpEndComponentTextBox, i+1, 0); i = i + 2; @@ -838,22 +751,16 @@ void ConnectionArray::saveArrayIndex() { QString startComponentName, endComponentName; // set start component name - if (mpConnectionLineAnnotation->getStartComponent()->getParentComponent()) - { + if (mpConnectionLineAnnotation->getStartComponent()->getParentComponent()) { startComponentName = QString(mpConnectionLineAnnotation->getStartComponent()->getParentComponent()->getName()).append(".") .append(mpConnectionLineAnnotation->getStartComponent()->getName()); - } - else - { + } else { startComponentName = mpConnectionLineAnnotation->getStartComponent()->getName(); } // set the start component name if start component is an array - if (mpConnectionLineAnnotation->getStartComponent()->getComponentInfo()) - { - if (mpConnectionLineAnnotation->getStartComponent()->getComponentInfo()->isArray()) - { - if (mpStartComponentTextBox->text().isEmpty()) - { + if (mpConnectionLineAnnotation->getStartComponent()->getComponentInfo()) { + if (mpConnectionLineAnnotation->getStartComponent()->getComponentInfo()->isArray()) { + if (mpStartComponentTextBox->text().isEmpty()) { QMessageBox::critical(mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow(), QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(GUIMessages::ENTER_VALID_INTEGER).arg(startComponentName), Helper::ok); @@ -863,22 +770,16 @@ void ConnectionArray::saveArrayIndex() } } // set end component name - if (mpConnectionLineAnnotation->getEndComponent()->getParentComponent()) - { + if (mpConnectionLineAnnotation->getEndComponent()->getParentComponent()) { endComponentName = QString(mpConnectionLineAnnotation->getEndComponent()->getParentComponent()->getName()).append(".") .append(mpConnectionLineAnnotation->getEndComponent()->getName()); - } - else - { + } else { endComponentName = mpConnectionLineAnnotation->getEndComponent()->getName(); } // set the end component name if end component is an array - if (mpConnectionLineAnnotation->getEndComponent()->getComponentInfo()) - { - if (mpConnectionLineAnnotation->getEndComponent()->getComponentInfo()->isArray()) - { - if (mpEndComponentTextBox->text().isEmpty()) - { + if (mpConnectionLineAnnotation->getEndComponent()->getComponentInfo()) { + if (mpConnectionLineAnnotation->getEndComponent()->getComponentInfo()->isArray()) { + if (mpEndComponentTextBox->text().isEmpty()) { QMessageBox::critical(mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow(), QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(GUIMessages::ENTER_VALID_INTEGER).arg(endComponentName), Helper::ok); diff --git a/OMEdit/OMEditGUI/Annotations/LineAnnotation.h b/OMEdit/OMEditGUI/Annotations/LineAnnotation.h index 885f9bfa97a..e87bd941e23 100644 --- a/OMEdit/OMEditGUI/Annotations/LineAnnotation.h +++ b/OMEdit/OMEditGUI/Annotations/LineAnnotation.h @@ -48,7 +48,6 @@ class LineAnnotation : public ShapeAnnotation { Q_OBJECT public: - enum GeometryType {Vertical, Horizontal, Diagonal}; enum LineType { ComponentType, /* Line is within Component. */ ConnectionType, /* Line is a connection. */ @@ -71,6 +70,8 @@ class LineAnnotation : public ShapeAnnotation void setEndComponent(Component *pEndComponent); Component* getEndComponent(); void addPoint(QPointF point); + void removePoint(int index); + void clearPoints(); void updateStartPoint(QPointF point); void updateEndPoint(QPointF point); void moveAllPoints(qreal offsetX, qreal offsetY); @@ -85,7 +86,6 @@ class LineAnnotation : public ShapeAnnotation QString mStartComponentName; Component *mpEndComponent; QString mEndComponentName; - QList mGeometries; public slots: void handleComponentMoved(); void handleComponentRotation(); diff --git a/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.cpp b/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.cpp index 80fe1f9164c..0ab1c6da455 100644 --- a/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.cpp +++ b/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.cpp @@ -217,6 +217,18 @@ void PolygonAnnotation::addPoint(QPointF point) mPoints.back() = mPoints.first(); } +void PolygonAnnotation::removePoint(int index) +{ + if (mPoints.size() > index) { + mPoints.removeAt(index); + } +} + +void PolygonAnnotation::clearPoints() +{ + mPoints.clear(); +} + void PolygonAnnotation::updateEndPoint(QPointF point) { // we update the second last point for polygon since the last point is connected to first one @@ -237,8 +249,7 @@ void PolygonAnnotation::duplicate() pPolygonAnnotation->setFillPattern(getFillPattern()); pPolygonAnnotation->setLineThickness(getLineThickness()); pPolygonAnnotation->setSmooth(getSmooth()); - pPolygonAnnotation->setPoints(getPoints()); - pPolygonAnnotation->addPoint(QPoint(0, 0)); + pPolygonAnnotation->mPoints = getPoints(); pPolygonAnnotation->drawCornerItems(); pPolygonAnnotation->setCornerItemsPassive(); pPolygonAnnotation->update(); diff --git a/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.h b/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.h index 12f25361643..d9d0457861f 100644 --- a/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.h +++ b/OMEdit/OMEditGUI/Annotations/PolygonAnnotation.h @@ -58,6 +58,8 @@ class PolygonAnnotation : public ShapeAnnotation void drawPolygonAnnotaion(QPainter *painter); QString getShapeAnnotation(); void addPoint(QPointF point); + void removePoint(int index); + void clearPoints(); void updateEndPoint(QPointF point); public slots: void duplicate(); diff --git a/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp b/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp index 509a317e6ff..2a4892f98fb 100644 --- a/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp +++ b/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp @@ -580,32 +580,23 @@ void ShapeAnnotation::initializeTransformation() */ void ShapeAnnotation::drawCornerItems() { - if (dynamic_cast(this) || dynamic_cast(this)) - { + if (dynamic_cast(this) || dynamic_cast(this)) { LineAnnotation *pLineAnnotation = dynamic_cast(this); LineAnnotation::LineType lineType = LineAnnotation::ShapeType; - if (pLineAnnotation) - { + if (pLineAnnotation) { lineType = pLineAnnotation->getLineType(); } - // remove the last point since mouse double click event has added an extra point to it..... - mPoints.removeLast(); - for (int i = 0 ; i < mPoints.size() ; i++) - { + for (int i = 0 ; i < mPoints.size() ; i++) { QPointF point = mPoints.at(i); CornerItem *pCornerItem = new CornerItem(point.x(), point.y(), i, this); /* if line is a connection then make the first and last point non moveable. */ - if ((lineType == LineAnnotation::ConnectionType) && (i == 0 || i == mPoints.size() - 1)) - { + if ((lineType == LineAnnotation::ConnectionType) && (i == 0 || i == mPoints.size() - 1)) { pCornerItem->setFlag(QGraphicsItem::ItemIsMovable, false); } mCornerItemsList.append(pCornerItem); } - } - else - { - for (int i = 0 ; i < mExtents.size() ; i++) - { + } else { + for (int i = 0 ; i < mExtents.size() ; i++) { QPointF extent = mExtents.at(i); CornerItem *pCornerItem = new CornerItem(extent.x(), extent.y(), i, this); mCornerItemsList.append(pCornerItem); @@ -665,6 +656,16 @@ QPointF ShapeAnnotation::getOldPosition() return mOldPosition; } +void ShapeAnnotation::addPoint(QPointF point) +{ + Q_UNUSED(point); +} + +void ShapeAnnotation::clearPoints() +{ + +} + /*! Adds the extent point value. \param index - the index of extent point. @@ -708,15 +709,6 @@ Transformation* ShapeAnnotation::getTransformation() return mpTransformation; } -/*! - Sets the points list. - \param points - the points list. - */ -void ShapeAnnotation::setPoints(QList points) -{ - mPoints = points; -} - /*! Returns the points list. \return the points list. @@ -1145,11 +1137,9 @@ void ShapeAnnotation::applyRotation(qreal angle) void ShapeAnnotation::adjustPointsWithOrigin() { QList points; - foreach (QPointF point, mPoints) - { - point.setX(point.x() - mOrigin.x()); - point.setY(point.y() - mOrigin.y()); - points.append(point); + foreach (QPointF point, mPoints) { + QPointF adjustedPoint = mpGraphicsView->snapPointToGrid(point - mOrigin); + points.append(adjustedPoint); } mPoints = points; } @@ -1169,6 +1159,54 @@ void ShapeAnnotation::adjustExtentsWithOrigin() mExtents = extents; } +CornerItem* ShapeAnnotation::getCornerItem(int index) +{ + for (int i = 0 ; i < mCornerItemsList.size() ; i++) { + if (mCornerItemsList[i]->getConnectetPointIndex() == index) { + return mCornerItemsList[i]; + } + } + return 0; +} + +void ShapeAnnotation::updateCornerItem(int index) +{ + CornerItem *pCornerItem = getCornerItem(index); + if (pCornerItem) { + bool state = pCornerItem->blockSignals(true); + pCornerItem->setPos(mPoints.at(index)); + pCornerItem->blockSignals(state); + } +} + +void ShapeAnnotation::insertPointsGeometriesAndCornerItems(int index) +{ + QPointF point = (mPoints[index - 1] + mPoints[index]) / 2; + point = mpGraphicsView->snapPointToGrid(point); + mPoints.insert(index, point); + mPoints.insert(index, point); + if (mGeometries[index - 1] == ShapeAnnotation::HorizontalLine) { + mGeometries.insert(index, ShapeAnnotation::HorizontalLine); + mGeometries.insert(index, ShapeAnnotation::VerticalLine); + } else if (mGeometries[index - 1] == ShapeAnnotation::VerticalLine) { + mGeometries.insert(index, ShapeAnnotation::VerticalLine); + mGeometries.insert(index, ShapeAnnotation::HorizontalLine); + } + // if we add new points then we need to add new CornerItems and also need to adjust CornerItems connected indexes. + mCornerItemsList.insert(index, new CornerItem(point.x(), point.y(), index, this)); + mCornerItemsList.insert(index, new CornerItem(point.x(), point.y(), index, this)); + adjustCornerItemsConnectedIndexes(); +} + +void ShapeAnnotation::adjustCornerItemsConnectedIndexes() +{ + for (int i = 0 ; i < mPoints.size() ; i++) { + if (i < mCornerItemsList.size()) { + mCornerItemsList[i]->setConnectedPointIndex(i); + } + } +} + /*! Slot activated when Delete option is choosen from context menu of the shape.\n Deletes the connection. @@ -1551,35 +1589,81 @@ void ShapeAnnotation::cornerItemReleased() /*! Slot activated when CornerItem around the shape is moved. Sends the new position values for the associated shape point. + \param index - the index of the CornerItem + \param point - the new CornerItem position */ void ShapeAnnotation::updateCornerItemPoint(int index, QPointF point) { - if (dynamic_cast(this) || dynamic_cast(this)) - { - mPoints.replace(index, point); - /* if shape is the PolygonAnnotation then update the start and end point together */ - if (dynamic_cast(this)) - { - /* if first point */ - if (index == 0) - { - mPoints.back() = point; - mCornerItemsList[mPoints.size() - 1]->setPos(point); + if (dynamic_cast(this)) { + LineAnnotation *pLineAnnotation = dynamic_cast(this); + if (pLineAnnotation->getLineType() == LineAnnotation::ConnectionType) { + // if moving the 2nd last point then we need to add more points after it to keep the last point manhattanized with connector + int secondLastIndex = mPoints.size() - 2; + if (index == secondLastIndex) { + // just check if additional points are really needed or not. + if ((mGeometries[secondLastIndex] == ShapeAnnotation::HorizontalLine && mPoints[index].y() != point.y()) || + (mGeometries[secondLastIndex] == ShapeAnnotation::VerticalLine && mPoints[index].x() != point.x())) { + insertPointsGeometriesAndCornerItems(mPoints.size() - 1); + } } - /* if last point */ - else if (index == mPoints.size() - 1) - { - mPoints.first() = point; - mCornerItemsList[0]->setPos(point); + // if moving the 2nd point then we need to add more points behind it to keep the first point manhattanized with connector + if (index == 1) { + // just check if additional points are really needed or not. + if ((mGeometries[0] == ShapeAnnotation::HorizontalLine && mPoints[index].y() != point.y()) || + (mGeometries[0] == ShapeAnnotation::VerticalLine && mPoints[index].x() != point.x())) { + insertPointsGeometriesAndCornerItems(1); + index = index + 2; + } + } + qreal dx = point.x() - mPoints[index].x(); + qreal dy = point.y() - mPoints[index].y(); + mPoints.replace(index, point); + // update previous point + if (mGeometries[index - 1] == ShapeAnnotation::HorizontalLine) { + mPoints[index - 1] = QPointF(mPoints[index - 1].x(), mPoints[index - 1].y() + dy); + updateCornerItem(index - 1); + } else if (mGeometries[index - 1] == ShapeAnnotation::VerticalLine) { + mPoints[index - 1] = QPointF(mPoints[index - 1].x() + dx, mPoints[index - 1].y()); + updateCornerItem(index - 1); } + // update next point + if (mGeometries[index] == ShapeAnnotation::HorizontalLine) { + mPoints[index + 1] = QPointF(mPoints[index + 1].x(), mPoints[index + 1].y() + dy); + updateCornerItem(index + 1); + } else if (mGeometries[index] == ShapeAnnotation::VerticalLine) { + mPoints[index + 1] = QPointF(mPoints[index + 1].x() + dx, mPoints[index + 1].y()); + updateCornerItem(index + 1); + } + } else { + mPoints.replace(index, point); } - } - else - { + } else if (dynamic_cast(this)) { /* if shape is the PolygonAnnotation then update the start and end point together */ + mPoints.replace(index, point); + /* if first point */ + if (index == 0) { + mPoints.back() = point; + updateCornerItem(mPoints.size() - 1); + } else if (index == mPoints.size() - 1) { /* if last point */ + mPoints.first() = point; + updateCornerItem(0); + } + } else { mExtents.replace(index, point); } } +ShapeAnnotation::LineGeometryType ShapeAnnotation::findLineGeometryType(QPointF point1, QPointF point2) +{ + QLineF line(point1, point2); + qreal angle = StringHandler::getNormalizedAngle(line.angle()); + + if ((angle > 45 && angle < 135) || (angle > 225 && angle < 315)) { + return ShapeAnnotation::VerticalLine; + } else { + return ShapeAnnotation::HorizontalLine; + } +} + /*! Slot activated when Properties option is choosen from context menu of the shape. */ @@ -1588,7 +1672,7 @@ void ShapeAnnotation::showShapeProperties() if (!mpGraphicsView) return; MainWindow *pMainWindow = mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow(); ShapePropertiesDialog *pShapePropertiesDialog = new ShapePropertiesDialog(this, pMainWindow); - pShapePropertiesDialog->show(); + pShapePropertiesDialog->exec(); } /*! diff --git a/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h b/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h index 7076bfda87c..4dc30daff6a 100644 --- a/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h +++ b/OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h @@ -110,6 +110,7 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem bool mIsCornerItemClicked; QAction *mpShapePropertiesAction; public: + enum LineGeometryType {VerticalLine, HorizontalLine}; ShapeAnnotation(QGraphicsItem *pParent); ShapeAnnotation(bool inheritedShape, GraphicsView *pGraphicsView, QGraphicsItem *pParent = 0); ~ShapeAnnotation(); @@ -129,11 +130,12 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem void removeCornerItems(); void setOldPosition(QPointF oldPosition); QPointF getOldPosition(); + virtual void addPoint(QPointF point); + virtual void clearPoints(); virtual void replaceExtent(int index, QPointF point); virtual void updateEndExtent(QPointF point); GraphicsView* getGraphicsView(); Transformation* getTransformation(); - void setPoints(QList points); QList getPoints(); void setStartArrow(StringHandler::Arrow startArrow); StringHandler::Arrow getStartArrow(); @@ -174,6 +176,10 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem void applyRotation(qreal angle); void adjustPointsWithOrigin(); void adjustExtentsWithOrigin(); + CornerItem* getCornerItem(int index); + void updateCornerItem(int index); + void insertPointsGeometriesAndCornerItems(int index); + void adjustCornerItemsConnectedIndexes(); signals: void updateClassAnnotation(); public slots: @@ -199,11 +205,13 @@ public slots: void cornerItemPressed(); void cornerItemReleased(); void updateCornerItemPoint(int index, QPointF point); + LineGeometryType findLineGeometryType(QPointF point1, QPointF point2); void showShapeProperties(); protected: GraphicsView *mpGraphicsView; Transformation *mpTransformation; QList mPoints; + QList mGeometries; QList mArrow; qreal mArrowSize; StringHandler::Smooth mSmooth; diff --git a/OMEdit/OMEditGUI/Annotations/ShapePropertiesDialog.cpp b/OMEdit/OMEditGUI/Annotations/ShapePropertiesDialog.cpp index adc266614bf..24976c1e066 100644 --- a/OMEdit/OMEditGUI/Annotations/ShapePropertiesDialog.cpp +++ b/OMEdit/OMEditGUI/Annotations/ShapePropertiesDialog.cpp @@ -53,7 +53,6 @@ ShapePropertiesDialog::ShapePropertiesDialog(ShapeAnnotation *pShapeAnnotation, QString title = getTitle(); setWindowTitle(QString(Helper::applicationName).append(" - ").append(title).append(" ").append(Helper::properties)); setAttribute(Qt::WA_DeleteOnClose); - setModal(true); // heading label mpShapePropertiesHeading = new Label(QString(title).append(" ").append(Helper::properties)); mpShapePropertiesHeading->setFont(QFont(Helper::systemFontInfo.family(), Helper::headingFontSize)); @@ -779,10 +778,10 @@ bool ShapePropertiesDialog::applyShapeProperties() mpShapeAnnotation->setFillPattern(StringHandler::getFillPatternType(mpFillPatternComboBox->currentText())); } /* save points */ - QList points; - for (int i = 0 ; i < mpPointsTableWidget->rowCount() ; i++) - points.append(QPointF(mpPointsTableWidget->item(i, 0)->text().toFloat(), mpPointsTableWidget->item(i, 1)->text().toFloat())); - mpShapeAnnotation->setPoints(points); + mpShapeAnnotation->clearPoints(); + for (int i = 0 ; i < mpPointsTableWidget->rowCount() ; i++) { + mpShapeAnnotation->addPoint(QPointF(mpPointsTableWidget->item(i, 0)->text().toFloat(), mpPointsTableWidget->item(i, 1)->text().toFloat())); + } /* save bitmap file name and image source */ if (mpBitmapAnnotation) { @@ -833,7 +832,6 @@ bool ShapePropertiesDialog::applyShapeProperties() if (mpLineAnnotation && lineType == LineAnnotation::ConnectionType) { mpShapeAnnotation->removeCornerItems(); - mpLineAnnotation->addPoint(QPointF(0, 0)); mpShapeAnnotation->drawCornerItems(); mpLineAnnotation->updateConnectionAnnotation(); mpLineAnnotation->update(); @@ -842,10 +840,6 @@ bool ShapePropertiesDialog::applyShapeProperties() { mpShapeAnnotation->initializeTransformation(); mpShapeAnnotation->removeCornerItems(); - if (mpLineAnnotation) - mpLineAnnotation->addPoint(QPointF(0, 0)); - else if (mpPolygonAnnotation) - mpPolygonAnnotation->addPoint(QPointF(0, 0)); mpShapeAnnotation->drawCornerItems(); mpShapeAnnotation->update(); mpShapeAnnotation->getGraphicsView()->addClassAnnotation(); diff --git a/OMEdit/OMEditGUI/Component/Component.cpp b/OMEdit/OMEditGUI/Component/Component.cpp index 831f9874bbb..512f6c24c82 100644 --- a/OMEdit/OMEditGUI/Component/Component.cpp +++ b/OMEdit/OMEditGUI/Component/Component.cpp @@ -1064,23 +1064,16 @@ void Component::duplicate() { QPointF gridStep(mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep(), mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep()); - if (mpGraphicsView->addComponent(mClassName, scenePos() + gridStep)) - { - if (mType == StringHandler::Connector) - { - if (mpGraphicsView->getViewType() == StringHandler::Diagram) - { + if (mpGraphicsView->addComponent(mClassName, scenePos() + gridStep)) { + if (mType == StringHandler::Connector) { + if (mpGraphicsView->getViewType() == StringHandler::Diagram) { duplicateHelper(mpGraphicsView); duplicateHelper(mpGraphicsView->getModelWidget()->getIconGraphicsView()); - } - else - { + } else { duplicateHelper(mpGraphicsView); duplicateHelper(mpGraphicsView->getModelWidget()->getDiagramGraphicsView()); } - } - else - { + } else { duplicateHelper(mpGraphicsView); } } diff --git a/OMEdit/OMEditGUI/Component/CornerItem.cpp b/OMEdit/OMEditGUI/Component/CornerItem.cpp index 4cedd5eac75..6e9fc978fa0 100644 --- a/OMEdit/OMEditGUI/Component/CornerItem.cpp +++ b/OMEdit/OMEditGUI/Component/CornerItem.cpp @@ -114,9 +114,10 @@ void CornerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option */ void CornerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (event->button() == Qt::LeftButton) - { - emit cornerItemPress(); + if (event->button() == Qt::LeftButton) { + if (!signalsBlocked()) { + emit cornerItemPress(); + } mClickPos = mapToScene(event->pos()); } QGraphicsItem::mousePressEvent(event); @@ -132,12 +133,14 @@ void CornerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) void CornerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { QGraphicsItem::mouseReleaseEvent(event); - if (event->button() == Qt::LeftButton) - { - emit cornerItemRelease(); - if (mClickPos != mapToScene(event->pos())) - { - emit cornerItemPositionChanged(); + if (event->button() == Qt::LeftButton) { + if (!signalsBlocked()) { + emit cornerItemRelease(); + } + if (mClickPos != mapToScene(event->pos())) { + if (!signalsBlocked()) { + emit cornerItemPositionChanged(); + } mpShapeAnnotation->getGraphicsView()->setCanAddClassAnnotation(true); } } @@ -153,7 +156,9 @@ QVariant CornerItem::itemChange(GraphicsItemChange change, const QVariant &value { QGraphicsItem::itemChange(change, value); if (change == QGraphicsItem::ItemPositionHasChanged) { - emit cornerItemMoved(mConnectedPointIndex, value.toPointF()); + if (!signalsBlocked()) { + emit cornerItemMoved(mConnectedPointIndex, value.toPointF()); + } } else if (change == QGraphicsItem::ItemPositionChange && scene()) { // snap to grid while dragging shapes QPointF newPos = value.toPointF(); diff --git a/OMEdit/OMEditGUI/Component/CornerItem.h b/OMEdit/OMEditGUI/Component/CornerItem.h index f5f73d45ff6..d73df6b558f 100644 --- a/OMEdit/OMEditGUI/Component/CornerItem.h +++ b/OMEdit/OMEditGUI/Component/CornerItem.h @@ -56,6 +56,8 @@ class CornerItem : public QObject, public QGraphicsItem int mConnectedPointIndex; public: CornerItem(qreal x, qreal y, int connectedPointIndex, ShapeAnnotation *pParent); + void setConnectedPointIndex(int connectedPointIndex) {mConnectedPointIndex = connectedPointIndex;} + int getConnectetPointIndex() {return mConnectedPointIndex;} QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); signals: diff --git a/OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp b/OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp index 049c4b23a4e..9f271f57583 100644 --- a/OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp +++ b/OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp @@ -952,7 +952,6 @@ void GraphicsView::addConnection(Component *pComponent) else endComponentName = pComponent->getName(); createConnection(startComponentName, endComponentName); - mpConnectionLineAnnotation->addPoint(QPointF(0, 0)); mpConnectionLineAnnotation->drawCornerItems(); mpConnectionLineAnnotation->setCornerItemsPassive(); } @@ -1425,16 +1424,17 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event) void GraphicsView::mouseDoubleClickEvent(QMouseEvent *event) { - if (event->button() == Qt::RightButton) + if (event->button() == Qt::RightButton) { return; + } MainWindow *pMainWindow = mpModelWidget->getModelWidgetContainer()->getMainWindow(); - if (isCreatingLineShape()) - { + if (isCreatingLineShape()) { // set the transformation matrix - mpLineShapeAnnotation->setOrigin(mpLineShapeAnnotation->sceneBoundingRect().center()); + mpLineShapeAnnotation->setOrigin(snapPointToGrid(mpLineShapeAnnotation->sceneBoundingRect().center())); mpLineShapeAnnotation->adjustPointsWithOrigin(); mpLineShapeAnnotation->initializeTransformation(); // draw corner items for the Line shape + mpLineShapeAnnotation->removePoint(mpLineShapeAnnotation->getPoints().size() - 1); mpLineShapeAnnotation->drawCornerItems(); mpLineShapeAnnotation->setSelected(true); // finish creating the line @@ -1445,14 +1445,13 @@ void GraphicsView::mouseDoubleClickEvent(QMouseEvent *event) addClassAnnotation(); setCanAddClassAnnotation(true); return; - } - else if (isCreatingPolygonShape()) - { + } else if (isCreatingPolygonShape()) { // set the transformation matrix - mpPolygonShapeAnnotation->setOrigin(mpPolygonShapeAnnotation->sceneBoundingRect().center()); + mpPolygonShapeAnnotation->setOrigin(snapPointToGrid(mpPolygonShapeAnnotation->sceneBoundingRect().center())); mpPolygonShapeAnnotation->adjustPointsWithOrigin(); mpPolygonShapeAnnotation->initializeTransformation(); // draw corner items for the polygon shape + mpPolygonShapeAnnotation->removePoint(mpPolygonShapeAnnotation->getPoints().size() - 1); mpPolygonShapeAnnotation->drawCornerItems(); mpPolygonShapeAnnotation->setSelected(true); // finish creating the polygon @@ -1465,14 +1464,12 @@ void GraphicsView::mouseDoubleClickEvent(QMouseEvent *event) return; } ShapeAnnotation *pShapeAnnotation = dynamic_cast(itemAt(event->pos())); - if (pShapeAnnotation) - { + if (pShapeAnnotation) { /* - Double click on Component also end up here. - But we don't have GraphicsView for the shapes inside the Component so we can go out of this block. - */ - if (pShapeAnnotation->getGraphicsView()) - { + Double click on Component also end up here. + But we don't have GraphicsView for the shapes inside the Component so we can go out of this block. + */ + if (pShapeAnnotation->getGraphicsView()) { pShapeAnnotation->showShapeProperties(); return; } @@ -2361,13 +2358,7 @@ void ModelWidget::getModelIconDiagramShapes(QString className, QString annotatio shape = shape.mid(QString("Line").length()); shape = StringHandler::removeFirstLastBrackets(shape); LineAnnotation *pLineAnnotation = new LineAnnotation(shape, inheritedCycle, pGraphicsView); - /* - before drawing the corner items add one point to line since drawcorneritems - deletes one point. Why? because we end the line shape with double click which adds an extra - point to it. so we need to delete this point. - */ pLineAnnotation->initializeTransformation(); - pLineAnnotation->addPoint(QPoint(0, 0)); pLineAnnotation->drawCornerItems(); pLineAnnotation->setCornerItemsPassive(); } @@ -2376,13 +2367,7 @@ void ModelWidget::getModelIconDiagramShapes(QString className, QString annotatio shape = shape.mid(QString("Polygon").length()); shape = StringHandler::removeFirstLastBrackets(shape); PolygonAnnotation *pPolygonAnnotation = new PolygonAnnotation(shape, inheritedCycle, pGraphicsView); - /* - before drawing the corner items add one point to polygon since drawcorneritems - deletes one point. Why? because we end the polygon shape with double click which adds an extra - point to it. so we need to delete this point. - */ pPolygonAnnotation->initializeTransformation(); - pPolygonAnnotation->addPoint(QPoint(0, 0)); pPolygonAnnotation->drawCornerItems(); pPolygonAnnotation->setCornerItemsPassive(); } @@ -2535,13 +2520,14 @@ void ModelWidget::getModelConnections(QString className, bool inheritedCycle) shape = StringHandler::removeFirstLastBrackets(shape); LineAnnotation *pConnectionLineAnnotation = new LineAnnotation(shape, inheritedCycle, pStartConnectorComponent, pEndConnectorComponent, mpDiagramGraphicsView); - if (pStartConnectorComponent) + if (pStartConnectorComponent) { pStartConnectorComponent->getRootParentComponent()->addConnectionDetails(pConnectionLineAnnotation); + } pConnectionLineAnnotation->setStartComponentName(connectionList.at(0)); - if (pEndConnectorComponent) + if (pEndConnectorComponent) { pEndConnectorComponent->getRootParentComponent()->addConnectionDetails(pConnectionLineAnnotation); + } pConnectionLineAnnotation->setEndComponentName(connectionList.at(1)); - pConnectionLineAnnotation->addPoint(QPointF(0, 0)); pConnectionLineAnnotation->drawCornerItems(); pConnectionLineAnnotation->setCornerItemsPassive(); mpDiagramGraphicsView->addConnectionObject(pConnectionLineAnnotation);