mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-24 03:10:52 +01:00
Change the way to add new element in a diagram.
Drag & drop an element in a diagram and click left to add it, click right to finish git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3803 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
237
sources/diagramevent/diagrameventaddelement.cpp
Normal file
237
sources/diagramevent/diagrameventaddelement.cpp
Normal file
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
Copyright 2006-2015 The QElectroTech Team
|
||||
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"
|
||||
#include "elementscollectionitem.h"
|
||||
#include "qetapp.h"
|
||||
#include "integrationmoveelementshandler.h"
|
||||
#include "elementfactory.h"
|
||||
#include "diagram.h"
|
||||
#include "element.h"
|
||||
#include "diagramcommands.h"
|
||||
#include "conductorautonumerotation.h"
|
||||
|
||||
|
||||
/**
|
||||
* @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) )
|
||||
*/
|
||||
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
|
||||
ElementsCollectionItem *item = QETApp::collectionItem(location);
|
||||
if (item)
|
||||
{
|
||||
//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);
|
||||
m_diagram -> addItem(m_element);
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::~DiagramEventAddElement
|
||||
* Destructor
|
||||
* Enable context menu for each view of diagram
|
||||
*/
|
||||
DiagramEventAddElement::~DiagramEventAddElement()
|
||||
{
|
||||
if (m_element) delete m_element;
|
||||
foreach(QGraphicsView *view, m_diagram->views())
|
||||
view -> setContextMenuPolicy(Qt::DefaultContextMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::mouseMoveEvent
|
||||
* Move the element to new pos of mouse
|
||||
* @param event
|
||||
* @return always true
|
||||
*/
|
||||
bool DiagramEventAddElement::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (m_element)
|
||||
m_element -> setPos(Diagram::snapToGrid(event->scenePos()));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::mousePressEvent
|
||||
* Do nothing, but return true for not transit the event to other thing in diagram.
|
||||
* @param event
|
||||
* @return always true
|
||||
*/
|
||||
bool DiagramEventAddElement::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::mouseReleaseEvent
|
||||
* Right button finish this event (isRunning = false)
|
||||
* Left button add an element to diagram
|
||||
* @param event
|
||||
* @return always true
|
||||
*/
|
||||
bool DiagramEventAddElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (m_element)
|
||||
{
|
||||
if (event->button() == Qt::RightButton)
|
||||
{
|
||||
delete m_element;
|
||||
m_element = nullptr;
|
||||
m_running = false;
|
||||
}
|
||||
else if (event->button() == Qt::LeftButton)
|
||||
{
|
||||
addElement();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::mouseDoubleClickEvent
|
||||
* If mouse left double clic, finish this event (isRunning = false)
|
||||
* @param event
|
||||
* @return always true
|
||||
*/
|
||||
bool DiagramEventAddElement::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (m_element && (event -> button() == Qt::LeftButton))
|
||||
{
|
||||
delete m_element;
|
||||
m_element = nullptr;
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::keyPressEvent
|
||||
* Press space key rotate the element to 90° (return true)
|
||||
* else call DiagramEventInterface::keyPressEvent(event), and return the value.
|
||||
* @param event
|
||||
* @return
|
||||
*/
|
||||
bool DiagramEventAddElement::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
if (m_element && event->key() == Qt::Key_Space)
|
||||
{
|
||||
m_element->rotateBy(90);
|
||||
return true;
|
||||
}
|
||||
|
||||
return DiagramEventInterface::keyPressEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::init
|
||||
* Init this event.
|
||||
*/
|
||||
void DiagramEventAddElement::init()
|
||||
{
|
||||
foreach(QGraphicsView *view, m_diagram->views())
|
||||
view->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventAddElement::buildElement
|
||||
* Build the element, if the element is build successfully, we return true, otherwise false
|
||||
*/
|
||||
bool DiagramEventAddElement::buildElement()
|
||||
{
|
||||
if (QETProject::integrateElementToProject(m_location, m_diagram -> project()))
|
||||
{
|
||||
QString error_msg;
|
||||
IntegrationMoveElementsHandler *integ_handler = new IntegrationMoveElementsHandler();
|
||||
QString integ_path = m_diagram -> project() -> integrateElement(m_location.toString(), integ_handler, error_msg);
|
||||
delete integ_handler;
|
||||
if (integ_path.isEmpty())
|
||||
{
|
||||
qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element. Motif : " << qPrintable(error_msg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int state;
|
||||
m_element = ElementFactory::Instance() -> createElement(m_location, 0, &state);
|
||||
//The creation of element failed, we delete it
|
||||
if (state)
|
||||
{
|
||||
delete m_element;
|
||||
return(false);
|
||||
}
|
||||
//Everything is good
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
void DiagramEventAddElement::addElement()
|
||||
{
|
||||
int state;
|
||||
Element *element = ElementFactory::Instance() -> createElement(m_location, 0, &state);
|
||||
//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);
|
||||
|
||||
QUndoCommand *undo_object = new AddItemCommand<Element *>(element, m_diagram, m_element -> pos());
|
||||
|
||||
while (!element -> AlignedFreeTerminals().isEmpty() && m_diagram -> project() -> autoConductor())
|
||||
{
|
||||
QPair <Terminal *, Terminal *> pair = element -> AlignedFreeTerminals().takeFirst();
|
||||
|
||||
Conductor *conductor = new Conductor(pair.first, pair.second);
|
||||
conductor -> setProperties(m_diagram -> defaultConductorProperties);
|
||||
|
||||
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();
|
||||
};
|
||||
m_diagram -> undoStack().push(undo_object);
|
||||
}
|
||||
53
sources/diagramevent/diagrameventaddelement.h
Normal file
53
sources/diagramevent/diagrameventaddelement.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Copyright 2006-2015 The QElectroTech Team
|
||||
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/>.
|
||||
*/
|
||||
#ifndef DIAGRAMEVENTADDELEMENT_H
|
||||
#define DIAGRAMEVENTADDELEMENT_H
|
||||
|
||||
#include "diagrameventinterface.h"
|
||||
#include "elementslocation.h"
|
||||
|
||||
class Element;
|
||||
|
||||
/**
|
||||
* @brief The DiagramEventAddElement class
|
||||
* This diagram event add a new element, for each left click button at the position of click.
|
||||
* Space key rotate current element by 90°, right click button finish this event.
|
||||
*/
|
||||
class DiagramEventAddElement : public DiagramEventInterface
|
||||
{
|
||||
public:
|
||||
DiagramEventAddElement(ElementsLocation &location, Diagram *diagram, QPointF pos = QPointF(0,0));
|
||||
virtual ~DiagramEventAddElement();
|
||||
|
||||
virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool keyPressEvent (QKeyEvent *event);
|
||||
virtual void init();
|
||||
|
||||
private:
|
||||
bool buildElement();
|
||||
void addElement();
|
||||
|
||||
private:
|
||||
ElementsLocation m_location;
|
||||
Element *m_element;
|
||||
};
|
||||
|
||||
#endif // DIAGRAMEVENTADDELEMENT_H
|
||||
86
sources/diagramevent/diagrameventinterface.cpp
Normal file
86
sources/diagramevent/diagrameventinterface.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright 2006-2015 The QElectroTech Team
|
||||
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 "diagrameventinterface.h"
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QKeyEvent>
|
||||
|
||||
|
||||
DiagramEventInterface::DiagramEventInterface(Diagram *diagram) :
|
||||
m_diagram(diagram),
|
||||
m_running(false),
|
||||
m_abort(false)
|
||||
{
|
||||
}
|
||||
|
||||
DiagramEventInterface::~DiagramEventInterface() {};
|
||||
|
||||
bool DiagramEventInterface::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED (event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED (event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED (event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED (event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::wheelEvent(QGraphicsSceneWheelEvent *event) {
|
||||
Q_UNUSED (event);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramEventInterface::keyPressEvent
|
||||
* By default, press escape key abort the curent action
|
||||
* @param event
|
||||
* @return
|
||||
*/
|
||||
bool DiagramEventInterface::keyPressEvent(QKeyEvent *event) {
|
||||
if (event->key() == Qt::Key_Escape) {
|
||||
m_running = false;
|
||||
m_abort = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::KeyReleaseEvent(QKeyEvent *event) {
|
||||
Q_UNUSED (event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::isRunning() const {
|
||||
return m_running;
|
||||
}
|
||||
|
||||
bool DiagramEventInterface::isFinish() const {
|
||||
return !m_running;
|
||||
}
|
||||
|
||||
void DiagramEventInterface::init()
|
||||
{}
|
||||
70
sources/diagramevent/diagrameventinterface.h
Normal file
70
sources/diagramevent/diagrameventinterface.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright 2006-2015 The QElectroTech Team
|
||||
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/>.
|
||||
*/
|
||||
#ifndef DIAGRAMEVENTINTERFACE_H
|
||||
#define DIAGRAMEVENTINTERFACE_H
|
||||
|
||||
class QGraphicsSceneMouseEvent;
|
||||
class QGraphicsSceneWheelEvent;
|
||||
class QKeyEvent;
|
||||
class Diagram;
|
||||
|
||||
/**
|
||||
* @brief The DiagramEventInterface class
|
||||
* Each method return a bool: True if the methode do something else return false.
|
||||
* Each method of DVEventInterface return false;
|
||||
* isRunning() return true if action is started but not finish. By default return false.
|
||||
* isFinish() return true when the action is finish, or not started. By default return true.
|
||||
*
|
||||
* ##USE DiagramEventInterface##
|
||||
* This class is the basic interface for manage event on a diagram.
|
||||
* To create a behavior for event diagram, we need to herite this class.
|
||||
* This interface work like this :
|
||||
* You need to create an interface and call diagram::setEventInterface(*your_interface).
|
||||
* When a diagram get an event (mouse or key) if they have an event interface,
|
||||
* they send the event to the interface (for exemple mousePressEvent).
|
||||
* If the interface do something with this event, you need to return true to signal the diagram you work with this event.
|
||||
* (if you do nothing by defaut the interface return false, so diagram do nothing)
|
||||
* after that, the diagram call interface::isRunning(), if true diagram do nothing, else if false,
|
||||
* that mean interface has finish is action (interface::isFinish return true) so the diagram will delete this interface.
|
||||
* Be carreful with the destructor, diagram can at any time (even if interface is still running) delete the interface,
|
||||
* the bool m_abort is here for that at destruction time.
|
||||
*
|
||||
*/
|
||||
class DiagramEventInterface
|
||||
{
|
||||
public:
|
||||
DiagramEventInterface(Diagram *diagram);
|
||||
virtual ~DiagramEventInterface() = 0;
|
||||
virtual bool mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
|
||||
virtual bool wheelEvent (QGraphicsSceneWheelEvent *event);
|
||||
virtual bool keyPressEvent (QKeyEvent *event);
|
||||
virtual bool KeyReleaseEvent (QKeyEvent *event);
|
||||
virtual bool isRunning () const;
|
||||
virtual bool isFinish () const;
|
||||
virtual void init();
|
||||
|
||||
protected:
|
||||
Diagram *m_diagram;
|
||||
bool m_running;
|
||||
bool m_abort;
|
||||
};
|
||||
|
||||
#endif // DIAGRAMEVENTINTERFACE_H
|
||||
Reference in New Issue
Block a user