Seamly2D
Code documentation
vmaingraphicsscene.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vmaingraphicsscene.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vmaingraphicsscene.h"
53 
54 #include <QBrush>
55 #include <QEvent>
56 #include <QGraphicsItem>
57 #include <QGraphicsLineItem>
58 #include <QGraphicsSceneMouseEvent>
59 #include <QGraphicsSimpleTextItem>
60 #include <QLineF>
61 #include <QPen>
62 #include <QStaticStringData>
63 #include <QStringData>
64 #include <QStringDataPtr>
65 #include <Qt>
66 
67 #include "global.h"
68 #include "../vmisc/vcommonsettings.h"
69 #include "../vmisc/vabstractapplication.h"
70 
71 //---------------------------------------------------------------------------------------------------------------------
72 /**
73  * @brief VMainGraphicsScene default constructor.
74  */
76  : QGraphicsScene(parent)
77  , horScrollBar(0)
78  , verScrollBar(0)
79  , m_previousTransform(QTransform())
80  , m_currentTransform(QTransform())
81  , scenePos(QPointF())
82  , origins()
83 {}
84 
85 //---------------------------------------------------------------------------------------------------------------------
86 /**
87  * @brief VMainGraphicsScene constructor.
88  * @param sceneRect scene rect.
89  * @param parent parent object.
90  */
91 VMainGraphicsScene::VMainGraphicsScene(const QRectF & sceneRect, QObject * parent)
92  :QGraphicsScene ( sceneRect, parent )
93  , horScrollBar(0)
94  , verScrollBar(0)
95  , m_previousTransform(QTransform())
96  , m_currentTransform(QTransform())
97  , scenePos()
98  , origins()
99 {}
100 
101 //---------------------------------------------------------------------------------------------------------------------
102 /**
103  * @brief mouseMoveEvent handle mouse move events.
104  * @param event mouse move event.
105  */
106 void VMainGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
107 {
108  scenePos = event->scenePos();
109  emit mouseMove(event->scenePos());
110  QGraphicsScene::mouseMoveEvent(event);
111 }
112 
113 //---------------------------------------------------------------------------------------------------------------------
114 /**
115  * @brief mousePressEvent mouse press events.
116  * @param event mouse press event
117  */
118 void VMainGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
119 {
120  if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
121  {
122  emit MouseLeftPressed();
123  }
124 
125  QGraphicsScene::mousePressEvent(event);
126 
127  QTransform t;
128  QGraphicsItem* pItem = itemAt(event->scenePos(), t);
129  emit ItemClicked(pItem);
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
133 void VMainGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
134 {
135  if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
136  {
137  emit MouseLeftReleased();
138  }
139  QGraphicsScene::mouseReleaseEvent(event);
140 }
141 
142 //---------------------------------------------------------------------------------------------------------------------
144 {
145  origins.clear();
146 
147  QColor orginColor = (QColor(qApp->Settings()->getAxisOrginColor()));
148  QPen originsPen(orginColor, widthHairLine, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
149  QBrush axisTextBrush(orginColor);
150  const qreal arrowAngle = 35.0;
151  const qreal arrowLength = 12.0;
152 
153  {
154  // X axis
155  const QLineF lineX(QPointF(25, 0), QPointF(-5, 0));
156  QGraphicsLineItem *xLine1 = new QGraphicsLineItem(lineX);
157  xLine1->setPen(originsPen);
158  xLine1->setFlag(QGraphicsItem::ItemIgnoresTransformations);
159  xLine1->setZValue(-1.0);
160  addItem(xLine1);
161  origins.append(xLine1);
162 
163  // Arrow left side
164  QLineF arrowLeftLine = lineX;
165  arrowLeftLine.setAngle(arrowLeftLine.angle()-arrowAngle);
166  arrowLeftLine.setLength(arrowLength);
167  QGraphicsLineItem *xLine2 = new QGraphicsLineItem(arrowLeftLine);
168  xLine2->setPen(originsPen);
169  xLine2->setFlag(QGraphicsItem::ItemIgnoresTransformations);
170  xLine2->setZValue(-1.0);
171  addItem(xLine2);
172  origins.append(xLine2);
173 
174  // Arrow right side
175  QLineF arrowRightLine = lineX;
176  arrowRightLine.setAngle(arrowRightLine.angle()+arrowAngle);
177  arrowRightLine.setLength(arrowLength);
178  QGraphicsLineItem *xLine3 = new QGraphicsLineItem(arrowRightLine);
179  xLine3->setPen(originsPen);
180  xLine3->setFlag(QGraphicsItem::ItemIgnoresTransformations);
181  xLine3->setZValue(-1.0);
182  addItem(xLine3);
183  origins.append(xLine3);
184 
185  // X axis text
186  QGraphicsSimpleTextItem *xOrigin = new QGraphicsSimpleTextItem(QStringLiteral("X"), xLine1);
187  xOrigin->setBrush(axisTextBrush);
188  xOrigin->setFlag(QGraphicsItem::ItemIgnoresTransformations);
189  xOrigin->setZValue(-1.0);
190  xOrigin->setPos(25, -(xOrigin->boundingRect().height()/2));
191  origins.append(xOrigin);
192  }
193 
194  {
195  // Y axis
196  const QLineF lineY(QPointF(0, 25), QPointF(0, -5));
197  QGraphicsLineItem *yLine1 = new QGraphicsLineItem(lineY);
198  yLine1->setPen(originsPen);
199  yLine1->setFlag(QGraphicsItem::ItemIgnoresTransformations);
200  yLine1->setZValue(-1.0);
201  addItem(yLine1);
202  origins.append(yLine1);
203 
204  // Arrow left side
205  QLineF arrowLeftLine = lineY;
206  arrowLeftLine.setAngle(arrowLeftLine.angle()-arrowAngle);
207  arrowLeftLine.setLength(arrowLength);
208  QGraphicsLineItem *yLine2 = new QGraphicsLineItem(arrowLeftLine);
209  yLine2->setPen(originsPen);
210  yLine2->setFlag(QGraphicsItem::ItemIgnoresTransformations);
211  yLine2->setZValue(-1.0);
212  addItem(yLine2);
213  origins.append(yLine2);
214 
215  // Arrow right side
216  QLineF arrowRightLine = lineY;
217  arrowRightLine.setAngle(arrowRightLine.angle()+arrowAngle);
218  arrowRightLine.setLength(arrowLength);
219  QGraphicsLineItem *yLine3 = new QGraphicsLineItem(arrowRightLine);
220  yLine3->setPen(originsPen);
221  yLine3->setFlag(QGraphicsItem::ItemIgnoresTransformations);
222  yLine3->setZValue(-1.0);
223  addItem(yLine3);
224  origins.append(yLine3);
225 
226  // Y axis text
227  QGraphicsSimpleTextItem *yOrigin = new QGraphicsSimpleTextItem(QStringLiteral("Y"), yLine1);
228  yOrigin->setBrush(axisTextBrush);
229  yOrigin->setFlag(QGraphicsItem::ItemIgnoresTransformations);
230  yOrigin->setZValue(-1.0);
231  yOrigin->setPos(-(yOrigin->boundingRect().width()/2), 25);
232  origins.append(yOrigin);
233  }
234 }
235 
236 //---------------------------------------------------------------------------------------------------------------------
238 {
239  foreach (QGraphicsItem *item, origins)
240  {
241  item->setVisible(visible);
242  }
243 }
244 
245 //---------------------------------------------------------------------------------------------------------------------
247 {
248  return scenePos;
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
253 {
254  QRectF rect;
255  foreach(QGraphicsItem *item, items())
256  {
257  if(not item->isVisible())
258  {
259  continue;
260  }
261  rect = rect.united(item->sceneBoundingRect());
262  }
263  return rect;
264 }
265 
266 //---------------------------------------------------------------------------------------------------------------------
267 /**
268  * @brief transform return view transformation.
269  * @return view transformation.
270  */
272 {
273  return m_currentTransform;
274 }
275 
276 //---------------------------------------------------------------------------------------------------------------------
277 /**
278  * @brief setCurrentTransform set view transformation.
279  * @param transform view transformation.
280  */
281 void VMainGraphicsScene::setCurrentTransform(const QTransform &transform)
282 {
285 }
286 
287 //---------------------------------------------------------------------------------------------------------------------
288 /**
289  * @brief swapTransforms.
290  */
292 {
293  QTransform tempTransform = m_currentTransform;
295  m_previousTransform = tempTransform;
296 }
297 
298 //---------------------------------------------------------------------------------------------------------------------
299 void VMainGraphicsScene::SetDisableTools(bool disable, const QString &draftBlockName)
300 {
301  emit DisableItem(disable, draftBlockName);
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
305 /**
306  * @brief chosenItem emit ChosenObject signal.
307  * @param id object id.
308  * @param type object scene type.
309  */
310 void VMainGraphicsScene::chosenItem(quint32 id, const SceneObject &type)
311 {
312  emit ChosenObject(id, type);
313 }
314 
315 //---------------------------------------------------------------------------------------------------------------------
316 void VMainGraphicsScene::SelectedItem(bool selected, quint32 object, quint32 tool)
317 {
318  emit SelectedObject(selected, object, tool);
319 }
320 
321 //---------------------------------------------------------------------------------------------------------------------
323 {
324  emit EnableToolMove(move);
325 }
326 
327 //---------------------------------------------------------------------------------------------------------------------
329 {
330  emit curvePiecesMode(mode);
331 }
332 
333 //---------------------------------------------------------------------------------------------------------------------
335 {
336  emit ItemSelection(type);
337 }
338 
339 //---------------------------------------------------------------------------------------------------------------------
341 {
342  emit highlightPiece(id);
343 }
344 
345 //---------------------------------------------------------------------------------------------------------------------
347 {
348  emit enableTextItemSelection(enabled);
349 }
350 
351 //---------------------------------------------------------------------------------------------------------------------
353 {
354  emit EnablePointItemSelection(enabled);
355 }
356 
357 //---------------------------------------------------------------------------------------------------------------------
359 {
360  emit EnableLineItemSelection(enabled);
361 }
362 
363 //---------------------------------------------------------------------------------------------------------------------
365 {
366  emit EnableArcItemSelection(enabled);
367 }
368 
369 //---------------------------------------------------------------------------------------------------------------------
371 {
372  emit EnableElArcItemSelection(enabled);
373 }
374 
375 //---------------------------------------------------------------------------------------------------------------------
377 {
378  emit EnableSplineItemSelection(enabled);
379 }
380 
381 //---------------------------------------------------------------------------------------------------------------------
383 {
384  emit EnableSplinePathItemSelection(enabled);
385 }
386 
387 //---------------------------------------------------------------------------------------------------------------------
389 {
390  emit EnableNodeLabelItemSelection(enabled);
391 }
392 
393 //---------------------------------------------------------------------------------------------------------------------
395 {
396  emit EnableNodePointItemSelection(enabled);
397 }
398 
399 //---------------------------------------------------------------------------------------------------------------------
401 {
402  emit EnableDetailItemSelection(enabled);
403 }
404 
405 //---------------------------------------------------------------------------------------------------------------------
407 {
408  emit enableTextItemHover(enabled);
409 }
410 
411 //---------------------------------------------------------------------------------------------------------------------
413 {
414  emit EnablePointItemHover(enabled);
415 }
416 
417 //---------------------------------------------------------------------------------------------------------------------
419 {
420  emit EnableLineItemHover(enabled);
421 }
422 
423 //---------------------------------------------------------------------------------------------------------------------
425 {
426  emit EnableArcItemHover(enabled);
427 }
428 
429 //---------------------------------------------------------------------------------------------------------------------
431 {
432  emit EnableElArcItemHover(enabled);
433 }
434 
435 //---------------------------------------------------------------------------------------------------------------------
437 {
438  emit EnableSplineItemHover(enabled);
439 }
440 
441 //---------------------------------------------------------------------------------------------------------------------
443 {
444  emit EnableSplinePathItemHover(enabled);
445 }
446 
447 //---------------------------------------------------------------------------------------------------------------------
449 {
450  emit EnableNodeLabelItemHover(enabled);
451 }
452 
453 //---------------------------------------------------------------------------------------------------------------------
455 {
456  emit EnableNodePointItemHover(enabled);
457 }
458 
459 //---------------------------------------------------------------------------------------------------------------------
461 {
462  emit EnableDetailItemHover(enabled);
463 }
void EnableSplinePathItemHover(bool enable)
void chosenItem(quint32 id, const SceneObject &type)
chosenItem emit ChosenObject signal.
void SetDisableTools(bool disable, const QString &draftBlockName)
QTransform transform() const
transform return view transformation.
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
void EnableItemMove(bool move)
void EnableArcItemHover(bool enable)
void EnablePointItemSelection(bool enable)
void EnableElArcItemHover(bool enable)
QRectF visibleItemsBoundingRect() const
void EnableLineItemHover(bool enabled)
void setOriginsVisible(bool visible)
void ToggleArcHover(bool enabled)
void enableTextItemSelection(bool enable)
QPointF getScenePos() const
void EnableNodeLabelItemHover(bool enabled)
void curvePiecesMode(bool mode)
void ItemClicked(QGraphicsItem *pItem)
void EnableSplineItemSelection(bool enable)
void mouseMove(const QPointF &scenePos)
mouseMove send new mouse position.
void EnableDetailItemHover(bool enabled)
void ItemSelection(const SelectionType &type)
void ToggleSplinePathSelection(bool enabled)
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
mousePressEvent mouse press events.
void EnablePointItemHover(bool enable)
void TogglePointHover(bool enabled)
void EnableDetailItemSelection(bool enabled)
QVector< QGraphicsItem * > origins
void EnableArcItemSelection(bool enable)
void enableTextItemHover(bool enable)
void EnableToolMove(bool move)
void ToggleElArcHover(bool enabled)
void ToggleElArcSelection(bool enabled)
void togglePieceSelection(bool enabled)
void ToggleNodePointSelection(bool enabled)
void EnableNodeLabelItemSelection(bool enabled)
void ToggleNodeLabelHover(bool enabled)
void EnableElArcItemSelection(bool enable)
void DisableItem(bool disable, const QString &draftBlockName)
void ToggleNodePointHover(bool enabled)
void SelectedObject(bool selected, quint32 object, quint32 tool)
void ToggleSplinePathHover(bool enabled)
void ToggleNodeLabelSelection(bool enabled)
void ChosenObject(quint32 id, SceneObject type)
ChosenObject send option choosed object.
void highlightPiece(quint32 id)
void ToggleSplineHover(bool enabled)
void ToggleLabelSelection(bool enabled)
void ToggleSplineSelection(bool enabled)
void EnableNodePointItemSelection(bool enabled)
void SelectedItem(bool selected, quint32 object, quint32 tool)
void EnableLineItemSelection(bool enable)
void togglePieceHover(bool enabled)
void ToggleArcSelection(bool enabled)
void ToggleLabelHover(bool enabled)
void HighlightItem(quint32 id)
QTransform m_previousTransform
_transform view transform value.
void setCurrentTransform(const QTransform &transform)
setCurrentTransform set view transformation.
void swapTransforms()
swapTransforms.
void EnableSplineItemHover(bool enable)
void EnableSplinePathItemSelection(bool enable)
void enablePiecesMode(bool mode)
void ItemsSelection(const SelectionType &type)
void ToggleLineSelection(bool enabled)
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
mouseMoveEvent handle mouse move events.
VMainGraphicsScene(QObject *parent=nullptr)
VMainGraphicsScene default constructor.
void TogglePointSelection(bool enabled)
void ToggleLineHover(bool enabled)
void EnableNodePointItemHover(bool enabled)
SceneObject
Definition: def.h:103
SelectionType
Definition: def.h:108
const qreal widthHairLine
Definition: global.cpp:61
#define qApp
Definition: vapplication.h:67