diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index b06be55b3..3cdf30bbd 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -79,14 +79,6 @@ void ElementScene::slot_addCircle() { if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; } -/** - Passe la scene en mode "ajout d'ellipse" -*/ -void ElementScene::slot_addEllipse() { - behavior = Ellipse; - if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; -} - /** Passe la scene en mode "ajout de polygone" */ @@ -160,11 +152,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { QPolygonF temp_polygon; if (e -> buttons() & Qt::LeftButton) { switch(behavior) { - case Ellipse: - temp_rect = current_ellipse -> rect(); - temp_rect.setBottomRight(event_pos); - current_ellipse -> setRect(temp_rect); - break; case Arc: temp_rect = current_arc -> rect(); temp_rect.setBottomRight(event_pos); @@ -211,11 +198,6 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) { QPolygonF temp_polygon; if (e -> button() & Qt::LeftButton) { switch(behavior) { - case Ellipse: - current_ellipse = new PartEllipse(element_editor, 0, this); - current_ellipse -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0))); - current_ellipse -> setProperty("antialias", true); - break; case Arc: current_arc = new PartArc(element_editor, 0, this); current_arc -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0))); @@ -272,13 +254,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { if (e -> button() & Qt::LeftButton) { switch(behavior) { - case Ellipse: - if (qgiManager().manages(current_ellipse)) break; - current_ellipse -> setRect(current_ellipse -> rect().normalized()); - undo_stack.push(new AddPartCommand(tr("ellipse"), this, current_ellipse)); - emit(partsAdded()); - endCurrentBehavior(e); - break; case Arc: if (qgiManager().manages(current_arc)) break; current_arc-> setRect(current_arc -> rect().normalized()); diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index aa87220a5..182fefd93 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -28,7 +28,6 @@ class CustomElementPart; class ElementEditionCommand; class ElementPrimitiveDecorator; class QETElementEditor; -class PartEllipse; class PartPolygon; class PartArc; class ESEventInterface; @@ -44,7 +43,7 @@ class ElementScene : public QGraphicsScene { // enum public: - enum Behavior { Normal, Circle, Ellipse, Polygon, Text, Terminal, Arc, TextField, PasteArea }; + enum Behavior { Normal, Circle, Polygon, Text, Terminal, Arc, TextField, PasteArea }; enum ItemOption { SortByZValue = 1, IncludeTerminals = 2, @@ -89,7 +88,6 @@ class ElementScene : public QGraphicsScene { /// Variables related to drawing ESEventInterface *m_event_interface; Behavior behavior; - PartEllipse *current_ellipse; PartPolygon *current_polygon; PartArc *current_arc; QETElementEditor *element_editor; @@ -167,7 +165,6 @@ class ElementScene : public QGraphicsScene { public slots: void slot_move(); void slot_addCircle(); - void slot_addEllipse(); void slot_addPolygon(); void slot_addText(); void slot_addArc(); diff --git a/sources/editor/esevent/eseventaddellipse.cpp b/sources/editor/esevent/eseventaddellipse.cpp new file mode 100644 index 000000000..28a52ad4d --- /dev/null +++ b/sources/editor/esevent/eseventaddellipse.cpp @@ -0,0 +1,106 @@ +/* + 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 "eseventaddellipse.h" +#include "partellipse.h" +#include "editorcommands.h" +#include "elementscene.h" + +/** + * @brief ESEventAddEllipse::ESEventAddEllipse + * @param scene + */ +ESEventAddEllipse::ESEventAddEllipse(ElementScene *scene) : + ESEventInterface(scene), + m_ellipse(nullptr) +{} + +/** + * @brief ESEventAddEllipse::~ESEventAddEllipse + */ +ESEventAddEllipse::~ESEventAddEllipse() { + if (m_running || m_abort){ + delete m_ellipse; + } +} + +/** + * @brief ESEventAddEllipse::mousePressEvent + * @param event + * @return + */ +bool ESEventAddEllipse::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::LeftButton) { + if(!m_running) m_running = true; + QPointF pos = m_scene->snapToGrid(event -> scenePos()); + + //create new ellpise + if (!m_ellipse) { + m_ellipse = new PartEllipse(m_editor, 0, m_scene); + m_ellipse -> setRect(QRectF(pos, pos)); + m_origin = pos; + return true; + } + + //Add ellipse to scene + m_ellipse -> setRect(m_ellipse -> rect().normalized()); + m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Rectangle"), m_scene, m_ellipse)); + + //Set m_ellipse to nullptr for create new ellipse at next mouse press + m_ellipse = nullptr; + + return true; + } + return false; +} + +/** + * @brief ESEventAddRect::mouseMoveEvent + * @param event + * @return + */ +bool ESEventAddEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + updateHelpCross(event -> scenePos()); + if (!m_ellipse) return false; + + QPointF mouse_pos = m_scene -> snapToGrid(event -> scenePos()); + + qreal width = (mouse_pos.x() - m_origin.x())*2; + qreal height = (mouse_pos.y() - m_origin.y())*2; + + QPointF pos(m_origin.x() - width/2, + m_origin.y() - height/2); + + m_ellipse -> setRect(QRectF(pos, QSizeF(width, height))); + return true; +} + +/** + * @brief ESEventAddEllipse::mouseReleaseEvent + * @param event + * @return + */ +bool ESEventAddEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::RightButton) { + if (m_ellipse) {delete m_ellipse; m_ellipse = nullptr;} + else {m_running = false;} + return true; + } + return false; +} diff --git a/sources/editor/esevent/eseventaddellipse.h b/sources/editor/esevent/eseventaddellipse.h new file mode 100644 index 000000000..11de44bf6 --- /dev/null +++ b/sources/editor/esevent/eseventaddellipse.h @@ -0,0 +1,43 @@ +/* + 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 ESEVENTADDELLIPSE_H +#define ESEVENTADDELLIPSE_H + +#include "eseventinterface.h" +#include + +class ElementScene; +class PartEllipse; +class QGraphicsSceneMouseEvent; + +class ESEventAddEllipse : public ESEventInterface +{ + public: + ESEventAddEllipse(ElementScene *scene); + ~ESEventAddEllipse(); + + virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event); + + private: + PartEllipse *m_ellipse; + QPointF m_origin; +}; + +#endif // ESEVENTADDELLIPSE_H diff --git a/sources/editor/qetelementeditor.cpp b/sources/editor/qetelementeditor.cpp index fa2db6769..a7d71c876 100644 --- a/sources/editor/qetelementeditor.cpp +++ b/sources/editor/qetelementeditor.cpp @@ -43,6 +43,7 @@ #include "eseventaddline.h" #include "eseventaddrect.h" +#include "eseventaddellipse.h" #include /* @@ -249,7 +250,7 @@ void QETElementEditor::setupActions() { 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_ellipse, SIGNAL(triggered()), this, SLOT(addEllipse())); connect(add_polygon, SIGNAL(triggered()), ce_scene, SLOT(slot_addPolygon())); connect(add_text, SIGNAL(triggered()), ce_scene, SLOT(slot_addText())); connect(add_arc, SIGNAL(triggered()), ce_scene, SLOT(slot_addArc())); @@ -939,6 +940,14 @@ void QETElementEditor::addRect() { ce_scene -> setEventInterface(new ESEventAddRect(ce_scene)); } +/** + * @brief QETElementEditor::addEllipse + * Set ellipse creation interface to scene + */ +void QETElementEditor::addEllipse() { + ce_scene -> setEventInterface(new ESEventAddEllipse(ce_scene)); +} + /** Lance l'assistant de creation d'un nouvel element. */ diff --git a/sources/editor/qetelementeditor.h b/sources/editor/qetelementeditor.h index a44be3491..403da7cc7 100644 --- a/sources/editor/qetelementeditor.h +++ b/sources/editor/qetelementeditor.h @@ -128,6 +128,7 @@ class QETElementEditor : public QETMainWindow { public slots: void addLine(); void addRect(); + void addEllipse(); void slot_new(); void slot_open();