2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2021-02-06 19:00:48 +01:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2007-12-01 10:47:15 +00:00
|
|
|
This file is part of QElectroTech.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2007-12-01 10:47:15 +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
|
|
|
|
2007-12-01 10:47:15 +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
|
|
|
|
2007-12-01 10:47:15 +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/>.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
#include "partpolygon.h"
|
2020-12-09 15:28:43 +01:00
|
|
|
|
2020-12-10 18:44:03 +01:00
|
|
|
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
|
|
|
#include "../../QetGraphicsItemModeler/qetgraphicshandleritem.h"
|
|
|
|
#include "../../QetGraphicsItemModeler/qetgraphicshandlerutility.h"
|
|
|
|
#include "../../qeticons.h"
|
|
|
|
#include "../elementscene.h"
|
2021-02-01 19:52:15 +01:00
|
|
|
#include "../qetelementeditor.h"
|
2007-12-05 21:16:01 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::PartPolygon
|
|
|
|
Constructor
|
|
|
|
@param editor : editor of this item
|
|
|
|
@param parent : parent item
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
PartPolygon::PartPolygon(QETElementEditor *editor, QGraphicsItem *parent) :
|
|
|
|
CustomElementGraphicPart(editor, parent),
|
2015-07-20 17:45:37 +00:00
|
|
|
m_closed(false),
|
2015-07-20 21:06:00 +00:00
|
|
|
m_undo_command(nullptr)
|
2018-06-30 21:41:27 +00:00
|
|
|
{
|
|
|
|
m_insert_point = new QAction(tr("Ajouter un point"), this);
|
|
|
|
m_insert_point->setIcon(QET::Icons::Add);
|
|
|
|
connect(m_insert_point, &QAction::triggered, this, &PartPolygon::insertPoint);
|
|
|
|
m_remove_point = new QAction(tr("Supprimer ce point"), this);
|
|
|
|
m_remove_point->setIcon(QET::Icons::Remove);
|
|
|
|
connect(m_remove_point, &QAction::triggered, this, &PartPolygon::removePoint);
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::~PartPolygon
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
PartPolygon::~PartPolygon()
|
|
|
|
{
|
2015-07-20 21:06:00 +00:00
|
|
|
if(m_undo_command) delete m_undo_command;
|
2017-08-02 15:26:14 +00:00
|
|
|
removeHandler();
|
2015-07-20 21:06:00 +00:00
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::paint
|
|
|
|
Draw this polygon
|
|
|
|
@param painter
|
|
|
|
@param options
|
|
|
|
@param widget
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
|
2007-06-30 17:41:07 +00:00
|
|
|
{
|
2020-10-18 10:45:05 +02:00
|
|
|
Q_UNUSED(widget)
|
2007-06-30 17:41:07 +00:00
|
|
|
|
2015-02-09 08:57:40 +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
|
2015-02-09 08:57:40 +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()) t.setColor(Qt::red);
|
|
|
|
painter -> setPen(t);
|
|
|
|
|
|
|
|
m_closed ? painter -> drawPolygon (m_polygon) :
|
|
|
|
painter -> drawPolyline(m_polygon);
|
|
|
|
|
|
|
|
if (m_hovered)
|
|
|
|
drawShadowShape(painter);
|
2007-12-05 21:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::fromXml
|
|
|
|
Import the properties of this polygon from a xml element
|
|
|
|
@param qde : Xml document to use
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::fromXml(const QDomElement &qde)
|
|
|
|
{
|
2007-06-30 17:41:07 +00:00
|
|
|
stylesFromXml(qde);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2007-06-30 17:41:07 +00:00
|
|
|
int i = 1;
|
2015-02-09 08:57:40 +00:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
if (QET::attributeIsAReal(qde, QString("x%1").arg(i)) &&\
|
|
|
|
QET::attributeIsAReal(qde, QString("y%1").arg(i)))
|
|
|
|
++ i;
|
|
|
|
|
2007-06-30 17:41:07 +00:00
|
|
|
else break;
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2007-06-30 17:41:07 +00:00
|
|
|
QPolygonF temp_polygon;
|
2015-02-09 08:57:40 +00:00
|
|
|
for (int j = 1 ; j < i ; ++ j)
|
|
|
|
{
|
|
|
|
temp_polygon << QPointF(qde.attribute(QString("x%1").arg(j)).toDouble(),
|
|
|
|
qde.attribute(QString("y%1").arg(j)).toDouble());
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
m_polygon = temp_polygon;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2014-05-29 13:46:04 +00:00
|
|
|
m_closed = qde.attribute("closed") != "false";
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::toXml
|
|
|
|
Export this polygin in xml
|
|
|
|
@param xml_document : Xml document to use for create the xml element
|
|
|
|
@return an xml element that describe this polygon
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
const QDomElement PartPolygon::toXml(QDomDocument &xml_document) const
|
|
|
|
{
|
2007-06-30 17:41:07 +00:00
|
|
|
QDomElement xml_element = xml_document.createElement("polygon");
|
|
|
|
int i = 1;
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QPointF point, m_polygon) {
|
2007-07-10 22:54:22 +00:00
|
|
|
point = mapToScene(point);
|
2010-03-28 16:27:48 +00:00
|
|
|
xml_element.setAttribute(QString("x%1").arg(i), QString("%1").arg(point.x()));
|
|
|
|
xml_element.setAttribute(QString("y%1").arg(i), QString("%1").arg(point.y()));
|
2007-06-30 17:41:07 +00:00
|
|
|
++ i;
|
|
|
|
}
|
2014-05-29 13:46:04 +00:00
|
|
|
if (!m_closed) xml_element.setAttribute("closed", "false");
|
2007-06-30 17:41:07 +00:00
|
|
|
stylesToXml(xml_element);
|
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::isUseless
|
|
|
|
@return true if this part is irrelevant and does not deserve to be Retained / registered.
|
|
|
|
A polygon is relevant when he have 2 differents points
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
bool PartPolygon::isUseless() const
|
|
|
|
{
|
|
|
|
if (m_polygon.count() < 2) return(true);
|
2007-06-30 17:41:07 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
for (int i = 1 ; i < m_polygon.count() ; ++ i)
|
|
|
|
if (m_polygon[i] != m_polygon[i-1]) return(false);
|
2007-12-15 21:57:00 +00:00
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
2008-01-07 19:40:08 +00:00
|
|
|
|
2013-02-11 18:35:13 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::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 PartPolygon::sceneGeometricRect() const
|
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
return(mapToScene(m_polygon.boundingRect()).boundingRect());
|
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 PartPolygon::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 PartPolygon::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
|
|
|
saved_points_ = mapToScene(m_polygon).toList();
|
2013-02-08 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::handleUserTransformation
|
|
|
|
Handle the user-induced transformation from initial_selection_rect to new_selection_rect
|
|
|
|
@param initial_selection_rect
|
|
|
|
@param new_selection_rect
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::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_);
|
2015-02-09 08:57:40 +00:00
|
|
|
m_polygon = (mapFromScene(QPolygonF(mapped_points.toVector())));
|
2013-02-08 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 18:51:29 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::preferredScalingMethod
|
|
|
|
This method is called by the decorator when it needs to determine the best
|
|
|
|
way to interactively scale a primitive. It is typically called when only a
|
|
|
|
single primitive is being scaled.
|
|
|
|
@return : This reimplementation systematically returns QET::RoundScaleRatios.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QET::ScalingMethod PartPolygon::preferredScalingMethod() const
|
|
|
|
{
|
2013-03-06 18:51:29 +00:00
|
|
|
return(QET::RoundScaleRatios);
|
|
|
|
}
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::polygon
|
|
|
|
@return the item's polygon, or an empty polygon if no polygon has been set.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QPolygonF PartPolygon::polygon() const
|
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
return m_polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::setPolygon
|
|
|
|
Sets the item's polygon to be the given polygon.
|
|
|
|
@param polygon
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::setPolygon(const QPolygonF &polygon)
|
|
|
|
{
|
|
|
|
if (m_polygon == polygon) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_polygon = polygon;
|
2017-08-02 15:26:14 +00:00
|
|
|
adjusteHandlerPos();
|
2015-07-24 11:11:50 +00:00
|
|
|
emit polygonChanged();
|
2015-02-09 08:57:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 11:14:56 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::addPoint
|
|
|
|
Add new point to polygon
|
|
|
|
@param point
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::addPoint(const QPointF &point)
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_polygon << point;
|
2014-11-08 11:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::setLastPoint
|
2020-08-20 21:57:35 +02:00
|
|
|
Set the last point of polygon to point
|
2020-08-16 11:19:36 +02:00
|
|
|
@param point
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::setLastPoint(const QPointF &point)
|
|
|
|
{
|
|
|
|
if (m_polygon.size())
|
|
|
|
m_polygon.pop_back();
|
2014-11-08 11:14:56 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
prepareGeometryChange();
|
|
|
|
m_polygon << point;
|
2014-11-08 11:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::removeLastPoint
|
|
|
|
Remove the last point of polygon
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartPolygon::removeLastPoint()
|
|
|
|
{
|
|
|
|
if (m_polygon.size())
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_polygon.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 11:11:50 +00:00
|
|
|
void PartPolygon::setClosed(bool close)
|
|
|
|
{
|
|
|
|
if (m_closed == close) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_closed = close;
|
2020-08-16 14:25:31 +02:00
|
|
|
emit closedChange();
|
2019-09-01 20:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::setHandlerColor
|
2020-08-20 21:57:35 +02:00
|
|
|
Set the handler at pos pos (in polygon coordinate) to color color.
|
2020-08-16 11:19:36 +02:00
|
|
|
@param pos
|
|
|
|
@param color
|
|
|
|
*/
|
2019-09-01 20:29:26 +02:00
|
|
|
void PartPolygon::setHandlerColor(QPointF pos, const QColor &color)
|
|
|
|
{
|
2020-07-15 20:20:07 +02:00
|
|
|
for (QetGraphicsHandlerItem *qghi : m_handler_vector) {
|
|
|
|
if (qghi->pos() == mapToScene(pos)) {
|
|
|
|
qghi->setColor(color);
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 20:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::resetAllHandlerColor
|
|
|
|
Reset the color of every handlers
|
2019-09-01 20:29:26 +02:00
|
|
|
*/
|
|
|
|
void PartPolygon::resetAllHandlerColor()
|
|
|
|
{
|
2020-07-15 20:20:07 +02:00
|
|
|
for (QetGraphicsHandlerItem *qghi : m_handler_vector) {
|
|
|
|
qghi->setColor(Qt::blue);
|
|
|
|
}
|
2015-07-24 11:11:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::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 PartPolygon::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, &PartPolygon::sceneSelectionChanged);
|
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
if (scene()->selectedItems().size() == 1)
|
|
|
|
addHandler();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartPolygon::sceneSelectionChanged);
|
|
|
|
removeHandler();
|
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
else if (change == ItemPositionHasChanged)
|
|
|
|
{
|
|
|
|
adjusteHandlerPos();
|
|
|
|
}
|
|
|
|
else if (change == ItemSceneChange)
|
|
|
|
{
|
|
|
|
if(scene())
|
|
|
|
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartPolygon::sceneSelectionChanged);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
setSelected(false); //This is item 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 PartPolygon::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 PartPolygon::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
|
|
|
}
|
|
|
|
|
2018-06-30 21:41:27 +00:00
|
|
|
void PartPolygon::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
m_context_menu_pos = event->pos();
|
|
|
|
event->ignore();
|
|
|
|
if (isSelected() && elementScene() && (elementScene()->behavior() == ElementScene::Normal))
|
|
|
|
{
|
|
|
|
QList<QAction *> list;
|
|
|
|
list << m_insert_point;
|
|
|
|
if (m_handler_vector.count() > 2)
|
|
|
|
{
|
|
|
|
for (QetGraphicsHandlerItem *qghi : m_handler_vector)
|
|
|
|
{
|
|
|
|
if (qghi->contains(qghi->mapFromScene(event->scenePos())))
|
|
|
|
{
|
|
|
|
list << m_remove_point;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elementScene()->editor()->contextMenu(event->screenPos(), list);
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:37 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::adjusteHandlerPos
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartPolygon::adjusteHandlerPos()
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
if(m_handler_vector.isEmpty())
|
|
|
|
return;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
if (m_handler_vector.size() == m_polygon.size())
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
QVector <QPointF> points_vector = mapToScene(m_polygon);
|
|
|
|
for (int i = 0 ; i < points_vector.size() ; ++i)
|
|
|
|
m_handler_vector.at(i)->setPos(points_vector.at(i));
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2018-06-30 21:41:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
|
|
|
addHandler();
|
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::handlerMousePressEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartPolygon::handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
Q_UNUSED(qghi);
|
|
|
|
Q_UNUSED(event);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
m_undo_command = new QPropertyUndoCommand(this, "polygon", QVariant(m_polygon));
|
|
|
|
m_undo_command->setText(tr("Modifier un polygone"));
|
|
|
|
}
|
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 PartPolygon::handlerMouseMoveEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartPolygon::handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
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
|
|
|
prepareGeometryChange();
|
|
|
|
m_polygon.replace(m_vector_index, new_pos);
|
|
|
|
adjusteHandlerPos();
|
|
|
|
emit polygonChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::handlerMouseReleaseEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartPolygon::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(qghi);
|
|
|
|
Q_UNUSED(event);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
m_undo_command->setNewValue(QVariant(m_polygon));
|
|
|
|
elementScene()->undoStack().push(m_undo_command);
|
|
|
|
m_undo_command = nullptr;
|
|
|
|
m_vector_index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::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 PartPolygon::sceneSelectionChanged()
|
|
|
|
{
|
|
|
|
if (this->isSelected() && scene()->selectedItems().size() == 1)
|
|
|
|
addHandler();
|
|
|
|
else
|
|
|
|
removeHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::addHandler
|
|
|
|
Add handlers for this item
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartPolygon::addHandler()
|
|
|
|
{
|
|
|
|
if (m_handler_vector.isEmpty() && scene())
|
2020-10-03 15:48:40 +02:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
m_handler_vector = QetGraphicsHandlerItem::handlerForPoint(mapToScene(m_polygon));
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
for(QetGraphicsHandlerItem *handler : m_handler_vector)
|
|
|
|
{
|
|
|
|
handler->setColor(Qt::blue);
|
|
|
|
scene()->addItem(handler);
|
|
|
|
handler->installSceneEventFilter(this);
|
|
|
|
handler->setZValue(this->zValue()+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::removeHandler
|
|
|
|
Remove the handlers of this item
|
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartPolygon::removeHandler()
|
|
|
|
{
|
|
|
|
if (!m_handler_vector.isEmpty())
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2017-08-02 15:26:14 +00:00
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:41:27 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::insertPoint
|
|
|
|
Insert a point in this polygone
|
|
|
|
*/
|
2018-06-30 21:41:27 +00:00
|
|
|
void PartPolygon::insertPoint()
|
2018-07-03 14:25:46 +00:00
|
|
|
{
|
|
|
|
QPolygonF new_polygon = QetGraphicsHandlerUtility::polygonForInsertPoint(m_polygon, m_closed, elementScene()->snapToGrid(m_context_menu_pos));
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-07-03 14:25:46 +00:00
|
|
|
if(new_polygon != m_polygon)
|
2018-06-30 21:41:27 +00:00
|
|
|
{
|
2018-07-03 14:25:46 +00:00
|
|
|
//Wrap the undo for avoid to merge the undo commands when user add several points.
|
|
|
|
QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone"));
|
|
|
|
new QPropertyUndoCommand(this, "polygon", m_polygon, new_polygon, undo);
|
|
|
|
elementScene()->undoStack().push(undo);
|
2018-06-30 21:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::removePoint
|
|
|
|
remove a point on this polygon
|
|
|
|
*/
|
2018-06-30 21:41:27 +00:00
|
|
|
void PartPolygon::removePoint()
|
|
|
|
{
|
|
|
|
if (m_handler_vector.size() == 2)
|
|
|
|
return;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-06-30 21:41:27 +00:00
|
|
|
QPointF point = mapToScene(m_context_menu_pos);
|
|
|
|
int index = -1;
|
|
|
|
for (int i=0 ; i<m_handler_vector.size() ; i++)
|
|
|
|
{
|
|
|
|
QetGraphicsHandlerItem *qghi = m_handler_vector.at(i);
|
|
|
|
if (qghi->contains(qghi->mapFromScene(point)))
|
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index > -1 && index<m_handler_vector.count())
|
|
|
|
{
|
|
|
|
QPolygonF polygon = this->polygon();
|
2020-08-16 14:25:31 +02:00
|
|
|
qDebug() << index;
|
2018-06-30 21:41:27 +00:00
|
|
|
polygon.removeAt(index);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-06-30 21:41:27 +00:00
|
|
|
//Wrap the undo for avoid to merge the undo commands when user add several points.
|
|
|
|
QUndoCommand *undo = new QUndoCommand(tr("Supprimer un point d'un polygone"));
|
|
|
|
new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
|
|
|
|
elementScene()->undoStack().push(undo);
|
|
|
|
}
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2018-06-30 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::shape
|
|
|
|
@return the shape of this item
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPath PartPolygon::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
|
|
|
shape.addPolygon(m_polygon);
|
|
|
|
|
2015-07-21 12:29:43 +00:00
|
|
|
if (m_closed)
|
|
|
|
shape.lineTo(m_polygon.first());
|
|
|
|
|
|
|
|
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 PartPolygon::shadowShape() const
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
|
|
|
shape.addPolygon(m_polygon);
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
if (m_closed)
|
|
|
|
shape.lineTo(m_polygon.first());
|
2014-11-08 11:14:56 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPathStroker pps;
|
|
|
|
pps.setWidth(penWeight());
|
2014-11-08 11:14:56 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
return (pps.createStroke(shape));
|
2014-11-08 11:14:56 +00:00
|
|
|
}
|
|
|
|
|
2008-01-07 19:40:08 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief PartPolygon::boundingRect
|
|
|
|
@return the bounding rect of this item
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QRectF PartPolygon::boundingRect() const
|
|
|
|
{
|
|
|
|
QRectF r = m_polygon.boundingRect();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-01-07 19:40:08 +00:00
|
|
|
r.adjust(-adjust, -adjust, adjust, adjust);
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2008-01-07 19:40:08 +00:00
|
|
|
return(r);
|
|
|
|
}
|