diff --git a/sources/diagramevent/diagrameventaddelement.h b/sources/diagramevent/diagrameventaddelement.h index 8d0ecc0b4..85c531641 100644 --- a/sources/diagramevent/diagrameventaddelement.h +++ b/sources/diagramevent/diagrameventaddelement.h @@ -30,6 +30,8 @@ class Element; */ class DiagramEventAddElement : public DiagramEventInterface { + Q_OBJECT + public: DiagramEventAddElement(ElementsLocation &location, Diagram *diagram, QPointF pos = QPointF(0,0)); virtual ~DiagramEventAddElement(); diff --git a/sources/dvevent/dveventaddshape.cpp b/sources/diagramevent/diagrameventaddshape.cpp similarity index 57% rename from sources/dvevent/dveventaddshape.cpp rename to sources/diagramevent/diagrameventaddshape.cpp index 0592417eb..782c6a0e2 100644 --- a/sources/dvevent/dveventaddshape.cpp +++ b/sources/diagramevent/diagrameventaddshape.cpp @@ -15,110 +15,109 @@ You should have received a copy of the GNU General Public License along with QElectroTech. If not, see . */ -#include "dveventaddshape.h" -#include "diagramview.h" -#include "qetshapeitem.h" +#include "diagrameventaddshape.h" #include "diagram.h" #include "diagramcommands.h" -#include /** - * @brief DVEventAddShape::DVEventAddShape + * @brief DiagramEventAddShape::DiagramEventAddShape * Default constructor - * @param dv, the diagram view where operate this event - * @param shape_type, the shape type to draw + * @param diagram : the diagram where this event must operate + * @param shape_type : the type of shape to draw */ -DVEventAddShape::DVEventAddShape(DiagramView *dv, QetShapeItem::ShapeType shape_type) : - DVEventInterface(dv), +DiagramEventAddShape::DiagramEventAddShape(Diagram *diagram, QetShapeItem::ShapeType shape_type) : + DiagramEventInterface(diagram), m_shape_type (shape_type), m_shape_item (nullptr), m_help_horiz (nullptr), m_help_verti (nullptr) { - m_dv -> setContextMenuPolicy(Qt::NoContextMenu); + m_running = true; + init(); } /** - * @brief DVEventAddShape::~DVEventAddShape + * @brief DiagramEventAddShape::~DiagramEventAddShape */ -DVEventAddShape::~DVEventAddShape() +DiagramEventAddShape::~DiagramEventAddShape() { - if (m_running || m_abort) + if ((m_running || m_abort) && m_shape_item) { - m_diagram -> removeItem(m_shape_item); + m_diagram->removeItem(m_shape_item); delete m_shape_item; } delete m_help_horiz; delete m_help_verti; - m_dv -> setContextMenuPolicy(Qt::DefaultContextMenu); + + foreach (QGraphicsView *v, m_diagram->views()) + v->setContextMenuPolicy(Qt::DefaultContextMenu); } /** - * @brief DVEventAddShape::mousePressEvent + * @brief DiagramEventAddShape::mousePressEvent * Action when mouse is pressed * @param event : event of mouse press * @return : true if this event is managed, otherwise false */ -bool DVEventAddShape::mousePressEvent(QMouseEvent *event) +bool DiagramEventAddShape::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (!m_dv->isInteractive() && m_diagram->isReadOnly()) return false; + if (Q_UNLIKELY(m_diagram->isReadOnly())) return false; - QPointF pos = m_dv->mapToScene(event->pos()); + QPointF pos = event->scenePos(); if (event->modifiers() != Qt::ControlModifier) pos = Diagram::snapToGrid(pos); //Action for left mouse click - if (event -> button() == Qt::LeftButton) + if (event->button() == Qt::LeftButton) { //Create shape item if (!m_shape_item) { m_shape_item = new QetShapeItem(pos, pos, m_shape_type); - m_diagram -> addItem (m_shape_item); - m_running = true; + m_diagram->addItem (m_shape_item); return true; } //If current item isn't a polyline, add it with an undo command if (m_shape_type != QetShapeItem::Polygon) { - m_shape_item -> setP2 (pos); - m_diagram -> undoStack().push (new AddItemCommand (m_shape_item, m_diagram)); + m_shape_item->setP2 (pos); + m_diagram->undoStack().push (new AddItemCommand (m_shape_item, m_diagram)); m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic } //Else add a new point to polyline else { - m_shape_item -> setNextPoint (pos); + m_shape_item->setNextPoint (pos); } return true; } - if (event -> button() == Qt::RightButton) + if (event->button() == Qt::RightButton) return true; return false; + } /** - * @brief DVEventAddShape::mouseMoveEvent + * @brief DiagramEventAddShape::mouseMoveEvent * Action when mouse move * @param event : event of mouse move * @return : true if this event is managed, otherwise false */ -bool DVEventAddShape::mouseMoveEvent(QMouseEvent *event) +bool DiagramEventAddShape::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - updateHelpCross(event->pos()); - if (!m_running) return false; + updateHelpCross(event->scenePos()); - if (m_shape_item && event -> buttons() == Qt::NoButton) + if (m_shape_item && event->buttons() == Qt::NoButton) { - QPointF pos = m_dv->mapToScene(event->pos()); + QPointF pos = event->scenePos(); if (event->modifiers() != Qt::ControlModifier) pos = Diagram::snapToGrid(pos); - m_shape_item -> setP2 (pos); + m_shape_item->setP2 (pos); return true; } @@ -126,33 +125,33 @@ bool DVEventAddShape::mouseMoveEvent(QMouseEvent *event) } /** - * @brief DVEventAddShape::mouseReleaseEvent + * @brief DiagramEventAddShape::mouseReleaseEvent * Action when mouse button is released * @param event : event of mouse release * @return : true if this event is managed, otherwise false */ -bool DVEventAddShape::mouseReleaseEvent(QMouseEvent *event) +bool DiagramEventAddShape::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (event -> button() == Qt::RightButton) + if (event->button() == Qt::RightButton) { //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 - if (m_shape_type == QetShapeItem::Polygon && (m_shape_item -> pointsCount() >= 3) ) + if (m_shape_type == QetShapeItem::Polygon && (m_shape_item->pointsCount() >= 3) ) { - m_shape_item -> removePoints(); + m_shape_item->removePoints(); - QPointF pos = m_dv->mapToScene(event->pos()); + QPointF pos = event->scenePos(); if (event->modifiers() != Qt::ControlModifier) pos = Diagram::snapToGrid(pos); - m_shape_item -> setP2(pos); //Set the new last point under the cursor + m_shape_item->setP2(pos); //Set the new last point under the cursor return true; } //For other case, we remove item from scene - m_diagram -> removeItem(m_shape_item); + m_diagram->removeItem(m_shape_item); delete m_shape_item; m_shape_item = nullptr; return true; @@ -169,19 +168,20 @@ bool DVEventAddShape::mouseReleaseEvent(QMouseEvent *event) } /** - * @brief DVEventAddShape::mouseDoubleClickEvent - * @param event - * @return + * @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 */ -bool DVEventAddShape::mouseDoubleClickEvent(QMouseEvent *event) +bool DiagramEventAddShape::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { //If current item is a polyline, add it with an undo command - if (m_shape_item && m_shape_type == QetShapeItem::Polygon && event -> button() == Qt::LeftButton) + if (m_shape_item && m_shape_type == QetShapeItem::Polygon && event->button() == Qt::LeftButton) { //removePoints(); - m_diagram -> undoStack().push (new AddItemCommand (m_shape_item, m_diagram)); + m_diagram->undoStack().push (new AddItemCommand (m_shape_item, m_diagram)); m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic return true; } @@ -189,12 +189,18 @@ bool DVEventAddShape::mouseDoubleClickEvent(QMouseEvent *event) return false; } +void DiagramEventAddShape::init() +{ + foreach (QGraphicsView *v, m_diagram->views()) + v->setContextMenuPolicy(Qt::NoContextMenu); +} + /** - * @brief DVEventAddShape::updateHelpCross + * @brief DiagramEventAddShape::updateHelpCross * Create and update the position of the cross to help user for draw new shape - * @param event + * @param p : the center of the cross */ -void DVEventAddShape::updateHelpCross(const QPoint &p) +void DiagramEventAddShape::updateHelpCross(const QPointF &p) { //If line isn't created yet, we create it. if (!m_help_horiz || !m_help_verti) @@ -204,25 +210,25 @@ void DVEventAddShape::updateHelpCross(const QPoint &p) pen.setCosmetic(true); pen.setColor(Diagram::background_color == Qt::darkGray ? Qt::lightGray : Qt::darkGray); - QRectF rect = m_diagram -> border_and_titleblock.insideBorderRect(); + QRectF rect = m_diagram->border_and_titleblock.insideBorderRect(); if (!m_help_horiz) { m_help_horiz = new QGraphicsLineItem(rect.topLeft().x(), 0, rect.topRight().x(), 0); - m_help_horiz -> setPen(pen); - m_diagram -> addItem(m_help_horiz); + m_help_horiz->setPen(pen); + m_diagram->addItem(m_help_horiz); } if (!m_help_verti) { m_help_verti = new QGraphicsLineItem(0, rect.topLeft().y(), 0, rect.bottomLeft().y()); - m_help_verti -> setPen(pen); - m_diagram -> addItem(m_help_verti); + m_help_verti->setPen(pen); + m_diagram->addItem(m_help_verti); } } //Update the position of the cross - QPointF point = Diagram::snapToGrid(m_dv->mapToScene(p)); + QPointF point = Diagram::snapToGrid(p); m_help_horiz->setY(point.y()); m_help_verti->setX(point.x()); diff --git a/sources/dvevent/dveventaddshape.h b/sources/diagramevent/diagrameventaddshape.h similarity index 54% rename from sources/dvevent/dveventaddshape.h rename to sources/diagramevent/diagrameventaddshape.h index 1d23bfa61..10742101e 100644 --- a/sources/dvevent/dveventaddshape.h +++ b/sources/diagramevent/diagrameventaddshape.h @@ -15,28 +15,32 @@ You should have received a copy of the GNU General Public License along with QElectroTech. If not, see . */ -#ifndef DVEVENTADDSHAPE_H -#define DVEVENTADDSHAPE_H +#ifndef DIAGRAMEVENTADDSHAPE_H +#define DIAGRAMEVENTADDSHAPE_H -#include "dveventinterface.h" +#include "diagrameventinterface.h" #include "qetshapeitem.h" -class QMouseEvent; - -class DVEventAddShape : public DVEventInterface +/** + * @brief The DiagramEventAddShape class + * This event manage the creation of a shape. + */ +class DiagramEventAddShape : public DiagramEventInterface { - Q_OBJECT + Q_OBJECT public: - DVEventAddShape(DiagramView *dv, QetShapeItem::ShapeType shape_type); - virtual ~DVEventAddShape (); - virtual bool mousePressEvent (QMouseEvent *event); - virtual bool mouseMoveEvent (QMouseEvent *event); - virtual bool mouseReleaseEvent (QMouseEvent *event); - virtual bool mouseDoubleClickEvent (QMouseEvent *event); + DiagramEventAddShape(Diagram *diagram, QetShapeItem::ShapeType shape_type); + + virtual ~DiagramEventAddShape(); + virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event); + virtual void init(); private: - void updateHelpCross (const QPoint &p); + void updateHelpCross (const QPointF &p); protected: QetShapeItem::ShapeType m_shape_type; @@ -44,4 +48,4 @@ class DVEventAddShape : public DVEventInterface QGraphicsLineItem *m_help_horiz, *m_help_verti; }; -#endif // DVEVENTADDSHAPE_H +#endif // DIAGRAMEVENTADDSHAPE_H diff --git a/sources/diagramevent/diagrameventinterface.cpp b/sources/diagramevent/diagrameventinterface.cpp index 1d3cd48a4..aaf0b9efc 100644 --- a/sources/diagramevent/diagrameventinterface.cpp +++ b/sources/diagramevent/diagrameventinterface.cpp @@ -66,6 +66,7 @@ bool DiagramEventInterface::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Escape) { m_running = false; m_abort = true; + emit finish(); return true; } return false; diff --git a/sources/diagramevent/diagrameventinterface.h b/sources/diagramevent/diagrameventinterface.h index aef5e3831..1de9dd232 100644 --- a/sources/diagramevent/diagrameventinterface.h +++ b/sources/diagramevent/diagrameventinterface.h @@ -18,6 +18,8 @@ #ifndef DIAGRAMEVENTINTERFACE_H #define DIAGRAMEVENTINTERFACE_H +#include + class QGraphicsSceneMouseEvent; class QGraphicsSceneWheelEvent; class QKeyEvent; @@ -45,8 +47,10 @@ class Diagram; * the bool m_abort is here for that at destruction time. * */ -class DiagramEventInterface +class DiagramEventInterface : public QObject { + Q_OBJECT + public: DiagramEventInterface(Diagram *diagram); virtual ~DiagramEventInterface() = 0; @@ -61,6 +65,9 @@ class DiagramEventInterface virtual bool isFinish () const; virtual void init(); + signals: + void finish(); //Emited when the interface finish is job. + protected: Diagram *m_diagram; bool m_running; diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index 2d2af8303..8c91367d8 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -36,10 +36,10 @@ #include "diagramfoliolist.h" #include "qetshapeitem.h" #include "dveventaddimage.h" -#include "dveventaddshape.h" #include "dveventaddtext.h" #include "reportproperties.h" #include "diagrampropertieseditordockwidget.h" +#include "diagrameventaddshape.h" #include "ui/dialogautonum.h" @@ -1158,19 +1158,32 @@ void QETDiagramEditor::addItemGroupTriggered(QAction *action) else dvevent = event; } - else if (value == "line") - dvevent = new DVEventAddShape(dv, QetShapeItem::Line); - else if (value == "rectangle") - dvevent = new DVEventAddShape(dv, QetShapeItem::Rectangle); - else if (value == "ellipse") - dvevent = new DVEventAddShape(dv, QetShapeItem::Ellipse); - else if (value == "polyline") - dvevent = new DVEventAddShape(dv, QetShapeItem::Polygon); if (dvevent) { dv->setEventInterface(dvevent); connect(dvevent, &DVEventInterface::finish, [action](){action->setChecked(false);}); + return; + } + + if (Q_UNLIKELY (!dv->diagram())) return; + + Diagram *d = dv->diagram(); + DiagramEventInterface *diagram_event = nullptr; + + if (value == "line") + diagram_event = new DiagramEventAddShape (d, QetShapeItem::Line); + else if (value == "rectangle") + diagram_event = new DiagramEventAddShape (d, QetShapeItem::Rectangle); + else if (value == "ellipse") + diagram_event = new DiagramEventAddShape (d, QetShapeItem::Ellipse); + else if (value == "polyline") + diagram_event = new DiagramEventAddShape (d, QetShapeItem::Polygon); + + if (diagram_event) + { + d->setEventInterface(diagram_event); + connect(diagram_event, &DiagramEventInterface::finish, [action](){action->setChecked(false);}); } }