Seamly2D
Code documentation
visualization.cpp
Go to the documentation of this file.
1 /**************************************************************************
2  **
3  ** @file visualization.cpp
4  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
5  ** @date 15 8, 2014
6  **
7  ** @author Douglas S Caskey
8  ** @date 7.23.2022
9  **
10  ** @brief
11  ** @copyright
12  ** Copyright (C) 2013-2022 Seamly2D project.
13  ** This source code is part of the Seamly2D project, a pattern making
14  ** program, whose allow create and modeling patterns of clothing.
15  **
16  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
17  **
18  ** Seamly2D is free software: you can redistribute it and/or modify
19  ** it under the terms of the GNU General Public License as published
20  ** by the Free Software Foundation, either version 3 of the License,
21  ** or (at your option) any later version.
22  **
23  ** Seamly2D is distributed in the hope that it will be useful,
24  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
25  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  ** GNU General Public License for more details.
27  **
28  ** You should have received a copy of the GNU General Public License
29  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
30  **
31  *************************************************************************/
32 
33 #include "visualization.h"
34 
35 #include "../ifc/ifcdef.h"
36 #include "../qmuparser/qmuparsererror.h"
37 #include "../tools/drawTools/vdrawtool.h"
38 #include "../vmisc/vcommonsettings.h"
39 #include "../vpatterndb/calculator.h"
40 #include "../vpatterndb/vcontainer.h"
41 #include "../vpatterndb/vtranslatevars.h"
42 #include "../vwidgets/scalesceneitems.h"
43 #include "../vwidgets/vcurvepathitem.h"
44 #include "../vwidgets/vmaingraphicsscene.h"
45 
46 #include <qnumeric.h>
47 #include <QBrush>
48 #include <QColor>
49 #include <QGraphicsEllipseItem>
50 #include <QGraphicsItem>
51 #include <QGraphicsLineItem>
52 #include <QLineF>
53 #include <QMessageLogger>
54 #include <QPen>
55 #include <QPointF>
56 #include <QRectF>
57 #include <QScopedPointer>
58 #include <QString>
59 #include <Qt>
60 #include <QtDebug>
61 
62 template <class K, class V> class QHash;
63 
64 Q_LOGGING_CATEGORY(vVis, "v.visualization")
65 
66 //---------------------------------------------------------------------------------------------------------------------
68  : QObject()
69  , data(data)
70  , scenePos(QPointF())
71  , mainColor(Qt::red)
72  , supportColor(QColor(qApp->Settings()->getPrimarySupportColor()))
73  , lineStyle(Qt::SolidLine)
74  , lineWeight(0.35)
75  , object1Id(NULL_ID)
76  , toolTip(QString())
77  , mode(Mode::Creation)
78 {}
79 
80 //---------------------------------------------------------------------------------------------------------------------
81 void Visualization::setObject1Id(const quint32 &value)
82 {
83  object1Id = value;
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------------
87 void Visualization::setLineStyle(const Qt::PenStyle &value)
88 {
89  lineStyle = value;
90  initPen();
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
94 void Visualization::setLineWeight(const QString &value)
95 {
96  lineWeight = ToPixel(value.toDouble(), Unit::Mm);
97  initPen();
98 }
99 
100 //---------------------------------------------------------------------------------------------------------------------
101 // cppcheck-suppress unusedFunction
102 void Visualization::setScenePos(const QPointF &value)
103 {
104  scenePos = value;
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
108 void Visualization::VisualMode(const quint32 &pointId)
109 {
110  VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
111  SCASSERT(scene != nullptr)
112 
113  this->object1Id = pointId;
114  this->scenePos = scene->getScenePos();
115  RefreshGeometry();
116 
117  AddOnScene();
118 }
119 
120 //---------------------------------------------------------------------------------------------------------------------
121 // cppcheck-suppress unusedFunction
122 void Visualization::setMainColor(const QColor &value)
123 {
124  mainColor = value;
125  initPen();
126 }
127 
128 //---------------------------------------------------------------------------------------------------------------------
130 {
131  return data;
132 }
133 
134 //---------------------------------------------------------------------------------------------------------------------
136 {
137  this->data = data;
138 }
139 
140 //---------------------------------------------------------------------------------------------------------------------
141 void Visualization::mousePos(const QPointF &scenePos)
142 {
143  this->scenePos = scenePos;
144  RefreshGeometry();
145  if (toolTip.isEmpty() == false)
146  {
147  emit ToolTip(toolTip);
148  }
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
152 VScaledEllipse *Visualization::InitPoint(const QColor &color, QGraphicsItem *parent, qreal z) const
153 {
154  return initPointItem(color, parent, z);
155 }
156 
157 //---------------------------------------------------------------------------------------------------------------------
158 qreal Visualization::FindLength(const QString &expression,
159  const QHash<QString, QSharedPointer<VInternalVariable> > *vars)
160 {
161  return qApp->toPixel(FindVal(expression, vars));
162 }
163 
164 //---------------------------------------------------------------------------------------------------------------------
165 qreal Visualization::FindVal(const QString &expression,
166  const QHash<QString, QSharedPointer<VInternalVariable> > *vars)
167 {
168  qreal val = 0;
169  if (expression.isEmpty())
170  {
171  val = 0;
172  }
173  else
174  {
175  try
176  {
177  // Replace line return with spaces for calc if exist
178  QString formula = expression;
179  formula.replace("\n", " ");
180  formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
181  QScopedPointer<Calculator> cal(new Calculator());
182  val = cal->EvalFormula(vars, formula);
183 
184  if (qIsInf(val) || qIsNaN(val))
185  {
186  val = 0;
187  }
188  }
189  catch (qmu::QmuParserError &e)
190  {
191  val = 0;
192  qDebug() << "\nMath parser error:\n"
193  << "--------------------------------------\n"
194  << "Message: " << e.GetMsg() << "\n"
195  << "Expression: " << e.GetExpr() << "\n"
196  << "--------------------------------------";
197  }
198  }
199  return val;
200 }
201 
202 //---------------------------------------------------------------------------------------------------------------------
203 void Visualization::DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color, Qt::PenStyle style)
204 {
205  SCASSERT (point != nullptr)
206 
207  point->setPos(pos);
208 
209  QPen visPen = point->pen();
210  visPen.setColor(color);
211  visPen.setStyle(style);
212 
213  point->setPen(visPen);
214  point->setVisible(true);
215 }
216 
217 //---------------------------------------------------------------------------------------------------------------------
218 void Visualization::DrawLine(VScaledLine *lineItem, const QLineF &line, const QColor &color,
219  const qreal &lineWeight, Qt::PenStyle style)
220 {
221  SCASSERT (lineItem != nullptr)
222 
223  QPen visPen = lineItem->pen();
224  visPen.setColor(color);
225  visPen.setStyle(style);
226  visPen.setWidthF(lineWeight);
227 
228  lineItem->setPen(visPen);
229  lineItem->setLine(line);
230  lineItem->setVisible(true);
231 }
232 
233 //---------------------------------------------------------------------------------------------------------------------
234 void Visualization::DrawPath(VCurvePathItem *pathItem, const QPainterPath &path, const QColor &color,
235  Qt::PenStyle style, const qreal &lineWeight, Qt::PenCapStyle cap)
236 {
237  DrawPath(pathItem, path, QVector<DirectionArrow>(), color, style, lineWeight, cap);
238 }
239 
240 //---------------------------------------------------------------------------------------------------------------------
241 void Visualization::DrawPath(VCurvePathItem *pathItem, const QPainterPath &path,
242  const QVector<DirectionArrow> &directionArrows, const QColor &color, Qt::PenStyle style,
243  const qreal &lineWeight, Qt::PenCapStyle cap)
244 {
245  SCASSERT (pathItem != nullptr)
246 
247  QPen visPen = pathItem->pen();
248  visPen.setColor(color);
249  visPen.setStyle(style);
250  visPen.setWidthF(lineWeight);
251  visPen.setCapStyle(cap);
252 
253  pathItem->setPen(visPen);
254  pathItem->setPath(path);
255  pathItem->SetDirectionArrows(directionArrows);
256  pathItem->setVisible(true);
257 }
258 
259 
260 //---------------------------------------------------------------------------------------------------------------------
261 void Visualization::drawArrowedLine(ArrowedLineItem *item, const QLineF &line, const QColor &color,
262  Qt::PenStyle style)
263 {
264  SCASSERT (item != nullptr)
265 
266  QPen visPen = item->pen();
267  visPen.setColor(color);
268  visPen.setStyle(style);
269 
270  item->setPen(visPen);
271  item->setLine(line);
272  item->setVisible(true);
273 /*
274  QPainterPath path;
275  path.moveTo(line.p1());
276  path.lineTo(line.p2());
277 
278  qreal arrow_step = 60;
279  qreal arrow_size = 10;
280 
281  if (line.length() < arrow_step)
282  {
283  drawArrow(line, path, arrow_size);
284  }
285 
286  QLineF axis;
287  axis.setP1(line.p1());
288  axis.setAngle(line.angle());
289  axis.setLength(arrow_step);
290 
291  int steps = qFloor(line.length()/arrow_step);
292  for (int i=0; i<steps; ++i)
293  {
294  drawArrow(axis, path, arrow_size);
295  axis.setLength(axis.length()+arrow_step);
296  }
297  item->setPath(path);
298  item->setVisible(true);
299 
300 */
301 }
302 
303 //---------------------------------------------------------------------------------------------------------------------
304 void Visualization::drawArrow(const QLineF &axis, QPainterPath &path, const qreal &arrow_size)
305 {
306  QLineF arrowPart1;
307  arrowPart1.setP1(axis.p2());
308  arrowPart1.setLength(arrow_size);
309  arrowPart1.setAngle(axis.angle()+180+35);
310 
311  path.moveTo(arrowPart1.p1());
312  path.lineTo(arrowPart1.p2());
313 
314  QLineF arrowPart2;
315  arrowPart2.setP1(axis.p2());
316  arrowPart2.setLength(arrow_size);
317  arrowPart2.setAngle(axis.angle()+180-35);
318 
319  path.moveTo(arrowPart2.p1());
320  path.lineTo(arrowPart2.p2());
321 }
322 
323 //---------------------------------------------------------------------------------------------------------------------
325  const QColor &color, QGraphicsItem *parent)
326 {
327  if (not points.isEmpty() && static_cast<quint32>(points.size() - 1) >= i)
328  {
329  return points.at(static_cast<int>(i));
330  }
331  else
332  {
333  auto point = initPointItem(color, parent);
334  points.append(point);
335  return point;
336  }
337  return nullptr;
338 }
339 
340 //---------------------------------------------------------------------------------------------------------------------
341 VScaledEllipse *Visualization::initPointItem(const QColor &color, QGraphicsItem *parent, qreal z)
342 {
343  VScaledEllipse *point = new VScaledEllipse(parent);
344  point->setZValue(1);
345  point->setBrush(QBrush(Qt::NoBrush));
346 
347  QPen visPen = point->pen();
348  visPen.setColor(color);
349 
350  point->setPen(visPen);
351  point->setRect(PointRect(defPointRadiusPixel));
352  point->setPos(QPointF());
353  point->setFlags(QGraphicsItem::ItemStacksBehindParent);
354  point->setZValue(z);
355  point->setVisible(false);
356  return point;
357 }
358 
359 //---------------------------------------------------------------------------------------------------------------------
361 {
362  return mode;
363 }
364 
365 //---------------------------------------------------------------------------------------------------------------------
366 void Visualization::SetMode(const Mode &value)
367 {
368  mode = value;
369 }
The Calculator class for calculation formula.
Definition: calculator.h:84
The VContainer class container of all variables.
Definition: vcontainer.h:141
void SetDirectionArrows(const QVector< QPair< QLineF, QLineF >> &arrows)
The VMainGraphicsScene class main scene.
QPointF getScenePos() const
void mousePos(const QPointF &scenePos)
void ToolTip(const QString &toolTip)
QColor mainColor
Definition: visualization.h:99
void setScenePos(const QPointF &value)
void DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color, Qt::PenStyle style=Qt::SolidLine)
void setLineWeight(const QString &value)
static qreal FindLength(const QString &expression, const QHash< QString, QSharedPointer< VInternalVariable > > *vars)
void drawArrow(const QLineF &axis, QPainterPath &path, const qreal &arrow_size)
void SetMode(const Mode &value)
const VContainer * data
Definition: visualization.h:97
virtual void VisualMode(const quint32 &pointId)
static VScaledEllipse * initPointItem(const QColor &color, QGraphicsItem *parent, qreal z=0)
void setData(const VContainer *data)
void DrawPath(VCurvePathItem *pathItem, const QPainterPath &path, const QColor &color, Qt::PenStyle style=Qt::SolidLine, const qreal &weight=0.35, Qt::PenCapStyle cap=Qt::SquareCap)
void setObject1Id(const quint32 &value)
static VScaledEllipse * GetPointItem(QVector< VScaledEllipse * > &points, quint32 i, const QColor &color, QGraphicsItem *parent)
const VContainer * getData() const
virtual void initPen()=0
QPointF scenePos
Definition: visualization.h:98
void drawArrowedLine(ArrowedLineItem *item, const QLineF &line, const QColor &color, Qt::PenStyle style=Qt::SolidLine)
virtual void RefreshGeometry()=0
virtual void DrawLine(VScaledLine *lineItem, const QLineF &line, const QColor &color, const qreal &lineWeight, Qt::PenStyle style=Qt::SolidLine)
VScaledEllipse * InitPoint(const QColor &color, QGraphicsItem *parent, qreal z=0) const
virtual void AddOnScene()=0
Mode GetMode() const
quint32 object1Id
void setLineStyle(const Qt::PenStyle &value)
void setMainColor(const QColor &value)
static qreal FindVal(const QString &expression, const QHash< QString, QSharedPointer< VInternalVariable > > *vars)
Qt::PenStyle lineStyle
Error class of the parser.
const QString & GetMsg() const
Returns the message string for this error.
const QString & GetExpr() const
gets the expression related tp this error.
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231
#define SCASSERT(cond)
Definition: def.h:317
const qreal defPointRadiusPixel
Definition: global.cpp:59
QRectF PointRect(qreal radius)
Definition: global.cpp:95
#define NULL_ID
Definition: ifcdef.h:76
#define qApp
Definition: vapplication.h:67
Mode
Definition: visualization.h:59
@ Creation