2014-09-24 09:38:16 +00:00
|
|
|
/*
|
2016-05-13 17:40:36 +00:00
|
|
|
Copyright 2006-2016 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"
|
2014-09-24 09:38:16 +00:00
|
|
|
#include "diagram.h"
|
|
|
|
#include "diagramcommands.h"
|
|
|
|
|
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::DiagramEventAddShape
|
2014-09-24 09:38:16 +00:00
|
|
|
* Default constructor
|
2015-11-07 12:28:43 +00:00
|
|
|
* @param diagram : the diagram where this event must operate
|
|
|
|
* @param shape_type : the type of shape to draw
|
2014-09-24 09:38:16 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::~DiagramEventAddShape
|
2014-09-24 09:38:16 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
foreach (QGraphicsView *v, m_diagram->views())
|
|
|
|
v->setContextMenuPolicy(Qt::DefaultContextMenu);
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::mousePressEvent
|
2014-09-24 09:38:16 +00:00
|
|
|
* Action when mouse is pressed
|
|
|
|
* @param event : event of mouse press
|
|
|
|
* @return : true if this event is managed, otherwise false
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
bool DiagramEventAddShape::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2014-12-29 23:18:00 +00:00
|
|
|
{
|
2015-11-07 12:28:43 +00:00
|
|
|
if (Q_UNLIKELY(m_diagram->isReadOnly())) return false;
|
2014-09-24 09:38:16 +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);
|
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);
|
2014-12-29 23:18:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
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);
|
|
|
|
m_diagram->undoStack().push (new AddItemCommand<QetShapeItem *> (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
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
if (event->button() == Qt::RightButton)
|
2014-09-24 09:38:16 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2015-11-07 12:28:43 +00:00
|
|
|
|
2014-09-24 09:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::mouseMoveEvent
|
2014-09-24 09:38:16 +00:00
|
|
|
* Action when mouse move
|
|
|
|
* @param event : event of mouse move
|
|
|
|
* @return : true if this event is managed, otherwise false
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
bool 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();
|
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);
|
2014-09-24 09:38:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-29 23:18:00 +00:00
|
|
|
|
2014-09-24 09:38:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::mouseReleaseEvent
|
2014-09-24 09:38:16 +00:00
|
|
|
* Action when mouse button is released
|
|
|
|
* @param event : event of mouse release
|
|
|
|
* @return : true if this event is managed, otherwise false
|
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
bool 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
|
2014-12-29 23:18:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//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;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//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();
|
2014-09-24 09:38:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-29 23:18:00 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::mouseDoubleClickEvent
|
|
|
|
* Action when mouse button is double clicked
|
|
|
|
* @param event : event of mouse double click
|
|
|
|
* @return : true if this event is managed, otherwise false
|
2014-12-29 23:18:00 +00:00
|
|
|
*/
|
2015-11-07 12:28:43 +00:00
|
|
|
bool 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();
|
2015-11-07 12:28:43 +00:00
|
|
|
m_diagram->undoStack().push (new AddItemCommand<QetShapeItem *> (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
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-24 09:38:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-28 11:42:08 +00:00
|
|
|
|
2015-11-07 12:28:43 +00:00
|
|
|
void DiagramEventAddShape::init()
|
|
|
|
{
|
|
|
|
foreach (QGraphicsView *v, m_diagram->views())
|
|
|
|
v->setContextMenuPolicy(Qt::NoContextMenu);
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:42:08 +00:00
|
|
|
/**
|
2015-11-07 12:28:43 +00:00
|
|
|
* @brief DiagramEventAddShape::updateHelpCross
|
2014-09-28 11:42:08 +00:00
|
|
|
* Create and update the position of the cross to help user for draw new shape
|
2015-11-07 12:28:43 +00:00
|
|
|
* @param p : the center of the cross
|
2014-09-28 11:42:08 +00:00
|
|
|
*/
|
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());
|
|
|
|
}
|