Skip to content

Commit

Permalink
Improve the drawing of connection start and end points (#11185)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Sep 14, 2023
1 parent d358900 commit afb86a3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 82 deletions.
45 changes: 33 additions & 12 deletions OMEdit/OMEditLIB/Annotations/LineAnnotation.cpp
Expand Up @@ -661,33 +661,34 @@ void LineAnnotation::parseShapeAnnotation()

QPainterPath LineAnnotation::getShape() const
{
PointArrayAnnotation points = adjustPointsForDrawing();
QPainterPath path;
if (mPoints.size() > 0) {
if (points.size() > 0) {
// mPoints.size() is at least 1
path.moveTo(mPoints.at(0));
path.moveTo(points.at(0));
if (mSmooth) {
if (mPoints.size() == 2) {
if (points.size() == 2) {
// if points are only two then spline acts as simple line
path.lineTo(mPoints.at(1));
path.lineTo(points.at(1));
} else {
for (int i = 2 ; i < mPoints.size() ; i++) {
QPointF point3 = mPoints.at(i);
for (int i = 2 ; i < points.size() ; i++) {
QPointF point3 = points.at(i);
// calculate middle points for bezier curves
QPointF point2 = mPoints.at(i - 1);
QPointF point1 = mPoints.at(i - 2);
QPointF point2 = points.at(i - 1);
QPointF point1 = points.at(i - 2);
QPointF point12((point1.x() + point2.x())/2, (point1.y() + point2.y())/2);
QPointF point23((point2.x() + point3.x())/2, (point2.y() + point3.y())/2);
path.lineTo(point12);
path.cubicTo(point12, point2, point23);
// if its the last point
if (i == mPoints.size() - 1) {
if (i == points.size() - 1) {
path.lineTo(point3);
}
}
}
} else {
for (int i = 1 ; i < mPoints.size() ; i++) {
path.lineTo(mPoints.at(i));
for (int i = 1 ; i < points.size() ; i++) {
path.lineTo(points.at(i));
}
}
}
Expand Down Expand Up @@ -752,7 +753,7 @@ void LineAnnotation::drawAnnotation(QPainter *painter, bool scene)
applyLinePattern(painter);

QPainterPath path = getShape();
PointArrayAnnotation points = mPoints;
PointArrayAnnotation points = adjustPointsForDrawing();
if (scene) {
path = mapToScene(path);
for (int i = 0; i < points.size(); ++i) {
Expand Down Expand Up @@ -1410,6 +1411,26 @@ QColor LineAnnotation::findLineColorForConnection(Element *pComponent)
return lineColor;
}

/*!
* \brief LineAnnotation::adjustPointsForDrawing
* Adjusts the start and end points of the connection to the center of start and end connectors.
* This only updates the points for drawing and does not modify the actual values for Modelica code.
* \return
*/
PointArrayAnnotation LineAnnotation::adjustPointsForDrawing() const
{
PointArrayAnnotation points = mPoints;
if (isConnection()) {
if (mpStartElement && points.size() > 0) {
points.setPoint(0, mpStartElement->sceneBoundingRect().center());
}
if (mpEndElement && points.size() > 1) {
points.setPoint(points.size() - 1, mpEndElement->sceneBoundingRect().center());
}
}
return points;
}

QVariant LineAnnotation::itemChange(GraphicsItemChange change, const QVariant &value)
{
ShapeAnnotation::itemChange(change, value);
Expand Down
6 changes: 6 additions & 0 deletions OMEdit/OMEditLIB/Annotations/LineAnnotation.h
Expand Up @@ -107,6 +107,10 @@ class LineAnnotation : public ShapeAnnotation
ModelInstance::Line* getLine() {return mpLine;}
void setLineType(LineType lineType) {mLineType = lineType;}
LineType getLineType() {return mLineType;}
bool isConnection() const {return mLineType == LineAnnotation::ConnectionType;}
bool isTransition() const {return mLineType == LineAnnotation::TransitionType;}
bool isInitialState() const {return mLineType == LineAnnotation::InitialStateType;}
bool isLineShape() const {return mLineType == LineAnnotation::ShapeType;}
void setStartElement(Element *pStartElement) {mpStartElement = pStartElement;}
Element* getStartElement() {return mpStartElement;}
void setStartElementName(QString name) {mStartElementName = name;}
Expand Down Expand Up @@ -154,6 +158,8 @@ class LineAnnotation : public ShapeAnnotation
static QColor findLineColorForConnection(Element *pComponent);
private:
ModelInstance::Line *mpLine;

PointArrayAnnotation adjustPointsForDrawing() const;
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;

Expand Down
22 changes: 9 additions & 13 deletions OMEdit/OMEditLIB/Annotations/ShapeAnnotation.cpp
Expand Up @@ -687,7 +687,7 @@ void ShapeAnnotation::applyTransformation()
pGraphicsView = mpReferenceShapeAnnotation->getGraphicsView();
}

if (!mpParentComponent && pGraphicsView && !(pLineAnnotation && pLineAnnotation->getLineType() != LineAnnotation::ShapeType)
if (!mpParentComponent && pGraphicsView && (pLineAnnotation && pLineAnnotation->isLineShape())
&& ((mpReferenceShapeAnnotation && mpReferenceShapeAnnotation->getGraphicsView()) || (pGraphicsView->getModelWidget()->isNewApi() && mIsInheritedShape))) {
QList<QPointF> extendsCoOrdinateExtents = getExtentsForInheritedShapeFromIconDiagramMap(pGraphicsView, mpReferenceShapeAnnotation);
ExtentAnnotation extent = pGraphicsView->mMergedCoOrdinateSystem.getExtent();
Expand Down Expand Up @@ -1312,11 +1312,11 @@ void ShapeAnnotation::deleteMe()
{
// delete the shape
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
if (pLineAnnotation && pLineAnnotation->getLineType() == LineAnnotation::ConnectionType) {
if (pLineAnnotation && pLineAnnotation->isConnection()) {
mpGraphicsView->deleteConnection(pLineAnnotation);
} else if (pLineAnnotation && pLineAnnotation->getLineType() == LineAnnotation::TransitionType) {
} else if (pLineAnnotation && pLineAnnotation->isTransition()) {
mpGraphicsView->deleteTransition(pLineAnnotation);
} else if (pLineAnnotation && pLineAnnotation->getLineType() == LineAnnotation::InitialStateType) {
} else if (pLineAnnotation && pLineAnnotation->isInitialState()) {
mpGraphicsView->deleteInitialState(pLineAnnotation);
} else {
mpGraphicsView->deleteShape(this);
Expand Down Expand Up @@ -1562,13 +1562,13 @@ void ShapeAnnotation::cornerItemReleased(const bool changed)
return;
}
} else {
if (pLineAnnotation && pLineAnnotation->getLineType() == LineAnnotation::ConnectionType) {
if (pLineAnnotation && pLineAnnotation->isConnection()) {
manhattanizeShape(false);
removeRedundantPointsGeometriesAndCornerItems();
// Call getOMCShapeAnnotation() after manhattanizeShape() and removeRedundantPointsGeometriesAndCornerItems() to get a correct new annotation
QString newAnnotation = getOMCShapeAnnotation();
pModelWidget->getUndoStack()->push(new UpdateConnectionCommand(pLineAnnotation, mOldAnnotation, newAnnotation));
} else if (pLineAnnotation && pLineAnnotation->getLineType() == LineAnnotation::TransitionType) {
} else if (pLineAnnotation && pLineAnnotation->isTransition()) {
manhattanizeShape(false);
removeRedundantPointsGeometriesAndCornerItems();
QString newAnnotation = getOMCShapeAnnotation();
Expand Down Expand Up @@ -1610,7 +1610,7 @@ void ShapeAnnotation::updateCornerItemPoint(int index, QPointF point)
if (dynamic_cast<LineAnnotation*>(this)) {
point = mapFromScene(point);
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
if (pLineAnnotation->getLineType() == LineAnnotation::ConnectionType) {
if (pLineAnnotation && pLineAnnotation->isConnection()) {
if (mPoints.size() > index) {
// 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;
Expand Down Expand Up @@ -1868,10 +1868,6 @@ QVariant ShapeAnnotation::itemChange(GraphicsItemChange change, const QVariant &
QGraphicsItem::itemChange(change, value);
if (change == QGraphicsItem::ItemSelectedHasChanged) {
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (pLineAnnotation) {
lineType = pLineAnnotation->getLineType();
}
if (isSelected()) {
setCornerItemsActiveOrPassive();
setCursor(Qt::SizeAllCursor);
Expand All @@ -1881,7 +1877,7 @@ QVariant ShapeAnnotation::itemChange(GraphicsItemChange change, const QVariant &
connect(mpGraphicsView, SIGNAL(manhattanize()), this, SLOT(manhattanizeShape()), Qt::UniqueConnection);
}
connect(mpGraphicsView, SIGNAL(deleteSignal()), this, SLOT(deleteMe()), Qt::UniqueConnection);
if (lineType == LineAnnotation::ShapeType) {
if (pLineAnnotation && pLineAnnotation->isLineShape()) {
connect(mpGraphicsView, SIGNAL(duplicate()), this, SLOT(duplicate()), Qt::UniqueConnection);
connect(mpGraphicsView->getBringToFrontAction(), SIGNAL(triggered()), this, SLOT(bringToFront()), Qt::UniqueConnection);
connect(mpGraphicsView->getBringForwardAction(), SIGNAL(triggered()), this, SLOT(bringForward()), Qt::UniqueConnection);
Expand Down Expand Up @@ -1914,7 +1910,7 @@ QVariant ShapeAnnotation::itemChange(GraphicsItemChange change, const QVariant &
disconnect(mpGraphicsView, SIGNAL(manhattanize()), this, SLOT(manhattanizeShape()));
}
disconnect(mpGraphicsView, SIGNAL(deleteSignal()), this, SLOT(deleteMe()));
if (lineType == LineAnnotation::ShapeType) {
if (pLineAnnotation && pLineAnnotation->isLineShape()) {
disconnect(mpGraphicsView, SIGNAL(duplicate()), this, SLOT(duplicate()));
disconnect(mpGraphicsView->getBringToFrontAction(), SIGNAL(triggered()), this, SLOT(bringToFront()));
disconnect(mpGraphicsView->getBringForwardAction(), SIGNAL(triggered()), this, SLOT(bringForward()));
Expand Down
53 changes: 19 additions & 34 deletions OMEdit/OMEditLIB/Annotations/ShapePropertiesDialog.cpp
Expand Up @@ -419,18 +419,16 @@ ShapePropertiesDialog::ShapePropertiesDialog(ShapeAnnotation *pShapeAnnotation,
QVector<QPointF> points = mpShapeAnnotation->getPoints();
mpPointsTableWidget->setRowCount(points.size());
int rowIndex = 0;
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (mpLineAnnotation) lineType = mpLineAnnotation->getLineType();
foreach (QPointF point, points) {
QTableWidgetItem *pTableWidgetItemX = new QTableWidgetItem(QString::number(point.x()));
if ((rowIndex == 0 || rowIndex == points.size() - 1) && (lineType == LineAnnotation::ConnectionType)) {
if ((rowIndex == 0 || rowIndex == points.size() - 1) && (mpLineAnnotation && mpLineAnnotation->isConnection())) {
pTableWidgetItemX->setFlags(Qt::NoItemFlags);
} else {
pTableWidgetItemX->setFlags(pTableWidgetItemX->flags() | Qt::ItemIsEditable);
}
mpPointsTableWidget->setItem(rowIndex, 0, pTableWidgetItemX);
QTableWidgetItem *pTableWidgetItemY = new QTableWidgetItem(QString::number(point.y()));
if ((rowIndex == 0 || rowIndex == points.size() - 1) && (lineType == LineAnnotation::ConnectionType)) {
if ((rowIndex == 0 || rowIndex == points.size() - 1) && (mpLineAnnotation && mpLineAnnotation->isConnection())) {
pTableWidgetItemY->setFlags(Qt::NoItemFlags);
} else {
pTableWidgetItemY->setFlags(pTableWidgetItemY->flags() | Qt::ItemIsEditable);
Expand All @@ -439,7 +437,7 @@ ShapePropertiesDialog::ShapePropertiesDialog(ShapeAnnotation *pShapeAnnotation,
rowIndex++;
}
if (rowIndex > 0) {
if (lineType == LineAnnotation::ConnectionType) {
if (mpLineAnnotation && mpLineAnnotation->isConnection()) {
mpPointsTableWidget->setCurrentCell(1, 1);
} else {
mpPointsTableWidget->setCurrentCell(0, 0);
Expand Down Expand Up @@ -540,11 +538,11 @@ ShapePropertiesDialog::ShapePropertiesDialog(ShapeAnnotation *pShapeAnnotation,
QString ShapePropertiesDialog::getTitle()
{
if (mpLineAnnotation) {
if (mpLineAnnotation->getLineType() == LineAnnotation::ConnectionType) {
if (mpLineAnnotation->isConnection()) {
return "Connection";
} else if (mpLineAnnotation->getLineType() == LineAnnotation::TransitionType) {
} else if (mpLineAnnotation->isTransition()) {
return "Transition";
} else if (mpLineAnnotation->getLineType() == LineAnnotation::InitialStateType) {
} else if (mpLineAnnotation->isInitialState()) {
return "Initial State";
} else {
return "Line";
Expand Down Expand Up @@ -640,17 +638,15 @@ void ShapePropertiesDialog::fillPickColor()

void ShapePropertiesDialog::movePointUp()
{
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (mpLineAnnotation) lineType = mpLineAnnotation->getLineType();
if (lineType == LineAnnotation::InitialStateType) {
if (mpLineAnnotation && mpLineAnnotation->isInitialState()) {
return;
}
if (mpPointsTableWidget->selectedItems().size() > 0) {
int row = mpPointsTableWidget->selectedItems().at(0)->row();
if (row == 0) {
return;
}
if (row == 1 && (lineType == LineAnnotation::ConnectionType || lineType == LineAnnotation::TransitionType)) {
if (row == 1 && (mpLineAnnotation && (mpLineAnnotation->isConnection() || mpLineAnnotation->isTransition()))) {
return;
}
QTableWidgetItem *pSourceItemX = mpPointsTableWidget->takeItem(row, 0);
Expand All @@ -667,17 +663,15 @@ void ShapePropertiesDialog::movePointUp()

void ShapePropertiesDialog::movePointDown()
{
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (mpLineAnnotation) lineType = mpLineAnnotation->getLineType();
if (lineType == LineAnnotation::InitialStateType) {
if (mpLineAnnotation && mpLineAnnotation->isInitialState()) {
return;
}
if (mpPointsTableWidget->selectedItems().size() > 0) {
int row = mpPointsTableWidget->selectedItems().at(0)->row();
if (row == mpPointsTableWidget->rowCount() - 1) {
return;
}
if (row == mpPointsTableWidget->rowCount() - 2 && (lineType == LineAnnotation::ConnectionType || lineType == LineAnnotation::TransitionType)) {
if (row == mpPointsTableWidget->rowCount() - 2 && (mpLineAnnotation && (mpLineAnnotation->isConnection() || mpLineAnnotation->isTransition()))) {
return;
}
QTableWidgetItem *pSourceItemX = mpPointsTableWidget->takeItem(row, 0);
Expand All @@ -695,9 +689,7 @@ void ShapePropertiesDialog::movePointDown()
void ShapePropertiesDialog::addPoint()
{
if (mpPointsTableWidget->selectedItems().size() > 0) {
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (mpLineAnnotation) lineType = mpLineAnnotation->getLineType();
if (lineType == LineAnnotation::InitialStateType) {
if (mpLineAnnotation && mpLineAnnotation->isInitialState()) {
return;
}
int row = mpPointsTableWidget->selectedItems().at(0)->row();
Expand Down Expand Up @@ -726,9 +718,7 @@ void ShapePropertiesDialog::addPoint()
mpPointsTableWidget->setItem(row + 1, 1, pTableWidgetItemY);
mpPointsTableWidget->setCurrentCell(row + 1, 0);
}
} else if (mpLineAnnotation && mpPointsTableWidget->rowCount() == 2 &&
(mpLineAnnotation->getLineType() == LineAnnotation::ConnectionType ||
mpLineAnnotation->getLineType() == LineAnnotation::TransitionType)) {
} else if (mpLineAnnotation && mpPointsTableWidget->rowCount() == 2 && (mpLineAnnotation->isConnection() || mpLineAnnotation->isTransition())) {
/* get middle of two surronding points */
QPointF point1(mpPointsTableWidget->item(0, 0)->text().toDouble(), mpPointsTableWidget->item(0, 1)->text().toDouble());
QPointF point2(mpPointsTableWidget->item(1, 0)->text().toDouble(), mpPointsTableWidget->item(1, 1)->text().toDouble());
Expand All @@ -749,10 +739,8 @@ void ShapePropertiesDialog::removePoint()
{
if (mpPointsTableWidget->selectedItems().size() > 0) {
if ((mpLineAnnotation && mpPointsTableWidget->rowCount() > 2) || (mpPolygonAnnotation && mpPointsTableWidget->rowCount() > 4)) {
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (mpLineAnnotation) lineType = mpLineAnnotation->getLineType();
int row = mpPointsTableWidget->selectedItems().at(0)->row();
if (lineType == LineAnnotation::InitialStateType && row == 0) {
if (mpLineAnnotation && mpLineAnnotation->isInitialState() && row == 0) {
return;
}
mpPointsTableWidget->removeRow(row);
Expand Down Expand Up @@ -914,23 +902,20 @@ bool ShapePropertiesDialog::applyShapeProperties()
#endif // QT_VERSION_CHECK
}
}
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (mpLineAnnotation) {
lineType = mpLineAnnotation->getLineType();
if (lineType == LineAnnotation::ConnectionType || lineType == LineAnnotation::TransitionType) {
mpLineAnnotation->adjustGeometries();
}

if (mpLineAnnotation && (mpLineAnnotation->isConnection() || mpLineAnnotation->isTransition())) {
mpLineAnnotation->adjustGeometries();
}
// if nothing has changed then just simply return true.
if (mOldAnnotation.compare(mpShapeAnnotation->getOMCShapeAnnotation()) == 0) {
return true;
} else if (mpLineAnnotation && lineType == LineAnnotation::ConnectionType) {
} else if (mpLineAnnotation && mpLineAnnotation->isConnection()) {
// create a UpdateConnectionCommand object and push it to the undo stack.
UpdateConnectionCommand *pUpdateConnectionCommand;
pUpdateConnectionCommand = new UpdateConnectionCommand(mpLineAnnotation, mOldAnnotation, mpShapeAnnotation->getOMCShapeAnnotation());
mpShapeAnnotation->getGraphicsView()->getModelWidget()->getUndoStack()->push(pUpdateConnectionCommand);
mpShapeAnnotation->getGraphicsView()->getModelWidget()->updateModelText();
} else if (mpLineAnnotation && lineType == LineAnnotation::TransitionType) {
} else if (mpLineAnnotation && mpLineAnnotation->isTransition()) {
// create a UpdateTransitionCommand object and push it to the undo stack.
UpdateTransitionCommand *pUpdateTransitionCommand;
pUpdateTransitionCommand = new UpdateTransitionCommand(mpLineAnnotation, mpLineAnnotation->getCondition(), mpLineAnnotation->getImmediate(), mpLineAnnotation->getReset(),
Expand All @@ -939,7 +924,7 @@ bool ShapePropertiesDialog::applyShapeProperties()
mpLineAnnotation->getPriority(), mpShapeAnnotation->getOMCShapeAnnotation());
mpShapeAnnotation->getGraphicsView()->getModelWidget()->getUndoStack()->push(pUpdateTransitionCommand);
mpShapeAnnotation->getGraphicsView()->getModelWidget()->updateModelText();
} else if (mpLineAnnotation && lineType == LineAnnotation::InitialStateType) {
} else if (mpLineAnnotation && mpLineAnnotation->isInitialState()) {
// create a UpdateInitialStateCommand object and push it to the undo stack.
UpdateInitialStateCommand *pUpdateInitialStateCommand;
pUpdateInitialStateCommand = new UpdateInitialStateCommand(mpLineAnnotation, mOldAnnotation, mpShapeAnnotation->getOMCShapeAnnotation());
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp
Expand Up @@ -343,7 +343,7 @@ void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
}
// text annotation on a transition
LineAnnotation *pTransitionLineAnnotation = dynamic_cast<LineAnnotation*>(parentItem());
if (pTransitionLineAnnotation && pTransitionLineAnnotation->getLineType() == LineAnnotation::TransitionType
if (pTransitionLineAnnotation && pTransitionLineAnnotation->isTransition()
&& pTransitionLineAnnotation->getGraphicsView() && pTransitionLineAnnotation->getGraphicsView()->isVisualizationView()) {
if (pTransitionLineAnnotation->isActiveState()) {
painter->setOpacity(1.0);
Expand Down
5 changes: 2 additions & 3 deletions OMEdit/OMEditLIB/Element/CornerItem.cpp
Expand Up @@ -155,9 +155,8 @@ void CornerItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(mpShapeAnnotation);
if (pLineAnnotation) {
QVector<QPointF> points = pLineAnnotation->getPoints();
LineAnnotation::LineType lineType = pLineAnnotation->getLineType();
if ((((lineType == LineAnnotation::ConnectionType || lineType == LineAnnotation::TransitionType) && (mConnectedPointIndex == 0 || mConnectedPointIndex == points.size() - 1))
|| (lineType == LineAnnotation::InitialStateType && mConnectedPointIndex == 0))) {
if ((((pLineAnnotation->isConnection() || pLineAnnotation->isTransition()) && (mConnectedPointIndex == 0 || mConnectedPointIndex == points.size() - 1))
|| (pLineAnnotation->isInitialState() && mConnectedPointIndex == 0))) {
QGraphicsItem::mouseMoveEvent(event);
return;
}
Expand Down

0 comments on commit afb86a3

Please sign in to comment.