QElectroTech  0.8-dev
elementprimitivedecorator.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 ELEMENTPRIMITIVEDECORATOR_H
19 #define ELEMENTPRIMITIVEDECORATOR_H
20 
21 #include <QGraphicsObject>
22 #include "qet.h"
23 
25 class ElementScene;
26 class CustomElementPart;
28 
40 class ElementPrimitiveDecorator : public QGraphicsObject
41 {
42  Q_OBJECT
43 
44  public:
45  ElementPrimitiveDecorator(QGraphicsItem * = nullptr);
46  ~ElementPrimitiveDecorator() override;
47 
48  enum { Type = UserType + 2200 };
49 
50  // methods
51  QRectF internalBoundingRect() const;
52  QRectF boundingRect () const override;
53  void paint(QPainter *,
54  const QStyleOptionGraphicsItem *,
55  QWidget * = nullptr) override;
56  int type() const override { return Type; }
57  void setItems(const QList<QGraphicsItem *> &);
58  void setItems(const QList<CustomElementPart *> &);
59  QList<CustomElementPart *> items() const;
60  QList<QGraphicsItem *> graphicsItems() const;
61 
62  public slots:
63  void adjust();
64 
65  signals:
67 
68  protected:
69  void mousePressEvent(QGraphicsSceneMouseEvent *) override;
70  void mouseMoveEvent(QGraphicsSceneMouseEvent *) override;
71  void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override;
72  void keyPressEvent(QKeyEvent *) override;
73  void keyReleaseEvent(QKeyEvent *) override;
74  QPointF deltaForRoundScaling(const QRectF &, const QRectF &, qreal);
75  QPointF snapConstPointToGrid(const QPointF &) const;
76  void snapPointToGrid(QPointF &) const;
77  bool mustSnapToGrid(QGraphicsSceneMouseEvent *);
78  QET::ScalingMethod scalingMethod(QGraphicsSceneMouseEvent *);
79  QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
80  bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
81 
82  private:
83  void init();
86  void startMovement();
87  void applyMovementToRect(int, const QPointF &, QRectF &);
89  void translateItems(const QPointF &);
90  void scaleItems(const QRectF &, const QRectF &);
91  QRectF getSceneBoundingRect(QGraphicsItem *) const;
92  QVector <QPointF> getResizingsPoints() const;
93 
94 
95  private:
96  void adjusteHandlerPos();
97  void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
98  void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
99  void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
100 
101  void addHandler();
102  void removeHandler();
103 
104 
105 
106 
107  QList<CustomElementPart *> decorated_items_;
111 
119  QPointF first_pos_;
120  QPointF latest_pos_;
121  QPointF mouse_offset_;
123  QPointF keys_movement_;
124  QVector<QetGraphicsHandlerItem *> m_handler_vector;
125  int m_vector_index = -1;
126 };
127 
128 #endif
ElementPrimitiveDecorator::items
QList< CustomElementPart * > items() const
Definition: elementprimitivedecorator.cpp:142
QET::NoOperation
@ NoOperation
Definition: qet.h:69
ElementPrimitiveDecorator::grid_step_x_
int grid_step_x_
Grid horizontal step.
Definition: elementprimitivedecorator.h:117
ElementPrimitiveDecorator
Definition: elementprimitivedecorator.h:41
ElementPrimitiveDecorator::saveOriginalBoundingRect
void saveOriginalBoundingRect()
Definition: elementprimitivedecorator.cpp:340
QetGraphicsHandlerItem::Type
@ Type
Definition: qetgraphicshandleritem.h:42
ElementPrimitiveDecorator::graphicsItems
QList< QGraphicsItem * > graphicsItems() const
Definition: elementprimitivedecorator.cpp:149
ElementPrimitiveDecorator::removeHandler
void removeHandler()
ElementPrimitiveDecorator::removeHandler Remove the handlers of this item.
Definition: elementprimitivedecorator.cpp:621
ElementPrimitiveDecorator::adjusteHandlerPos
void adjusteHandlerPos()
ElementPrimitiveDecorator::adjusteHandlerPos.
Definition: elementprimitivedecorator.cpp:487
ElementPrimitiveDecorator::m_vector_index
int m_vector_index
Definition: elementprimitivedecorator.h:125
ElementPrimitiveDecorator::adjustEffectiveBoundingRect
void adjustEffectiveBoundingRect()
Definition: elementprimitivedecorator.cpp:348
ElementPrimitiveDecorator::ElementPrimitiveDecorator
ElementPrimitiveDecorator(QGraphicsItem *=nullptr)
Definition: elementprimitivedecorator.cpp:33
ElementPrimitiveDecorator::handlerMouseMoveEvent
void handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
ElementPrimitiveDecorator::handlerMouseMoveEvent.
Definition: elementprimitivedecorator.cpp:516
QET::round
qreal round(qreal, qreal)
Definition: qet.cpp:537
ElementPrimitiveDecorator::singleItem
CustomElementPart * singleItem() const
Definition: elementprimitivedecorator.cpp:412
ElementPrimitiveDecorator::~ElementPrimitiveDecorator
~ElementPrimitiveDecorator() override
Definition: elementprimitivedecorator.cpp:42
ElementPrimitiveDecorator::latest_pos_
QPointF latest_pos_
Latest point involved within the current resizing operation.
Definition: elementprimitivedecorator.h:120
ElementPrimitiveDecorator::keyPressEvent
void keyPressEvent(QKeyEvent *) override
ElementPrimitiveDecorator::keyPressEvent.
Definition: elementprimitivedecorator.cpp:257
QET::ResizeFromBottomRightCorner
@ ResizeFromBottomRightCorner
Definition: qet.h:77
ElementPrimitiveDecorator::effective_bounding_rect_
QRectF effective_bounding_rect_
actual, effective bounding rect – never shrinks
Definition: elementprimitivedecorator.h:108
ElementPrimitiveDecorator::getSceneBoundingRect
QRectF getSceneBoundingRect(QGraphicsItem *) const
Definition: elementprimitivedecorator.cpp:453
QET::SnapScalingPointToGrid
@ SnapScalingPointToGrid
snap the point used to define the new bounding rectangle to the grid
Definition: qet.h:83
elementprimitivedecorator.h
QET::FreeScaling
@ FreeScaling
do not interfer with the default scaling process
Definition: qet.h:82
ElementPrimitiveDecorator::actionFinished
void actionFinished(ElementEditionCommand *)
ElementPrimitiveDecorator::applyMovementToRect
void applyMovementToRect(int, const QPointF &, QRectF &)
Definition: elementprimitivedecorator.cpp:369
ElementPrimitiveDecorator::snapConstPointToGrid
QPointF snapConstPointToGrid(const QPointF &) const
Definition: elementprimitivedecorator.cpp:658
ElementPrimitiveDecorator::modified_bounding_rect_
QRectF modified_bounding_rect_
new bounding rect, after the user moved or resized items
Definition: elementprimitivedecorator.h:110
ElementPrimitiveDecorator::mustSnapToGrid
bool mustSnapToGrid(QGraphicsSceneMouseEvent *)
Definition: elementprimitivedecorator.cpp:680
ElementPrimitiveDecorator::first_pos_
QPointF first_pos_
First point involved within the current resizing operation.
Definition: elementprimitivedecorator.h:119
ElementPrimitiveDecorator::type
int type() const override
Definition: elementprimitivedecorator.h:56
ElementPrimitiveDecorator::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *) override
Definition: elementprimitivedecorator.cpp:195
ElementEditionCommand
The ElementEditionCommand class ElementEditionCommand is the base class for all commands classes invo...
Definition: editorcommands.h:33
ElementPrimitiveDecorator::mouse_offset_
QPointF mouse_offset_
Offset between the mouse position and the point to be snapped to grid when moving selection.
Definition: elementprimitivedecorator.h:121
ElementPrimitiveDecorator::adjust
void adjust()
Definition: elementprimitivedecorator.cpp:163
ElementPrimitiveDecorator::original_bounding_rect_
QRectF original_bounding_rect_
original bounding rect
Definition: elementprimitivedecorator.h:109
qet.h
ElementPrimitiveDecorator::handlerMouseReleaseEvent
void handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
ElementPrimitiveDecorator::handlerMouseReleaseEvent.
Definition: elementprimitivedecorator.cpp:571
QET::ResizeFromTopLeftCorner
@ ResizeFromTopLeftCorner
Definition: qet.h:70
ElementPrimitiveDecorator::sceneEventFilter
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override
ElementPrimitiveDecorator::sceneEventFilter.
Definition: elementprimitivedecorator.cpp:736
QET::ResizeFromTopCenterCorner
@ ResizeFromTopCenterCorner
Definition: qet.h:71
ElementPrimitiveDecorator::init
void init()
Definition: elementprimitivedecorator.cpp:330
ElementPrimitiveDecorator::deltaForRoundScaling
QPointF deltaForRoundScaling(const QRectF &, const QRectF &, qreal)
Definition: elementprimitivedecorator.cpp:640
ElementPrimitiveDecorator::scaleItems
void scaleItems(const QRectF &, const QRectF &)
Definition: elementprimitivedecorator.cpp:437
elementscene.h
ElementPrimitiveDecorator::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *) override
Definition: elementprimitivedecorator.cpp:174
ElementPrimitiveDecorator::decorated_items_
QList< CustomElementPart * > decorated_items_
Definition: elementprimitivedecorator.h:107
ElementPrimitiveDecorator::Type
@ Type
Definition: elementprimitivedecorator.h:48
ElementPrimitiveDecorator::current_operation_square_
int current_operation_square_
Definition: elementprimitivedecorator.h:116
QET::ResizeFromBottomCenterCorner
@ ResizeFromBottomCenterCorner
Definition: qet.h:76
QET::ResizeFromTopRightCorner
@ ResizeFromTopRightCorner
Definition: qet.h:72
qetgraphicshandleritem.h
ElementPrimitiveDecorator::scalingMethod
QET::ScalingMethod scalingMethod(QGraphicsSceneMouseEvent *)
Definition: elementprimitivedecorator.cpp:690
editorcommands.h
QetGraphicsHandlerItem
The QetGraphicsHandlerItem class This graphics item represents a point, destined to be used as an han...
Definition: qetgraphicshandleritem.h:37
MovePartsCommand
Definition: editorcommands.h:99
ElementPrimitiveDecorator::addHandler
void addHandler()
ElementPrimitiveDecorator::addHandler Add handlers for this item.
Definition: elementprimitivedecorator.cpp:601
QET::MoveArea
@ MoveArea
Definition: qet.h:68
ElementPrimitiveDecorator::paint
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *=nullptr) override
ElementPrimitiveDecorator::paint Paint the contents of an item in local coordinates,...
Definition: elementprimitivedecorator.cpp:88
ElementPrimitiveDecorator::itemChange
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
ElementPrimitiveDecorator::itemChange.
Definition: elementprimitivedecorator.cpp:706
ElementPrimitiveDecorator::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *) override
ElementPrimitiveDecorator::keyReleaseEvent.
Definition: elementprimitivedecorator.cpp:310
ElementPrimitiveDecorator::moving_by_keys_
bool moving_by_keys_
Whether we are currently moving our decorated items using the arrow keys.
Definition: elementprimitivedecorator.h:122
QET::ResizeFromMiddleLeftCorner
@ ResizeFromMiddleLeftCorner
Definition: qet.h:73
ElementPrimitiveDecorator::handlerMousePressEvent
void handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
ElementPrimitiveDecorator::handlerMousePressEvent.
Definition: elementprimitivedecorator.cpp:499
ElementPrimitiveDecorator::startMovement
void startMovement()
Definition: elementprimitivedecorator.cpp:358
ElementPrimitiveDecorator::grid_step_y_
int grid_step_y_
Grid horizontal step.
Definition: elementprimitivedecorator.h:118
ElementPrimitiveDecorator::getResizingsPoints
QVector< QPointF > getResizingsPoints() const
Definition: elementprimitivedecorator.cpp:458
CustomElementPart
The CustomElementPart class This abstract class represents a primitive of the visual representation o...
Definition: customelementpart.h:40
ElementPrimitiveDecorator::m_handler_vector
QVector< QetGraphicsHandlerItem * > m_handler_vector
Definition: elementprimitivedecorator.h:124
ElementPrimitiveDecorator::snapPointToGrid
void snapPointToGrid(QPointF &) const
Definition: elementprimitivedecorator.cpp:671
QET::ResizeFromBottomLeftCorner
@ ResizeFromBottomLeftCorner
Definition: qet.h:75
QET::ScalingMethod
ScalingMethod
Supported types of interactive scaling, typically for a single element primitive.
Definition: qet.h:81
QET::RoundScaleRatios
@ RoundScaleRatios
adjust the scaling movement so that the induced scaling ratios are rounded
Definition: qet.h:84
QET::ResizeFromMiddleRightCorner
@ ResizeFromMiddleRightCorner
Definition: qet.h:74
ElementPrimitiveDecorator::boundingRect
QRectF boundingRect() const override
Definition: elementprimitivedecorator.cpp:70
ElementScene
The ElementScene class This class is the canvas allowing the visual edition of an electrial element....
Definition: elementscene.h:40
QetGraphicsHandlerItem::handlerForPoint
static QVector< QetGraphicsHandlerItem * > handlerForPoint(const QVector< QPointF > &points, int size=10)
QetGraphicsHandlerItem::handlerForPoint.
Definition: qetgraphicshandleritem.cpp:87
ElementPrimitiveDecorator::setItems
void setItems(const QList< QGraphicsItem * > &)
Definition: elementprimitivedecorator.cpp:126
customelementpart.h
ElementPrimitiveDecorator::internalBoundingRect
QRectF internalBoundingRect() const
Definition: elementprimitivedecorator.cpp:51
ElementPrimitiveDecorator::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override
Definition: elementprimitivedecorator.cpp:227
ElementPrimitiveDecorator::translateItems
void translateItems(const QPointF &)
Definition: elementprimitivedecorator.cpp:422
ScalePartsCommand
Definition: editorcommands.h:231
ElementPrimitiveDecorator::keys_movement_
QPointF keys_movement_
Movement applied to our decorated items using the arrow keys.
Definition: elementprimitivedecorator.h:123