2009-04-03 19:30:25 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2009-04-03 19:30:25 +00:00
|
|
|
This file is part of QElectroTech.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "partrectangle.h"
|
2015-07-20 17:45:37 +00:00
|
|
|
#include "elementscene.h"
|
2015-07-20 21:06:00 +00:00
|
|
|
#include "QPropertyUndoCommand/qpropertyundocommand.h"
|
2017-08-02 15:26:14 +00:00
|
|
|
#include "QetGraphicsItemModeler/qetgraphicshandleritem.h"
|
|
|
|
#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
|
2009-04-03 19:30:25 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::PartRectangle
|
|
|
|
Constructor
|
|
|
|
@param editor the QETElementEditor of this item
|
|
|
|
@param parent parent item
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
PartRectangle::PartRectangle(QETElementEditor *editor, QGraphicsItem *parent) :
|
2018-07-17 12:19:56 +00:00
|
|
|
CustomElementGraphicPart(editor, parent)
|
2015-02-09 08:57:40 +00:00
|
|
|
{}
|
2009-04-03 19:30:25 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::~PartRectangle
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
PartRectangle::~PartRectangle()
|
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
removeHandler();
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2009-04-03 19:30:25 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::paint
|
|
|
|
Draw this Rectangle
|
|
|
|
@param painter
|
|
|
|
@param options
|
|
|
|
@param widget
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
|
|
|
|
{
|
2009-11-22 16:12:22 +00:00
|
|
|
Q_UNUSED(widget);
|
2009-04-03 19:30:25 +00:00
|
|
|
applyStylesToQPainter(*painter);
|
|
|
|
QPen t = painter -> pen();
|
2020-10-03 15:48:40 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
|
2009-04-18 18:54:25 +00:00
|
|
|
t.setCosmetic(options && options -> levelOfDetail < 1.0);
|
2020-10-03 15:48:40 +02:00
|
|
|
#else
|
|
|
|
#if TODO_LIST
|
|
|
|
#pragma message("@TODO remove code for QT 6 or later")
|
|
|
|
#endif
|
|
|
|
t.setCosmetic(options && options -> levelOfDetailFromTransform(painter->worldTransform()) < 1.0);
|
|
|
|
#endif
|
2015-02-09 08:57:40 +00:00
|
|
|
if (isSelected())
|
2009-04-03 19:30:25 +00:00
|
|
|
t.setColor(Qt::red);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
t.setJoinStyle(Qt::MiterJoin);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
//Force the pen to width 0 if one of dimension is null
|
|
|
|
if (!rect().width() || !rect().height())
|
2009-04-03 19:30:25 +00:00
|
|
|
t.setWidth(0);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
painter->setPen(t);
|
|
|
|
painter->drawRoundedRect(m_rect, m_xRadius, m_yRadius);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
|
|
|
if (m_hovered)
|
|
|
|
drawShadowShape(painter);
|
|
|
|
|
|
|
|
if (isSelected())
|
|
|
|
drawCross(m_rect.center(), painter);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::toXml
|
|
|
|
Export this rectangle in xml
|
|
|
|
@param xml_document : Xml document to use for create the xml element.
|
|
|
|
@return an xml element that describe this ellipse
|
|
|
|
*/
|
2020-08-24 20:34:18 +02:00
|
|
|
QDomElement PartRectangle::toXml(QDomDocument &xml_document) const
|
2015-02-09 08:57:40 +00:00
|
|
|
{
|
2009-04-03 19:30:25 +00:00
|
|
|
QDomElement xml_element = xml_document.createElement("rect");
|
|
|
|
QPointF top_left(sceneTopLeft());
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
xml_element.appendChild(createXmlProperty(xml_document, "x", top_left.x()));
|
|
|
|
xml_element.appendChild(createXmlProperty(xml_document, "y", top_left.y()));
|
|
|
|
xml_element.appendChild(createXmlProperty(xml_document, "width", m_rect.width()));
|
|
|
|
xml_element.appendChild(createXmlProperty(xml_document, "height", m_rect.height()));
|
2018-07-17 12:19:56 +00:00
|
|
|
QRectF rect = m_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;
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
xml_element.setAttribute("rx", QString::number(m_xRadius));
|
|
|
|
xml_element.setAttribute("ry", QString::number(m_yRadius));
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
xml_element.appendChild(createXmlProperty(xml_document, "rx", m_xRadius));
|
|
|
|
xml_element.appendChild(createXmlProperty(xml_document, "ry", m_yRadius));
|
|
|
|
|
|
|
|
stylesToXml(xml_document, xml_element);
|
2009-04-03 19:30:25 +00:00
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::fromXml
|
|
|
|
Import the properties of this rectangle from a xml element.
|
|
|
|
@param qde : Xml document to use.
|
|
|
|
*/
|
2020-08-24 20:34:18 +02:00
|
|
|
bool PartRectangle::fromXml(const QDomElement &qde)
|
2015-02-09 08:57:40 +00:00
|
|
|
{
|
2009-04-03 19:30:25 +00:00
|
|
|
stylesFromXml(qde);
|
2015-07-20 17:45:37 +00:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
double x=0, y=0, w=0, h=0, rx=0, ry=0;
|
|
|
|
if (propertyDouble(qde, "x", &x) == PropertyFlags::NoValidConversion ||
|
|
|
|
propertyDouble(qde, "y", &y) == PropertyFlags::NoValidConversion)
|
|
|
|
return false;
|
2020-08-24 20:34:18 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
setPos(mapFromScene(x, y));
|
2020-08-24 20:34:18 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
if (propertyDouble(qde, "width", &w) == PropertyFlags::NoValidConversion ||
|
|
|
|
propertyDouble(qde, "width", &h) == PropertyFlags::NoValidConversion)
|
|
|
|
return false;
|
2020-08-24 20:34:18 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
QRectF rect(QPointF(x,y), QSizeF(w, h));
|
2015-07-20 17:45:37 +00:00
|
|
|
|
|
|
|
setRect(rect.normalized());
|
2020-08-24 20:34:18 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
if (propertyDouble(qde, "rx", &rx) == PropertyFlags::NoValidConversion ||
|
|
|
|
propertyDouble(qde, "ry", &ry) == PropertyFlags::NoValidConversion)
|
|
|
|
return false;
|
2020-08-24 20:34:18 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
setXRadius(rx);
|
|
|
|
setYRadius(ry);
|
2020-08-24 20:34:18 +02:00
|
|
|
|
2020-10-13 21:51:34 +02:00
|
|
|
return true;
|
2020-08-25 20:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PartRectangle::valideXml(QDomElement& element) {
|
2020-10-13 21:51:34 +02:00
|
|
|
// parameters have default values so no value is not a non valid xml element
|
|
|
|
if ((propertyDouble(element, "x") & PropertyFlags::NoValidConversion) |
|
|
|
|
(propertyDouble(element, "y") & PropertyFlags::NoValidConversion) |
|
|
|
|
(propertyDouble(element, "width") & PropertyFlags::NoValidConversion) |
|
|
|
|
(propertyDouble(element, "width") & PropertyFlags::NoValidConversion) |
|
|
|
|
(propertyDouble(element, "rx") & PropertyFlags::NoValidConversion) |
|
|
|
|
(propertyDouble(element, "ry") & PropertyFlags::NoValidConversion))
|
|
|
|
return false;
|
|
|
|
return true;
|
2015-02-09 08:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::rect
|
|
|
|
@return : Returns the item's rectangle.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QRectF PartRectangle::rect() const
|
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
return m_rect;
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::setRect
|
|
|
|
Sets the item's rectangle to be the given rectangle.
|
|
|
|
@param rect
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartRectangle::setRect(const QRectF &rect)
|
|
|
|
{
|
|
|
|
if (rect == m_rect) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_rect = rect;
|
2017-08-02 15:26:14 +00:00
|
|
|
adjusteHandlerPos();
|
2015-07-22 13:19:43 +00:00
|
|
|
emit rectChanged();
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
void PartRectangle::setXRadius(qreal X)
|
|
|
|
{
|
|
|
|
m_xRadius = X;
|
|
|
|
update();
|
|
|
|
adjusteHandlerPos();
|
|
|
|
emit XRadiusChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartRectangle::setYRadius(qreal Y)
|
|
|
|
{
|
|
|
|
m_yRadius = Y;
|
|
|
|
update();
|
|
|
|
adjusteHandlerPos();
|
|
|
|
emit YRadiusChanged();
|
|
|
|
}
|
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::sceneGeometricRect
|
|
|
|
@return the minimum, margin-less rectangle this part can fit into, in scene
|
|
|
|
coordinates. It is different from boundingRect() because it is not supposed
|
|
|
|
to imply any margin, and it is different from shape because it is a regular
|
|
|
|
rectangle, not a complex shape.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QRectF PartRectangle::sceneGeometricRect() const
|
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
return(mapToScene(rect()).boundingRect());
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::sceneTopLeft
|
|
|
|
@return the top left of rectangle, in scene coordinate
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QPointF PartRectangle::sceneTopLeft() const
|
|
|
|
{
|
2009-04-03 19:30:25 +00:00
|
|
|
return(mapToScene(rect().topLeft()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::shape
|
|
|
|
@return the shape of this item
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPath PartRectangle::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
2018-07-17 12:19:56 +00:00
|
|
|
shape.addRoundedRect(m_rect, m_xRadius, m_yRadius);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2015-07-21 12:29:43 +00:00
|
|
|
QPainterPathStroker pps;
|
2015-08-03 17:26:57 +00:00
|
|
|
pps.setWidth(m_hovered? penWeight()+SHADOWS_HEIGHT : penWeight());
|
2015-07-21 12:29:43 +00:00
|
|
|
shape = pps.createStroke(shape);
|
|
|
|
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath PartRectangle::shadowShape() const
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
2018-07-17 12:19:56 +00:00
|
|
|
shape.addRoundedRect(m_rect, m_xRadius, m_yRadius);
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPathStroker pps;
|
|
|
|
pps.setWidth(penWeight());
|
|
|
|
|
|
|
|
return (pps.createStroke(shape));
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-11 18:35:13 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::boundingRect
|
|
|
|
@return Bounding rectangle this part can fit into
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QRectF PartRectangle::boundingRect() const
|
|
|
|
{
|
|
|
|
qreal adjust = (SHADOWS_HEIGHT + penWeight()) / 2;
|
|
|
|
//We add 0.5 because CustomElementGraphicPart::drawShadowShape
|
|
|
|
//draw a shape bigger of 0.5 when pen weight is to 0.
|
|
|
|
if (penWeight() == 0) adjust += 0.5;
|
|
|
|
|
|
|
|
QRectF r = m_rect.normalized();
|
|
|
|
r.adjust(-adjust, -adjust, adjust, adjust);
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
return(r);
|
2013-02-11 18:35:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:12 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::isUseless
|
|
|
|
@return true if this part is irrelevant and does not deserve to be Retained / registered.
|
|
|
|
An rectangle is relevant when he's not null.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
bool PartRectangle::isUseless() const
|
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
return(rect().isNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::startUserTransformation
|
|
|
|
Start the user-induced transformation, provided this primitive is contained
|
|
|
|
within the initial_selection_rect bounding rectangle.
|
|
|
|
@param initial_selection_rect
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartRectangle::startUserTransformation(const QRectF &initial_selection_rect)
|
|
|
|
{
|
2013-02-08 22:05:12 +00:00
|
|
|
Q_UNUSED(initial_selection_rect)
|
2015-02-09 08:57:40 +00:00
|
|
|
// we keep track of our own rectangle at the moment in scene coordinates too
|
2013-02-08 22:05:12 +00:00
|
|
|
saved_points_.clear();
|
|
|
|
saved_points_ << mapToScene(rect().topLeft()) << mapToScene(rect().bottomRight());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::handleUserTransformation
|
|
|
|
Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
|
|
|
|
@param initial_selection_rect
|
|
|
|
@param new_selection_rect
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartRectangle::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect)
|
|
|
|
{
|
2013-02-08 22:05:12 +00:00
|
|
|
QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
|
|
|
|
setRect(QRectF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1))));
|
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::mouseReleaseEvent
|
|
|
|
Handle mouse release event
|
|
|
|
@param event
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton && event->buttonDownPos(Qt::LeftButton) == event->pos())
|
|
|
|
switchResizeMode();
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
CustomElementGraphicPart::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::itemChange
|
|
|
|
@param change
|
|
|
|
@param value
|
2020-10-03 15:48:40 +02:00
|
|
|
@return
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
QVariant PartRectangle::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
2015-08-03 17:26:57 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
if (change == ItemSelectedHasChanged && scene())
|
2015-08-03 17:26:57 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
if (value.toBool() == true)
|
|
|
|
{
|
|
|
|
//When item is selected, he must to be up to date whene the selection in the scene change, for display or not the handler,
|
|
|
|
//according to the number of selected items.
|
2020-10-03 15:48:40 +02:00
|
|
|
connect(scene(), &QGraphicsScene::selectionChanged, this, &PartRectangle::sceneSelectionChanged);
|
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
if (scene()->selectedItems().size() == 1)
|
|
|
|
addHandler();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartRectangle::sceneSelectionChanged);
|
|
|
|
removeHandler();
|
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
else if (change == ItemPositionHasChanged)
|
2015-08-03 17:26:57 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
adjusteHandlerPos();
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
else if (change == ItemSceneChange)
|
|
|
|
{
|
|
|
|
if(scene())
|
|
|
|
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartRectangle::sceneSelectionChanged);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
setSelected(false); //This item is removed from scene, then we deselect this, and so, the handlers is also removed.
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
return QGraphicsItem::itemChange(change, value);
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:37 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::sceneEventFilter
|
|
|
|
@param watched
|
|
|
|
@param event
|
2020-10-03 15:48:40 +02:00
|
|
|
@return
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
bool PartRectangle::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
//Watched must be an handler
|
|
|
|
if(watched->type() == QetGraphicsHandlerItem::Type)
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2018-07-30 15:24:29 +00:00
|
|
|
QetGraphicsHandlerItem *qghi = qgraphicsitem_cast<QetGraphicsHandlerItem *>(watched);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
if(m_handler_vector.contains(qghi)) //Handler must be in m_vector_index, then we can start resize
|
2015-07-20 21:06:00 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
m_vector_index = m_handler_vector.indexOf(qghi);
|
|
|
|
if (m_vector_index != -1)
|
2015-08-03 17:26:57 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
if(event->type() == QEvent::GraphicsSceneMousePress) //Click
|
|
|
|
{
|
|
|
|
handlerMousePressEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event->type() == QEvent::GraphicsSceneMouseMove) //Move
|
|
|
|
{
|
|
|
|
handlerMouseMoveEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::GraphicsSceneMouseRelease) //Release
|
|
|
|
{
|
|
|
|
handlerMouseReleaseEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
2015-07-20 21:06:00 +00:00
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
return false;
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::switchResizeMode
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::switchResizeMode()
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
if (m_resize_mode == 1)
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
m_resize_mode = 2;
|
|
|
|
for (QetGraphicsHandlerItem *qghi : m_handler_vector)
|
|
|
|
qghi->setColor(Qt::darkGreen);
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2018-07-17 12:19:56 +00:00
|
|
|
else if (m_resize_mode == 2)
|
|
|
|
{
|
|
|
|
m_resize_mode = 3;
|
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
|
|
|
addHandler();
|
|
|
|
for (QetGraphicsHandlerItem *qghi : m_handler_vector) {
|
|
|
|
qghi->setColor(Qt::magenta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_resize_mode == 3)
|
2017-08-02 15:26:14 +00:00
|
|
|
{
|
|
|
|
m_resize_mode = 1;
|
2018-07-17 12:19:56 +00:00
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
|
|
|
addHandler();
|
|
|
|
for (QetGraphicsHandlerItem *qghi : m_handler_vector) {
|
2017-08-02 15:26:14 +00:00
|
|
|
qghi->setColor(Qt::blue);
|
2018-07-17 12:19:56 +00:00
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::adjusteHandlerPos
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::adjusteHandlerPos()
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2018-07-17 12:19:56 +00:00
|
|
|
if (m_handler_vector.isEmpty()) {
|
2017-08-02 15:26:14 +00:00
|
|
|
return;
|
2018-07-17 12:19:56 +00:00
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
QVector <QPointF> points_vector;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
if(m_resize_mode != 3) {
|
|
|
|
points_vector = QetGraphicsHandlerUtility::pointsForRect(m_rect);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
points_vector = QetGraphicsHandlerUtility::pointForRadiusRect(m_rect, m_xRadius, m_yRadius);
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
if (m_handler_vector.size() == points_vector.size())
|
|
|
|
{
|
|
|
|
points_vector = mapToScene(points_vector);
|
|
|
|
for (int i = 0 ; i < points_vector.size() ; ++i)
|
|
|
|
m_handler_vector.at(i)->setPos(points_vector.at(i));
|
2016-07-19 17:12:30 +00:00
|
|
|
}
|
2018-07-17 12:19:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
|
|
|
addHandler();
|
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::handlerMousePressEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2018-07-17 12:19:56 +00:00
|
|
|
Q_UNUSED(qghi)
|
|
|
|
Q_UNUSED(event)
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
m_old_rect = m_rect;
|
|
|
|
m_old_xRadius = m_xRadius;
|
|
|
|
m_old_yRadius = m_yRadius;
|
|
|
|
if(m_xRadius == 0 && m_yRadius == 0) {
|
|
|
|
m_modifie_radius_equaly = true;
|
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::handlerMouseMoveEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2018-07-17 12:19:56 +00:00
|
|
|
Q_UNUSED(qghi)
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
QPointF new_pos = event->scenePos();
|
|
|
|
if (event->modifiers() != Qt::ControlModifier)
|
|
|
|
new_pos = elementScene()->snapToGrid(event->scenePos());
|
|
|
|
new_pos = mapFromScene(new_pos);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
if (m_resize_mode == 1)
|
|
|
|
setRect(QetGraphicsHandlerUtility::rectForPosAtIndex(m_rect, new_pos, m_vector_index));
|
2018-07-17 12:19:56 +00:00
|
|
|
else if (m_resize_mode == 2)
|
2017-08-02 15:26:14 +00:00
|
|
|
setRect(QetGraphicsHandlerUtility::mirrorRectForPosAtIndex(m_rect, new_pos, m_vector_index));
|
2018-07-17 12:19:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qreal radius = QetGraphicsHandlerUtility::radiusForPosAtIndex(m_rect, new_pos, m_vector_index);
|
|
|
|
if(m_modifie_radius_equaly) {
|
|
|
|
setXRadius(radius);
|
|
|
|
setYRadius(radius);
|
|
|
|
}
|
|
|
|
else if(m_vector_index == 0) {
|
|
|
|
setXRadius(radius);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setYRadius(radius);
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
adjusteHandlerPos();
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2016-07-19 17:12:30 +00:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
2016-07-19 17:12:30 +00:00
|
|
|
{
|
2018-07-17 12:19:56 +00:00
|
|
|
Q_UNUSED(qghi)
|
|
|
|
Q_UNUSED(event)
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
m_modifie_radius_equaly = false;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
QUndoCommand *undo = new QUndoCommand("Modifier un rectangle");
|
|
|
|
if (m_old_rect != m_rect) {
|
|
|
|
QPropertyUndoCommand *u = new QPropertyUndoCommand(this, "rect", QVariant(m_old_rect.normalized()), QVariant(m_rect.normalized()), undo);
|
|
|
|
u->setAnimated(true, false);
|
|
|
|
}
|
|
|
|
if (m_old_xRadius != m_xRadius) {
|
|
|
|
QPropertyUndoCommand *u = new QPropertyUndoCommand(this, "xRadius", QVariant(m_old_xRadius), QVariant(m_xRadius), undo);
|
|
|
|
u->setAnimated();
|
|
|
|
}
|
|
|
|
if (m_old_yRadius != m_yRadius) {
|
|
|
|
QPropertyUndoCommand *u = new QPropertyUndoCommand(this, "yRadius", QVariant(m_old_yRadius), QVariant(m_yRadius), undo);
|
|
|
|
u->setAnimated();
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
elementScene()->undoStack().push(undo);
|
2017-08-02 15:26:14 +00:00
|
|
|
m_vector_index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::sceneSelectionChanged
|
|
|
|
When the scene selection change, if there are several primitive selected, we remove the handler of this item
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::sceneSelectionChanged()
|
|
|
|
{
|
|
|
|
if (this->isSelected() && scene()->selectedItems().size() == 1)
|
|
|
|
addHandler();
|
|
|
|
else
|
|
|
|
removeHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::addHandler
|
|
|
|
Add handlers for this item
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::addHandler()
|
|
|
|
{
|
|
|
|
if (m_handler_vector.isEmpty() && scene())
|
2020-10-03 15:48:40 +02:00
|
|
|
{
|
2018-07-17 12:19:56 +00:00
|
|
|
if (m_resize_mode != 3) {
|
|
|
|
m_handler_vector = QetGraphicsHandlerItem::handlerForPoint(mapToScene(QetGraphicsHandlerUtility::pointsForRect(m_rect)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_handler_vector = QetGraphicsHandlerItem::handlerForPoint(mapToScene(QetGraphicsHandlerUtility::pointForRadiusRect(m_rect, m_xRadius, m_yRadius)));
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-17 12:19:56 +00:00
|
|
|
for (QetGraphicsHandlerItem *handler : m_handler_vector)
|
2017-08-02 15:26:14 +00:00
|
|
|
{
|
2018-07-17 12:19:56 +00:00
|
|
|
QColor color;
|
|
|
|
if(m_resize_mode == 1) {color = Qt::blue;}
|
|
|
|
else if (m_resize_mode == 2) {color = Qt::darkGreen;}
|
|
|
|
else {color = Qt::magenta;}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
handler->setColor(color);
|
|
|
|
scene()->addItem(handler);
|
|
|
|
handler->installSceneEventFilter(this);
|
|
|
|
handler->setZValue(this->zValue()+1);
|
|
|
|
}
|
2016-07-19 17:12:30 +00:00
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartRectangle::removeHandler
|
|
|
|
Remove the handlers of this item
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartRectangle::removeHandler()
|
|
|
|
{
|
|
|
|
if (!m_handler_vector.isEmpty())
|
|
|
|
{
|
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
2016-07-19 17:12:30 +00:00
|
|
|
}
|
|
|
|
}
|