QElectroTech  0.8-dev
elementscene.h
Go to the documentation of this file.
1 /*
2  Copyright 2006-2020 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef ELEMENT_SCENE_H
19 #define ELEMENT_SCENE_H
20 #include <QtWidgets>
21 #include <QtXml>
22 #include "qgimanager.h"
23 #include "elementcontent.h"
24 #include "diagramcontext.h"
25 #include "nameslist.h"
26 
27 class CustomElementPart;
30 class QETElementEditor;
31 class ESEventInterface;
32 class QKeyEvent;
39 class ElementScene : public QGraphicsScene
40 {
42  Q_OBJECT
43 
44  // enum
45  public:
47  enum ItemOption {
51  Selected = 8,
53  SelectedOrNot = 24
54  };
55  Q_DECLARE_FLAGS(ItemOptions, ItemOption)
56 
57  // constructors, destructor
58  public:
59  ElementScene(QETElementEditor *, QObject * = nullptr);
60  ~ElementScene() override;
61 
62  private:
64 
65  // attributes
66  private:
68  QString m_informations;
69  QString m_elmt_type;
73  QUndoStack m_undo_stack;
74 
78 
79  QGraphicsRectItem *m_paste_area;
81 
82  QString m_last_copied;
83 
86 
87  int m_x_grid,
89 
90  // methods
91  public:
92  void setEventInterface (ESEventInterface *event_interface);
93  void clearEventInterface();
96  QPointF snapToGrid(QPointF point);
97  void setNames(const NamesList &);
98  NamesList names() const;
99  QString informations() const;
100  void setInformations(const QString &);
101  QString elementType () const {return m_elmt_type;}
104  virtual int xGrid() const;
105  virtual int yGrid() const;
106  virtual void setGrid(int, int);
107  virtual const QDomDocument toXml(bool = true);
108  virtual QRectF boundingRectFromXml(const QDomDocument &);
109  virtual void fromXml(const QDomDocument &,
110  const QPointF & = QPointF(),
111  bool = true,
112  ElementContent * = nullptr);
113  virtual void reset();
114  virtual QList<CustomElementPart *> primitives() const;
115  virtual QList<QGraphicsItem *>
116  zItems(ItemOptions options = ItemOptions(SortByZValue
118  | SelectedOrNot)) const;
119  virtual ElementContent selectedContent() const;
120  virtual void getPasteArea(const QRectF &);
121  QRectF elementSceneGeometricRect () const;
122  bool containsTerminals() const;
123  QUndoStack &undoStack();
125  static bool clipboardMayContainElement();
126  bool wasCopiedFromThisElement(const QString &);
127  void cut();
128  void copy();
129  QETElementEditor* editor() const;
130  void setElementInfo(const DiagramContext& dc);
131 
132  protected:
133  void mouseMoveEvent (QGraphicsSceneMouseEvent *) override;
134  void mousePressEvent (QGraphicsSceneMouseEvent *) override;
135  void mouseReleaseEvent (QGraphicsSceneMouseEvent *) override;
136  void mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event) override;
137  void keyPressEvent (QKeyEvent *event) override;
138  void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
139 
140  void drawForeground(QPainter *, const QRectF &) override;
141 
142  private:
143  QRectF elementContentBoundingRect(const ElementContent &) const;
144  bool applyInformations(const QDomDocument &);
145  ElementContent loadContent(const QDomDocument &);
147  ElementContent addContentAtPos(const ElementContent &, const QPointF &);
148  void addPrimitive(QGraphicsItem *);
149  void initPasteArea();
150  static bool zValueLessThan(QGraphicsItem *, QGraphicsItem *);
152  void centerElementToOrigine();
153 
154  public slots:
155  void slot_select(const ElementContent &);
156  void slot_selectAll();
157  void slot_deselectAll();
158  void slot_invertSelection();
159  void slot_delete();
160  void slot_editNames();
162  void slot_editProperties();
163  void managePrimitivesGroups();
165 
166  signals:
168  void partsAdded();
170  void partsRemoved();
174  void pasteAreaDefined(const QRectF &);
176  void needZoomFit();
178 };
179 
180 Q_DECLARE_OPERATORS_FOR_FLAGS(ElementScene::ItemOptions)
181 
182 
183 
187 inline void ElementScene::setNames(const NamesList &nameslist) {
188  m_names_list = nameslist;
189 }
190 
196  return(m_names_list);
197 }
198 
203 inline QString ElementScene::informations() const {
204  return(m_informations);
205 }
206 
211 inline void ElementScene::setInformations(const QString &infos) {
212  m_informations = infos;
213 }
214 
215 #endif
QPropertyUndoCommand::enableAnimation
void enableAnimation(bool animate=true)
QPropertyUndoCommand::enableAnimation True to enable animation.
Definition: qpropertyundocommand.cpp:92
qgimanager.h
ElementScene::NonSelected
@ NonSelected
Definition: elementscene.h:52
ElementScene::m_behavior
Behavior m_behavior
Definition: elementscene.h:76
ElementScene::snapToGrid
QPointF snapToGrid(QPointF point)
ElementScene::snapToGrid Rounds the coordinates of the point passed as a parameter so that this point...
Definition: elementscene.cpp:1270
NameListWidget::setReadOnly
void setReadOnly(bool ro)
NameListWidget::setReadOnly Set this dialog to read only or not.
Definition: namelistwidget.cpp:104
ElementScene::setInformations
void setInformations(const QString &)
ElementScene::setInformations.
Definition: elementscene.h:211
ElementPrimitiveDecorator
Definition: elementprimitivedecorator.h:41
ElementScene::elementSceneGeometricRect
QRectF elementSceneGeometricRect() const
ElementScene::elementSceneGeometricRect.
Definition: elementscene.cpp:569
ElementScene::fromXml
virtual void fromXml(const QDomDocument &, const QPointF &=QPointF(), bool=true, ElementContent *=nullptr)
ElementScene::fromXml Imports the element described in an XML document. If a position is specified,...
Definition: elementscene.cpp:537
QetGraphicsHandlerItem::Type
@ Type
Definition: qetgraphicshandleritem.h:42
ElementScene::loadContent
ElementContent loadContent(const QDomDocument &)
ElementScene::loadContent Create and load the content describe in the xml document.
Definition: elementscene.cpp:1113
ElementScene::setNames
void setNames(const NamesList &)
ElementScene::setNames.
Definition: elementscene.h:187
ElementContent
QList< QGraphicsItem * > ElementContent
Definition: elementcontent.h:21
ElementScene::cut
void cut()
ElementScene::cut Handles the fact of cutting the selection = exporting it in XML to the clipboard th...
Definition: elementscene.cpp:648
ElementView
Definition: elementview.h:27
ESEventInterface::init
void init()
ESEventInterface::init Init this event interface.
Definition: eseventinterface.cpp:38
ElementScene::zItems
virtual QList< QGraphicsItem * > zItems(ItemOptions options=ItemOptions(SortByZValue|IncludeTerminals|SelectedOrNot)) const
ElementScene::zItems.
Definition: elementscene.cpp:911
NameListWidget::isEmpty
bool isEmpty() const
NameListWidget::isEmpty.
Definition: namelistwidget.cpp:125
PartText::Type
@ Type
Definition: parttext.h:52
ElementScene::undoStack
QUndoStack & undoStack()
ElementScene::undoStack.
Definition: elementscene.cpp:603
ElementScene::managePrimitivesGroups
void managePrimitivesGroups()
ElementScene::managePrimitivesGroups Ensure the decorator is adequately shown, hidden or updated so i...
Definition: elementscene.cpp:1318
ElementScene::addPrimitive
void addPrimitive(QGraphicsItem *)
ElementScene::addPrimitive Add a primitive to the scene by wrapping it within an ElementPrimitiveDeco...
Definition: elementscene.cpp:1233
DiagramContext::fromXml
void fromXml(const QDomElement &, const QString &="property")
Definition: diagramcontext.cpp:157
nameslist.h
ElementScene::mouseDoubleClickEvent
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
ElementScene::mouseDoubleClickEvent.
Definition: elementscene.cpp:174
ElementScene::slot_invertSelection
void slot_invertSelection()
ElementScene::slot_invertSelection Inverse Selection.
Definition: elementscene.cpp:753
partellipse.h
ElementScene::m_defined_paste_area
QRectF m_defined_paste_area
Definition: elementscene.h:80
QET::Icons::PartArc
QIcon PartArc
Definition: qeticons.cpp:127
QET::version
const QString version
QElectroTech version (as string, used to mark projects and elements XML documents)
Definition: qet.h:32
ElementScene::partsAdded
void partsAdded()
Signal emitted after one or several parts were added.
ElementScene::m_last_copied
QString m_last_copied
Definition: elementscene.h:82
ESEventInterface::isFinish
virtual bool isFinish() const
Definition: eseventinterface.cpp:106
elementprimitivedecorator.h
ElementScene::m_undo_stack
QUndoStack m_undo_stack
Definition: elementscene.h:73
ElementScene::contextMenuEvent
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
ElementScene::contextMenuEvent Display the context menu event, only if behavior are Normal.
Definition: elementscene.cpp:275
ElementScene::partsZValueChanged
void partsZValueChanged()
Signal emitted when the zValue of one or several parts change.
QET::Icons::PartText
QIcon PartText
Definition: qeticons.cpp:134
CutPartsCommand
Definition: editorcommands.h:87
ElementScene::stackAction
void stackAction(ElementEditionCommand *)
ElementScene::stackAction Push the provided command on the undo stack.
Definition: elementscene.cpp:1364
ElementScene::m_elmt_information
DiagramContext m_elmt_information
Definition: elementscene.h:71
ElementScene::toXml
virtual const QDomDocument toXml(bool=true)
ElementScene::toXml Export this element as a xml file.
Definition: elementscene.cpp:394
NameListDialog::setInformationText
void setInformationText(const QString &text)
Definition: namelistdialog.cpp:42
ElementScene::partsRemoved
void partsRemoved()
Signal emitted after one or several parts were removed.
ElementScene::centerElementToOrigine
void centerElementToOrigine()
ElementScene::centerElementToOrigine try to center better is possible the element to the scene (the c...
Definition: elementscene.cpp:1291
ElementScene::m_x_grid
int m_x_grid
Definition: elementscene.h:87
partline.h
ElementScene::m_qgi_manager
QGIManager m_qgi_manager
element kind info
Definition: elementscene.h:72
ElementScene::reset
virtual void reset()
ElementScene::reset Remove all QGraphicsItems in the scene and clear the undo stack.
Definition: elementscene.cpp:1006
ElementScene::zValueLessThan
static bool zValueLessThan(QGraphicsItem *, QGraphicsItem *)
ElementScene::zValueLessThan.
Definition: elementscene.cpp:1282
ElementScene::slot_delete
void slot_delete()
ElementScene::slot_delete Delete selected items.
Definition: elementscene.cpp:766
ElementScene::PasteArea
@ PasteArea
Definition: elementscene.h:46
CustomElementPart::fromXml
virtual void fromXml(const QDomElement &)=0
ElementScene::setGrid
virtual void setGrid(int, int)
ElementScene::setGrid.
Definition: elementscene.cpp:381
elementpropertieseditorwidget.h
ChangeInformationsCommand
Definition: editorcommands.h:207
ElementEditionCommand
The ElementEditionCommand class ElementEditionCommand is the base class for all commands classes invo...
Definition: editorcommands.h:33
namelistdialog.h
ElementScene::pasteAreaDefined
void pasteAreaDefined(const QRectF &)
Signal emitted when users have defined the copy/paste area.
ElementScene::behavior
ElementScene::Behavior behavior() const
Definition: elementscene.cpp:352
ElementScene::xGrid
virtual int xGrid() const
ElementScene::xGrid.
Definition: elementscene.cpp:361
ElementScene::selectedContent
virtual ElementContent selectedContent() const
ElementScene::selectedContent.
Definition: elementscene.cpp:977
qetelementeditor.h
ElementScene::m_y_grid
int m_y_grid
Definition: elementscene.h:88
ElementScene::~ElementScene
~ElementScene() override
ElementScene::~ElementScene.
Definition: elementscene.cpp:79
NameListDialog
The NameListDialog class Provide a dialog for let user define localized string;.
Definition: namelistdialog.h:34
ElementScene::yGrid
virtual int yGrid() const
ElementScene::yGrid.
Definition: elementscene.cpp:370
PartDynamicTextField::fromTextFieldXml
void fromTextFieldXml(const QDomElement &dom_element)
PartDynamicTextField::fromTextFieldXml Setup this text from the xml definition of a text field (The x...
Definition: partdynamictextfield.cpp:225
ElementScene::m_elmt_type
QString m_elmt_type
Extra informations.
Definition: elementscene.h:69
QET::Icons::PartRectangle
QIcon PartRectangle
Definition: qeticons.cpp:132
partterminal.h
ChangePropertiesCommand
Definition: editorcommands.h:265
ElementScene::editor
QETElementEditor * editor() const
ElementScene::editor.
Definition: elementscene.cpp:683
ElementScene::drawForeground
void drawForeground(QPainter *, const QRectF &) override
ElementScene::drawForeground Draws the background of the editor, ie the hotspot indicator.
Definition: elementscene.cpp:294
ElementScene::ElementScene
ElementScene(const ElementScene &)
partarc.h
ElementScene::IncludeTerminals
@ IncludeTerminals
Definition: elementscene.h:49
ElementScene::containsTerminals
bool containsTerminals() const
ElementScene::containsTerminals.
Definition: elementscene.cpp:589
PartTerminal
Definition: partterminal.h:30
PartDynamicTextField
The PartDynamicTextField class This class represents an editable dynamic text field which may be used...
Definition: partdynamictextfield.h:34
ElementScene::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *) override
ElementScene::mouseMoveEvent.
Definition: elementscene.cpp:97
ElementScene::primitives
virtual QList< CustomElementPart * > primitives() const
ElementScene::primitives.
Definition: elementscene.cpp:895
ElementScene::m_element_editor
QETElementEditor * m_element_editor
Definition: elementscene.h:77
partrectangle.h
elementscene.h
DeletePartsCommand
Definition: editorcommands.h:65
QGIManager::release
void release(QGraphicsItem *)
Definition: qgimanager.cpp:60
parttext.h
eseventinterface.h
ElementScene::slot_editAuthorInformations
void slot_editAuthorInformations()
ElementScene::slot_editAuthorInformations Starts a dialog to edit the additional information of this ...
Definition: elementscene.cpp:790
ElementScene::setEventInterface
void setEventInterface(ESEventInterface *event_interface)
ElementScene::setEventInterface Set a new event interface.
Definition: elementscene.cpp:317
ElementPrimitiveDecorator::Type
@ Type
Definition: elementprimitivedecorator.h:48
ElementScene::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override
ElementScene::mouseReleaseEvent.
Definition: elementscene.cpp:147
ElementScene::informations
QString informations() const
ElementScene::informations.
Definition: elementscene.h:203
ElementScene::names
NamesList names() const
ElementScene::names.
Definition: elementscene.h:195
qpropertyundocommand.h
ElementScene::SortByZValue
@ SortByZValue
Definition: elementscene.h:48
ElementScene::SelectedOrNot
@ SelectedOrNot
Definition: elementscene.h:53
ElementScene::slot_deselectAll
void slot_deselectAll()
ElementScene::slot_deselectAll deselect all item.
Definition: elementscene.cpp:744
ChangeNamesCommand
Definition: editorcommands.h:149
QET::Icons::PartEllipse
QIcon PartEllipse
Definition: qeticons.cpp:129
qetgraphicshandleritem.h
ElementScene::initPasteArea
void initPasteArea()
ElementScene::initPasteArea Initializes the paste area.
Definition: elementscene.cpp:1243
ElementScene::slot_selectAll
void slot_selectAll()
ElementScene::slot_selectAll Select all items.
Definition: elementscene.cpp:736
ElementScene::m_event_interface
ESEventInterface * m_event_interface
Definition: elementscene.h:75
editorcommands.h
ElementScene::qgiManager
QGIManager & qgiManager()
ElementScene::qgiManager.
Definition: elementscene.cpp:612
ElementScene::m_names_list
NamesList m_names_list
Definition: elementscene.h:67
ElementScene::needZoomFit
void needZoomFit()
Signal emitted when need zoomFit.
NameListWidget::names
NamesList names() const
NameListWidget::names.
Definition: namelistwidget.cpp:82
ElementScene::elementInfoChanged
void elementInfoChanged()
ElementScene::elementInformation
DiagramContext elementInformation() const
Definition: elementscene.h:103
ElementScene::slot_select
void slot_select(const ElementContent &)
ElementScene::slot_select Select the item in content, every others items in the scene are deselected.
Definition: elementscene.cpp:706
ElementScene::clearEventInterface
void clearEventInterface()
ElementScene::clearEventInterface Clear the current event interface.
Definition: elementscene.cpp:333
NamesList::fromXml
void fromXml(const QDomElement &, const QHash< QString, QString > &=QHash< QString, QString >())
Definition: nameslist.cpp:113
ElementScene::ItemOption
ItemOption
Definition: elementscene.h:47
ElementScene::m_paste_area
QGraphicsRectItem * m_paste_area
Definition: elementscene.h:79
ElementScene::applyInformations
bool applyInformations(const QDomDocument &)
ElementScene::applyInformations Applies the information (dimensions, hostpot, orientations,...
Definition: elementscene.cpp:1065
NamesList
Definition: nameslist.h:30
ElementScene::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *) override
ElementScene::mousePressEvent.
Definition: elementscene.cpp:128
ElementPropertiesEditorWidget
The ElementPropertiesEditorWidget class This class provide a dialog for edit various property of elem...
Definition: elementpropertieseditorwidget.h:35
elementcontent.h
ElementScene::addContentAtPos
ElementContent addContentAtPos(const ElementContent &, const QPointF &)
ElementScene::addContentAtPos Add content content to this element.
Definition: elementscene.cpp:1205
ElementScene::m_decorator_lock
QMutex * m_decorator_lock
Definition: elementscene.h:151
ElementScene::setBehavior
void setBehavior(ElementScene::Behavior)
ElementScene::setBehavior Modify the current behavior of this scene.
Definition: elementscene.cpp:348
ElementScene::copy
void copy()
ElementScene::copy Handles the fact of copying the selection = exporting it as XML to the clipboard.
Definition: elementscene.cpp:662
ElementScene::clipboardMayContainElement
static bool clipboardMayContainElement()
ElementScene::clipboardMayContainElement.
Definition: elementscene.cpp:621
ElementScene::elementType
QString elementType() const
Definition: elementscene.h:101
ElementScene::slot_editProperties
void slot_editProperties()
ElementScene::slot_editProperties Open dialog to edit the element properties.
Definition: elementscene.cpp:842
DiagramContext::toXml
void toXml(QDomElement &, const QString &="property") const
Definition: diagramcontext.cpp:142
CustomElementPart
The CustomElementPart class This abstract class represents a primitive of the visual representation o...
Definition: customelementpart.h:40
ElementScene::Behavior
Behavior
Definition: elementscene.h:46
QGIManager
Definition: qgimanager.h:27
ElementScene::ChangePropertiesCommand
friend class ChangePropertiesCommand
Definition: elementscene.h:41
QET::Icons::PartLine
QIcon PartLine
Definition: qeticons.cpp:130
diagramcontext.h
ElementScene::boundingRectFromXml
virtual QRectF boundingRectFromXml(const QDomDocument &)
ElementScene::boundingRectFromXml.
Definition: elementscene.cpp:496
NameListWidget
The NameListWidget class Provide a widget for let user define localized string;.
Definition: namelistwidget.h:33
ElementScene::m_informations
QString m_informations
List of localized names.
Definition: elementscene.h:68
NameListWidget::setNames
void setNames(const NamesList &name_list)
NameListWidget::setNames Set the current names of this dialog from name_list.
Definition: namelistwidget.cpp:60
ElementScene::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
ElementScene::keyPressEvent manage key press event.
Definition: elementscene.cpp:194
PartText
Definition: parttext.h:30
ElementScene::wasCopiedFromThisElement
bool wasCopiedFromThisElement(const QString &)
ElementScene::wasCopiedFromThisElement.
Definition: elementscene.cpp:637
namelistwidget.h
QET::Icons::PartPolygon
QIcon PartPolygon
Definition: qeticons.cpp:131
PartDynamicTextField::Type
@ Type
Definition: partdynamictextfield.h:67
ElementScene
The ElementScene class This class is the canvas allowing the visual edition of an electrial element....
Definition: elementscene.h:40
ElementScene::Normal
@ Normal
Definition: elementscene.h:46
QPropertyUndoCommand
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
Definition: qpropertyundocommand.h:34
ElementScene::m_decorator
ElementPrimitiveDecorator * m_decorator
Decorator item displayed when at least one item is selected.
Definition: elementscene.h:85
ElementScene::Selected
@ Selected
Definition: elementscene.h:51
ElementScene::elementKindInfo
DiagramContext elementKindInfo() const
Definition: elementscene.h:102
NamesList::toXml
QDomElement toXml(QDomDocument &, const QHash< QString, QString > &=QHash< QString, QString >()) const
Definition: nameslist.cpp:169
ElementScene::m_elmt_kindInfo
DiagramContext m_elmt_kindInfo
element type
Definition: elementscene.h:70
partpolygon.h
ElementScene::ElementScene
ElementScene(QETElementEditor *, QObject *=nullptr)
ElementScene::ElementScene constructor.
Definition: elementscene.cpp:51
QET::Icons::tr
QIcon tr
Definition: qeticons.cpp:206
ElementScene::elementContentBoundingRect
QRectF elementContentBoundingRect(const ElementContent &) const
ElementScene::elementContentBoundingRect.
Definition: elementscene.cpp:1039
ElementScene::IncludeHelperItems
@ IncludeHelperItems
Definition: elementscene.h:50
ElementScene::slot_editNames
void slot_editNames()
ElementScene::slot_editNames Launch a dialog for edit the names of the edited element.
Definition: elementscene.cpp:864
ElementScene::AddPart
@ AddPart
Definition: elementscene.h:46
partdynamictextfield.h
QET::Icons::Cancel
QIcon Cancel
Definition: qeticons.cpp:34
QETElementEditor
Definition: qetelementeditor.h:33
DiagramContext
Definition: diagramcontext.h:56
ESEventInterface
Definition: eseventinterface.h:30
NameListDialog::namelistWidget
NameListWidget * namelistWidget() const
NameListDialog::namelistWidget.
Definition: namelistdialog.cpp:51
ElementScene::addContent
ElementContent addContent(const ElementContent &)
ElementScene::addContent Add content content to this element.
Definition: elementscene.cpp:1185
ElementScene::setElementInfo
void setElementInfo(const DiagramContext &dc)
ElementScene::setElementInfo.
Definition: elementscene.cpp:691
ElementScene::getPasteArea
virtual void getPasteArea(const QRectF &)
ElementScene::getPasteArea.
Definition: elementscene.cpp:992