[qet] qet/qet: [5437] QetShapeItem : rounded rect is saved in the .qet file

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


Revision: 5437
Author:   blacksun
Date:     2018-07-14 13:10:23 +0200 (Sat, 14 Jul 2018)
Log Message:
-----------
QetShapeItem : rounded rect is saved in the .qet file

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

Modified: trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp
===================================================================
--- trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp	2018-07-12 10:01:34 UTC (rev 5436)
+++ trunk/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp	2018-07-14 11:10:23 UTC (rev 5437)
@@ -250,24 +250,35 @@
  * The points are always based on the top right corner of the rect.
  * the first point of vector is X the second Y
  */
-#include <QDebug>
 QVector<QPointF> QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
 {
-	Q_UNUSED(mode)
 	QVector<QPointF> v;
 	
-	qreal half_width = rect.width()/2;
-	qreal x_percent = std::min(xRadius, 100.00)/100;
-	QPointF X(rect.right() - half_width*x_percent,
-			  rect.top());
-	v << X;
+	if(mode == Qt::AbsoluteSize)
+	{
+		QPointF X = rect.topRight();
+		X.rx() -= xRadius;
+		v << X;
+		
+		QPointF Y = rect.topRight();
+		Y.ry() += yRadius;
+		v << Y;
+	}
+	else
+	{
+		qreal half_width = rect.width()/2;
+		qreal x_percent = std::min(xRadius, 100.00)/100;
+		QPointF X(rect.right() - half_width*x_percent,
+				  rect.top());
+		v << X;
+		
+		qreal half_height = rect.height()/2;
+		qreal y_percent = std::min(yRadius, 100.00)/100;
+		QPointF Y(rect.right(),
+				  rect.top()+ half_height*y_percent);
+		v << Y;
+	}
 	
-	qreal half_height = rect.height()/2;
-	qreal y_percent = std::min(yRadius, 100.00)/100;
-	QPointF Y(rect.right(),
-			  rect.top()+ half_height*y_percent);
-	v << Y;
-	
 	return v;
 }
 
@@ -281,35 +292,68 @@
  */
 qreal QetGraphicsHandlerUtility::radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode)
 {
-	Q_UNUSED(mode)
-	
-	if(index == 0) //X
+	if (mode == Qt::AbsoluteSize)
 	{
-		if (pos.x() < rect.center().x()) {
-			return 100;
+		if (index == 0)
+		{
+			QPointF tr = rect.topRight();
+			qreal x = tr.x() - pos.x();
+			if (x < 0) {
+				x = 0;
+			}
+			else if (x > rect.width()/2) {
+				x = rect.width()/2;
+			}
+			
+			return x;
 		}
-		else if (pos.x() > rect.right()) {
-			return 0;
+		else if (index == 1)
+		{
+			QPointF tr = rect.topRight();
+			qreal y = pos.y() - tr.y();
+			if (y < 0) {
+				y = 0;
+			}
+			else if (y > rect.height()/2) {
+				y = rect.height()/2;
+			}
+
+			return y;
 		}
 		else {
-			return (100 - percentageInRange(rect.center().x(), rect.right(), pos.x()));
+			return 0;
 		}
 	}
-	else if (index == 1) //Y
+	else
 	{
-		if (pos.y() < rect.top()) {
-			return 0;
+		if(index == 0) //X
+		{
+			if (pos.x() < rect.center().x()) {
+				return 100;
+			}
+			else if (pos.x() > rect.right()) {
+				return 0;
+			}
+			else {
+				return (100 - percentageInRange(rect.center().x(), rect.right(), pos.x()));
+			}
 		}
-		else if (pos.y() > rect.center().y()) {
-			return 100;
+		else if (index == 1) //Y
+		{
+			if (pos.y() < rect.top()) {
+				return 0;
+			}
+			else if (pos.y() > rect.center().y()) {
+				return 100;
+			}
+			else {
+				return percentageInRange(rect.top(), rect.center().y(), pos.y());
+			}
 		}
 		else {
-			return percentageInRange(rect.top(), rect.center().y(), pos.y());
+			return 0;
 		}
 	}
-	else {
-		return 0;
-	}
 }
 
 qreal QetGraphicsHandlerUtility::percentageInRange(qreal min, qreal max, qreal value) {

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2018-07-12 10:01:34 UTC (rev 5436)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2018-07-14 11:10:23 UTC (rev 5437)
@@ -264,7 +264,7 @@
 			path.lineTo(m_P2);                   
 			break;
 		case Rectangle: 
-			path.addRoundedRect(QRectF(m_P1, m_P2), m_xRadius, m_yRadius, Qt::RelativeSize);
+			path.addRoundedRect(QRectF(m_P1, m_P2), m_xRadius, m_yRadius);
 			break;
 		case Ellipse:
 			path.addEllipse(QRectF(m_P1, m_P2));
@@ -319,7 +319,7 @@
     switch (m_shapeType)
     {
         case Line:      painter->drawLine(QLineF(m_P1, m_P2)); break;
-        case Rectangle: painter->drawRoundedRect(QRectF(m_P1, m_P2), m_xRadius, m_yRadius, Qt::RelativeSize); break;
+        case Rectangle: painter->drawRoundedRect(QRectF(m_P1, m_P2), m_xRadius, m_yRadius); break;
         case Ellipse:   painter->drawEllipse(QRectF(m_P1, m_P2)); break;
         case Polygon:   m_closed ? painter->drawPolygon(m_polygon) : painter->drawPolyline(m_polygon); break;
     }
@@ -352,13 +352,12 @@
 void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
 	event->ignore();
+	QetGraphicsItem::mousePressEvent(event);
+	
 	if (event->button() == Qt::LeftButton) {
 		switchResizeMode();
 		event->accept();
 	}
-	else {
-		QetGraphicsItem::mousePressEvent(event);
-	}
 }
 
 /**
@@ -873,10 +872,18 @@
 		m_P1.setY(e.attribute("y1", nullptr).toDouble());
 		m_P2.setX(e.attribute("x2", nullptr).toDouble());
 		m_P2.setY(e.attribute("y2", nullptr).toDouble());
+		
+		if (m_shapeType == Rectangle)
+		{
+			setXRadius(e.attribute("rx", "0").toDouble());
+			setYRadius(e.attribute("ry", "0").toDouble());
+		}
 	}
-	else
-		foreach(QDomElement de, QET::findInDomElement(e, "points", "point"))
+	else {
+		for(QDomElement de : QET::findInDomElement(e, "points", "point")) {
 			m_polygon << QPointF(de.attribute("x", nullptr).toDouble(), de.attribute("y", nullptr).toDouble());
+		}
+	}
 	setZValue(e.attribute("z", QString::number(this->zValue())).toDouble());
 
 	return (true);
@@ -906,11 +913,28 @@
 		result.setAttribute("y1", QString::number(mapToScene(m_P1).y()));
 		result.setAttribute("x2", QString::number(mapToScene(m_P2).x()));
 		result.setAttribute("y2", QString::number(mapToScene(m_P2).y()));
+		
+		if (m_shapeType == Rectangle)
+		{
+			QRectF rect(m_P1, m_P2);
+			rect = rect.normalized();
+			qreal x = m_xRadius;
+			if (x > rect.width()/2) {
+				x = rect.width()/2;
+			}
+			qreal y = m_yRadius;
+			if (y > rect.height()/2) {
+				y = rect.height()/2;
+			}
+			
+			result.setAttribute("rx", QString::number(m_xRadius));
+			result.setAttribute("ry", QString::number(m_yRadius));
+		}
 	}
 	else
 	{
 		QDomElement points = document.createElement("points");
-		foreach(QPointF p, m_polygon)
+		for (QPointF p : m_polygon)
 		{
 			QDomElement point = document.createElement("point");
 			QPointF pf = mapToScene(p);


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