diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 87423859b..4820a1873 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -169,14 +169,7 @@ void Diagram::drawBackground(QPainter *p, const QRectF &r) { */ void Diagram::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { - if (m_event_interface) { - if (m_event_interface -> mouseDoubleClickEvent(event)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->mouseDoubleClickEvent(event)) return; QGraphicsScene::mouseDoubleClickEvent(event); } @@ -188,14 +181,7 @@ void Diagram::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) */ void Diagram::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (m_event_interface) { - if (m_event_interface -> mousePressEvent(event)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->mousePressEvent(event)) return; QGraphicsScene::mousePressEvent(event); } @@ -207,14 +193,7 @@ void Diagram::mousePressEvent(QGraphicsSceneMouseEvent *event) */ void Diagram::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if (m_event_interface) { - if (m_event_interface -> mouseMoveEvent(event)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->mouseMoveEvent(event)) return; QGraphicsScene::mouseMoveEvent(event); } @@ -226,14 +205,7 @@ void Diagram::mouseMoveEvent(QGraphicsSceneMouseEvent *event) */ void Diagram::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (m_event_interface) { - if (m_event_interface -> mouseReleaseEvent(event)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->mouseReleaseEvent(event)) return; QGraphicsScene::mouseReleaseEvent(event); } @@ -245,14 +217,9 @@ void Diagram::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) */ void Diagram::wheelEvent(QGraphicsSceneWheelEvent *event) { - if (m_event_interface) { - if (m_event_interface -> wheelEvent(event)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->wheelEvent(event)) return; + + QGraphicsScene::wheelEvent(event); } /** @@ -263,14 +230,7 @@ void Diagram::wheelEvent(QGraphicsSceneWheelEvent *event) */ void Diagram::keyPressEvent(QKeyEvent *e) { - if (m_event_interface) { - if (m_event_interface -> keyPressEvent(e)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->keyPressEvent(e)) return; bool transmit_event = true; if (!isReadOnly()) { @@ -301,14 +261,7 @@ void Diagram::keyPressEvent(QKeyEvent *e) */ void Diagram::keyReleaseEvent(QKeyEvent *e) { - if (m_event_interface) { - if (m_event_interface -> KeyReleaseEvent(e)) { - if (m_event_interface->isFinish()) { - delete m_event_interface; m_event_interface = nullptr; - } - return; - } - } + if (m_event_interface && m_event_interface->KeyReleaseEvent(e)) return; bool transmit_event = true; if (!isReadOnly()) { @@ -334,6 +287,8 @@ void Diagram::keyReleaseEvent(QKeyEvent *e) * Diagram become the ownership of event_interface * If there is a previous interface, they will be delete before * and call init() to the new interface. + * The derivated class of DiagramEventInterface need to emit the signal "finish" when the job is done, + * diagram use this signal to delete the interface. If the signal isn't send, the interface will never be deleted. * @param event_interface */ void Diagram::setEventInterface(DiagramEventInterface *event_interface) @@ -344,6 +299,7 @@ void Diagram::setEventInterface(DiagramEventInterface *event_interface) event_interface -> init(); } m_event_interface = event_interface; + connect (m_event_interface, &DiagramEventInterface::finish, this, [this]() { delete this->m_event_interface; this->m_event_interface = nullptr;}, Qt::QueuedConnection); } /** diff --git a/sources/diagramevent/diagrameventaddelement.cpp b/sources/diagramevent/diagrameventaddelement.cpp index 8286d1c8f..12ab93fd5 100644 --- a/sources/diagramevent/diagrameventaddelement.cpp +++ b/sources/diagramevent/diagrameventaddelement.cpp @@ -95,7 +95,7 @@ bool DiagramEventAddElement::mousePressEvent(QGraphicsSceneMouseEvent *event) /** * @brief DiagramEventAddElement::mouseReleaseEvent - * Right button finish this event (isRunning = false) + * Right button finish this event (isRunning = false) and emit finish. * Left button add an element to diagram * @param event * @return always true @@ -109,6 +109,7 @@ bool DiagramEventAddElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) delete m_element; m_element = nullptr; m_running = false; + emit finish(); } else if (event->button() == Qt::LeftButton) { @@ -121,7 +122,7 @@ bool DiagramEventAddElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) /** * @brief DiagramEventAddElement::mouseDoubleClickEvent - * If mouse left double clic, finish this event (isRunning = false) + * If mouse left double clic, finish this event (isRunning = false) and emit finish * @param event * @return always true */ @@ -132,6 +133,7 @@ bool DiagramEventAddElement::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *eve delete m_element; m_element = nullptr; m_running = false; + emit finish(); } return true; diff --git a/sources/diagramevent/diagrameventinterface.cpp b/sources/diagramevent/diagrameventinterface.cpp index aaf0b9efc..02c2ae539 100644 --- a/sources/diagramevent/diagrameventinterface.cpp +++ b/sources/diagramevent/diagrameventinterface.cpp @@ -81,9 +81,5 @@ bool DiagramEventInterface::isRunning() const { return m_running; } -bool DiagramEventInterface::isFinish() const { - return !m_running; -} - void DiagramEventInterface::init() {} diff --git a/sources/diagramevent/diagrameventinterface.h b/sources/diagramevent/diagrameventinterface.h index 1de9dd232..fa74d051a 100644 --- a/sources/diagramevent/diagrameventinterface.h +++ b/sources/diagramevent/diagrameventinterface.h @@ -29,8 +29,7 @@ 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. + * isRunning() return true if action is running (do something). By default return false. * * ##USE DiagramEventInterface## * This class is the basic interface for manage event on a diagram. @@ -41,8 +40,7 @@ class Diagram; * 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. + * When the interface job is done, we need to emit the signal finish(), the diagram use this signal to delete the 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. * @@ -62,7 +60,6 @@ class DiagramEventInterface : public QObject virtual bool keyPressEvent (QKeyEvent *event); virtual bool KeyReleaseEvent (QKeyEvent *event); virtual bool isRunning () const; - virtual bool isFinish () const; virtual void init(); signals: