[qet] qet/qet: [5427] Mutualize code

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 5427
Author:   blacksun
Date:     2018-07-03 16:25:46 +0200 (Tue, 03 Jul 2018)
Log Message:
-----------
Mutualize code

Modified Paths:
--------------
    trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp
    trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h
    trunk/sources/editor/graphicspart/partpolygon.cpp
    trunk/sources/qetgraphicsitem/qetshapeitem.cpp

Modified: trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp
===================================================================
--- trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp	2018-07-03 07:30:09 UTC (rev 5426)
+++ trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp	2018-07-03 14:25:46 UTC (rev 5427)
@@ -186,3 +186,58 @@
 	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<old_polygon.size() ; i++)
+	{
+		QPointF A = old_polygon.at(i-1);
+		QPointF B = old_polygon.at(i);
+		QLineF line_a(A, pos);
+		QLineF line_b(pos, B);
+		qreal angle = line_a.angleTo(line_b);
+		if(angle<180)
+			angle = 360-angle;
+		
+		if (i==1)
+		{
+			max_angle = angle;
+			index=i;
+		}
+		if (angle > 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;
+}

Modified: trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h
===================================================================
--- trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h	2018-07-03 07:30:09 UTC (rev 5426)
+++ trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h	2018-07-03 14:25:46 UTC (rev 5427)
@@ -21,6 +21,7 @@
 #include <QRectF>
 #include <QLineF>
 #include <QColor>
+#include <QPolygonF>
 
 class QPainter;
 
@@ -39,6 +40,7 @@
 		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

Modified: trunk/sources/editor/graphicspart/partpolygon.cpp
===================================================================
--- trunk/sources/editor/graphicspart/partpolygon.cpp	2018-07-03 07:30:09 UTC (rev 5426)
+++ trunk/sources/editor/graphicspart/partpolygon.cpp	2018-07-03 14:25:46 UTC (rev 5427)
@@ -21,6 +21,7 @@
 #include "QetGraphicsItemModeler/qetgraphicshandleritem.h"
 #include "qetelementeditor.h"
 #include "qeticons.h"
+#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
 
 
 /**
@@ -482,55 +483,16 @@
  * 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<m_polygon.size() ; i++)
+	if(new_polygon != m_polygon)
 	{
-		QPointF A = m_polygon.at(i-1);
-		QPointF B = m_polygon.at(i);
-		QLineF line_a(A, m_context_menu_pos);
-		QLineF line_b(m_context_menu_pos, B);
-		qreal angle = line_a.angleTo(line_b);
-		if(angle<180)
-			angle = 360-angle;
-
-		if (i==1)
-		{
-			max_angle = angle;
-			index=i;
-		}
-		if (angle > 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);
 }
 
 /**

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2018-07-03 07:30:09 UTC (rev 5426)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2018-07-03 14:25:46 UTC (rev 5427)
@@ -291,7 +291,7 @@
 	{
 		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::insertPoint()
 {
-	if (m_shapeType != QetShapeItem::Polygon) {
-		return;
-	}
-	
-	qreal max_angle = 0;
-	int index = 0;
-	
-	for (int i=1 ; i<m_polygon.size() ; i++)
+	if (m_shapeType == QetShapeItem::Polygon)
 	{
-		QPointF A = m_polygon.at(i-1);
-		QPointF B = m_polygon.at(i);
-		QLineF line_a(A, m_context_menu_pos);
-		QLineF line_b(m_context_menu_pos, B);
-		qreal angle = line_a.angleTo(line_b);
-		if(angle<180)
-			angle = 360-angle;
-
-		if (i==1)
-		{
-			max_angle = angle;
-			index=i;
-		}
-		if (angle > 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()


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/