diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index f08dbb6cb..549ce1dea 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -95,14 +95,6 @@ void ElementScene::slot_addTerminal() { if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; } -/** - Passe la scene en mode "ajout d'arc de cercle" -*/ -void ElementScene::slot_addArc() { - behavior = Arc; - if (m_event_interface) delete m_event_interface; m_event_interface = nullptr; -} - /** Passe la scene en mode "ajout de champ de texte" */ @@ -136,21 +128,8 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { paste_area_ -> setRect(current_rect); return; } - - QRectF temp_rect; - if (e -> buttons() & Qt::LeftButton) { - switch(behavior) { - case Arc: - temp_rect = current_arc -> rect(); - temp_rect.setBottomRight(event_pos); - current_arc -> setRect(temp_rect); - break; - case Normal: - default: - QGraphicsScene::mouseMoveEvent(e); - } - } - else QGraphicsScene::mouseMoveEvent(e); + + QGraphicsScene::mouseMoveEvent(e); } /** @@ -170,19 +149,8 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) { } QPointF event_pos = e -> scenePos(); if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos); - - if (e -> button() & Qt::LeftButton) { - switch(behavior) { - case Arc: - current_arc = new PartArc(element_editor, 0, this); - current_arc -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0))); - current_arc -> setProperty("antialias", true); - break; - case Normal: - default: - QGraphicsScene::mousePressEvent(e); - } - } else QGraphicsScene::mousePressEvent(e); + + QGraphicsScene::mousePressEvent(e); } /** @@ -218,13 +186,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { if (e -> button() & Qt::LeftButton) { switch(behavior) { - case Arc: - if (qgiManager().manages(current_arc)) break; - current_arc-> setRect(current_arc -> rect().normalized()); - undo_stack.push(new AddPartCommand(tr("arc"), this, current_arc)); - emit(partsAdded()); - endCurrentBehavior(e); - break; case Terminal: terminal = new PartTerminal(element_editor, 0, this); terminal -> setPos(event_pos); diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index 1bf4ebe5b..c76afceea 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -28,7 +28,6 @@ class CustomElementPart; class ElementEditionCommand; class ElementPrimitiveDecorator; class QETElementEditor; -class PartArc; class ESEventInterface; class QKeyEvent; /** @@ -42,7 +41,7 @@ class ElementScene : public QGraphicsScene { // enum public: - enum Behavior { Normal, Circle, Text, Terminal, Arc, TextField, PasteArea }; + enum Behavior { Normal, Circle, Text, Terminal, TextField, PasteArea }; enum ItemOption { SortByZValue = 1, IncludeTerminals = 2, @@ -87,7 +86,6 @@ class ElementScene : public QGraphicsScene { /// Variables related to drawing ESEventInterface *m_event_interface; Behavior behavior; - PartArc *current_arc; QETElementEditor *element_editor; /// Variables to manage the paste area on the scene @@ -165,7 +163,6 @@ class ElementScene : public QGraphicsScene { void slot_move(); void slot_addCircle(); void slot_addText(); - void slot_addArc(); void slot_addTerminal(); void slot_addTextField(); void slot_select(const ElementContent &); diff --git a/sources/editor/esevent/eseventaddarc.cpp b/sources/editor/esevent/eseventaddarc.cpp new file mode 100644 index 000000000..e801a4872 --- /dev/null +++ b/sources/editor/esevent/eseventaddarc.cpp @@ -0,0 +1,189 @@ +/* + 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 "elementscene.h" +#include "partarc.h" +#include "editorcommands.h" +#include "eseventaddarc.h" + +/** + * @brief ESEventAddArc::ESEventAddArc + * @param scene + */ +ESEventAddArc::ESEventAddArc(ElementScene *scene) : + ESEventInterface(scene), + m_arc(nullptr), + m_inverted(false) +{} + +/** + * @brief ESEventAddArc::~ESEventAddArc + */ +ESEventAddArc::~ESEventAddArc() { + if (m_running || m_abort) + delete m_arc; +} + +/** + * @brief ESEventAddPolygon::mousePressEvent + * @param event + * @return + */ +bool ESEventAddArc::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::LeftButton) { + if(!m_running) m_running = true; + QPointF pos = m_scene->snapToGrid(event -> scenePos()); + + //create new arc + if (!m_arc) { + m_arc = new PartArc(m_editor, 0, m_scene); + m_arc -> setRect(QRectF(pos, pos)); + m_arc -> setAngle(90); + m_arc -> setProperty("antialias", true); + m_origin = pos; + return true; + } + + //Add arc to scene + m_arc -> setRect(m_arc->rect().normalized()); + m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Arc"), m_scene, m_arc)); + + //Set m_arc to nullptr for create new ellipse at next mouse press + m_arc = nullptr; + + return true; + } + return false; +} + +/** + * @brief ESEventAddArc::mouseMoveEvent + * @param event + * @return + */ +bool ESEventAddArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + updateHelpCross(event -> scenePos()); + if (!m_arc) return false; + + m_mouse_pos = m_scene -> snapToGrid(event -> scenePos()); + updateArc(); + + return true; +} + +/** + * @brief ESEventAddArc::mouseReleaseEvent + * @param event + * @return + */ +bool ESEventAddArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if (event -> button() == Qt::RightButton) { + if (m_arc) {delete m_arc; m_arc = nullptr;} + else {m_running = false;} + return true; + } + return false; +} + +/** + * @brief ESEventAddArc::keyPressEvent + * @param event + * @return + */ +bool ESEventAddArc::keyPressEvent(QKeyEvent *event) { + if (m_arc && event->key() == Qt::Key_Space) { + m_inverted = m_inverted ? false : true; + updateArc(); + return true; + } + + return (ESEventInterface::keyPressEvent(event)); +} + +/** + * @brief ESEventAddArc::updateArc + * Redraw the arc with curent value + */ +void ESEventAddArc::updateArc() { + + qreal width = (m_mouse_pos.x() - m_origin.x())*2; + if (width < 0) width *= -1; + qreal height = (m_mouse_pos.y() - m_origin.y())*2; + if (height < 0) height *= -1; + + QPointF pos_ = m_origin; + + //Draw arc inverted + if (m_inverted) { + //Adjust the start angle to be snapped at the origin point of draw + if (m_mouse_pos.y() > m_origin.y()) { + + if (m_mouse_pos.x() > m_origin.x()) { + pos_.ry() -= height/2; + m_arc->setStartAngle(180); + } + else { + pos_.rx() -= width/2; + m_arc->setStartAngle(90); + } + } + else { + if (m_mouse_pos.x() > m_origin.x()) { + pos_.ry() -= height; + pos_.rx() -= width/2; + m_arc->setStartAngle(270); + } + else { + pos_.rx() -= width; + pos_.ry() -= height/2; + m_arc->setStartAngle(0); + } + } + } + + //Draw arc non inverted + else { + //Adjust the start angle to be snapped at the origin point of draw + if (m_mouse_pos.y() > m_origin.y()) { + + if (m_mouse_pos.x() > m_origin.x()) { + pos_.rx() -= width/2; + m_arc->setStartAngle(0); + } + else { + pos_.rx() -= width; + pos_.ry() -= height/2; + m_arc->setStartAngle(270); + } + } + else { + if (m_mouse_pos.x() > m_origin.x()) { + pos_.ry() -= height/2; + m_arc->setStartAngle(90); + } + else { + pos_.rx() -= width/2; + pos_.ry() -= height; + m_arc->setStartAngle(180); + } + } + } + + m_arc -> setRect(QRectF(pos_, QSizeF(width, height))); +} diff --git a/sources/editor/esevent/eseventaddarc.h b/sources/editor/esevent/eseventaddarc.h new file mode 100644 index 000000000..4aac4de34 --- /dev/null +++ b/sources/editor/esevent/eseventaddarc.h @@ -0,0 +1,51 @@ +/* + 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 ESEVENTADDARC_H +#define ESEVENTADDARC_H + +#include "eseventinterface.h" + +class ElementScene; +class PartArc; +class QGraphicsSceneMouseEvent; + +/** + * @brief The ESEventAddArc class + * This ESEvent manage creation of arc in an ElementScene + */ +class ESEventAddArc : public ESEventInterface +{ + public: + ESEventAddArc(ElementScene *scene); + virtual ~ESEventAddArc(); + + virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event); + virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event); + virtual bool keyPressEvent (QKeyEvent *event); + + private: + void updateArc (); + + private: + PartArc *m_arc; + QPointF m_origin, m_mouse_pos; + bool m_inverted; +}; + +#endif // ESEVENTADDARC_H diff --git a/sources/editor/esevent/eseventaddellipse.cpp b/sources/editor/esevent/eseventaddellipse.cpp index 4f2934ddc..28a52ad4d 100644 --- a/sources/editor/esevent/eseventaddellipse.cpp +++ b/sources/editor/esevent/eseventaddellipse.cpp @@ -54,7 +54,6 @@ bool ESEventAddEllipse::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (!m_ellipse) { m_ellipse = new PartEllipse(m_editor, 0, m_scene); m_ellipse -> setRect(QRectF(pos, pos)); - m_ellipse -> setProperty("antialias", true); m_origin = pos; return true; } diff --git a/sources/editor/qetelementeditor.cpp b/sources/editor/qetelementeditor.cpp index a2ced1bda..4d3a29ceb 100644 --- a/sources/editor/qetelementeditor.cpp +++ b/sources/editor/qetelementeditor.cpp @@ -45,6 +45,7 @@ #include "eseventaddrect.h" #include "eseventaddellipse.h" #include "eseventaddpolygon.h" +#include "eseventaddarc.h" #include /* @@ -254,7 +255,7 @@ void QETElementEditor::setupActions() { connect(add_ellipse, SIGNAL(triggered()), this, SLOT(addEllipse())); 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_arc, SIGNAL(triggered()), this, SLOT(addArc())); connect(add_terminal, SIGNAL(triggered()), ce_scene, SLOT(slot_addTerminal())); connect(add_textfield, SIGNAL(triggered()), ce_scene, SLOT(slot_addTextField())); @@ -957,6 +958,14 @@ void QETElementEditor::addPolygon() { ce_scene -> setEventInterface(new ESEventAddPolygon(ce_scene)); } +/** + * @brief QETElementEditor::addArc + * Set arc creation interface to scene + */ +void QETElementEditor::addArc() { + ce_scene -> setEventInterface(new ESEventAddArc(ce_scene)); +} + /** Lance l'assistant de creation d'un nouvel element. */ diff --git a/sources/editor/qetelementeditor.h b/sources/editor/qetelementeditor.h index 2bd7c1602..15ec7f00b 100644 --- a/sources/editor/qetelementeditor.h +++ b/sources/editor/qetelementeditor.h @@ -132,6 +132,7 @@ class QETElementEditor : public QETMainWindow { void addRect(); void addEllipse(); void addPolygon(); + void addArc(); void slot_new(); void slot_open();