diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index b48266e48..b06be55b3 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -71,14 +71,6 @@ void ElementScene::slot_move() { if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; } -/** - Passe la scene en mode "ajout de rectangle" -*/ -void ElementScene::slot_addRectangle() { - behavior = Rectangle; - if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; -} - /** Passe la scene en mode "ajout de cercle" */ @@ -168,11 +160,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { QPolygonF temp_polygon; if (e -> buttons() & Qt::LeftButton) { switch(behavior) { - case Rectangle: - temp_rect = current_rectangle -> rect(); - temp_rect.setBottomRight(event_pos); - current_rectangle -> setRect(temp_rect); - break; case Ellipse: temp_rect = current_ellipse -> rect(); temp_rect.setBottomRight(event_pos); @@ -224,10 +211,6 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) { QPolygonF temp_polygon; if (e -> button() & Qt::LeftButton) { switch(behavior) { - case Rectangle: - current_rectangle = new PartRectangle(element_editor, 0, this); - current_rectangle -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0))); - break; case Ellipse: current_ellipse = new PartEllipse(element_editor, 0, this); current_ellipse -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0))); @@ -289,13 +272,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { if (e -> button() & Qt::LeftButton) { switch(behavior) { - case Rectangle: - if (qgiManager().manages(current_rectangle)) break; - current_rectangle -> setRect(current_rectangle -> rect().normalized()); - undo_stack.push(new AddPartCommand(tr("rectangle"), this, current_rectangle)); - emit(partsAdded()); - endCurrentBehavior(e); - break; case Ellipse: if (qgiManager().manages(current_ellipse)) break; current_ellipse -> setRect(current_ellipse -> rect().normalized()); diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index 5b4ba95d7..aa87220a5 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -28,7 +28,6 @@ class CustomElementPart; class ElementEditionCommand; class ElementPrimitiveDecorator; class QETElementEditor; -class PartRectangle; class PartEllipse; class PartPolygon; class PartArc; @@ -45,7 +44,7 @@ class ElementScene : public QGraphicsScene { // enum public: - enum Behavior { Normal, Rectangle, Circle, Ellipse, Polygon, Text, Terminal, Arc, TextField, PasteArea }; + enum Behavior { Normal, Circle, Ellipse, Polygon, Text, Terminal, Arc, TextField, PasteArea }; enum ItemOption { SortByZValue = 1, IncludeTerminals = 2, @@ -54,7 +53,7 @@ class ElementScene : public QGraphicsScene { NonSelected = 16, SelectedOrNot = 24 }; - Q_DECLARE_FLAGS(ItemOptions, ItemOption); + Q_DECLARE_FLAGS(ItemOptions, ItemOption) // constructors, destructor public: @@ -90,7 +89,6 @@ class ElementScene : public QGraphicsScene { /// Variables related to drawing ESEventInterface *m_event_interface; Behavior behavior; - PartRectangle *current_rectangle; PartEllipse *current_ellipse; PartPolygon *current_polygon; PartArc *current_arc; @@ -168,7 +166,6 @@ class ElementScene : public QGraphicsScene { public slots: void slot_move(); - void slot_addRectangle(); void slot_addCircle(); void slot_addEllipse(); void slot_addPolygon(); diff --git a/sources/editor/esevent/eseventaddrect.cpp b/sources/editor/esevent/eseventaddrect.cpp new file mode 100644 index 000000000..6d194f289 --- /dev/null +++ b/sources/editor/esevent/eseventaddrect.cpp @@ -0,0 +1,97 @@ +/* + Copyright 2006-2014 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 . +*/ +#include + +#include "eseventaddrect.h" +#include "elementscene.h" +#include "partrectangle.h" +#include "editorcommands.h" + +/** + * @brief ESEventAddRect::ESEventAddRect + * @param scene + */ +ESEventAddRect::ESEventAddRect(ElementScene *scene) : + ESEventInterface(scene), + m_rect(nullptr) +{} + +/** + * @brief ESEventAddRect::~ESEventAddRect + */ +ESEventAddRect::~ESEventAddRect() { + if (m_running || m_abort) + delete m_rect; +} + +/** + * @brief ESEventAddRect::mousePressEvent + * @param event + * @return + */ +bool ESEventAddRect::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::LeftButton) { + if(!m_running) m_running = true; + QPointF pos = m_scene->snapToGrid(event -> scenePos()); + + //create new rectangle + if (!m_rect) { + m_rect = new PartRectangle(m_editor, 0, m_scene); + m_rect -> setRect(QRectF(pos, pos)); + return true; + } + + //Add rectangle to scene + m_rect -> setRect(m_rect -> rect().normalized()); + m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Rectangle"), m_scene, m_rect)); + + //Set m_rect to nullptr for create new rectangle at next mouse press + m_rect = nullptr; + + return true; + } + return false; +} + +/** + * @brief ESEventAddRect::mouseMoveEvent + * @param event + * @return + */ +bool ESEventAddRect::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + updateHelpCross(event -> scenePos()); + if (!m_rect) return false; + + QRectF rect(m_rect -> rect().topLeft(), m_scene->snapToGrid(event -> scenePos())); + m_rect -> setRect(rect); + return true; +} + +/** + * @brief ESEventAddRect::mouseReleaseEvent + * @param event + * @return + */ +bool ESEventAddRect::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::RightButton) { + if (m_rect) {delete m_rect; m_rect = nullptr;} + else {m_running = false;} + return true; + } + return false; +} diff --git a/sources/editor/esevent/eseventaddrect.h b/sources/editor/esevent/eseventaddrect.h new file mode 100644 index 000000000..9b26f68bc --- /dev/null +++ b/sources/editor/esevent/eseventaddrect.h @@ -0,0 +1,41 @@ +/* + Copyright 2006-2014 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 . +*/ +#ifndef ESEVENTADDRECT_H +#define ESEVENTADDRECT_H + +#include "eseventinterface.h" + +class ElementScene; +class PartRectangle; +class QGraphicsSceneMouseEvent; + +class ESEventAddRect : public ESEventInterface +{ + public: + ESEventAddRect(ElementScene *scene); + virtual ~ESEventAddRect(); + + virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event); + + private: + PartRectangle *m_rect; +}; + +#endif // ESEVENTADDRECT_H diff --git a/sources/editor/qetelementeditor.cpp b/sources/editor/qetelementeditor.cpp index 298ba3a57..fa2db6769 100644 --- a/sources/editor/qetelementeditor.cpp +++ b/sources/editor/qetelementeditor.cpp @@ -42,6 +42,7 @@ #include "parttextfield.h" #include "eseventaddline.h" +#include "eseventaddrect.h" #include /* @@ -246,8 +247,8 @@ void QETElementEditor::setupActions() { connect(edit_backward, SIGNAL(triggered()), ce_scene, SLOT(slot_sendBackward())); connect(move, SIGNAL(triggered()), ce_scene, SLOT(slot_move())); - connect(add_line, SIGNAL(triggered()), this, SLOT(addLine())); - connect(add_rectangle, SIGNAL(triggered()), ce_scene, SLOT(slot_addRectangle())); + connect(add_line, SIGNAL(triggered()), this, SLOT(addLine())); + connect(add_rectangle, SIGNAL(triggered()), this, SLOT(addRect())); connect(add_ellipse, SIGNAL(triggered()), ce_scene, SLOT(slot_addEllipse())); connect(add_polygon, SIGNAL(triggered()), ce_scene, SLOT(slot_addPolygon())); connect(add_text, SIGNAL(triggered()), ce_scene, SLOT(slot_addText())); @@ -930,6 +931,14 @@ void QETElementEditor::addLine() { ce_scene -> setEventInterface(new ESEventAddLine(ce_scene)); } +/** + * @brief QETElementEditor::addRect + * Set rectangle creation interface to scene + */ +void QETElementEditor::addRect() { + ce_scene -> setEventInterface(new ESEventAddRect(ce_scene)); +} + /** Lance l'assistant de creation d'un nouvel element. */ diff --git a/sources/editor/qetelementeditor.h b/sources/editor/qetelementeditor.h index c82e93415..a44be3491 100644 --- a/sources/editor/qetelementeditor.h +++ b/sources/editor/qetelementeditor.h @@ -127,6 +127,7 @@ class QETElementEditor : public QETMainWindow { public slots: void addLine(); + void addRect(); void slot_new(); void slot_open();