From 57e5ccd9439f6ef8bbfaed0a56d6a2e22fe3d702 Mon Sep 17 00:00:00 2001 From: blacksun Date: Tue, 3 Jul 2018 14:25:46 +0000 Subject: [PATCH] Mutualize code git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5427 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- .../qetgraphicshandlerutility.cpp | 55 ++++++++++++++++++ .../qetgraphicshandlerutility.h | 2 + sources/editor/graphicspart/partpolygon.cpp | 54 +++--------------- sources/qetgraphicsitem/qetshapeitem.cpp | 56 +++---------------- 4 files changed, 73 insertions(+), 94 deletions(-) diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp index 23d8d940c..a47452446 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp @@ -186,3 +186,58 @@ QLineF QetGraphicsHandlerUtility::lineForPosAtIndex(const QLineF &old_line, cons index == 0 ? line.setP1(pos) : line.setP2(pos); return line; } + +/** + * @brief QetGraphicsHandlerUtility::polygonForInsertPoint + * @param old_polygon : the polygon which we insert a new point. + * @param closed : polygon is closed or not + * @param pos : the pos where the new point must be added + * @return the new polygon + */ +QPolygonF QetGraphicsHandlerUtility::polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos) +{ + qreal max_angle = 0; + int index = 0; + + for (int i=1 ; i max_angle) + { + max_angle = angle; + index=i; + } + } + //Special case when polygon is close + if (closed) + { + QLineF line_a(old_polygon.last(), pos); + QLineF line_b(pos, old_polygon.first()); + + qreal angle = line_a.angleTo(line_b); + if (angle<180) + angle = 360-angle; + + if (angle > max_angle) + { + max_angle = angle; + index=old_polygon.size(); + } + } + + QPolygonF polygon = old_polygon; + polygon.insert(index, pos); + return polygon; +} diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h index 14e8e4b26..809435831 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h @@ -21,6 +21,7 @@ #include #include #include +#include class QPainter; @@ -39,6 +40,7 @@ class QetGraphicsHandlerUtility static QRectF rectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index); static QRectF mirrorRectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index); static QLineF lineForPosAtIndex (const QLineF &old_line, const QPointF &pos, int index); + static QPolygonF polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos); }; #endif // QETGRAPHICSHANDLERUTILITY_H diff --git a/sources/editor/graphicspart/partpolygon.cpp b/sources/editor/graphicspart/partpolygon.cpp index 5a0ac4ed0..c1c4c1486 100644 --- a/sources/editor/graphicspart/partpolygon.cpp +++ b/sources/editor/graphicspart/partpolygon.cpp @@ -21,6 +21,7 @@ #include "QetGraphicsItemModeler/qetgraphicshandleritem.h" #include "qetelementeditor.h" #include "qeticons.h" +#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h" /** @@ -482,55 +483,16 @@ void PartPolygon::removeHandler() * Insert a point in this polygone */ void PartPolygon::insertPoint() -{ - qreal max_angle = 0; - int index = 0; +{ + QPolygonF new_polygon = QetGraphicsHandlerUtility::polygonForInsertPoint(m_polygon, m_closed, elementScene()->snapToGrid(m_context_menu_pos)); - for (int i=1 ; i max_angle) - { - max_angle = angle; - index=i; - } + //Wrap the undo for avoid to merge the undo commands when user add several points. + QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone")); + new QPropertyUndoCommand(this, "polygon", m_polygon, new_polygon, undo); + elementScene()->undoStack().push(undo); } - //Special case when polygon is close - if (m_closed) - { - QLineF line_a(m_polygon.last(), m_context_menu_pos); - QLineF line_b(m_context_menu_pos, m_polygon.first()); - - qreal angle = line_a.angleTo(line_b); - if (angle<180) - angle = 360-angle; - - if (angle > max_angle) - { - max_angle = angle; - index=m_polygon.size(); - } - } - - QPolygonF polygon = this->polygon(); - polygon.insert(index, elementScene()->snapToGrid(m_context_menu_pos)); - - //Wrap the undo for avoid to merge the undo commands when user add several points. - QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone")); - new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo); - elementScene()->undoStack().push(undo); } /** diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index 08ac21ea0..f97c050c6 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -291,7 +291,7 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti { painter->save(); QColor color(Qt::darkBlue); - color.setAlpha(25); + color.setAlpha(50); painter -> setBrush (QBrush (color)); painter -> setPen (Qt::NoPen); painter -> drawPath (shape()); @@ -561,58 +561,18 @@ void QetShapeItem::adjusteHandlerPos() void QetShapeItem::insertPoint() { - if (m_shapeType != QetShapeItem::Polygon) { - return; - } - - qreal max_angle = 0; - int index = 0; - - for (int i=1 ; i max_angle) - { - max_angle = angle; - index=i; - } - } - //Special case when polygon is close - if (m_closed) - { - QLineF line_a(m_polygon.last(), m_context_menu_pos); - QLineF line_b(m_context_menu_pos, m_polygon.first()); + QPolygonF new_polygon = QetGraphicsHandlerUtility::polygonForInsertPoint(this->polygon(), m_closed, Diagram::snapToGrid(m_context_menu_pos)); - qreal angle = line_a.angleTo(line_b); - if (angle<180) - angle = 360-angle; - - if (angle > max_angle) + if(new_polygon != m_polygon) { - max_angle = angle; - index=m_polygon.size(); + //Wrap the undo for avoid to merge the undo commands when user add several points. + QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone")); + new QPropertyUndoCommand(this, "polygon", m_polygon, new_polygon, undo); + diagram()->undoStack().push(undo); } } - - QPolygonF polygon = this->polygon(); - polygon.insert(index, Diagram::snapToGrid(m_context_menu_pos)); - - //Wrap the undo for avoid to merge the undo commands when user add several points. - QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone")); - new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo); - diagram()->undoStack().push(undo); } void QetShapeItem::removePoint()