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