Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Implemented Manhattanize shape. Takes a curves connection and creates a right-angle lines connection of it.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23379 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Nov 15, 2014
1 parent 7aabce2 commit 3c5791f
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 147 deletions.
10 changes: 9 additions & 1 deletion OMEdit/OMEditGUI/Annotations/LineAnnotation.cpp
Expand Up @@ -430,6 +430,8 @@ void LineAnnotation::clearPoints()
*/
void LineAnnotation::updateStartPoint(QPointF point)
{
manhattanizeShape();
removeRedundantPointsGeometriesAndCornerItems();
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
Expand Down Expand Up @@ -466,6 +468,10 @@ void LineAnnotation::updateStartPoint(QPointF point)
void LineAnnotation::updateEndPoint(QPointF point)
{
if (mLineType == LineAnnotation::ConnectionType) {
if (!mpGraphicsView->isCreatingConnection()) {
manhattanizeShape();
removeRedundantPointsGeometriesAndCornerItems();
}
int lastIndex = mPoints.size() - 1;
int secondLastIndex = mPoints.size() - 2;
qreal dx = point.x() - mPoints[lastIndex].x();
Expand Down Expand Up @@ -497,7 +503,9 @@ void LineAnnotation::updateEndPoint(QPointF point)
}
updateCornerItem(secondLastIndex);
}
removeRedundantPointsGeometriesAndCornerItems();
if (!mpGraphicsView->isCreatingConnection()) {
removeRedundantPointsGeometriesAndCornerItems();
}
} else {
mPoints.back() = point;
}
Expand Down
164 changes: 121 additions & 43 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp
Expand Up @@ -436,9 +436,14 @@ bool ShapeAnnotation::isInheritedShape()
*/
void ShapeAnnotation::createActions()
{
// shape properties
mpShapePropertiesAction = new QAction(Helper::properties, mpGraphicsView);
mpShapePropertiesAction->setStatusTip(tr("Shows the shape properties"));
connect(mpShapePropertiesAction, SIGNAL(triggered()), SLOT(showShapeProperties()));
// manhattanize properties
mpManhattanizeShapeAction = new QAction(tr("Manhattanize"), mpGraphicsView);
mpManhattanizeShapeAction->setStatusTip(tr("Manhattanize the lines"));
connect(mpManhattanizeShapeAction, SIGNAL(triggered()), SLOT(manhattanizeShape()));
}

/*!
Expand Down Expand Up @@ -1132,6 +1137,11 @@ void ShapeAnnotation::adjustExtentsWithOrigin()
mExtents = extents;
}

/*!
Returns the CornerItem located at index.
\param index
\return CornerItem
*/
CornerItem* ShapeAnnotation::getCornerItem(int index)
{
for (int i = 0 ; i < mCornerItemsList.size() ; i++) {
Expand All @@ -1142,6 +1152,10 @@ CornerItem* ShapeAnnotation::getCornerItem(int index)
return 0;
}

/*!
Updates the position of the CornerItem located at index.
\param index
*/
void ShapeAnnotation::updateCornerItem(int index)
{
CornerItem *pCornerItem = getCornerItem(index);
Expand All @@ -1152,6 +1166,11 @@ void ShapeAnnotation::updateCornerItem(int index)
}
}

/*!
Adds new points, geometries & CornerItems at index. \n
This function is called when resizing the connection lines and new points are needed to keep the lines manhattanized.
\param index
*/
void ShapeAnnotation::insertPointsGeometriesAndCornerItems(int index)
{
QPointF point = (mPoints[index - 1] + mPoints[index]) / 2;
Expand All @@ -1171,6 +1190,9 @@ void ShapeAnnotation::insertPointsGeometriesAndCornerItems(int index)
adjustCornerItemsConnectedIndexes();
}

/*!
Makes the CornerItems & points indexes same.
*/
void ShapeAnnotation::adjustCornerItemsConnectedIndexes()
{
for (int i = 0 ; i < mPoints.size() ; i++) {
Expand All @@ -1180,6 +1202,10 @@ void ShapeAnnotation::adjustCornerItemsConnectedIndexes()
}
}

/*!
Finds and removes the unncessary points in a connection.\n
For example if there are three points on same horizontal line then the center point will be removed.
*/
void ShapeAnnotation::removeRedundantPointsGeometriesAndCornerItems()
{
for (int i = 0 ; i < mPoints.size() ; i++) {
Expand All @@ -1201,6 +1227,9 @@ void ShapeAnnotation::removeRedundantPointsGeometriesAndCornerItems()
}
}

/*!
Adjusts the Geometries list according to points list.
*/
void ShapeAnnotation::adjustGeometries()
{
mGeometries.clear();
Expand All @@ -1219,6 +1248,63 @@ void ShapeAnnotation::adjustGeometries()
}
}

/*!
Slot activated when mpManhattanizeShapeAction triggered signal is raised.\n
Finds the curved lines in the Line shape and makes in manhattanize/right-angle line.
*/
void ShapeAnnotation::manhattanizeShape()
{
int startIndex = -1;
for (int i = 0 ; i < mPoints.size() ; i++) {
if (i + 1 < mPoints.size()) {
if (!isLineStraight(mPoints[i], mPoints[i + 1])) {
startIndex = i;
break;
}
}
}
if (startIndex > -1) {
int lastIndex = mPoints.size() - 1;
for (int i = mPoints.size() - 1 ; i >= 0 ; i--) {
if (i - 1 > -1) {
if (!isLineStraight(mPoints[i], mPoints[i - 1])) {
lastIndex = i;
break;
}
}
}

QPointF startPoint = mPoints[startIndex];
QPointF lastPoint = mPoints[lastIndex];
qreal dx = lastPoint.x() - startPoint.x();
qreal dy = lastPoint.y() - startPoint.y();
QList<QPointF> points;
if (dx == 0) {
points.append(QPointF(startPoint.x(), startPoint.y() + dy));
} else if (dy == 0) {
points.append(QPointF(startPoint.x() + dx, startPoint.y()));
} else {
points.append(QPointF(startPoint.x(), startPoint.y() + dy));
points.append(QPointF(points[0].x() + dx, points[0].y()));
}
points.removeLast();
QList<QPointF> oldPoints = mPoints;
clearPoints();
for (int i = 0 ; i <= startIndex ; i++) {
addPoint(oldPoints[i]);
}
if (points.size() > 0) {
addPoint(points[0]);
}
for (int i = lastIndex ; i < oldPoints.size() ; i++) {
addPoint(oldPoints[i]);
}
removeCornerItems();
drawCornerItems();
cornerItemReleased();
}
}

/*!
Slot activated when Delete option is choosen from context menu of the shape.\n
Deletes the connection.
Expand Down Expand Up @@ -1589,12 +1675,9 @@ void ShapeAnnotation::cornerItemPressed()
void ShapeAnnotation::cornerItemReleased()
{
mIsCornerItemClicked = false;
if (isSelected())
{
if (isSelected()) {
setCornerItemsActive();
}
else
{
} else {
setSelected(true);
}
}
Expand Down Expand Up @@ -1676,6 +1759,18 @@ ShapeAnnotation::LineGeometryType ShapeAnnotation::findLineGeometryType(QPointF
}
}

bool ShapeAnnotation::isLineStraight(QPointF point1, QPointF point2)
{
QLineF line(point1, point2);
qreal angle = StringHandler::getNormalizedAngle(line.angle());

if (angle == 0 || angle == 90 || angle == 180 || angle == 270 || angle == 360) {
return true;
} else {
return false;
}
}

/*!
Slot activated when Properties option is choosen from context menu of the shape.
*/
Expand All @@ -1695,37 +1790,32 @@ void ShapeAnnotation::showShapeProperties()
*/
void ShapeAnnotation::contextMenuEvent(QGraphicsSceneContextMenuEvent *pEvent)
{
if (!mIsCustomShape)
{
if (!mIsCustomShape) {
QGraphicsItem::contextMenuEvent(pEvent);
return;
}
if (!isSelected())
{
if (!isSelected()) {
setSelected(true);
}
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (pLineAnnotation)
{
lineType = pLineAnnotation->getLineType();
}

QMenu menu(mpGraphicsView);
menu.addAction(mpShapePropertiesAction);
menu.addSeparator();
if (isInheritedShape())
{
if (isInheritedShape()) {
mpGraphicsView->getDeleteAction()->setDisabled(true);
mpGraphicsView->getDuplicateAction()->setDisabled(true);
mpGraphicsView->getRotateClockwiseAction()->setDisabled(true);
mpGraphicsView->getRotateAntiClockwiseAction()->setDisabled(true);
}
if (lineType == LineAnnotation::ConnectionType)
{
menu.addAction(mpGraphicsView->getDeleteConnectionAction());
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (pLineAnnotation) {
lineType = pLineAnnotation->getLineType();
menu.addAction(mpManhattanizeShapeAction);
}
else
{
if (lineType == LineAnnotation::ConnectionType) {
menu.addAction(mpGraphicsView->getDeleteConnectionAction());
} else {
menu.addAction(mpGraphicsView->getDeleteAction());
menu.addAction(mpGraphicsView->getDuplicateAction());
menu.addSeparator();
Expand All @@ -1749,24 +1839,18 @@ QVariant ShapeAnnotation::itemChange(GraphicsItemChange change, const QVariant &
{
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
LineAnnotation::LineType lineType = LineAnnotation::ShapeType;
if (pLineAnnotation)
{
if (pLineAnnotation) {
lineType = pLineAnnotation->getLineType();
}
if (isSelected())
{
if (isSelected()) {
setCornerItemsActive();
setCursor(Qt::SizeAllCursor);
/* Only allow manipulations on shapes if the class is not a system library class OR shape is not an inherited component. */
if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && !isInheritedShape())
{
if (lineType == LineAnnotation::ConnectionType)
{
if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && !isInheritedShape()) {
if (lineType == LineAnnotation::ConnectionType) {
connect(mpGraphicsView->getDeleteConnectionAction(), SIGNAL(triggered()), SLOT(deleteConnection()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(keyPressDelete()), SLOT(deleteConnection()), Qt::UniqueConnection);
}
else
{
} else {
connect(mpGraphicsView->getDeleteAction(), SIGNAL(triggered()), this, SLOT(deleteMe()), Qt::UniqueConnection);
connect(mpGraphicsView->getDuplicateAction(), SIGNAL(triggered()), this, SLOT(duplicate()), Qt::UniqueConnection);
connect(mpGraphicsView->getRotateClockwiseAction(), SIGNAL(triggered()), this, SLOT(rotateClockwiseMouseRightClick()), Qt::UniqueConnection);
Expand All @@ -1790,21 +1874,15 @@ QVariant ShapeAnnotation::itemChange(GraphicsItemChange change, const QVariant &
connect(mpGraphicsView, SIGNAL(keyRelease()), this, SIGNAL(updateClassAnnotation()), Qt::UniqueConnection);
}
}
}
else if (!mIsCornerItemClicked)
{
} else if (!mIsCornerItemClicked) {
setCornerItemsPassive();
unsetCursor();
/* Only allow manipulations on shapes if the class is not a system library class OR shape is not an inherited component. */
if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && !isInheritedShape())
{
if (lineType == LineAnnotation::ConnectionType)
{
if (!mpGraphicsView->getModelWidget()->getLibraryTreeNode()->isSystemLibrary() && !isInheritedShape()) {
if (lineType == LineAnnotation::ConnectionType) {
disconnect(mpGraphicsView->getDeleteConnectionAction(), SIGNAL(triggered()), this, SLOT(deleteConnection()));
disconnect(mpGraphicsView, SIGNAL(keyPressDelete()), this, SLOT(deleteConnection()));
}
else
{
} else {
disconnect(mpGraphicsView->getDeleteAction(), SIGNAL(triggered()), this, SLOT(deleteMe()));
disconnect(mpGraphicsView->getDuplicateAction(), SIGNAL(triggered()), this, SLOT(duplicate()));
disconnect(mpGraphicsView->getRotateClockwiseAction(), SIGNAL(triggered()), this, SLOT(rotateClockwiseMouseRightClick()));
Expand Down
3 changes: 3 additions & 0 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h
Expand Up @@ -109,6 +109,7 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem
QPointF mOldPosition;
bool mIsCornerItemClicked;
QAction *mpShapePropertiesAction;
QAction *mpManhattanizeShapeAction;
public:
enum LineGeometryType {VerticalLine, HorizontalLine};
ShapeAnnotation(QGraphicsItem *pParent);
Expand Down Expand Up @@ -209,7 +210,9 @@ public slots:
void cornerItemReleased();
void updateCornerItemPoint(int index, QPointF point);
LineGeometryType findLineGeometryType(QPointF point1, QPointF point2);
bool isLineStraight(QPointF point1, QPointF point2);
void showShapeProperties();
void manhattanizeShape();
protected:
GraphicsView *mpGraphicsView;
Transformation *mpTransformation;
Expand Down

0 comments on commit 3c5791f

Please sign in to comment.