Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Keep the connecting lines manhattanized.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23357 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Nov 14, 2014
1 parent f51fb0d commit 4fa24ee
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 411 deletions.
489 changes: 195 additions & 294 deletions OMEdit/OMEditGUI/Annotations/LineAnnotation.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions OMEdit/OMEditGUI/Annotations/LineAnnotation.h
Expand Up @@ -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. */
Expand All @@ -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);
Expand All @@ -85,7 +86,6 @@ class LineAnnotation : public ShapeAnnotation
QString mStartComponentName;
Component *mpEndComponent;
QString mEndComponentName;
QList<GeometryType> mGeometries;
public slots:
void handleComponentMoved();
void handleComponentRotation();
Expand Down
15 changes: 13 additions & 2 deletions OMEdit/OMEditGUI/Annotations/PolygonAnnotation.cpp
Expand Up @@ -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
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Annotations/PolygonAnnotation.h
Expand Up @@ -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();
Expand Down
182 changes: 133 additions & 49 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp
Expand Up @@ -580,32 +580,23 @@ void ShapeAnnotation::initializeTransformation()
*/
void ShapeAnnotation::drawCornerItems()
{
if (dynamic_cast<LineAnnotation*>(this) || dynamic_cast<PolygonAnnotation*>(this))
{
if (dynamic_cast<LineAnnotation*>(this) || dynamic_cast<PolygonAnnotation*>(this)) {
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -708,15 +709,6 @@ Transformation* ShapeAnnotation::getTransformation()
return mpTransformation;
}

/*!
Sets the points list.
\param points - the points list.
*/
void ShapeAnnotation::setPoints(QList<QPointF> points)
{
mPoints = points;
}

/*!
Returns the points list.
\return the points list.
Expand Down Expand Up @@ -1145,11 +1137,9 @@ void ShapeAnnotation::applyRotation(qreal angle)
void ShapeAnnotation::adjustPointsWithOrigin()
{
QList<QPointF> 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;
}
Expand All @@ -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.
Expand Down Expand Up @@ -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<LineAnnotation*>(this) || dynamic_cast<PolygonAnnotation*>(this))
{
mPoints.replace(index, point);
/* if shape is the PolygonAnnotation then update the start and end point together */
if (dynamic_cast<PolygonAnnotation*>(this))
{
/* if first point */
if (index == 0)
{
mPoints.back() = point;
mCornerItemsList[mPoints.size() - 1]->setPos(point);
if (dynamic_cast<LineAnnotation*>(this)) {
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(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<PolygonAnnotation*>(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.
*/
Expand All @@ -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();
}

/*!
Expand Down
10 changes: 9 additions & 1 deletion OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h
Expand Up @@ -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();
Expand All @@ -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<QPointF> points);
QList<QPointF> getPoints();
void setStartArrow(StringHandler::Arrow startArrow);
StringHandler::Arrow getStartArrow();
Expand Down Expand Up @@ -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:
Expand All @@ -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<QPointF> mPoints;
QList<LineGeometryType> mGeometries;
QList<StringHandler::Arrow> mArrow;
qreal mArrowSize;
StringHandler::Smooth mSmooth;
Expand Down

0 comments on commit 4fa24ee

Please sign in to comment.