QElectroTech  0.70
diagrameventaddelement.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "diagrameventaddelement.h"
19 #include "elementfactory.h"
20 #include "diagram.h"
21 #include "element.h"
22 #include "diagramcommands.h"
24 
25 
34  DiagramEventInterface(diagram),
35  m_location(location),
36  m_element(nullptr)
37 {
38  //Check if there is an element at this location
39  if (location.isElement() && location.exist())
40  {
41  //location is an element, we build it, if build fail,
42  //m_running stay to false (by default), so this interface will be deleted at next event
43  if (buildElement())
44  {
45  init();
46  m_element -> setPos(pos);
47  m_element -> displayHelpLine(true);
48  m_element -> setFlag(QGraphicsItem::ItemIsSelectable, false);
49  m_diagram -> addItem(m_element);
50  m_running = true;
51  }
52  }
53 }
54 
61 {
62  if (m_element) delete m_element;
63  foreach(QGraphicsView *view, m_diagram->views())
64  view -> setContextMenuPolicy(Qt::DefaultContextMenu);
65 }
66 
73 void DiagramEventAddElement::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
74 {
75  if (m_element) {
76  m_element->setPos(Diagram::snapToGrid(event->scenePos()));
77  }
78  event->setAccepted(true);
79 }
80 
87 void DiagramEventAddElement::mousePressEvent(QGraphicsSceneMouseEvent *event) {
88  event->setAccepted(true);
89 }
90 
98 void DiagramEventAddElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
99 {
100  if (m_element)
101  {
102  if (event->button() == Qt::RightButton)
103  {
104  delete m_element;
105  m_element = nullptr;
106  m_running = false;
107  emit finish();
108  }
109  else if (event->button() == Qt::LeftButton)
110  {
111  addElement();
112  }
113  }
114 
115  event->setAccepted(true);
116 }
117 
124 void DiagramEventAddElement::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
125 {
126  if (m_element && (event -> button() == Qt::LeftButton))
127  {
128  delete m_element;
129  m_element = nullptr;
130  m_running = false;
131  emit finish();
132  }
133 
134  event->setAccepted(true);
135 }
136 
144 {
145  if (m_element && event->key() == Qt::Key_Space)
146  {
147  m_element->setRotation(m_element->rotation() + 90);
148  event->setAccepted(true);
149  }
150  else {
152  }
153 }
154 
160 {
161  foreach(QGraphicsView *view, m_diagram->views())
162  view->setContextMenuPolicy(Qt::NoContextMenu);
163 }
164 
170 {
172  if (import_loc.exist()) {
174  }
175  else {
176  qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element.";
177  return false;
178  }
179 
180  int state;
182  m_element = ElementFactory::Instance() -> createElement(loc, nullptr, &state);
183  //The creation of element failed, we delete it
184  if (state) {
185  delete m_element;
186  return(false);
187  }
188  //Everything is good
189  return true;
190 }
191 
198 {
199  int state;
200  Element *element;
201  if (m_integrate_path.isEmpty())
202  element = ElementFactory::Instance() -> createElement(m_location, nullptr, &state);
203  else
204  element = ElementFactory::Instance() -> createElement(ElementsLocation(m_integrate_path), nullptr, &state);
205 
206  //Build failed
207  if (state)
208  {
209  delete element;
210  return;
211  }
212 
213  //We must add item to scene (even if addItemCommand do this)
214  //for create the autoconnection below
215  element -> setPos(m_element->pos());
216  element -> setRotation(m_element -> rotation());
217  m_diagram -> addItem(element);
218 
219  //The element is dropped by the user, the dynamic text field stored in m_converted_text_from_xml_description
220  //can be moved to m_dynamic_text_list, because we are sure fromXml will be not called.
221  element->m_dynamic_text_list.append(element->m_converted_text_from_xml_description.keys());
222  element->m_converted_text_from_xml_description.clear();
223 
224  QUndoCommand *undo_object = new QUndoCommand(tr("Ajouter %1").arg(element->name()));
225  new AddItemCommand<Element *>(element, m_diagram, m_element -> pos(), undo_object);
226 
227  while (!element -> AlignedFreeTerminals().isEmpty() && m_diagram -> project() -> autoConductor())
228  {
229  QPair <Terminal *, Terminal *> pair = element -> AlignedFreeTerminals().takeFirst();
230 
231  Conductor *conductor = new Conductor(pair.first, pair.second);
232  new AddItemCommand<Conductor *>(conductor, m_diagram, QPointF(), undo_object);
233 
234  //Autonum the new conductor, the undo command associated for this, have for parent undo_object
235  ConductorAutoNumerotation can (conductor, m_diagram, undo_object);
236  can.numerate();
238  conductor->setFreezeLabel(true);
239  }
240  };
241 
242  m_diagram -> undoStack().push(undo_object);
243  element->setUpFormula();
244  element->freezeNewAddedElement();
245 }
virtual void keyPressEvent(QKeyEvent *event)
DiagramEventInterface::keyPressEvent By default, press escape key abort the curent action...
ElementsLocation importElement(ElementsLocation &location)
QETProject::importElement Import the element represented by to the embbeded collection of this proje...
Definition: qetproject.cpp:962
QString projectCollectionPath() const
ElementsLocation::projectCollectionPath.
bool isFreezeNewConductors()
QETProject::isFreezeNewConductors.
Definition: qetproject.cpp:706
DiagramEventAddElement(ElementsLocation &location, Diagram *diagram, QPointF pos=QPointF(0, 0))
DiagramEventAddElement::DiagramEventAddElement Defaut constructor.
bool buildElement()
DiagramEventAddElement::buildElement Build the element, if the element is build successfully, we return true, otherwise false.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddElement::mouseDoubleClickEvent If mouse left double clic, finish this event (isRunning...
QString name() const override
Element::name.
Definition: element.cpp:1565
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddElement::mouseReleaseEvent Right button finish this event (isRunning = false) and emit...
The AddItemCommand class This command add an item in a diagram The item to add is template...
QIcon Conductor
Definition: qeticons.cpp:35
void keyPressEvent(QKeyEvent *event) override
DiagramEventAddElement::keyPressEvent Press space key rotate the element to 90° (return true) else ca...
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddElement::mouseMoveEvent Move the element to new pos of mouse the event is always accep...
bool freezeNewConductors()
Diagram::freezeNewConductors.
Definition: diagram.cpp:1589
QIcon tr
Definition: qeticons.cpp:204
~DiagramEventAddElement() override
DiagramEventAddElement::~DiagramEventAddElement Destructor Enable context menu for each view of diagr...
void init() override
DiagramEventAddElement::init Init this event.
bool exist() const
ElementsLocation::exist.
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:1653
QList< DynamicElementTextItem * > m_dynamic_text_list
Definition: element.h:194
The DiagramEventInterface class isRunning() return true if action is running (do something). By default return false.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddElement::mousePressEvent Do nothing, but return true for not transit the event to othe...
bool isElement() const
ElementsLocation::isElement.
virtual void setPos(const QPointF &p)
QetGraphicsItem::setPos set the position of the item to p.
QETProject * project() const
Definition: diagram.cpp:1716
QHash< DynamicElementTextItem *, QPointF > m_converted_text_from_xml_description
Definition: element.h:168
static ElementFactory * Instance()
void addElement()
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.