[qet] [4055] Qet shape item : rectangle and ellipse can be resized by the edges

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


Revision: 4055
Author:   blacksun
Date:     2015-07-19 17:52:44 +0200 (Sun, 19 Jul 2015)
Log Message:
-----------
Qet shape item : rectangle and ellipse can be resized by the edges

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

Modified: trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp
===================================================================
--- trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp	2015-07-19 15:44:58 UTC (rev 4054)
+++ trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp	2015-07-19 15:52:44 UTC (rev 4055)
@@ -29,12 +29,12 @@
 {}
 
 /**
- * @brief QetGraphicsHandlerUtility::DrawHandler
+ * @brief QetGraphicsHandlerUtility::drawHandler
  * Draw the handler at pos @point, using the QPainter @painter.
  * @param painter : painter to use for drawing the handler
  * @param point : point to draw the handler
  */
-void QetGraphicsHandlerUtility::DrawHandler(QPainter *painter, const QPointF &point, bool color2)
+void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2)
 {
 		//Color of handler
 	QColor inner(0xFF, 0xFF, 0xFF);
@@ -54,6 +54,18 @@
 }
 
 /**
+ * @brief QetGraphicsHandlerUtility::drawHandler
+ * Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2)
+ * @param painter
+ * @param points
+ * @param color2
+ */
+void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector<QPointF> &points, bool color2) {
+	foreach(QPointF point, points)
+		drawHandler(painter, point, color2);
+}
+
+/**
  * @brief QetGraphicsHandlerUtility::pointIsInHandler
  * @param point : point to compare
  * @param key_point : point at the center of handler (the point to modify, for exemple the corner of a rectangle)
@@ -109,15 +121,62 @@
 
 /**
  * @brief QetGraphicsHandlerUtility::pointsForRect
- * Return the point of the rect in vector.
- * The point are stored like this :
- * top left, top right, bottom left, bottom right;
+ * Return the keys points of the rectangle, stored in a vector.
+ * The points in the vector are stored like this :
+ * **********
+ *   0---1---2
+ *   |       |
+ *   3       4
+ *   |       |
+ *   5---6---7
+ * ************
  * @param rect
  * @return
  */
 QVector<QPointF> QetGraphicsHandlerUtility::pointsForRect(const QRectF &rect)
 {
 	QVector<QPointF> vector;
-	vector << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
+	QPointF point;
+	vector << rect.topLeft();//*****Top left
+	point = rect.center();
+	point.setY(rect.top());
+	vector << point;//**************Middle top
+	vector << rect.topRight();//****Top right
+	point = rect.center();
+	point.setX(rect.left());
+	vector << point;//**************Middle left
+	point.setX(rect.right());
+	vector << point;//**************Middle right
+	vector << rect.bottomLeft();//**Bottom left
+	point = rect.center();
+	point.setY(rect.bottom());
+	vector <<  point;//*************Middle bottom
+	vector << rect.bottomRight();//*Bottom right
 	return vector;
 }
+
+/**
+ * @brief QetGraphicsHandlerUtility::rectForPosAtIndex
+ * Return a rectangle after modification of the point '@pos' at index '@index' of original rectangle '@old_rect'.
+ * @param old_rect - the rectangle befor modification
+ * @param pos - the new position of a key point
+ * @param index - the index of the key point to modifie see QetGraphicsHandlerUtility::pointsForRect to know
+ * the index of each keys points of a rectangle)
+ * @return : the rectangle with modification. If index is lower than 0 or higher than 7, this method return old_rect.
+ */
+QRectF QetGraphicsHandlerUtility::rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
+{
+	if (index < 0 || index > 7) return old_rect;
+
+	QRectF rect = old_rect;
+	if (index == 0) rect.setTopLeft(pos);
+	else if (index == 1) rect.setTop(pos.y());
+	else if (index == 2) rect.setTopRight(pos);
+	else if (index == 3) rect.setLeft(pos.x());
+	else if (index == 4) rect.setRight(pos.x());
+	else if (index == 5) rect.setBottomLeft(pos);
+	else if (index == 6) rect.setBottom(pos.y());
+	else if (index == 7) rect.setBottomRight(pos);
+
+	return rect;
+}

Modified: trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h
===================================================================
--- trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h	2015-07-19 15:44:58 UTC (rev 4054)
+++ trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h	2015-07-19 15:52:44 UTC (rev 4055)
@@ -18,7 +18,6 @@
 #ifndef QETGRAPHICSHANDLERUTILITY_H
 #define QETGRAPHICSHANDLERUTILITY_H
 
-#include <QPointF>
 #include <QRectF>
 
 class QPainter;
@@ -27,13 +26,15 @@
  * @brief The QetGraphicsHandlerUtility class
  * This class provide some methods to create and use handler for
  * modify graphics shape like line rectangle etc...
+ * They also provide some conveniance static method.
  */
 class QetGraphicsHandlerUtility
 {
 	public:
 		QetGraphicsHandlerUtility (qreal size = 1);
 		void setSize(qreal size) {m_size = size;}
-		void DrawHandler (QPainter *painter, const QPointF & point, bool color2 = false);
+		void drawHandler (QPainter *painter, const QPointF & point, bool color2 = false);
+		void drawHandler(QPainter *painter, const QVector<QPointF> &points, bool color2 = false);
 		QPointF posForHandler(const QPointF &point) const;
 		bool pointIsInHandler (const QPointF &point, const QPointF &key_point) const;
 		int pointIsHoverHandler (const QPointF &point, const QVector<QPointF> &vector) const;
@@ -45,7 +46,8 @@
 		qreal m_zoom_factor;
 
 	public:
-		static QVector <QPointF> pointsForRect (const QRectF & rect);
+		static QVector <QPointF> pointsForRect (const QRectF &rect);
+		static QRectF rectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index);
 };
 
 #endif // QETGRAPHICSHANDLERUTILITY_H

Modified: trunk/sources/editor/elementprimitivedecorator.cpp
===================================================================
--- trunk/sources/editor/elementprimitivedecorator.cpp	2015-07-19 15:44:58 UTC (rev 4054)
+++ trunk/sources/editor/elementprimitivedecorator.cpp	2015-07-19 15:52:44 UTC (rev 4055)
@@ -97,9 +97,8 @@
 	pen.setCosmetic(true);
 	painter -> setPen(pen);
 	painter -> drawRect(modified_bounding_rect_);
-		//Draw the handler
-	foreach (QPointF point, getResizingsPoints())
-		m_handler.DrawHandler(painter, point, decorated_items_.size()-1);
+		//Draw the handlers
+	m_handler.drawHandler(painter, getResizingsPoints(), decorated_items_.size()-1);
 	
 	// uncomment to draw the real bouding rect (=adjusted internal bounding rect)
 	// painter -> setBrush(QBrush(QColor(240, 0, 0, 127)));

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2015-07-19 15:44:58 UTC (rev 4054)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2015-07-19 15:52:44 UTC (rev 4055)
@@ -260,48 +260,33 @@
 
 	painter -> setPen(pen);
 
-		//vector use to draw handler if needed
-	QVector <QPointF> point_vector;
-
-		//Draw the shape
+		//Draw the shape and handlers if is selected
 	switch (m_shapeType)
 	{
 		case Line:
 			painter->drawLine(QLineF(m_P1, m_P2));
 			if (isSelected())
-				point_vector << m_P1 << m_P2;
+				m_handler.drawHandler(painter, QVector<QPointF>{m_P1, m_P2});
 			break;
 
 		case Rectangle:
 			painter->drawRect(QRectF(m_P1, m_P2));
 			if (isSelected())
-			{
-				QRectF rect (m_P1, m_P2);
-				point_vector << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft();
-			}
+				m_handler.drawHandler(painter, m_handler.pointsForRect(QRectF(m_P1, m_P2)));
 			break;
 
 		case Ellipse:
 			painter->drawEllipse(QRectF(m_P1, m_P2));
 			if (isSelected())
-			{
-				QRectF rect (m_P1, m_P2);
-				point_vector << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft();
-			}
+				m_handler.drawHandler(painter, m_handler.pointsForRect(QRectF(m_P1, m_P2)));
 			break;
 
 		case Polyline:
-		{
 			painter->drawPolyline(m_polygon);
-			point_vector = m_polygon;
-		}
+			if (isSelected())
+				m_handler.drawHandler(painter, m_polygon);
 			break;
 	}
-
-		//Draw handler if shape is selected
-	if (isSelected())
-		foreach(QPointF point, point_vector)
-			m_handler.DrawHandler(painter, point);
 }
 
 /**
@@ -388,28 +373,13 @@
 			}
 				break;
 
-			case Rectangle: {
-				QRectF rect(m_P1, m_P2);
-				if (m_vector_index == 0) rect.setTopLeft(new_pos);
-				else if (m_vector_index == 1) rect.setTopRight(new_pos);
-				else if (m_vector_index == 2) rect.setBottomLeft(new_pos);
-				else if (m_vector_index == 3) rect.setBottomRight(new_pos);
+			case Rectangle:
+				setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
 
-				setRect(rect);
-			}
+			case Ellipse:
+				setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
 				break;
 
-			case Ellipse: {
-				QRectF rect(m_P1, m_P2);
-				if (m_vector_index == 0) rect.setTopLeft(new_pos);
-				else if (m_vector_index == 1) rect.setTopRight(new_pos);
-				else if (m_vector_index == 2) rect.setBottomLeft(new_pos);
-				else if (m_vector_index == 3) rect.setBottomRight(new_pos);
-
-				setRect(rect);
-			}
-				break;
-
 			case Polyline: {
 				prepareGeometryChange();
 				m_polygon.replace(m_vector_index, new_pos);


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