1 /************************************************************************
2  **
3  **  @file   visualization.h
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   15 8, 2014
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 #ifndef VISUALIZATION_H
30 #define VISUALIZATION_H
31 
32 #include <qcompilerdetection.h>
33 #include <QGraphicsItem>
34 #include <QObject>
35 #include <QtGlobal>
36 #include <QLoggingCategory>
37 
38 #include "../vmisc/def.h"
39 #include "../vmisc/vabstractvalapplication.h"
40 #include "../vwidgets/vmaingraphicsscene.h"
41 #include "../vwidgets/vcurvepathitem.h"
42 #include "../vwidgets/global.h"
43 #include "../vgeometry/vabstractcurve.h"
44 
45 Q_DECLARE_LOGGING_CATEGORY(vVis)
46 
47 class VScaledEllipse;
48 class VScaledLine;
49 class VContainer;
50 class VInternalVariable;
51 
52 enum class Mode : qint8 {Creation, Show};
53 
54 class Visualization : public QObject
55 {
56     Q_OBJECT
57 public:
58     explicit Visualization(const VContainer *data);
59     virtual ~Visualization() Q_DECL_EQ_DEFAULT;
60 
61     virtual void RefreshGeometry()=0;
62 
63     void         setObject1Id(const quint32 &value);
64     void         setLineStyle(const Qt::PenStyle &value);
65     void         setScenePos(const QPointF &value);
66     virtual void VisualMode(const quint32 &pointId);
67     void         setMainColor(const QColor &value);
68 
69     const VContainer *GetData() const;
70     void              SetData(const VContainer *data);
71 
72     Mode GetMode() const;
73     void SetMode(const Mode &value);
74 
75     static qreal FindLengthFromUser(const QString &expression, const QHash<QString,
76                                     QSharedPointer<VInternalVariable> > *vars, bool fromUser = true);
77     static qreal FindValFromUser(const QString &expression, const QHash<QString,
78                                  QSharedPointer<VInternalVariable> > *vars, bool fromUser = true);
79 
CurrentToolTip()80     QString CurrentToolTip() const {return toolTip;}
81 signals:
82     void         ToolTip(const QString &toolTip);
83 public slots:
84     void         MousePos(const QPointF &scenePos);
85 protected:
86     const VContainer *data;
87     QPointF          scenePos;
88     QColor           mainColor;
89     QColor           supportColor;
90     Qt::PenStyle     lineStyle;
91     quint32          object1Id;
92     QString          toolTip;
93     Mode             mode;
94 
95     virtual void InitPen()=0;
96     virtual void AddOnScene()=0;
97 
98     VScaledEllipse *InitPoint(const QColor &color, QGraphicsItem *parent, qreal z = 0) const;
99     void         DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color,
100                            Qt::PenStyle style = Qt::SolidLine);
101     virtual void DrawLine(VScaledLine *lineItem, const QLineF &line, const QColor &color,
102                           Qt::PenStyle style = Qt::SolidLine);
103     void         DrawPath(VCurvePathItem *pathItem, const QPainterPath &path, const QColor &color,
104                           Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap);
105     void         DrawPath(VCurvePathItem *pathItem, const QPainterPath &path,
106                           const QVector<DirectionArrow> &directionArrows, const QColor &color,
107                           Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap);
108 
109     template <typename Item>
110     void         AddItem(Item *item);
111 
112     template <class Item>
113     Item         *InitItem(const QColor &color, QGraphicsItem *parent);
114 
115     static VScaledEllipse *GetPointItem(QVector<VScaledEllipse *> &points, quint32 i, const QColor &color,
116                                         QGraphicsItem *parent);
117     static VCurvePathItem *GetCurveItem(QVector<VCurvePathItem *> &curves, quint32 i, const QColor &color,
118                                         QGraphicsItem *parent);
119 private:
120     Q_DISABLE_COPY(Visualization)
121 };
122 
123 //---------------------------------------------------------------------------------------------------------------------
124 template <typename Item>
AddItem(Item * item)125 inline void Visualization::AddItem(Item *item)
126 {
127     SCASSERT(item != nullptr)
128     VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(VAbstractValApplication::VApp()->getCurrentScene());
129     SCASSERT(scene != nullptr)
130 
131     scene->addItem(item);
132     connect(scene, &VMainGraphicsScene::mouseMove, item, &Visualization::MousePos);
133 }
134 
135 //---------------------------------------------------------------------------------------------------------------------
136 template <class Item>
InitItem(const QColor & color,QGraphicsItem * parent)137 inline Item *Visualization::InitItem(const QColor &color, QGraphicsItem *parent)
138 {
139     Item *item = new Item(parent);
140 
141     QPen visPen = item->pen();
142     visPen.setColor(color);
143 
144     item->setPen(visPen);
145     item->setZValue(1);
146     item->setFlags(QGraphicsItem::ItemStacksBehindParent);
147     item->setVisible(false);
148     return item;
149 }
150 
151 #endif // VISUALIZATION_H
152