2014-09-24 09:38:16 +00:00
|
|
|
/*
|
2025-01-04 13:37:40 +01:00
|
|
|
Copyright 2006-2025 The QElectroTech Team
|
2014-09-24 09:38:16 +00:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
#include "diagrameventaddshape.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
|
|
|
|
#include "../diagram.h"
|
2021-02-07 16:20:55 +01:00
|
|
|
#include "../undocommand/addgraphicsobjectcommand.h"
|
2014-09-24 09:38:16 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::DiagramEventAddShape
|
|
|
|
Default constructor
|
|
|
|
@param diagram : the diagram where this event must operate
|
|
|
|
@param shape_type : the type of shape to draw
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
DiagramEventAddShape::DiagramEventAddShape(Diagram *diagram, QetShapeItem::ShapeType shape_type) :
|
|
|
|
DiagramEventInterface(diagram),
|
2014-09-24 09:38:16 +00:00
|
|
|
m_shape_type (shape_type),
|
2014-09-28 11:42:08 +00:00
|
|
|
m_shape_item (nullptr),
|
|
|
|
m_help_horiz (nullptr),
|
|
|
|
m_help_verti (nullptr)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
m_running = true;
|
|
|
|
init();
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::~DiagramEventAddShape
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
DiagramEventAddShape::~DiagramEventAddShape()
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
if ((m_running || m_abort) && m_shape_item)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
m_diagram->removeItem(m_shape_item);
|
2014-09-24 09:38:16 +00:00
|
|
|
delete m_shape_item;
|
|
|
|
}
|
2014-09-28 11:42:08 +00:00
|
|
|
delete m_help_horiz;
|
|
|
|
delete m_help_verti;
|
2015-11-07 12:28:43 +00:00
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QGraphicsView *v, m_diagram->views())
|
2015-11-07 12:28:43 +00:00
|
|
|
v->setContextMenuPolicy(Qt::DefaultContextMenu);
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::mousePressEvent
|
|
|
|
Action when mouse is pressed
|
|
|
|
@param event : event of mouse press
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddShape::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2019-01-14 18:25:43 +00:00
|
|
|
if (Q_UNLIKELY(m_diagram->isReadOnly())) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
QPointF pos = event->scenePos();
|
2019-01-14 18:25:43 +00:00
|
|
|
if (event->modifiers() != Qt::ControlModifier) {
|
2015-07-13 12:01:02 +00:00
|
|
|
pos = Diagram::snapToGrid(pos);
|
2019-01-14 18:25:43 +00:00
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
|
2014-12-29 23:18:00 +00:00
|
|
|
//Action for left mouse click
|
2015-11-07 12:28:43 +00:00
|
|
|
if (event->button() == Qt::LeftButton)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
|
|
|
//Create shape item
|
|
|
|
if (!m_shape_item)
|
|
|
|
{
|
|
|
|
m_shape_item = new QetShapeItem(pos, pos, m_shape_type);
|
2015-11-07 12:28:43 +00:00
|
|
|
m_diagram->addItem (m_shape_item);
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
|
|
|
return;
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
|
2014-12-29 23:18:00 +00:00
|
|
|
//If current item isn't a polyline, add it with an undo command
|
2015-07-27 21:47:41 +00:00
|
|
|
if (m_shape_type != QetShapeItem::Polygon)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
m_shape_item->setP2 (pos);
|
2019-01-14 18:25:43 +00:00
|
|
|
if (m_shape_item->shapeType() == QetShapeItem::Rectangle || m_shape_item->shapeType() == QetShapeItem::Ellipse) {
|
2018-07-12 10:01:34 +00:00
|
|
|
m_shape_item->setRect(m_shape_item->rect().normalized());
|
|
|
|
}
|
2021-02-07 16:20:55 +01:00
|
|
|
m_diagram->undoStack().push (new AddGraphicsObjectCommand(m_shape_item, m_diagram));
|
2014-12-29 23:18:00 +00:00
|
|
|
m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic
|
|
|
|
}
|
|
|
|
//Else add a new point to polyline
|
|
|
|
else
|
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
m_shape_item->setNextPoint (pos);
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
|
|
|
return;
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 18:25:43 +00:00
|
|
|
if (event->button() == Qt::RightButton) {
|
|
|
|
event->setAccepted(true);
|
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::mouseMoveEvent
|
|
|
|
Action when mouse move
|
|
|
|
@param event : event of mouse move
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddShape::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
updateHelpCross(event->scenePos());
|
2014-12-29 23:18:00 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
if (m_shape_item && event->buttons() == Qt::NoButton)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
QPointF pos = event->scenePos();
|
2019-01-14 18:25:43 +00:00
|
|
|
if (event->modifiers() != Qt::ControlModifier) {
|
2015-07-13 12:01:02 +00:00
|
|
|
pos = Diagram::snapToGrid(pos);
|
2019-01-14 18:25:43 +00:00
|
|
|
}
|
2015-07-13 12:01:02 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
m_shape_item->setP2 (pos);
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::mouseReleaseEvent
|
|
|
|
Action when mouse button is released
|
|
|
|
@param event : event of mouse release
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddShape::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
if (event->button() == Qt::RightButton)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
|
|
|
//If shape is created, we manage right click
|
|
|
|
if (m_shape_item)
|
|
|
|
{
|
|
|
|
//Shape is a polyline and have three points or more we just remove the last point
|
2015-11-07 12:28:43 +00:00
|
|
|
if (m_shape_type == QetShapeItem::Polygon && (m_shape_item->pointsCount() >= 3) )
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
m_shape_item->removePoints();
|
2015-07-13 12:01:02 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
QPointF pos = event->scenePos();
|
2015-07-13 12:01:02 +00:00
|
|
|
if (event->modifiers() != Qt::ControlModifier)
|
|
|
|
pos = Diagram::snapToGrid(pos);
|
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
m_shape_item->setP2(pos); //Set the new last point under the cursor
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
|
|
|
return;
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//For other case, we remove item from scene
|
2015-11-07 12:28:43 +00:00
|
|
|
m_diagram->removeItem(m_shape_item);
|
2014-12-29 23:18:00 +00:00
|
|
|
delete m_shape_item;
|
|
|
|
m_shape_item = nullptr;
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
|
|
|
return;
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Else (no shape), we set to false the running status
|
|
|
|
//for indicate to the owner of this event that everything is done
|
2014-09-24 09:38:16 +00:00
|
|
|
m_running = false;
|
2015-09-19 13:27:06 +00:00
|
|
|
emit finish();
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::mouseDoubleClickEvent
|
|
|
|
Action when mouse button is double clicked
|
|
|
|
@param event : event of mouse double click
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddShape::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
|
|
|
//If current item is a polyline, add it with an undo command
|
2015-11-07 12:28:43 +00:00
|
|
|
if (m_shape_item && m_shape_type == QetShapeItem::Polygon && event->button() == Qt::LeftButton)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-07-13 12:01:02 +00:00
|
|
|
//<double clic is used to finish polyline, but they also add two points at the same pos
|
|
|
|
//<(double clic is a double press event), so we remove the last point of polyline
|
|
|
|
m_shape_item->removePoints();
|
2019-05-16 17:38:13 +00:00
|
|
|
|
|
|
|
//If the last is at the same pos of the first point
|
|
|
|
//that mean user want a closed polygon, so we remove the last point and close polygon
|
|
|
|
QPolygonF polygon = m_shape_item->polygon();
|
|
|
|
if (polygon.first() == polygon.last())
|
|
|
|
{
|
|
|
|
m_shape_item->removePoints();
|
|
|
|
m_shape_item->setClosed(true);
|
|
|
|
}
|
2021-02-07 16:20:55 +01:00
|
|
|
m_diagram->undoStack().push (new AddGraphicsObjectCommand(m_shape_item, m_diagram));
|
2014-12-29 23:18:00 +00:00
|
|
|
m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
2014-12-29 23:18:00 +00:00
|
|
|
}
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
2014-09-28 11:42:08 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
void DiagramEventAddShape::init()
|
|
|
|
{
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QGraphicsView *v, m_diagram->views())
|
2015-11-07 12:28:43 +00:00
|
|
|
v->setContextMenuPolicy(Qt::NoContextMenu);
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:42:08 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddShape::updateHelpCross
|
|
|
|
Create and update the position of the cross to help user for draw new shape
|
|
|
|
@param p : the center of the cross
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
void DiagramEventAddShape::updateHelpCross(const QPointF &p)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
|
|
|
//If line isn't created yet, we create it.
|
|
|
|
if (!m_help_horiz || !m_help_verti)
|
|
|
|
{
|
2014-09-28 11:42:08 +00:00
|
|
|
QPen pen;
|
2014-11-12 14:44:50 +00:00
|
|
|
pen.setWidthF(0.4);
|
|
|
|
pen.setCosmetic(true);
|
2015-06-28 12:37:10 +00:00
|
|
|
pen.setColor(Diagram::background_color == Qt::darkGray ? Qt::lightGray : Qt::darkGray);
|
2014-12-29 23:18:00 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
QRectF rect = m_diagram->border_and_titleblock.insideBorderRect();
|
2015-03-09 10:16:20 +00:00
|
|
|
|
2014-12-29 23:18:00 +00:00
|
|
|
if (!m_help_horiz)
|
|
|
|
{
|
2015-03-09 10:16:20 +00:00
|
|
|
m_help_horiz = new QGraphicsLineItem(rect.topLeft().x(), 0, rect.topRight().x(), 0);
|
2015-11-07 12:28:43 +00:00
|
|
|
m_help_horiz->setPen(pen);
|
|
|
|
m_diagram->addItem(m_help_horiz);
|
2014-09-28 11:42:08 +00:00
|
|
|
}
|
2014-12-29 23:18:00 +00:00
|
|
|
|
|
|
|
if (!m_help_verti)
|
|
|
|
{
|
2015-03-09 10:16:20 +00:00
|
|
|
m_help_verti = new QGraphicsLineItem(0, rect.topLeft().y(), 0, rect.bottomLeft().y());
|
2015-11-07 12:28:43 +00:00
|
|
|
m_help_verti->setPen(pen);
|
|
|
|
m_diagram->addItem(m_help_verti);
|
2014-09-28 11:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 23:18:00 +00:00
|
|
|
//Update the position of the cross
|
2015-11-07 12:28:43 +00:00
|
|
|
QPointF point = Diagram::snapToGrid(p);
|
2014-09-28 11:42:08 +00:00
|
|
|
|
|
|
|
m_help_horiz->setY(point.y());
|
|
|
|
m_help_verti->setX(point.x());
|
|
|
|
}
|