mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-18 22:00:35 +01:00
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
This commit is contained in:
@@ -250,12 +250,22 @@ QPolygonF QetGraphicsHandlerUtility::polygonForInsertPoint(const QPolygonF &old_
|
|||||||
* The points are always based on the top right corner of the rect.
|
* The points are always based on the top right corner of the rect.
|
||||||
* the first point of vector is X the second Y
|
* 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)
|
QVector<QPointF> QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
|
||||||
{
|
{
|
||||||
Q_UNUSED(mode)
|
|
||||||
QVector<QPointF> v;
|
QVector<QPointF> v;
|
||||||
|
|
||||||
|
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 half_width = rect.width()/2;
|
||||||
qreal x_percent = std::min(xRadius, 100.00)/100;
|
qreal x_percent = std::min(xRadius, 100.00)/100;
|
||||||
QPointF X(rect.right() - half_width*x_percent,
|
QPointF X(rect.right() - half_width*x_percent,
|
||||||
@@ -267,6 +277,7 @@ QVector<QPointF> QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rec
|
|||||||
QPointF Y(rect.right(),
|
QPointF Y(rect.right(),
|
||||||
rect.top()+ half_height*y_percent);
|
rect.top()+ half_height*y_percent);
|
||||||
v << Y;
|
v << Y;
|
||||||
|
}
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@@ -281,8 +292,40 @@ QVector<QPointF> QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rec
|
|||||||
*/
|
*/
|
||||||
qreal QetGraphicsHandlerUtility::radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode)
|
qreal QetGraphicsHandlerUtility::radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode)
|
||||||
{
|
{
|
||||||
Q_UNUSED(mode)
|
if (mode == Qt::AbsoluteSize)
|
||||||
|
{
|
||||||
|
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 (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 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if(index == 0) //X
|
if(index == 0) //X
|
||||||
{
|
{
|
||||||
if (pos.x() < rect.center().x()) {
|
if (pos.x() < rect.center().x()) {
|
||||||
@@ -310,6 +353,7 @@ qreal QetGraphicsHandlerUtility::radiusForPosAtIndex(const QRectF &rect, const Q
|
|||||||
else {
|
else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
qreal QetGraphicsHandlerUtility::percentageInRange(qreal min, qreal max, qreal value) {
|
qreal QetGraphicsHandlerUtility::percentageInRange(qreal min, qreal max, qreal value) {
|
||||||
|
|||||||
@@ -264,7 +264,7 @@ QPainterPath QetShapeItem::shape() const
|
|||||||
path.lineTo(m_P2);
|
path.lineTo(m_P2);
|
||||||
break;
|
break;
|
||||||
case Rectangle:
|
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;
|
break;
|
||||||
case Ellipse:
|
case Ellipse:
|
||||||
path.addEllipse(QRectF(m_P1, m_P2));
|
path.addEllipse(QRectF(m_P1, m_P2));
|
||||||
@@ -319,7 +319,7 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
|
|||||||
switch (m_shapeType)
|
switch (m_shapeType)
|
||||||
{
|
{
|
||||||
case Line: painter->drawLine(QLineF(m_P1, m_P2)); break;
|
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 Ellipse: painter->drawEllipse(QRectF(m_P1, m_P2)); break;
|
||||||
case Polygon: m_closed ? painter->drawPolygon(m_polygon) : painter->drawPolyline(m_polygon); 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)
|
void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||||
{
|
{
|
||||||
event->ignore();
|
event->ignore();
|
||||||
|
QetGraphicsItem::mousePressEvent(event);
|
||||||
|
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
switchResizeMode();
|
switchResizeMode();
|
||||||
event->accept();
|
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_P1.setY(e.attribute("y1", nullptr).toDouble());
|
||||||
m_P2.setX(e.attribute("x2", nullptr).toDouble());
|
m_P2.setX(e.attribute("x2", nullptr).toDouble());
|
||||||
m_P2.setY(e.attribute("y2", 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());
|
m_polygon << QPointF(de.attribute("x", nullptr).toDouble(), de.attribute("y", nullptr).toDouble());
|
||||||
|
}
|
||||||
|
}
|
||||||
setZValue(e.attribute("z", QString::number(this->zValue())).toDouble());
|
setZValue(e.attribute("z", QString::number(this->zValue())).toDouble());
|
||||||
|
|
||||||
return (true);
|
return (true);
|
||||||
@@ -906,11 +913,28 @@ QDomElement QetShapeItem::toXml(QDomDocument &document) const
|
|||||||
result.setAttribute("y1", QString::number(mapToScene(m_P1).y()));
|
result.setAttribute("y1", QString::number(mapToScene(m_P1).y()));
|
||||||
result.setAttribute("x2", QString::number(mapToScene(m_P2).x()));
|
result.setAttribute("x2", QString::number(mapToScene(m_P2).x()));
|
||||||
result.setAttribute("y2", QString::number(mapToScene(m_P2).y()));
|
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
|
else
|
||||||
{
|
{
|
||||||
QDomElement points = document.createElement("points");
|
QDomElement points = document.createElement("points");
|
||||||
foreach(QPointF p, m_polygon)
|
for (QPointF p : m_polygon)
|
||||||
{
|
{
|
||||||
QDomElement point = document.createElement("point");
|
QDomElement point = document.createElement("point");
|
||||||
QPointF pf = mapToScene(p);
|
QPointF pf = mapToScene(p);
|
||||||
|
|||||||
Reference in New Issue
Block a user