2015-03-04 21:13:13 +00:00
|
|
|
/*
|
2021-02-06 19:00:48 +01:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2015-03-04 21:13:13 +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/>.
|
|
|
|
*/
|
|
|
|
#include "diagrameventaddelement.h"
|
|
|
|
|
2020-12-08 19:57:35 +01:00
|
|
|
#include "../conductorautonumerotation.h"
|
|
|
|
#include "../diagram.h"
|
|
|
|
#include "../diagramcommands.h"
|
2020-12-10 21:19:45 +01:00
|
|
|
#include "../factory/elementfactory.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
#include "../qetgraphicsitem/element.h"
|
2015-03-04 21:13:13 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::DiagramEventAddElement
|
|
|
|
Defaut constructor
|
|
|
|
@param location :location of diagram
|
|
|
|
@param diagram : diagram owner of this event
|
|
|
|
@param pos : first pos of item ( optional, by defaut QPointF(0,0) )
|
|
|
|
*/
|
2015-03-04 21:13:13 +00:00
|
|
|
DiagramEventAddElement::DiagramEventAddElement(ElementsLocation &location, Diagram *diagram, QPointF pos) :
|
|
|
|
DiagramEventInterface(diagram),
|
|
|
|
m_location(location),
|
|
|
|
m_element(nullptr)
|
|
|
|
{
|
|
|
|
//Check if there is an element at this location
|
2016-03-29 17:23:58 +00:00
|
|
|
if (location.isElement() && location.exist())
|
2015-03-04 21:13:13 +00:00
|
|
|
{
|
|
|
|
//location is an element, we build it, if build fail,
|
|
|
|
//m_running stay to false (by default), so this interface will be deleted at next event
|
|
|
|
if (buildElement())
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
m_element -> setPos(pos);
|
|
|
|
m_element -> displayHelpLine(true);
|
2018-01-08 23:50:45 +00:00
|
|
|
m_element -> setFlag(QGraphicsItem::ItemIsSelectable, false);
|
2015-03-04 21:13:13 +00:00
|
|
|
m_diagram -> addItem(m_element);
|
|
|
|
m_running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::~DiagramEventAddElement
|
|
|
|
Destructor
|
|
|
|
Enable context menu for each view of diagram
|
|
|
|
*/
|
2015-03-04 21:13:13 +00:00
|
|
|
DiagramEventAddElement::~DiagramEventAddElement()
|
|
|
|
{
|
2020-07-01 18:31:19 +02:00
|
|
|
if (m_element)
|
|
|
|
{
|
|
|
|
m_diagram->removeItem(m_element);
|
|
|
|
m_element->deleteLater();
|
|
|
|
}
|
|
|
|
for (auto view : m_diagram->views())
|
2015-03-04 21:13:13 +00:00
|
|
|
view -> setContextMenuPolicy(Qt::DefaultContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::mouseMoveEvent
|
|
|
|
Move the element to new pos of mouse
|
|
|
|
the event is always accepted
|
|
|
|
@param event
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddElement::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
2015-03-04 21:13:13 +00:00
|
|
|
{
|
2019-01-14 18:25:43 +00:00
|
|
|
if (m_element) {
|
|
|
|
m_element->setPos(Diagram::snapToGrid(event->scenePos()));
|
|
|
|
}
|
|
|
|
event->setAccepted(true);
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::mousePressEvent
|
|
|
|
Do nothing, but return true for not transit the event to other thing in diagram.
|
|
|
|
the event is always accepted
|
|
|
|
@param event
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddElement::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
event->setAccepted(true);
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::mouseReleaseEvent
|
|
|
|
Right button finish this event (isRunning = false) and emit finish.
|
|
|
|
Left button add an element to diagram
|
|
|
|
the event is always accepted
|
|
|
|
@param event
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2015-03-04 21:13:13 +00:00
|
|
|
{
|
|
|
|
if (m_element)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::RightButton)
|
|
|
|
{
|
2020-07-01 18:31:19 +02:00
|
|
|
m_diagram->removeItem(m_element);
|
|
|
|
m_element->deleteLater();
|
2015-03-04 21:13:13 +00:00
|
|
|
m_element = nullptr;
|
|
|
|
m_running = false;
|
2015-11-09 20:20:11 +00:00
|
|
|
emit finish();
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
else if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
addElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::mouseDoubleClickEvent
|
|
|
|
If mouse left double clic, finish this event (isRunning = false) and emit finish
|
|
|
|
the event is always accepted
|
|
|
|
@param event
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddElement::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
2015-03-04 21:13:13 +00:00
|
|
|
{
|
|
|
|
if (m_element && (event -> button() == Qt::LeftButton))
|
|
|
|
{
|
2020-07-01 18:31:19 +02:00
|
|
|
m_diagram->removeItem(m_element);
|
|
|
|
m_element->deleteLater();
|
2015-03-04 21:13:13 +00:00
|
|
|
m_element = nullptr;
|
|
|
|
m_running = false;
|
2015-11-09 20:20:11 +00:00
|
|
|
emit finish();
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
2019-01-14 18:25:43 +00:00
|
|
|
|
|
|
|
event->setAccepted(true);
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::keyPressEvent
|
|
|
|
Press space key rotate the element to 90° (return true)
|
|
|
|
else call DiagramEventInterface::keyPressEvent(event), and return the value.
|
|
|
|
@param event
|
|
|
|
*/
|
2019-01-14 18:25:43 +00:00
|
|
|
void DiagramEventAddElement::keyPressEvent(QKeyEvent *event)
|
2015-03-04 21:13:13 +00:00
|
|
|
{
|
|
|
|
if (m_element && event->key() == Qt::Key_Space)
|
|
|
|
{
|
2018-03-27 19:04:43 +00:00
|
|
|
m_element->setRotation(m_element->rotation() + 90);
|
2019-01-14 18:25:43 +00:00
|
|
|
event->setAccepted(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DiagramEventInterface::keyPressEvent(event);
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::init
|
|
|
|
Init this event.
|
|
|
|
*/
|
2015-03-04 21:13:13 +00:00
|
|
|
void DiagramEventAddElement::init()
|
|
|
|
{
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QGraphicsView *view, m_diagram->views())
|
2015-03-04 21:13:13 +00:00
|
|
|
view->setContextMenuPolicy(Qt::NoContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::buildElement
|
|
|
|
Build the element, if the element is build successfully, we return true, otherwise false
|
|
|
|
*/
|
2015-03-04 21:13:13 +00:00
|
|
|
bool DiagramEventAddElement::buildElement()
|
|
|
|
{
|
2016-05-22 14:51:09 +00:00
|
|
|
ElementsLocation import_loc = m_diagram->project()->importElement(m_location);
|
|
|
|
if (import_loc.exist()) {
|
|
|
|
m_integrate_path = import_loc.projectCollectionPath();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element.";
|
|
|
|
return false;
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int state;
|
2016-05-05 13:31:04 +00:00
|
|
|
ElementsLocation loc(m_integrate_path);
|
2017-08-05 02:06:59 +00:00
|
|
|
m_element = ElementFactory::Instance() -> createElement(loc, nullptr, &state);
|
2015-03-04 21:13:13 +00:00
|
|
|
//The creation of element failed, we delete it
|
2016-05-05 13:31:04 +00:00
|
|
|
if (state) {
|
2015-03-04 21:13:13 +00:00
|
|
|
delete m_element;
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
//Everything is good
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-25 14:33:49 +00:00
|
|
|
|
2015-03-04 21:13:13 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramEventAddElement::addElement
|
|
|
|
Add an element at the current pos en current rotation,
|
|
|
|
if project autoconductor option is enable, and the element can be wired, we do it.
|
|
|
|
*/
|
2015-03-04 21:13:13 +00:00
|
|
|
void DiagramEventAddElement::addElement()
|
|
|
|
{
|
|
|
|
int state;
|
2015-03-18 14:10:25 +00:00
|
|
|
Element *element;
|
|
|
|
if (m_integrate_path.isEmpty())
|
2017-08-05 02:06:59 +00:00
|
|
|
element = ElementFactory::Instance() -> createElement(m_location, nullptr, &state);
|
2015-03-18 14:10:25 +00:00
|
|
|
else
|
2017-08-05 02:06:59 +00:00
|
|
|
element = ElementFactory::Instance() -> createElement(ElementsLocation(m_integrate_path), nullptr, &state);
|
2015-03-18 14:10:25 +00:00
|
|
|
|
2015-03-04 21:13:13 +00:00
|
|
|
//Build failed
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
delete element;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//We must add item to scene (even if addItemCommand do this)
|
|
|
|
//for create the autoconnection below
|
|
|
|
element -> setPos(m_element->pos());
|
|
|
|
element -> setRotation(m_element -> rotation());
|
|
|
|
m_diagram -> addItem(element);
|
2017-10-19 09:10:23 +00:00
|
|
|
|
|
|
|
//The element is dropped by the user, the dynamic text field stored in m_converted_text_from_xml_description
|
|
|
|
//can be moved to m_dynamic_text_list, because we are sure fromXml will be not called.
|
|
|
|
element->m_dynamic_text_list.append(element->m_converted_text_from_xml_description.keys());
|
|
|
|
element->m_converted_text_from_xml_description.clear();
|
2015-03-04 21:13:13 +00:00
|
|
|
|
2016-10-17 14:26:13 +00:00
|
|
|
QUndoCommand *undo_object = new QUndoCommand(tr("Ajouter %1").arg(element->name()));
|
|
|
|
new AddItemCommand<Element *>(element, m_diagram, m_element -> pos(), undo_object);
|
2015-03-04 21:13:13 +00:00
|
|
|
|
2021-02-07 13:25:21 +01:00
|
|
|
//When we search for free aligned terminal we
|
|
|
|
//temporally remove m_element to avoid any interaction with the function Element::AlignedFreeTerminals
|
|
|
|
//this is useful when an element who have two (or more) terminals opposite,
|
|
|
|
//because m_element is exactly at the same pos of the new element
|
|
|
|
//added to the scene so new conductor are created between terminal of the new element
|
|
|
|
//and the opposite terminal of m_element.
|
|
|
|
m_diagram->removeItem(m_element);
|
2015-03-04 21:13:13 +00:00
|
|
|
while (!element -> AlignedFreeTerminals().isEmpty() && m_diagram -> project() -> autoConductor())
|
|
|
|
{
|
|
|
|
QPair <Terminal *, Terminal *> pair = element -> AlignedFreeTerminals().takeFirst();
|
|
|
|
|
2018-07-30 15:24:29 +00:00
|
|
|
Conductor *conductor = new Conductor(pair.first, pair.second);
|
2015-03-04 21:13:13 +00:00
|
|
|
new AddItemCommand<Conductor *>(conductor, m_diagram, QPointF(), undo_object);
|
|
|
|
|
|
|
|
//Autonum the new conductor, the undo command associated for this, have for parent undo_object
|
|
|
|
ConductorAutoNumerotation can (conductor, m_diagram, undo_object);
|
|
|
|
can.numerate();
|
2016-12-12 10:52:44 +00:00
|
|
|
if (m_diagram->freezeNewConductors() || m_diagram->project()->isFreezeNewConductors()) {
|
2016-10-17 14:26:13 +00:00
|
|
|
conductor->setFreezeLabel(true);
|
|
|
|
}
|
2020-08-16 14:23:25 +02:00
|
|
|
}
|
2021-02-07 13:25:21 +01:00
|
|
|
m_diagram->addItem(m_element);
|
2016-12-01 09:09:50 +00:00
|
|
|
|
2015-03-04 21:13:13 +00:00
|
|
|
m_diagram -> undoStack().push(undo_object);
|
2016-12-01 09:09:50 +00:00
|
|
|
element->setUpFormula();
|
|
|
|
element->freezeNewAddedElement();
|
2015-03-04 21:13:13 +00:00
|
|
|
}
|