QElectroTech  0.70
diagrameventaddshape.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 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 #include "diagrameventaddshape.h"
19 #include "diagram.h"
20 #include "diagramcommands.h"
21 
29  DiagramEventInterface(diagram),
30  m_shape_type (shape_type),
31  m_shape_item (nullptr),
32  m_help_horiz (nullptr),
33  m_help_verti (nullptr)
34 {
35  m_running = true;
36  init();
37 }
38 
43 {
44  if ((m_running || m_abort) && m_shape_item)
45  {
47  delete m_shape_item;
48  }
49  delete m_help_horiz;
50  delete m_help_verti;
51 
52  foreach (QGraphicsView *v, m_diagram->views())
53  v->setContextMenuPolicy(Qt::DefaultContextMenu);
54 }
55 
61 void DiagramEventAddShape::mousePressEvent(QGraphicsSceneMouseEvent *event)
62 {
63  if (Q_UNLIKELY(m_diagram->isReadOnly())) {
64  return;
65  }
66 
67  QPointF pos = event->scenePos();
68  if (event->modifiers() != Qt::ControlModifier) {
69  pos = Diagram::snapToGrid(pos);
70  }
71 
72  //Action for left mouse click
73  if (event->button() == Qt::LeftButton)
74  {
75  //Create shape item
76  if (!m_shape_item)
77  {
78  m_shape_item = new QetShapeItem(pos, pos, m_shape_type);
80  event->setAccepted(true);
81  return;
82  }
83 
84  //If current item isn't a polyline, add it with an undo command
86  {
87  m_shape_item->setP2 (pos);
89  m_shape_item->setRect(m_shape_item->rect().normalized());
90  }
92  m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic
93  }
94  //Else add a new point to polyline
95  else
96  {
98  }
99 
100  event->setAccepted(true);
101  return;
102  }
103 
104  if (event->button() == Qt::RightButton) {
105  event->setAccepted(true);
106  }
107 }
108 
114 void DiagramEventAddShape::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
115 {
116  updateHelpCross(event->scenePos());
117 
118  if (m_shape_item && event->buttons() == Qt::NoButton)
119  {
120  QPointF pos = event->scenePos();
121  if (event->modifiers() != Qt::ControlModifier) {
122  pos = Diagram::snapToGrid(pos);
123  }
124 
125  m_shape_item->setP2 (pos);
126  event->setAccepted(true);
127  }
128 }
129 
135 void DiagramEventAddShape::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
136 {
137  if (event->button() == Qt::RightButton)
138  {
139  //If shape is created, we manage right click
140  if (m_shape_item)
141  {
142  //Shape is a polyline and have three points or more we just remove the last point
144  {
146 
147  QPointF pos = event->scenePos();
148  if (event->modifiers() != Qt::ControlModifier)
149  pos = Diagram::snapToGrid(pos);
150 
151  m_shape_item->setP2(pos); //Set the new last point under the cursor
152  event->setAccepted(true);
153  return;
154  }
155 
156  //For other case, we remove item from scene
158  delete m_shape_item;
159  m_shape_item = nullptr;
160  event->setAccepted(true);
161  return;
162  }
163 
164  //Else (no shape), we set to false the running status
165  //for indicate to the owner of this event that everything is done
166  m_running = false;
167  emit finish();
168  event->setAccepted(true);
169  }
170 }
171 
177 void DiagramEventAddShape::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
178 {
179  //If current item is a polyline, add it with an undo command
180  if (m_shape_item && m_shape_type == QetShapeItem::Polygon && event->button() == Qt::LeftButton)
181  {
182  //<double clic is used to finish polyline, but they also add two points at the same pos
183  //<(double clic is a double press event), so we remove the last point of polyline
185 
186  //If the last is at the same pos of the first point
187  //that mean user want a closed polygon, so we remove the last point and close polygon
188  QPolygonF polygon = m_shape_item->polygon();
189  if (polygon.first() == polygon.last())
190  {
192  m_shape_item->setClosed(true);
193  }
195  m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic
196  event->setAccepted(true);
197  }
198 }
199 
201 {
202  foreach (QGraphicsView *v, m_diagram->views())
203  v->setContextMenuPolicy(Qt::NoContextMenu);
204 }
205 
212 {
213  //If line isn't created yet, we create it.
214  if (!m_help_horiz || !m_help_verti)
215  {
216  QPen pen;
217  pen.setWidthF(0.4);
218  pen.setCosmetic(true);
219  pen.setColor(Diagram::background_color == Qt::darkGray ? Qt::lightGray : Qt::darkGray);
220 
222 
223  if (!m_help_horiz)
224  {
225  m_help_horiz = new QGraphicsLineItem(rect.topLeft().x(), 0, rect.topRight().x(), 0);
226  m_help_horiz->setPen(pen);
228  }
229 
230  if (!m_help_verti)
231  {
232  m_help_verti = new QGraphicsLineItem(0, rect.topLeft().y(), 0, rect.bottomLeft().y());
233  m_help_verti->setPen(pen);
235  }
236  }
237 
238  //Update the position of the cross
239  QPointF point = Diagram::snapToGrid(p);
240 
241  m_help_horiz->setY(point.y());
242  m_help_verti->setX(point.x());
243 }
QetShapeItem::ShapeType m_shape_type
DiagramEventAddShape(Diagram *diagram, QetShapeItem::ShapeType shape_type)
DiagramEventAddShape::DiagramEventAddShape Default constructor.
void removePoints(int number=1)
QetShapeItem::removePoints Number of point to remove on the polygon If is superior to number of poly...
QRectF insideBorderRect() const
BorderTitleBlock::insideBorderRect.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddShape::mouseDoubleClickEvent Action when mouse button is double clicked.
bool isReadOnly() const
Diagram::isReadOnly.
Definition: diagram.cpp:1764
The AddItemCommand class This command add an item in a diagram The item to add is template...
QGraphicsLineItem * m_help_verti
void updateHelpCross(const QPointF &p)
DiagramEventAddShape::updateHelpCross Create and update the position of the cross to help user for dr...
void setP2(const QPointF &P2)
QetShapeItem::setP2 Set the second point of this item. If this item is a polyline, the last point of the polyline is replaced by P2.
virtual void removeItem(QGraphicsItem *item)
Diagram::removeItem Réimplemented from QGraphicsScene::removeItem(QGraphicsItem *item) Do some specif...
Definition: diagram.cpp:1133
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddShape::mouseMoveEvent Action when mouse move.
int pointsCount() const
QetShapeItem::pointCount.
~DiagramEventAddShape() override
DiagramEventAddShape::~DiagramEventAddShape.
void setClosed(bool close)
QetShapeItem::setClosed Close this item, have effect only if this item is a polygon.
bool setRect(const QRectF &rect)
QetShapeItem::setRect Set this item geometry to rect (only available if shape is a rectangle or an el...
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddShape::mouseReleaseEvent Action when mouse button is released.
The QetShapeItem class this class is used to draw a basic shape (line, rectangle, ellipse) into a dia...
Definition: qetshapeitem.h:35
static QColor background_color
background color of diagram
Definition: diagram.h:91
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:1653
QUndoStack & undoStack()
Definition: diagram.h:337
The DiagramEventInterface class isRunning() return true if action is running (do something). By default return false.
QGraphicsLineItem * m_help_horiz
ShapeType shapeType() const
Definition: qetshapeitem.h:76
virtual void addItem(QGraphicsItem *item)
Diagram::addItem Réimplemented from QGraphicsScene::addItem(QGraphicsItem *item) Do some specific ope...
Definition: diagram.cpp:1108
BorderTitleBlock border_and_titleblock
Diagram dimensions and title block.
Definition: diagram.h:74
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddShape::mousePressEvent Action when mouse is pressed.
void setNextPoint(QPointF P)
QetShapeItem::setNextPoint Add a new point to the curent polygon.
QPolygonF polygon
Definition: qetshapeitem.h:43