diff --git a/qelectrotech.pro b/qelectrotech.pro index eb2861eaf..ce0ce47fd 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -63,16 +63,18 @@ DEFINES += QET_ALLOW_OVERRIDE_CD_OPTION TEMPLATE = app DEPENDPATH += . -INCLUDEPATH += sources sources/editor sources/titleblock sources/ui sources/qetgraphicsitem sources/richtext sources/factory sources/properties +INCLUDEPATH += sources sources/editor sources/titleblock sources/ui sources/qetgraphicsitem sources/richtext sources/factory sources/properties sources/dvevent # Fichiers sources HEADERS += $$files(sources/*.h) $$files(sources/ui/*.h) $$files(sources/editor/*.h) $$files(sources/titleblock/*.h) $$files(sources/richtext/*.h) $$files(sources/qetgraphicsitem/*.h) $$files(sources/factory/*.cpp) \ $$files(sources/properties/*.h) \ - $$files(sources/editor/ui/*.h) + $$files(sources/editor/ui/*.h) \ + $$files(sources/dvevent/*.h) SOURCES += $$files(sources/*.cpp) $$files(sources/editor/*.cpp) $$files(sources/titleblock/*.cpp) $$files(sources/richtext/*.cpp) $$files(sources/ui/*.cpp) $$files(sources/qetgraphicsitem/*.cpp) $$files(sources/factory/*.cpp) \ $$files(sources/properties/*.cpp) \ - $$files(sources/editor/ui/*.cpp) + $$files(sources/editor/ui/*.cpp) \ + $$files(sources/dvevent/*.cpp) # Liste des fichiers qui seront incorpores au binaire en tant que ressources Qt RESOURCES += qelectrotech.qrc diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index 4411d3598..e8323ce5b 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -41,18 +41,18 @@ #include #include "factory/elementfactory.h" #include "diagrampropertiesdialog.h" +#include "dveventinterface.h" /** Constructeur @param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise @param parent Le QWidget parent de cette vue de schema */ -DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : QGraphicsView(parent), newShapeItem(nullptr){ +DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : QGraphicsView(parent) { grabGesture(Qt::PinchGesture); setAttribute(Qt::WA_DeleteOnClose, true); setInteractive(true); - current_behavior = noAction; - m_polyline_added = false; + m_event_interface = nullptr; QString whatsthis = tr( "Ceci est la zone dans laquelle vous concevez vos sch\351mas en y ajoutant" @@ -308,14 +308,12 @@ void DiagramView::handleTitleBlockDrop(QDropEvent *e) { /** * @brief DiagramView::handleTextDrop - *handle the drop of text, html markup are automatically detected. + *handle the drop of text * @param e the QDropEvent describing the current drag'n drop */ void DiagramView::handleTextDrop(QDropEvent *e) { - if (e -> mimeData() -> hasText()) { - if (e -> mimeData() -> hasHtml()) addDiagramTextAtPos(e->pos()) -> setHtml(e -> mimeData() -> text()); - else addDiagramTextAtPos(e -> pos(), e -> mimeData() -> text()); - } + if (scene -> isReadOnly() || (e -> mimeData() -> hasText() == false) ) return; + scene -> undoStack().push(new AddTextCommand(scene, new IndependentTextItem (e -> mimeData() -> text()), mapToScene(e->pos()))); } /** @@ -463,63 +461,25 @@ void DiagramView::pasteHere() { * click to add an independent text field */ void DiagramView::mousePressEvent(QMouseEvent *e) { - rubber_band_origin = mapToScene(e -> pos()); if (fresh_focus_in_) { switchToVisualisationModeIfNeeded(e); fresh_focus_in_ = false; } - if (isInteractive() && !scene -> isReadOnly() && e -> button() == Qt::LeftButton) { - switch (current_behavior) { - case noAction: - QGraphicsView::mousePressEvent(e); - break; - case addingText: - addDiagramTextAtPos(rubber_band_origin); - current_behavior = noAction; - break; - case addingImage: - addDiagramImageAtPos(rubber_band_origin); - current_behavior = noAction; - break; - case addingLine: - newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Line); - scene -> addItem(newShapeItem); - break; - case addingRectangle: - newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Rectangle); - scene -> addItem(newShapeItem); - break; - case addingEllipse: - newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Ellipse); - scene -> addItem(newShapeItem); - break; - case addingPolyline: - if (!m_polyline_added) { - setContextMenuPolicy(Qt::NoContextMenu); //< for finish the polyline we must to right click, - // We disable the context menu - newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Polyline); - scene -> addItem(newShapeItem); - m_polyline_added = true; - } else { - newShapeItem->setNextPoint(Diagram::snapToGrid(rubber_band_origin)); //< this point is ok for pos - newShapeItem->setNextPoint(Diagram::snapToGrid(rubber_band_origin)); //< Add new point for next segment. the pos of this point - // can be changed by calling QetShapItem::setP2() - } - break; - case dragView: - current_behavior = noAction; - QGraphicsView::mousePressEvent(e); - break; - default: - current_behavior = noAction; - break; + if (m_event_interface) { + if (m_event_interface -> mousePressEvent(e)) { + if (m_event_interface->isFinish()) { + emit (itemAdded()); + delete m_event_interface; m_event_interface = nullptr; + } + return; } } //Start drag view when hold the middle button - else if (e -> button() == Qt::MidButton) { + if (e -> button() == Qt::MidButton) { + rubber_band_origin = mapToScene(e -> pos()); setCursor(Qt::ClosedHandCursor); center_view_ = mapToScene(this -> viewport() -> rect().center()); } @@ -532,6 +492,16 @@ void DiagramView::mousePressEvent(QMouseEvent *e) { * Manage the event move mouse */ void DiagramView::mouseMoveEvent(QMouseEvent *e) { + + if (m_event_interface) { + if (m_event_interface -> mouseMoveEvent(e)) { + if (m_event_interface->isFinish()) { + emit (itemAdded()); + delete m_event_interface; m_event_interface = nullptr; + } + return; + } + } //Drag the view if (e -> buttons() == Qt::MidButton) { QPointF move = rubber_band_origin - mapToScene(e -> pos()); @@ -539,12 +509,6 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e) { center_view_ = mapToScene( this -> viewport() -> rect().center() ); } - //Add point P2 to the curent shape - else if ( (e -> buttons() == Qt::LeftButton && current_behavior &addingShape) || - (current_behavior == addingPolyline && m_polyline_added) ) { - newShapeItem->setP2(mapToScene(e->pos())); - } - else QGraphicsView::mouseMoveEvent(e); } @@ -553,28 +517,19 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e) { * Manage event release click mouse */ void DiagramView::mouseReleaseEvent(QMouseEvent *e) { + + if (m_event_interface) { + if (m_event_interface -> mouseReleaseEvent(e)) { + if (m_event_interface->isFinish()) { + emit (itemAdded()); + delete m_event_interface; m_event_interface = nullptr; + } + return; + } + } //Stop drag view if (e -> button() == Qt::MidButton) setCursor(Qt::ArrowCursor); - // Add shape define by 2 points - else if (current_behavior & adding2PShape) { - // place it to the good position with an undo command - scene -> undoStack().push(new AddShapeCommand(scene, newShapeItem, rubber_band_origin)); - emit(itemAdded()); - current_behavior = noAction; - } - - // Add polyline shape - else if (e -> button() == Qt::RightButton && current_behavior == addingPolyline) { - newShapeItem->setP2(rubber_band_origin); - scene -> undoStack().push(new AddShapeCommand(scene, newShapeItem, newShapeItem->scenePos())); - emit(itemAdded()); - current_behavior = noAction; - m_polyline_added = false; - setContextMenuPolicy(Qt::DefaultContextMenu); //< the polyline is finish, - // We make context menu available - } - else QGraphicsView::mouseReleaseEvent(e); } @@ -591,6 +546,16 @@ bool DiagramView::gestures() const { @param e QWheelEvent */ void DiagramView::wheelEvent(QWheelEvent *e) { + if (m_event_interface) { + if (m_event_interface -> wheelEvent(e)) { + if (m_event_interface->isFinish()) { + emit (itemAdded()); + delete m_event_interface; m_event_interface = nullptr; + } + return; + } + } + //Zoom and scrolling if ( gestures() ) { if (e -> modifiers() & Qt::ControlModifier) @@ -1040,7 +1005,6 @@ bool DiagramView::event(QEvent *e) { bool DiagramView::switchToVisualisationModeIfNeeded(QInputEvent *e) { if (isCtrlShifting(e) && !selectedItemHasFocus()) { if (dragMode() != QGraphicsView::ScrollHandDrag) { - current_behavior = dragView; setVisualisationMode(); return(true); } @@ -1054,9 +1018,8 @@ bool DiagramView::switchToVisualisationModeIfNeeded(QInputEvent *e) { otherwise. */ bool DiagramView::switchToSelectionModeIfNeeded(QInputEvent *e) { - if (current_behavior == dragView && !selectedItemHasFocus() && !isCtrlShifting(e)) { + if (!selectedItemHasFocus() && !isCtrlShifting(e)) { setSelectionMode(); - current_behavior = noAction; return(true); } return(false); @@ -1092,16 +1055,6 @@ bool DiagramView::selectedItemHasFocus() { ); } -/** - Passe le DiagramView en mode "ajout de texte". Un clic cree alors un - nouveau champ de texte. -*/ -void DiagramView::addText() { - if (scene -> isReadOnly()) return; - current_behavior = addingText; -} - - /** To edit the text through the htmlEditor */ void DiagramView::editText() { @@ -1121,56 +1074,6 @@ void DiagramView::editText() { else texts_to_edit.at(0)->edit(); } - -/** -* @brief DiagramView::addImage -*/ -void DiagramView::addImage() { - if (scene -> isReadOnly()) return; - - QString pathPictures = QDesktopServices::storageLocation ( QDesktopServices::PicturesLocation ); - QString fileName = QFileDialog::getOpenFileName(this, tr("Selectionner une image..."), pathPictures.toStdString().c_str(), tr("Image Files (*.png *.jpg *.bmp *.svg)")); - if(fileName.isEmpty()) { - emit ImageAddedCanceled(false); - return; - } - - int ret = image_to_add_.load(fileName); - if(!ret){ - QMessageBox::critical(this, tr("Erreur"), tr("Impossible de charger l'image...D\351soler :(")); - return; - } - current_behavior = addingImage; -} - -/** -* @brief DiagramView::addLine -*/ -void DiagramView::addLine() { - current_behavior = addingLine; -} - -/** -* @brief DiagramView::addRectangle -*/ -void DiagramView::addRectangle() { - current_behavior = addingRectangle; -} - -/** -* @brief DiagramView::addEllipse -*/ -void DiagramView::addEllipse() { - current_behavior = addingEllipse; -} - -/** - * @brief DiagramView::addPolyline - */ -void DiagramView::addPolyline() { - current_behavior = addingPolyline; -} - /** * @brief DiagramView::editImage * open edit image dialog if only one image is selected @@ -1200,51 +1103,12 @@ void DiagramView::editShape() { } /** -* @brief DiagramView::addDiagramImageAtPos -* @param pos -* @return -*/ -DiagramImageItem *DiagramView::addDiagramImageAtPos(const QPointF &pos) { - - if (!isInteractive() || scene -> isReadOnly()) return(0); - - // cree un nouveau champ image - DiagramImageItem *Imageitem = new DiagramImageItem( QPixmap::fromImage(image_to_add_) ); - - // le place a la position pos en gerant l'annulation - scene -> undoStack().push(new AddImageCommand(scene, Imageitem, pos)); - adjustSceneRect(); - - // emet le signal ImageAdded - emit(itemAdded()); - - return(Imageitem); - -} - -/** - Cree un nouveau champ de texte et le place a la position pos - en gerant l'annulation ; enfin, le signal textAdded est emis. - @param pos Position du champ de texte ajoute - @return le champ de texte ajoute -*/ -IndependentTextItem *DiagramView::addDiagramTextAtPos(const QPointF &pos, const QString &text) { - if (!isInteractive() || scene -> isReadOnly()) return(0); - - // cree un nouveau champ de texte - IndependentTextItem *iti; - if (text.isEmpty()) { - iti = new IndependentTextItem("_"); - } else iti = new IndependentTextItem(text); - - // le place a la position pos en gerant l'annulation - scene -> undoStack().push(new AddTextCommand(scene, iti, pos)); - adjustSceneRect(); - - // emet le signal textAdded - emit(itemAdded()); - - return(iti); + * @brief DiagramView::setEventInterface + * Set an event interface to diagram view. + */ +void DiagramView::setEventInterface(DVEventInterface *interface) { + if (m_event_interface) delete m_event_interface; + m_event_interface = interface; } /** @@ -1295,9 +1159,21 @@ QETDiagramEditor *DiagramView::diagramEditor() const { } /** - Gere les double-clics sur le schema -*/ + * @brief DiagramView::mouseDoubleClickEvent + * @param e + */ void DiagramView::mouseDoubleClickEvent(QMouseEvent *e) { + + if (m_event_interface) { + if (m_event_interface -> mouseDoubleClickEvent(e)) { + if (m_event_interface->isFinish()) { + emit (itemAdded()); + delete m_event_interface; m_event_interface = nullptr; + } + return; + } + } + BorderTitleBlock &bi = scene -> border_and_titleblock; //Get the rectangle of the titleblock diff --git a/sources/diagramview.h b/sources/diagramview.h index e41e827d9..853ab61c0 100644 --- a/sources/diagramview.h +++ b/sources/diagramview.h @@ -20,13 +20,12 @@ #include #include "elementslocation.h" #include "templatelocation.h" -#include "qetgraphicsitem/qetshapeitem.h" + class Conductor; class Diagram; -class Element; -class IndependentTextItem; -class DiagramImageItem; class QETDiagramEditor; +class DVEventInterface; + /** This class provides a widget to render an electric diagram in an editable, interactive way. @@ -38,18 +37,6 @@ class DiagramView : public QGraphicsView { public: DiagramView(Diagram * = 0, QWidget * = 0); virtual ~DiagramView(); - - Q_ENUMS(behavior) - enum behavior {noAction =1, - addingText =2, - addingImage =4, - addingLine =8, - addingRectangle =16, - addingEllipse =32, - adding2PShape =56, - addingPolyline =64, - addingShape =120, - dragView =124}; private: DiagramView(const DiagramView &); @@ -61,15 +48,12 @@ class DiagramView : public QGraphicsView { QAction *paste_here; QAction *find_element_; QPoint paste_here_pos; - behavior current_behavior; bool fresh_focus_in_; ///< Indicate the focus was freshly gained ElementsLocation next_location_; QPoint next_position_; QPointF center_view_; - QImage image_to_add_; - QetShapeItem *newShapeItem; QPointF rubber_band_origin; - bool m_polyline_added; + DVEventInterface *m_event_interface; // methods public: @@ -87,15 +71,9 @@ class DiagramView : public QGraphicsView { bool hasDeletableItems(); void addText(); void editText(); - void addImage(); - void addLine(); - void addRectangle(); - void addEllipse(); - void addPolyline(); void editImage(); void editShape(); - IndependentTextItem *addDiagramTextAtPos(const QPointF &, const QString &text = 0); - DiagramImageItem *addDiagramImageAtPos(const QPointF &); + void setEventInterface (DVEventInterface *interface); protected: virtual void mouseDoubleClickEvent(QMouseEvent *); @@ -147,8 +125,6 @@ class DiagramView : public QGraphicsView { void editTitleBlockTemplate(const QString &, bool); /// Signal emitted after an item is added void itemAdded(); - /// Signal emmitted fater windows selection image have been canceled - void ImageAddedCanceled(bool); /// Signal emmitted when diagram must be show void showDiagram (Diagram *); diff --git a/sources/dvevent/dveventaddimage.cpp b/sources/dvevent/dveventaddimage.cpp new file mode 100644 index 000000000..44f32ad88 --- /dev/null +++ b/sources/dvevent/dveventaddimage.cpp @@ -0,0 +1,155 @@ +/* + 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 "diagramimageitem.h" +#include "diagramcommands.h" +#include "dveventaddimage.h" +#include "diagramview.h" +#include "diagram.h" +#include + +/** + * @brief DVEventAddImage::DVEventAddImage + * Defaultconstructor + * @param dv : diagram view where operate this event + */ +DVEventAddImage::DVEventAddImage(DiagramView *dv) : + DVEventInterface (dv), + m_image (nullptr), + m_is_added (false) +{ + openDialog(); +} + +/** + * @brief DVEventAddImage::~DVEventAddImage + */ +DVEventAddImage::~DVEventAddImage() { + if (m_running) { + if (m_is_added) m_diagram -> removeItem(m_image); + delete m_image; + } + m_dv -> setContextMenuPolicy(Qt::DefaultContextMenu); +} + +/** + * @brief DVEventAddImage::mousePressEvent + * Action when mouse is pressed + * @param event : event of mouse pressed + * @return : true if this event is managed, otherwise false + */ +bool DVEventAddImage::mousePressEvent(QMouseEvent *event) { + if (m_image && event -> button() == Qt::LeftButton) { + m_diagram -> undoStack().push(new AddImageCommand(m_diagram, m_image, m_dv->mapToScene(event->pos()))); + m_dv -> setContextMenuPolicy(Qt::DefaultContextMenu); + m_running = false; + return true; + } + + if (m_image && event -> button() == Qt::RightButton) { + m_image -> rotateBy(90); + return true; + } + + return false; +} + +/** + * @brief DVEventAddImage::mouseMoveEvent + * Action when mouse move + * @param event : event of mouse move + * @return : true if this event is managed, otherwise false + */ +bool DVEventAddImage::mouseMoveEvent(QMouseEvent *event) { + //@m_image isn't loaded, we return true and @m_running return + //so the diagram view owner of this DVEevent will delete it. + //if (!m_image) return true; + + if (!m_image || event -> buttons() != Qt::NoButton) return false; + + QPointF pos = m_dv -> mapToScene(event -> pos()); + + if (!m_is_added) { + m_dv -> setContextMenuPolicy(Qt::NoContextMenu); + m_diagram -> addItem(m_image); + m_is_added = true; + } + m_image -> setPos(pos - m_image -> boundingRect().center()); + return true; +} + +/** + * @brief DVEventAddImage::mouseDoubleClickEvent + * This methode is only use to overwrite double click. + * When double clic, image propertie dialog isn't open. + * @param event : event of mouse double click + * @return : true if this event is managed, otherwise false + */ +bool DVEventAddImage::mouseDoubleClickEvent(QMouseEvent *event) { + Q_UNUSED(event); + return true; +} + +/** + * @brief DVEventAddImage::wheelEvent + * Action when mouse wheel is rotate + * @param event : event of mouse wheel + * @return : true if this event is managed, otherwise false + */ +bool DVEventAddImage::wheelEvent(QWheelEvent *event) { + + if (!m_is_added || !m_image || event -> modifiers() != Qt::CTRL) return false; + + qreal scaling = m_image->scale(); + event->delta() > 1? scaling += 0.01 : scaling -= 0.01; + if (scaling>0.01 && scaling <= 2) + m_image->setScale(scaling); + + return true; +} + +/** + * @brief DVEventAddImage::isNull + * @return true if image can't be loaded, otherwise return false. + */ +bool DVEventAddImage::isNull() const { + if (!m_image) return true; + return false; +} + +/** + * @brief DVEventAddImage::openDialog + * Open dialog for select the image to add. + */ +void DVEventAddImage::openDialog() { + if (m_diagram -> isReadOnly()) return; + + //Open dialog for select image + QString pathPictures = QDesktopServices::storageLocation ( QDesktopServices::PicturesLocation ); + QString fileName = QFileDialog::getOpenFileName(m_dv, QObject::tr("Selectionner une image..."), pathPictures, QObject::tr("Image Files (*.png *.jpg *.bmp *.svg)")); + + if (fileName.isEmpty()) return; + + QImage image(fileName); + if(image.isNull()) { + QMessageBox::critical(m_dv, QObject::tr("Erreur"), QObject::tr("Impossible de charger l'image.")); + return; + } + + m_image = new DiagramImageItem (QPixmap::fromImage(image)); + m_running = true; +} diff --git a/sources/dvevent/dveventaddimage.h b/sources/dvevent/dveventaddimage.h new file mode 100644 index 000000000..ed9a38e65 --- /dev/null +++ b/sources/dvevent/dveventaddimage.h @@ -0,0 +1,50 @@ +/* + 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 DVEVENTADDIMAGE_H +#define DVEVENTADDIMAGE_H + +#include "dveventinterface.h" + +class DiagramImageItem; + +/** + * @brief The DVEventAddImage class + * This dv event, open an image and add it to diagram view. + */ +class DVEventAddImage : public DVEventInterface +{ + public: + DVEventAddImage(DiagramView *dv); + virtual ~DVEventAddImage(); + + virtual bool mousePressEvent (QMouseEvent *event); + virtual bool mouseMoveEvent (QMouseEvent *event); + virtual bool mouseDoubleClickEvent (QMouseEvent *event); + virtual bool wheelEvent (QWheelEvent *event); + + bool isNull () const; + + private: + void openDialog(); + + DiagramImageItem *m_image; + bool m_is_added; + +}; + +#endif // DVEVENTADDIMAGE_H diff --git a/sources/dvevent/dveventaddshape.cpp b/sources/dvevent/dveventaddshape.cpp new file mode 100644 index 000000000..610d70694 --- /dev/null +++ b/sources/dvevent/dveventaddshape.cpp @@ -0,0 +1,130 @@ +/* + 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 "dveventaddshape.h" +#include "diagramview.h" +#include "qetshapeitem.h" +#include "diagram.h" +#include "diagramcommands.h" +#include + +/** + * @brief DVEventAddShape::DVEventAddShape + * Default constructor + * @param dv, the diagram view where operate this event + * @param shape_type, the shape type to draw + */ +DVEventAddShape::DVEventAddShape(DiagramView *dv, QetShapeItem::ShapeType shape_type) : + DVEventInterface(dv), + m_shape_type (shape_type), + m_shape_item (nullptr) +{} + +/** + * @brief DVEventAddShape::~DVEventAddShape + */ +DVEventAddShape::~DVEventAddShape() { + if (m_running) { + m_diagram -> removeItem(m_shape_item); + delete m_shape_item; + } + m_dv -> setContextMenuPolicy(Qt::DefaultContextMenu); +} + +/** + * @brief DVEventAddShape::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) { + + if (!m_dv->isInteractive() && m_diagram->isReadOnly()) return false; + + QPointF pos = m_dv->mapToScene(event->pos()); + + //@m_running false => shape isn't created yet, we create a new shape + if (m_running == false && event -> button() == Qt::LeftButton) { + m_shape_item = new QetShapeItem(pos, pos, m_shape_type); + m_dv -> setContextMenuPolicy (Qt::NoContextMenu); + m_diagram -> addItem (m_shape_item); + m_running = true; + return true; + } + + //At this point m_shape_item must be created + if (!m_shape_item) return false; + + // Next left click finish all shape item except the polyline + if (m_shape_type != QetShapeItem::Polyline && event->button() == Qt::LeftButton) { + m_shape_item -> setP2 (pos); + m_diagram -> undoStack().push (new AddShapeCommand(m_diagram, m_shape_item, pos)); + m_dv -> setContextMenuPolicy(Qt::DefaultContextMenu); + m_running = false; + return true; + } + + // Next left click create new segment for polyline + if (m_shape_type == QetShapeItem::Polyline && event -> button() == Qt::LeftButton) { + m_shape_item -> setNextPoint (Diagram::snapToGrid(pos)); //< this point is ok for pos + m_shape_item -> setNextPoint (Diagram::snapToGrid(pos)); //< Add new point for next segment. the pos of this point + //< can be changed by calling QetShapItem::setP2() + return true; + } + + // If shape item is polyline and click is right button, the shape item is finish + // m_running is set to false at the release of right button. + if (m_shape_type == QetShapeItem::Polyline && event -> button() == Qt::RightButton) { + m_shape_item -> setP2 (pos); + m_diagram -> undoStack().push(new AddShapeCommand(m_diagram, m_shape_item, pos)); + return true; + } + + return false; +} + +/** + * @brief DVEventAddShape::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) { + if (!m_running) return false; + if (m_shape_item && event -> buttons() == Qt::NoButton) { + m_shape_item -> setP2 (m_dv -> mapToScene (event -> pos())); + return true; + } + return false; +} + +/** + * @brief DVEventAddShape::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) { + //When the shape is polyline, we set default context menu to diagram view + //only when the right button is released + if (m_shape_type == QetShapeItem::Polyline && event -> button() == Qt::RightButton ) { + m_dv -> setContextMenuPolicy(Qt::DefaultContextMenu); + m_running = false; + return true; + } + return false; +} diff --git a/sources/dvevent/dveventaddshape.h b/sources/dvevent/dveventaddshape.h new file mode 100644 index 000000000..5c3523375 --- /dev/null +++ b/sources/dvevent/dveventaddshape.h @@ -0,0 +1,40 @@ +/* + 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 DVEVENTADDSHAPE_H +#define DVEVENTADDSHAPE_H + +#include "dveventinterface.h" +#include "qetshapeitem.h" + +class QMouseEvent; + +class DVEventAddShape : public DVEventInterface +{ + 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); + + protected: + QetShapeItem::ShapeType m_shape_type; + QetShapeItem *m_shape_item; +}; + +#endif // DVEVENTADDSHAPE_H diff --git a/sources/dvevent/dveventaddtext.cpp b/sources/dvevent/dveventaddtext.cpp new file mode 100644 index 000000000..9e51c3ce7 --- /dev/null +++ b/sources/dvevent/dveventaddtext.cpp @@ -0,0 +1,39 @@ +/* + 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 "dveventaddtext.h" +#include "independenttextitem.h" +#include "diagram.h" +#include "diagramcommands.h" +#include "diagramview.h" +#include + +DVEventAddText::DVEventAddText(DiagramView *dv) : + DVEventInterface (dv) +{} + +DVEventAddText::~DVEventAddText() {} + +bool DVEventAddText::mousePressEvent(QMouseEvent *event) { + if (event->button() == Qt::LeftButton) { + m_diagram -> undoStack().push(new AddTextCommand(m_diagram, + new IndependentTextItem("_"), + m_dv -> mapToScene(event -> pos()))); + return true; + } + return false; +} diff --git a/sources/dvevent/dveventaddtext.h b/sources/dvevent/dveventaddtext.h new file mode 100644 index 000000000..da4785646 --- /dev/null +++ b/sources/dvevent/dveventaddtext.h @@ -0,0 +1,31 @@ +/* + 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 DVEVENTADDTEXT_H +#define DVEVENTADDTEXT_H + +#include "dveventinterface.h" + +class DVEventAddText : public DVEventInterface +{ + public: + DVEventAddText(DiagramView *dv); + virtual ~DVEventAddText (); + virtual bool mousePressEvent (QMouseEvent *event); +}; + +#endif // DVEVENTADDTEXT_H diff --git a/sources/dvevent/dveventinterface.cpp b/sources/dvevent/dveventinterface.cpp new file mode 100644 index 000000000..446b89d45 --- /dev/null +++ b/sources/dvevent/dveventinterface.cpp @@ -0,0 +1,62 @@ +/* + 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 "dveventinterface.h" +#include "diagramview.h" +#include + +DVEventInterface::DVEventInterface(DiagramView *dv) : + m_dv(dv), + m_diagram(dv->diagram()), + m_running(false) +{ +} + +DVEventInterface::~DVEventInterface() {}; + +bool DVEventInterface::mouseDoubleClickEvent(QMouseEvent *event) { + Q_UNUSED (event); + return false; +} + +bool DVEventInterface::mousePressEvent(QMouseEvent *event) { + Q_UNUSED (event); + return false; +} + +bool DVEventInterface::mouseMoveEvent(QMouseEvent *event) { + Q_UNUSED (event); + return false; +} + +bool DVEventInterface::mouseReleaseEvent(QMouseEvent *event) { + Q_UNUSED (event); + return false; +} + +bool DVEventInterface::wheelEvent(QWheelEvent *event) { + Q_UNUSED (event); + return false; +} + +bool DVEventInterface::isRunning() const { + return m_running; +} + +bool DVEventInterface::isFinish() const { + return !m_running; +} diff --git a/sources/dvevent/dveventinterface.h b/sources/dvevent/dveventinterface.h new file mode 100644 index 000000000..58113b0e8 --- /dev/null +++ b/sources/dvevent/dveventinterface.h @@ -0,0 +1,54 @@ +/* + 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 DVEVENTINTERFACE_H +#define DVEVENTINTERFACE_H + +class QMouseEvent; +class QWheelEvent; +class DiagramView; +class Diagram; + +/** + * @brief The DVEventInterface class + * This class is the main interface for manage event of a Diagram View. + * This do nothing, for create new event behavior, we must to create new class from this. + * Each method return a bool: True if the methode do something else return false. + * Each method of DVEventInterface return false; + * isRunning() return true if action is started but not finish. By default return false. + * isFinish() return true when the action is finish, or not started. By default return true. + */ +class DVEventInterface +{ + public: + DVEventInterface(DiagramView *dv); + virtual ~DVEventInterface () = 0; + virtual bool mouseDoubleClickEvent (QMouseEvent *event); + virtual bool mousePressEvent (QMouseEvent *event); + virtual bool mouseMoveEvent (QMouseEvent *event); + virtual bool mouseReleaseEvent (QMouseEvent *event); + virtual bool wheelEvent (QWheelEvent *event); + virtual bool isRunning () const; + virtual bool isFinish () const; + + protected: + DiagramView *m_dv; + Diagram *m_diagram; + bool m_running; +}; + +#endif // DVEVENTINTERFACE_H diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index bd3a92221..a6a476f5f 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -34,6 +34,10 @@ #include "genericpanel.h" #include "nomenclature.h" #include "diagramfoliolist.h" +#include "qetshapeitem.h" +#include "dveventaddimage.h" +#include "dveventaddshape.h" +#include "dveventaddtext.h" #include "ui/dialogautonum.h" @@ -1510,7 +1514,8 @@ void QETDiagramEditor::slot_resetConductors() { * add text to curent diagram */ void QETDiagramEditor::slot_addText() { - if (DiagramView *dv = currentDiagram()) dv -> addText(); + if (DiagramView *dv = currentDiagram()) + dv -> setEventInterface(new DVEventAddText(dv)); } /** @@ -1518,7 +1523,15 @@ void QETDiagramEditor::slot_addText() { * add image to curent diagram */ void QETDiagramEditor::slot_addImage() { - if (DiagramView *dv = currentDiagram()) dv -> addImage(); + if (DiagramView *dv = currentDiagram()) { + DVEventAddImage *event = new DVEventAddImage(dv); + if (event -> isNull()) { + delete event; + m_add_item_actions_group.checkedAction()->setChecked(false); + } else { + dv -> setEventInterface(event); + } + } } /** @@ -1526,15 +1539,16 @@ void QETDiagramEditor::slot_addImage() { * add line to curent diagram */ void QETDiagramEditor::slot_addLine() { - if (DiagramView *dv = currentDiagram()) dv -> addLine(); + if (DiagramView *dv = currentDiagram()) + dv -> setEventInterface(new DVEventAddShape(dv, QetShapeItem::Line)); } /** * @brief QETDiagramEditor::slot_addRectangle - * add recatngle to curent diagram + * add rectangle to curent diagram */ void QETDiagramEditor::slot_addRectangle() { - if (DiagramView *dv = currentDiagram()) dv -> addRectangle(); + if (DiagramView *dv = currentDiagram()) dv -> setEventInterface(new DVEventAddShape(dv, QetShapeItem::Rectangle)); } /** @@ -1542,7 +1556,7 @@ void QETDiagramEditor::slot_addRectangle() { * add ellipse to curent diagram */ void QETDiagramEditor::slot_addEllipse() { - if (DiagramView *dv = currentDiagram()) dv -> addEllipse(); + if (DiagramView *dv = currentDiagram()) dv -> setEventInterface(new DVEventAddShape(dv, QetShapeItem::Ellipse)); } /** @@ -1550,7 +1564,7 @@ void QETDiagramEditor::slot_addEllipse() { * add polyline to current diagram */ void QETDiagramEditor::slot_addPolyline() { - if (DiagramView *dv = currentDiagram()) dv -> addPolyline(); + if (DiagramView *dv = currentDiagram()) dv -> setEventInterface(new DVEventAddShape(dv, QetShapeItem::Polyline)); } /** @@ -1851,7 +1865,6 @@ void QETDiagramEditor::diagramWasAdded(DiagramView *dv) { undo_group.addStack(&(dv -> diagram() -> undoStack())); connect(dv, SIGNAL(selectionChanged()), this, SLOT(slot_updateComplexActions())); connect(dv, SIGNAL(modeChanged()), this, SLOT(slot_updateModeActions())); - connect(dv, SIGNAL(ImageAddedCanceled(bool)), this, SLOT(addItemFinish())); connect(dv, SIGNAL(itemAdded()), this, SLOT(addItemFinish())); } @@ -1963,7 +1976,7 @@ void QETDiagramEditor::showError(const QString &error) { * Uncheck all action in m_add_item_actions_group */ void QETDiagramEditor::addItemFinish() { - foreach(QAction *action, m_add_item_actions_group.actions()) action->setChecked(false); + m_add_item_actions_group.checkedAction()->setChecked(false); } /**