Diagram view: All add action (text/shape/image) is now managed outside of diagram view by subclass of DVEventInterface

DVEventInterface :this abstract class is used by diagram view to manage is event action (mouse event).
For add new action to diagram view, we must to create subclass of DVEventInterface and give it to diagram view,
when diagram view get the new dvevent, they manage it, and delete it when action is finish. 


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3329 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2014-09-24 09:38:16 +00:00
parent c7523b5d37
commit 69f239d197
12 changed files with 659 additions and 231 deletions

View File

@ -63,16 +63,18 @@ DEFINES += QET_ALLOW_OVERRIDE_CD_OPTION
TEMPLATE = app TEMPLATE = app
DEPENDPATH += . 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 # 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) \ 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/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) \ 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/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 # Liste des fichiers qui seront incorpores au binaire en tant que ressources Qt
RESOURCES += qelectrotech.qrc RESOURCES += qelectrotech.qrc

View File

@ -41,18 +41,18 @@
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include "factory/elementfactory.h" #include "factory/elementfactory.h"
#include "diagrampropertiesdialog.h" #include "diagrampropertiesdialog.h"
#include "dveventinterface.h"
/** /**
Constructeur Constructeur
@param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise @param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise
@param parent Le QWidget parent de cette vue de schema @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); grabGesture(Qt::PinchGesture);
setAttribute(Qt::WA_DeleteOnClose, true); setAttribute(Qt::WA_DeleteOnClose, true);
setInteractive(true); setInteractive(true);
current_behavior = noAction; m_event_interface = nullptr;
m_polyline_added = false;
QString whatsthis = tr( QString whatsthis = tr(
"Ceci est la zone dans laquelle vous concevez vos sch\351mas en y ajoutant" "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 * @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 * @param e the QDropEvent describing the current drag'n drop
*/ */
void DiagramView::handleTextDrop(QDropEvent *e) { void DiagramView::handleTextDrop(QDropEvent *e) {
if (e -> mimeData() -> hasText()) { if (scene -> isReadOnly() || (e -> mimeData() -> hasText() == false) ) return;
if (e -> mimeData() -> hasHtml()) addDiagramTextAtPos(e->pos()) -> setHtml(e -> mimeData() -> text()); scene -> undoStack().push(new AddTextCommand(scene, new IndependentTextItem (e -> mimeData() -> text()), mapToScene(e->pos())));
else addDiagramTextAtPos(e -> pos(), e -> mimeData() -> text());
}
} }
/** /**
@ -463,63 +461,25 @@ void DiagramView::pasteHere() {
* click to add an independent text field * click to add an independent text field
*/ */
void DiagramView::mousePressEvent(QMouseEvent *e) { void DiagramView::mousePressEvent(QMouseEvent *e) {
rubber_band_origin = mapToScene(e -> pos());
if (fresh_focus_in_) { if (fresh_focus_in_) {
switchToVisualisationModeIfNeeded(e); switchToVisualisationModeIfNeeded(e);
fresh_focus_in_ = false; fresh_focus_in_ = false;
} }
if (isInteractive() && !scene -> isReadOnly() && e -> button() == Qt::LeftButton) { if (m_event_interface) {
switch (current_behavior) { if (m_event_interface -> mousePressEvent(e)) {
case noAction: if (m_event_interface->isFinish()) {
QGraphicsView::mousePressEvent(e); emit (itemAdded());
break; delete m_event_interface; m_event_interface = nullptr;
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; return;
case dragView:
current_behavior = noAction;
QGraphicsView::mousePressEvent(e);
break;
default:
current_behavior = noAction;
break;
} }
} }
//Start drag view when hold the middle button //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); setCursor(Qt::ClosedHandCursor);
center_view_ = mapToScene(this -> viewport() -> rect().center()); center_view_ = mapToScene(this -> viewport() -> rect().center());
} }
@ -532,6 +492,16 @@ void DiagramView::mousePressEvent(QMouseEvent *e) {
* Manage the event move mouse * Manage the event move mouse
*/ */
void DiagramView::mouseMoveEvent(QMouseEvent *e) { 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 //Drag the view
if (e -> buttons() == Qt::MidButton) { if (e -> buttons() == Qt::MidButton) {
QPointF move = rubber_band_origin - mapToScene(e -> pos()); QPointF move = rubber_band_origin - mapToScene(e -> pos());
@ -539,12 +509,6 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e) {
center_view_ = mapToScene( this -> viewport() -> rect().center() ); 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); else QGraphicsView::mouseMoveEvent(e);
} }
@ -553,28 +517,19 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e) {
* Manage event release click mouse * Manage event release click mouse
*/ */
void DiagramView::mouseReleaseEvent(QMouseEvent *e) { 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 //Stop drag view
if (e -> button() == Qt::MidButton) setCursor(Qt::ArrowCursor); 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); else QGraphicsView::mouseReleaseEvent(e);
} }
@ -591,6 +546,16 @@ bool DiagramView::gestures() const {
@param e QWheelEvent @param e QWheelEvent
*/ */
void DiagramView::wheelEvent(QWheelEvent *e) { 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 //Zoom and scrolling
if ( gestures() ) { if ( gestures() ) {
if (e -> modifiers() & Qt::ControlModifier) if (e -> modifiers() & Qt::ControlModifier)
@ -1040,7 +1005,6 @@ bool DiagramView::event(QEvent *e) {
bool DiagramView::switchToVisualisationModeIfNeeded(QInputEvent *e) { bool DiagramView::switchToVisualisationModeIfNeeded(QInputEvent *e) {
if (isCtrlShifting(e) && !selectedItemHasFocus()) { if (isCtrlShifting(e) && !selectedItemHasFocus()) {
if (dragMode() != QGraphicsView::ScrollHandDrag) { if (dragMode() != QGraphicsView::ScrollHandDrag) {
current_behavior = dragView;
setVisualisationMode(); setVisualisationMode();
return(true); return(true);
} }
@ -1054,9 +1018,8 @@ bool DiagramView::switchToVisualisationModeIfNeeded(QInputEvent *e) {
otherwise. otherwise.
*/ */
bool DiagramView::switchToSelectionModeIfNeeded(QInputEvent *e) { bool DiagramView::switchToSelectionModeIfNeeded(QInputEvent *e) {
if (current_behavior == dragView && !selectedItemHasFocus() && !isCtrlShifting(e)) { if (!selectedItemHasFocus() && !isCtrlShifting(e)) {
setSelectionMode(); setSelectionMode();
current_behavior = noAction;
return(true); return(true);
} }
return(false); 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 /** To edit the text through the htmlEditor
*/ */
void DiagramView::editText() { void DiagramView::editText() {
@ -1121,56 +1074,6 @@ void DiagramView::editText() {
else texts_to_edit.at(0)->edit(); 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 * @brief DiagramView::editImage
* open edit image dialog if only one image is selected * open edit image dialog if only one image is selected
@ -1200,51 +1103,12 @@ void DiagramView::editShape() {
} }
/** /**
* @brief DiagramView::addDiagramImageAtPos * @brief DiagramView::setEventInterface
* @param pos * Set an event interface to diagram view.
* @return */
*/ void DiagramView::setEventInterface(DVEventInterface *interface) {
DiagramImageItem *DiagramView::addDiagramImageAtPos(const QPointF &pos) { if (m_event_interface) delete m_event_interface;
m_event_interface = interface;
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);
} }
/** /**
@ -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) { 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; BorderTitleBlock &bi = scene -> border_and_titleblock;
//Get the rectangle of the titleblock //Get the rectangle of the titleblock

View File

@ -20,13 +20,12 @@
#include <QtGui> #include <QtGui>
#include "elementslocation.h" #include "elementslocation.h"
#include "templatelocation.h" #include "templatelocation.h"
#include "qetgraphicsitem/qetshapeitem.h"
class Conductor; class Conductor;
class Diagram; class Diagram;
class Element;
class IndependentTextItem;
class DiagramImageItem;
class QETDiagramEditor; class QETDiagramEditor;
class DVEventInterface;
/** /**
This class provides a widget to render an electric diagram in an editable, This class provides a widget to render an electric diagram in an editable,
interactive way. interactive way.
@ -39,18 +38,6 @@ class DiagramView : public QGraphicsView {
DiagramView(Diagram * = 0, QWidget * = 0); DiagramView(Diagram * = 0, QWidget * = 0);
virtual ~DiagramView(); 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: private:
DiagramView(const DiagramView &); DiagramView(const DiagramView &);
@ -61,15 +48,12 @@ class DiagramView : public QGraphicsView {
QAction *paste_here; QAction *paste_here;
QAction *find_element_; QAction *find_element_;
QPoint paste_here_pos; QPoint paste_here_pos;
behavior current_behavior;
bool fresh_focus_in_; ///< Indicate the focus was freshly gained bool fresh_focus_in_; ///< Indicate the focus was freshly gained
ElementsLocation next_location_; ElementsLocation next_location_;
QPoint next_position_; QPoint next_position_;
QPointF center_view_; QPointF center_view_;
QImage image_to_add_;
QetShapeItem *newShapeItem;
QPointF rubber_band_origin; QPointF rubber_band_origin;
bool m_polyline_added; DVEventInterface *m_event_interface;
// methods // methods
public: public:
@ -87,15 +71,9 @@ class DiagramView : public QGraphicsView {
bool hasDeletableItems(); bool hasDeletableItems();
void addText(); void addText();
void editText(); void editText();
void addImage();
void addLine();
void addRectangle();
void addEllipse();
void addPolyline();
void editImage(); void editImage();
void editShape(); void editShape();
IndependentTextItem *addDiagramTextAtPos(const QPointF &, const QString &text = 0); void setEventInterface (DVEventInterface *interface);
DiagramImageItem *addDiagramImageAtPos(const QPointF &);
protected: protected:
virtual void mouseDoubleClickEvent(QMouseEvent *); virtual void mouseDoubleClickEvent(QMouseEvent *);
@ -147,8 +125,6 @@ class DiagramView : public QGraphicsView {
void editTitleBlockTemplate(const QString &, bool); void editTitleBlockTemplate(const QString &, bool);
/// Signal emitted after an item is added /// Signal emitted after an item is added
void itemAdded(); void itemAdded();
/// Signal emmitted fater windows selection image have been canceled
void ImageAddedCanceled(bool);
/// Signal emmitted when diagram must be show /// Signal emmitted when diagram must be show
void showDiagram (Diagram *); void showDiagram (Diagram *);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "diagramimageitem.h"
#include "diagramcommands.h"
#include "dveventaddimage.h"
#include "diagramview.h"
#include "diagram.h"
#include <QObject>
/**
* @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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "dveventaddshape.h"
#include "diagramview.h"
#include "qetshapeitem.h"
#include "diagram.h"
#include "diagramcommands.h"
#include <QMouseEvent>
/**
* @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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "dveventaddtext.h"
#include "independenttextitem.h"
#include "diagram.h"
#include "diagramcommands.h"
#include "diagramview.h"
#include <QMouseEvent>
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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "dveventinterface.h"
#include "diagramview.h"
#include <QMouseEvent>
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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -34,6 +34,10 @@
#include "genericpanel.h" #include "genericpanel.h"
#include "nomenclature.h" #include "nomenclature.h"
#include "diagramfoliolist.h" #include "diagramfoliolist.h"
#include "qetshapeitem.h"
#include "dveventaddimage.h"
#include "dveventaddshape.h"
#include "dveventaddtext.h"
#include "ui/dialogautonum.h" #include "ui/dialogautonum.h"
@ -1510,7 +1514,8 @@ void QETDiagramEditor::slot_resetConductors() {
* add text to curent diagram * add text to curent diagram
*/ */
void QETDiagramEditor::slot_addText() { 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 * add image to curent diagram
*/ */
void QETDiagramEditor::slot_addImage() { 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 * add line to curent diagram
*/ */
void QETDiagramEditor::slot_addLine() { 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 * @brief QETDiagramEditor::slot_addRectangle
* add recatngle to curent diagram * add rectangle to curent diagram
*/ */
void QETDiagramEditor::slot_addRectangle() { 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 * add ellipse to curent diagram
*/ */
void QETDiagramEditor::slot_addEllipse() { 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 * add polyline to current diagram
*/ */
void QETDiagramEditor::slot_addPolyline() { 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())); undo_group.addStack(&(dv -> diagram() -> undoStack()));
connect(dv, SIGNAL(selectionChanged()), this, SLOT(slot_updateComplexActions())); connect(dv, SIGNAL(selectionChanged()), this, SLOT(slot_updateComplexActions()));
connect(dv, SIGNAL(modeChanged()), this, SLOT(slot_updateModeActions())); connect(dv, SIGNAL(modeChanged()), this, SLOT(slot_updateModeActions()));
connect(dv, SIGNAL(ImageAddedCanceled(bool)), this, SLOT(addItemFinish()));
connect(dv, SIGNAL(itemAdded()), 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 * Uncheck all action in m_add_item_actions_group
*/ */
void QETDiagramEditor::addItemFinish() { void QETDiagramEditor::addItemFinish() {
foreach(QAction *action, m_add_item_actions_group.actions()) action->setChecked(false); m_add_item_actions_group.checkedAction()->setChecked(false);
} }
/** /**