diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index 3cdf30bbd..f08dbb6cb 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -49,7 +49,6 @@ ElementScene::ElementScene(QETElementEditor *editor, QObject *parent) : decorator_(0) { setItemIndexMethod(NoIndex); - current_polygon = NULL; setGrid(1, 1); initPasteArea(); undo_stack.setClean(); @@ -61,6 +60,7 @@ ElementScene::ElementScene(QETElementEditor *editor, QObject *parent) : /// Destructeur ElementScene::~ElementScene() { delete decorator_lock_; + if (m_event_interface) delete m_event_interface; } /** @@ -79,15 +79,6 @@ void ElementScene::slot_addCircle() { if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; } -/** - Passe la scene en mode "ajout de polygone" -*/ -void ElementScene::slot_addPolygon() { - behavior = Polygon; - if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; -} - - /** Passe la scene en mode "ajout de texte statique" */ @@ -139,7 +130,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { QPointF event_pos = e -> scenePos(); if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos); - if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL; if (behavior == PasteArea) { QRectF current_rect(paste_area_ -> rect()); current_rect.moveCenter(event_pos); @@ -148,8 +138,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { } QRectF temp_rect; - QPointF temp_point; - QPolygonF temp_polygon; if (e -> buttons() & Qt::LeftButton) { switch(behavior) { case Arc: @@ -157,23 +145,12 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { temp_rect.setBottomRight(event_pos); current_arc -> setRect(temp_rect); break; - case Polygon: - if (current_polygon == NULL) break; - temp_polygon = current_polygon -> polygon(); - temp_polygon.pop_back(); - temp_polygon << event_pos; - current_polygon -> setPolygon(temp_polygon); - break; case Normal: default: QGraphicsScene::mouseMoveEvent(e); } - } else if (behavior == Polygon && current_polygon != NULL) { - temp_polygon = current_polygon -> polygon(); - temp_polygon.pop_back(); - temp_polygon << event_pos; - current_polygon -> setPolygon(temp_polygon); - } else QGraphicsScene::mouseMoveEvent(e); + } + else QGraphicsScene::mouseMoveEvent(e); } /** @@ -194,8 +171,6 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) { QPointF event_pos = e -> scenePos(); if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos); - if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL; - QPolygonF temp_polygon; if (e -> button() & Qt::LeftButton) { switch(behavior) { case Arc: @@ -203,16 +178,6 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) { current_arc -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0))); current_arc -> setProperty("antialias", true); break; - case Polygon: - if (current_polygon == NULL) { - current_polygon = new PartPolygon(element_editor, 0, this); - temp_polygon = QPolygonF(0); - } else temp_polygon = current_polygon -> polygon(); - // au debut, on insere deux points - if (!temp_polygon.count()) temp_polygon << event_pos; - temp_polygon << event_pos; - current_polygon -> setPolygon(temp_polygon); - break; case Normal: default: QGraphicsScene::mousePressEvent(e); @@ -242,7 +207,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { PartTerminal *terminal; PartText *text; PartTextField *textfield; - if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL; if (behavior == PasteArea) { defined_paste_area_ = paste_area_ -> rect(); @@ -288,14 +252,23 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { QGraphicsScene::mouseReleaseEvent(e); moving_parts_ = false; } - } else if (e -> button() & Qt::RightButton) { - if (behavior == Polygon && current_polygon != NULL) { - undo_stack.push(new AddPartCommand(tr("polygone"), this, current_polygon)); - current_polygon = NULL; - emit(partsAdded()); - endCurrentBehavior(e); - } else QGraphicsScene::mouseReleaseEvent(e); - } else QGraphicsScene::mouseReleaseEvent(e); + } + else QGraphicsScene::mouseReleaseEvent(e); +} + +void ElementScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { + if (m_event_interface) { + if (m_event_interface -> mouseDoubleClickEvent(event)) { + if (m_event_interface->isFinish()) { + emit(partsAdded()); + emit(needNormalMode()); + delete m_event_interface; m_event_interface = nullptr; + } + return; + } + } + + QGraphicsScene::mouseDoubleClickEvent(event); } /** diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index 182fefd93..1bf4ebe5b 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -28,7 +28,6 @@ class CustomElementPart; class ElementEditionCommand; class ElementPrimitiveDecorator; class QETElementEditor; -class PartPolygon; class PartArc; class ESEventInterface; class QKeyEvent; @@ -43,7 +42,7 @@ class ElementScene : public QGraphicsScene { // enum public: - enum Behavior { Normal, Circle, Polygon, Text, Terminal, Arc, TextField, PasteArea }; + enum Behavior { Normal, Circle, Text, Terminal, Arc, TextField, PasteArea }; enum ItemOption { SortByZValue = 1, IncludeTerminals = 2, @@ -88,7 +87,6 @@ class ElementScene : public QGraphicsScene { /// Variables related to drawing ESEventInterface *m_event_interface; Behavior behavior; - PartPolygon *current_polygon; PartArc *current_arc; QETElementEditor *element_editor; @@ -141,10 +139,11 @@ class ElementScene : public QGraphicsScene { QETElementEditor* editor() const; protected: - virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *); - virtual void mousePressEvent (QGraphicsSceneMouseEvent *); - virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *); - virtual void keyPressEvent (QKeyEvent *event); + virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *); + virtual void mousePressEvent (QGraphicsSceneMouseEvent *); + virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *); + virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event); + virtual void keyPressEvent (QKeyEvent *event); virtual void drawForeground(QPainter *, const QRectF &); virtual void endCurrentBehavior(const QGraphicsSceneMouseEvent *); @@ -165,7 +164,6 @@ class ElementScene : public QGraphicsScene { public slots: void slot_move(); void slot_addCircle(); - void slot_addPolygon(); void slot_addText(); void slot_addArc(); void slot_addTerminal(); diff --git a/sources/editor/esevent/eseventaddellipse.h b/sources/editor/esevent/eseventaddellipse.h index 11de44bf6..d31bf9b0c 100644 --- a/sources/editor/esevent/eseventaddellipse.h +++ b/sources/editor/esevent/eseventaddellipse.h @@ -25,6 +25,10 @@ class ElementScene; class PartEllipse; class QGraphicsSceneMouseEvent; +/** + * @brief The ESEventAddEllipse class + * This ESEvent manage creation of ellpise in an ElementScene + */ class ESEventAddEllipse : public ESEventInterface { public: diff --git a/sources/editor/esevent/eseventaddpolygon.cpp b/sources/editor/esevent/eseventaddpolygon.cpp new file mode 100644 index 000000000..0e6870226 --- /dev/null +++ b/sources/editor/esevent/eseventaddpolygon.cpp @@ -0,0 +1,117 @@ +/* + 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 "eseventaddpolygon.h" +#include "elementscene.h" +#include "partpolygon.h" +#include "editorcommands.h" + +/** + * @brief ESEventAddPolygon::ESEventAddPolygon + * @param scene + */ +ESEventAddPolygon::ESEventAddPolygon(ElementScene *scene) : + ESEventInterface(scene), + m_polygon(nullptr) +{} + +/** + * @brief ESEventAddPolygon::~ESEventAddPolygon + */ +ESEventAddPolygon::~ESEventAddPolygon() { + if (m_running || m_abort) + delete m_polygon; +} + +/** + * @brief ESEventAddPolygon::mousePressEvent + * @param event + * @return + */ +bool ESEventAddPolygon::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::LeftButton) { + if(!m_running) m_running = true; + QPointF pos = m_scene->snapToGrid(event -> scenePos()); + + //create new polygon + if (!m_polygon) { + m_polygon = new PartPolygon(m_editor, 0, m_scene); + m_polygon -> addPoint(pos); + } + + m_polygon -> addPoint(pos); + return true; + } + return false; +} + +/** + * @brief ESEventAddPolygon::mouseMoveEvent + * @param event + * @return + */ +bool ESEventAddPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + updateHelpCross(event -> scenePos()); + if (!m_polygon) return false; + + m_polygon -> setLastPoint(m_scene -> snapToGrid(event -> scenePos())); + return true; +} + +/** + * @brief ESEventAddPolygon::mouseReleaseEvent + * @param event + * @return + */ +bool ESEventAddPolygon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::RightButton) { + if (m_polygon) { + m_polygon -> removeLastPoint(); + + if (m_polygon -> polygon().size() > 1) + { m_polygon -> setLastPoint(m_scene -> snapToGrid(event -> scenePos())); } + else + { delete m_polygon; m_polygon = nullptr; } + } + else + { m_running = false; } + + return true; + } + return false; +} + +/** + * @brief ESEventAddPolygon::mouseDoubleClickEvent + * @param event + * @return + */ +bool ESEventAddPolygon::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::LeftButton) { + if (m_polygon) { + m_polygon -> addPoint(m_scene -> snapToGrid(event -> scenePos())); + m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Polygone"), m_scene, m_polygon)); + + //Set m_polygon to nullptr for create new polygon at next mouse press + m_polygon = nullptr; + return true; + } + } + return false; +} diff --git a/sources/editor/esevent/eseventaddpolygon.h b/sources/editor/esevent/eseventaddpolygon.h new file mode 100644 index 000000000..145777f5a --- /dev/null +++ b/sources/editor/esevent/eseventaddpolygon.h @@ -0,0 +1,46 @@ +/* + 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 ESEVENTADDPOLYGON_H +#define ESEVENTADDPOLYGON_H + +#include "eseventinterface.h" + +class ElementScene; +class PartPolygon; +class QGraphicsSceneMouseEvent; + +/** + * @brief The ESEventAddPolygon class + * This ESEvent manage creation of polygon in an ElementScene + */ +class ESEventAddPolygon : public ESEventInterface +{ + public: + ESEventAddPolygon(ElementScene *scene); + virtual ~ESEventAddPolygon(); + + virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event); + + private: + PartPolygon *m_polygon; +}; + +#endif // ESEVENTADDPOLYGON_H diff --git a/sources/editor/partpolygon.cpp b/sources/editor/partpolygon.cpp index 95ebe77ad..1c3060388 100644 --- a/sources/editor/partpolygon.cpp +++ b/sources/editor/partpolygon.cpp @@ -173,6 +173,45 @@ QET::ScalingMethod PartPolygon::preferredScalingMethod() const { return(QET::RoundScaleRatios); } +/** + * @brief PartPolygon::addPoint + * Add new point to polygon + * @param point + */ +void PartPolygon::addPoint(const QPointF &point) { + QPolygonF poly = polygon(); + poly << point; + setPolygon(poly); +} + +/** + * @brief PartPolygon::setLastPoint + * Set the last point of polygon to @point + * @param point + */ +void PartPolygon::setLastPoint(const QPointF &point) { + QPolygonF poly = polygon(); + + if (poly.size()) + poly.pop_back(); + + poly << point; + setPolygon(poly); +} + +/** + * @brief PartPolygon::removeLastPoint + * Remove the last point of polygon + */ +void PartPolygon::removeLastPoint() { + QPolygonF poly = polygon(); + + if (poly.size()) + poly.pop_back(); + + setPolygon(poly); +} + /** @return le rectangle delimitant cette partie. */ diff --git a/sources/editor/partpolygon.h b/sources/editor/partpolygon.h index ba3dd3b70..d52556c20 100644 --- a/sources/editor/partpolygon.h +++ b/sources/editor/partpolygon.h @@ -58,6 +58,10 @@ class PartPolygon : public CustomElementGraphicPart, public QGraphicsPolygonItem virtual void handleUserTransformation(const QRectF &, const QRectF &); virtual QET::ScalingMethod preferredScalingMethod() const; + void addPoint (const QPointF &point); + void setLastPoint (const QPointF &point); + void removeLastPoint (); + ///PROPERTY // Closed (join the first and last point by a line) Q_PROPERTY(bool closed READ isClosed WRITE setClosed) diff --git a/sources/editor/qetelementeditor.cpp b/sources/editor/qetelementeditor.cpp index a7d71c876..a2ced1bda 100644 --- a/sources/editor/qetelementeditor.cpp +++ b/sources/editor/qetelementeditor.cpp @@ -44,6 +44,7 @@ #include "eseventaddline.h" #include "eseventaddrect.h" #include "eseventaddellipse.h" +#include "eseventaddpolygon.h" #include /* @@ -251,7 +252,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()), this, SLOT(addEllipse())); - connect(add_polygon, SIGNAL(triggered()), ce_scene, SLOT(slot_addPolygon())); + connect(add_polygon, SIGNAL(triggered()), this, SLOT(addPolygon())); connect(add_text, SIGNAL(triggered()), ce_scene, SLOT(slot_addText())); connect(add_arc, SIGNAL(triggered()), ce_scene, SLOT(slot_addArc())); connect(add_terminal, SIGNAL(triggered()), ce_scene, SLOT(slot_addTerminal())); @@ -948,6 +949,14 @@ void QETElementEditor::addEllipse() { ce_scene -> setEventInterface(new ESEventAddEllipse(ce_scene)); } +/** + * @brief QETElementEditor::addPolygon + * Set polygon creation interface to scene + */ +void QETElementEditor::addPolygon() { + ce_scene -> setEventInterface(new ESEventAddPolygon(ce_scene)); +} + /** Lance l'assistant de creation d'un nouvel element. */ diff --git a/sources/editor/qetelementeditor.h b/sources/editor/qetelementeditor.h index 403da7cc7..2bd7c1602 100644 --- a/sources/editor/qetelementeditor.h +++ b/sources/editor/qetelementeditor.h @@ -23,8 +23,10 @@ #include "elementscene.h" #include "orientationset.h" #include "elementslocation.h" + class ElementItemEditor; class ElementView; + /** This class represents an element editor, allowing users to draw, change and configure a particular electrical element. @@ -129,6 +131,7 @@ class QETElementEditor : public QETMainWindow { void addLine(); void addRect(); void addEllipse(); + void addPolygon(); void slot_new(); void slot_open();