Element editor : start work for change how draw primitive (work in progress)

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3451 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2014-11-04 22:08:42 +00:00
parent 5f26e47a8a
commit 46ddc4741a
7 changed files with 452 additions and 73 deletions

View File

@ -63,17 +63,28 @@ 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 sources/dvevent
INCLUDEPATH += sources \
sources/titleblock \
sources/ui sources/qetgraphicsitem \
sources/richtext \
sources/factory \
sources/properties \
sources/dvevent \
sources/editor \
sources/editor/esevent
# 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/*.h) \
$$files(sources/properties/*.h) \
$$files(sources/editor/ui/*.h) \
$$files(sources/editor/esevent/*.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/esevent/*.cpp) \
$$files(sources/dvevent/*.cpp)
# Liste des fichiers qui seront incorpores au binaire en tant que ressources Qt

View File

@ -31,6 +31,9 @@
#include "elementcontent.h"
#include "nameslist.h"
#include "ui/elementpropertieseditorwidget.h"
#include "eseventinterface.h"
#include <QKeyEvent>
/**
Constructeur
@ -41,6 +44,7 @@ ElementScene::ElementScene(QETElementEditor *editor, QObject *parent) :
QGraphicsScene(parent),
m_elmt_type("simple"),
qgi_manager(this),
m_event_interface(nullptr),
element_editor(editor),
decorator_(0)
{
@ -66,13 +70,6 @@ void ElementScene::slot_move() {
behavior = Normal;
}
/**
Passe la scene en mode "ajout de ligne"
*/
void ElementScene::slot_addLine() {
behavior = Line;
}
/**
Passe la scene en mode "ajout de rectangle"
*/
@ -135,8 +132,19 @@ void ElementScene::slot_addTextField() {
@param e objet decrivant l'evenement
*/
void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
if (m_event_interface) {
if (m_event_interface -> mouseMoveEvent(e)) {
if (m_event_interface->isFinish()) {
emit(partsAdded());
emit(needNormalMode());
delete m_event_interface; m_event_interface = nullptr;
}
return;
}
}
QPointF event_pos = e -> scenePos();
if (mustSnapToGrid(e)) snapToGrid(event_pos);
if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos);
if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL;
if (behavior == PasteArea) {
@ -151,9 +159,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
QPolygonF temp_polygon;
if (e -> buttons() & Qt::LeftButton) {
switch(behavior) {
case Line:
current_line -> setLine(QLineF(current_line -> line().p1(), event_pos));
break;
case Rectangle:
temp_rect = current_rectangle -> rect();
temp_rect.setBottomRight(event_pos);
@ -193,17 +198,23 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
@param e objet decrivant l'evenement
*/
void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
if (m_event_interface) {
if (m_event_interface -> mousePressEvent(e)) {
if (m_event_interface->isFinish()) {
emit(partsAdded());
emit(needNormalMode());
delete m_event_interface; m_event_interface = nullptr;
}
return;
}
}
QPointF event_pos = e -> scenePos();
if (mustSnapToGrid(e)) snapToGrid(event_pos);
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 Line:
current_line = new PartLine(element_editor, 0, this);
current_line -> setLine(QLineF(event_pos, event_pos));
break;
case Rectangle:
current_rectangle = new PartRectangle(element_editor, 0, this);
current_rectangle -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0)));
@ -240,8 +251,19 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
@param e objet decrivant l'evenement
*/
void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
if (m_event_interface) {
if (m_event_interface -> mouseReleaseEvent(e)) {
if (m_event_interface->isFinish()) {
emit(partsAdded());
emit(needNormalMode());
delete m_event_interface; m_event_interface = nullptr;
}
return;
}
}
QPointF event_pos = e -> scenePos();
if (mustSnapToGrid(e)) snapToGrid(event_pos);
if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos);
PartTerminal *terminal;
PartText *text;
@ -258,12 +280,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
if (e -> button() & Qt::LeftButton) {
switch(behavior) {
case Line:
if (qgiManager().manages(current_line)) break;
undo_stack.push(new AddPartCommand(tr("ligne"), this, current_line));
emit(partsAdded());
endCurrentBehavior(e);
break;
case Rectangle:
if (qgiManager().manages(current_rectangle)) break;
current_rectangle -> setRect(current_rectangle -> rect().normalized());
@ -322,6 +338,25 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
} else QGraphicsScene::mouseReleaseEvent(e);
}
/**
* @brief ElementScene::keyPressEvent
* manage key press event
* @param event
*/
void ElementScene::keyPressEvent(QKeyEvent *event) {
if (m_event_interface) {
if (m_event_interface -> keyPressEvent(event)) {
if (m_event_interface->isFinish()) {
emit(partsAdded());
emit(needNormalMode());
delete m_event_interface; m_event_interface = nullptr;
}
return;
}
}
QGraphicsScene::keyPressEvent(event);
}
/**
Dessine l'arriere-plan de l'editeur, cad l'indicateur de hotspot.
@param p Le QPainter a utiliser pour dessiner
@ -357,6 +392,16 @@ void ElementScene::endCurrentBehavior(const QGraphicsSceneMouseEvent *event) {
}
}
/**
* @brief ElementScene::setInterface
* Set a new event interface
* @param interface
*/
void ElementScene::setInterface(ESEventInterface *interface) {
if (m_event_interface) delete m_event_interface;
m_event_interface = interface;
}
/**
@return la taille horizontale de la grille
*/
@ -620,6 +665,10 @@ void ElementScene::contextMenu(QContextMenuEvent *event) {
element_editor -> contextMenu(event);
}
QETElementEditor* ElementScene::editor() const {
return element_editor;
}
/**
Selectionne une liste de parties
@param content liste des parties a selectionner
@ -1097,9 +1146,10 @@ void ElementScene::initPasteArea() {
@param point une reference vers un QPointF. Cet objet sera modifie.
*/
void ElementScene::snapToGrid(QPointF &point) {
QPointF ElementScene::snapToGrid(QPointF point) {
point.rx() = qRound(point.x() / x_grid) * x_grid;
point.ry() = qRound(point.y() / y_grid) * y_grid;
return point;
}
/**

View File

@ -23,15 +23,17 @@
#include "qgimanager.h"
#include "elementcontent.h"
#include "diagramcontext.h"
class CustomElementPart;
class ElementEditionCommand;
class ElementPrimitiveDecorator;
class QETElementEditor;
class PartLine;
class PartRectangle;
class PartEllipse;
class PartPolygon;
class PartArc;
class ESEventInterface;
class QKeyEvent;
/**
This class is the canvas allowing the visual edition of an electrial element.
It displays the various primitives composing the drawing of the element, the
@ -43,7 +45,7 @@ class ElementScene : public QGraphicsScene {
// enum
public:
enum Behavior { Normal, Line, Rectangle, Circle, Ellipse, Polygon, Text, Terminal, Arc, TextField, PasteArea };
enum Behavior { Normal, Rectangle, Circle, Ellipse, Polygon, Text, Terminal, Arc, TextField, PasteArea };
enum ItemOption {
SortByZValue = 1,
IncludeTerminals = 2,
@ -64,57 +66,57 @@ class ElementScene : public QGraphicsScene {
// attributes
private:
/// List of localized names
NamesList _names;
/// Extra informations
QString informations_;
/// element type
QString m_elmt_type;
/// element kind info
DiagramContext m_elmt_kindInfo;
/// QGraphicsItem manager
QGIManager qgi_manager;
/// Undo stack
QUndoStack undo_stack;
/**
fsi_pos (first selected item pos) : Position of the forst selected item: used
to cancel mouse movements; also used to handle movements using keybard
arrwows.
*/
QPointF fsi_pos;
QPointF moving_press_pos;
bool moving_parts_;
/// List of localized names
NamesList _names;
/// Extra informations
QString informations_;
/// element type
QString m_elmt_type;
/// element kind info
DiagramContext m_elmt_kindInfo;
/// QGraphicsItem manager
QGIManager qgi_manager;
/// Undo stack
QUndoStack undo_stack;
/**
fsi_pos (first selected item pos) : Position of the forst selected item: used
to cancel mouse movements; also used to handle movements using keybard
arrwows.
*/
QPointF fsi_pos;
QPointF moving_press_pos;
bool moving_parts_;
/// Variables related to drawing
Behavior behavior;
PartLine *current_line;
PartRectangle *current_rectangle;
PartEllipse *current_ellipse;
PartPolygon *current_polygon;
PartArc *current_arc;
QETElementEditor *element_editor;
/// Variables related to drawing
ESEventInterface *m_event_interface;
Behavior behavior;
PartRectangle *current_rectangle;
PartEllipse *current_ellipse;
PartPolygon *current_polygon;
PartArc *current_arc;
QETElementEditor *element_editor;
/// Variables to manage the paste area on the scene
QGraphicsRectItem *paste_area_;
QRectF defined_paste_area_;
/// Variables to manage the paste area on the scene
QGraphicsRectItem *paste_area_;
QRectF defined_paste_area_;
/// Variables to handle copy/paste with offset
QString last_copied_;
/// Variables to handle copy/paste with offset
QString last_copied_;
/// Decorator item displayed when at least one item is selected
ElementPrimitiveDecorator *decorator_;
/// Decorator item displayed when at least one item is selected
ElementPrimitiveDecorator *decorator_;
///< Size of the horizontal grid step
int x_grid;
///< Size of the vertical grid step
int y_grid;
///< Size of the horizontal grid step
int x_grid;
///< Size of the vertical grid step
int y_grid;
// methods
public:
void setInterface (ESEventInterface *interface);
QPointF snapToGrid(QPointF point);
void setNames(const NamesList &);
NamesList names() const;
bool internalConnections();
void setInternalConnections(bool);
QString informations() const;
void setInformations(const QString &);
QString elementType () const {return m_elmt_type;}
@ -140,11 +142,14 @@ class ElementScene : public QGraphicsScene {
void copy();
void paste();
void contextMenu (QContextMenuEvent *event);
QETElementEditor* editor() const;
protected:
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *);
virtual void mousePressEvent (QGraphicsSceneMouseEvent *);
virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *);
virtual void keyPressEvent (QKeyEvent *event);
virtual void drawForeground(QPainter *, const QRectF &);
virtual void endCurrentBehavior(const QGraphicsSceneMouseEvent *);
@ -156,7 +161,6 @@ class ElementScene : public QGraphicsScene {
ElementContent addContentAtPos(const ElementContent &, const QPointF &, QString * = 0);
void addPrimitive(QGraphicsItem *);
void initPasteArea();
void snapToGrid(QPointF &);
bool mustSnapToGrid(QGraphicsSceneMouseEvent *);
static bool zValueLessThan(QGraphicsItem *, QGraphicsItem *);
QMutex *decorator_lock_;
@ -164,7 +168,6 @@ class ElementScene : public QGraphicsScene {
public slots:
void slot_move();
void slot_addLine();
void slot_addRectangle();
void slot_addCircle();
void slot_addEllipse();

View File

@ -0,0 +1,103 @@
/*
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 <QGraphicsSceneMouseEvent>
#include <QObject>
#include "eseventaddline.h"
#include "elementscene.h"
#include "qetelementeditor.h"
#include "partline.h"
#include "editorcommands.h"
/**
* @brief ESEventAddLine::ESEventAddLine
* Constructor
* @param scene : scene where we operate this action
*/
ESEventAddLine::ESEventAddLine(ElementScene *scene) :
ESEventInterface (scene),
m_line (nullptr)
{}
/**
* @brief ESEventAddLine::~ESEventAddLine
* destructor
*/
ESEventAddLine::~ESEventAddLine() {
if (m_running || m_abort)
delete m_line;
}
/**
* @brief ESEventAddLine::mousePressEvent
* @param event
* @return
*/
bool ESEventAddLine::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (event -> button() == Qt::LeftButton) {
if (! m_running) m_running = true;
QPointF pos = m_scene -> snapToGrid(event -> scenePos());
//Create new line
if (!m_line) {
m_line = new PartLine(m_editor, 0, m_scene);
m_line -> setLine(QLineF(pos, pos));
return true;
}
//Add new line to scene
m_line -> setLine(QLineF(m_line->line().p1(), pos));
m_scene -> undoStack().push(new AddPartCommand(QObject::tr("ligne"), m_scene, m_line));
//Set m_line to nullptr for create new line at next mouse press
m_line = nullptr;
return true;
}
//Remove the current line if exist
//or finish if no line
if (event -> button() == Qt::RightButton) {
if (m_line) {
delete m_line; m_line = nullptr;
}
else {
m_running = false;
}
return true;
}
return false;
}
/**
* @brief ESEventAddLine::mouseMoveEvent
* @param event
* @return
*/
bool ESEventAddLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
updateHelpCross (event -> scenePos());
if (!m_line) return false;
QPointF pos = m_scene -> snapToGrid(event -> scenePos());
m_line -> setLine(QLineF(m_line->line().p1(), pos));
return true;
}

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 ESEVENTADDLINE_H
#define ESEVENTADDLINE_H
#include "eseventinterface.h"
class ElementScene;
class PartLine;
class QGraphicsSceneMouseEvent;
class ESEventAddLine : public ESEventInterface
{
public:
ESEventAddLine(ElementScene *scene);
virtual ~ESEventAddLine();
virtual bool mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event);
private:
PartLine *m_line;
};
#endif // ESEVENTADDLINE_H

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "eseventinterface.h"
#include "elementscene.h"
#include "qetelementeditor.h"
#include <QGraphicsSceneMouseEvent>
ESEventInterface::ESEventInterface(ElementScene *scene) :
m_scene (scene),
m_editor (scene->editor()),
m_help_horiz (nullptr),
m_help_verti (nullptr),
m_running (false),
m_abort (false)
{
foreach (QGraphicsView *qgv, m_scene->views())
qgv->setContextMenuPolicy(Qt::NoContextMenu);
}
ESEventInterface::~ESEventInterface() {
delete m_help_horiz;
delete m_help_verti;
foreach (QGraphicsView *qgv, m_scene->views())
qgv->setContextMenuPolicy(Qt::DefaultContextMenu);
}
bool ESEventInterface::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
Q_UNUSED (event);
return false;
}
bool ESEventInterface::mousePressEvent(QGraphicsSceneMouseEvent *event) {
Q_UNUSED (event);
return false;
}
bool ESEventInterface::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
Q_UNUSED (event);
return false;
}
bool ESEventInterface::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
Q_UNUSED (event);
return false;
}
bool ESEventInterface::wheelEvent(QGraphicsSceneWheelEvent *event) {
Q_UNUSED (event);
return false;
}
/**
* @brief ESEventInterface::keyPressEvent
* By default, press escape key abort the curent action
* @param event
* @return
*/
bool ESEventInterface::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Escape) {
m_running = false;
m_abort = true;
return true;
}
return false;
}
bool ESEventInterface::KeyReleaseEvent(QKeyEvent *event) {
Q_UNUSED (event);
return false;
}
bool ESEventInterface::isRunning() const {
return m_running;
}
bool ESEventInterface::isFinish() const {
return !m_running;
}
void ESEventInterface::updateHelpCross(const QPointF &p) {
//If line isn't created yet, we create it.
if (!m_help_horiz || !m_help_verti) {
QPen pen;
pen.setColor(Qt::darkBlue);
if (!m_help_horiz) {
m_help_horiz = new QGraphicsLineItem(-10000, 0, 10000, 0, 0, m_scene);
m_help_horiz -> setPen(pen);
}
if (!m_help_verti) {
m_help_verti = new QGraphicsLineItem(0, -10000, 0, 10000, 0, m_scene);
m_help_verti -> setPen(pen);
}
}
//Update the position of the cross
QPointF point = m_scene -> snapToGrid(p);
m_help_horiz -> setY(point.y());
m_help_verti -> setX(point.x());
}

View File

@ -0,0 +1,55 @@
/*
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 ESEVENTINTERFACE_H
#define ESEVENTINTERFACE_H
class QGraphicsSceneMouseEvent;
class QGraphicsSceneWheelEvent;
class QKeyEvent;
class ElementScene;
class QETElementEditor;
class QGraphicsLineItem;
class QPointF;
class ESEventInterface
{
public:
ESEventInterface(ElementScene *scene);
virtual ~ESEventInterface();
virtual bool mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event);
virtual bool mousePressEvent (QGraphicsSceneMouseEvent *event);
virtual bool mouseMoveEvent (QGraphicsSceneMouseEvent *event);
virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
virtual bool wheelEvent (QGraphicsSceneWheelEvent *event);
virtual bool keyPressEvent (QKeyEvent *event);
virtual bool KeyReleaseEvent (QKeyEvent *event);
virtual bool isRunning () const;
virtual bool isFinish () const;
protected:
void updateHelpCross (const QPointF &p);
protected:
ElementScene *m_scene;
QETElementEditor *m_editor;
QGraphicsLineItem *m_help_horiz, *m_help_verti;
bool m_running, m_abort;
};
#endif // ESEVENTINTERFACE_H