From eeb49d5a1859dec378271b28bd569260c66279bd Mon Sep 17 00:00:00 2001 From: blacksun Date: Sat, 14 Jul 2018 11:10:23 +0000 Subject: [PATCH] QetShapeItem : rounded rect is saved in the .qet file git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5437 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- .../qetgraphicshandlerutility.cpp | 108 ++++++++++++------ sources/qetgraphicsitem/qetshapeitem.cpp | 40 +++++-- 2 files changed, 108 insertions(+), 40 deletions(-) diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp index df06ce920..b8208ed14 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp @@ -250,23 +250,34 @@ QPolygonF QetGraphicsHandlerUtility::polygonForInsertPoint(const QPolygonF &old_ * The points are always based on the top right corner of the rect. * the first point of vector is X the second Y */ -#include QVector QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode) { - Q_UNUSED(mode) QVector 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; - - 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; + 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; + } return v; } @@ -281,35 +292,68 @@ QVector QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rec */ 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())); - } - } - else if (index == 1) //Y - { - if (pos.y() < rect.top()) { return 0; } - else if (pos.y() > rect.center().y()) { - return 100; + } + else + { + 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 (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) { diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index 681f9a647..959e3ff19 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -264,7 +264,7 @@ QPainterPath QetShapeItem::shape() const 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 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti 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::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) 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 @@ bool QetShapeItem::fromXml(const QDomElement &e) 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 @@ QDomElement QetShapeItem::toXml(QDomDocument &document) const 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);