Seamly2D
Code documentation
global.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
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 14 6, 2017
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) 2017 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 "global.h"
53 #include "../vmisc/def.h"
54 
55 #include <QGraphicsItem>
56 #include <QGraphicsScene>
57 #include <QGraphicsView>
58 
59 const qreal defPointRadiusPixel = (2./*mm*/ / 25.4) * PrintDPI;
60 const qreal widthMainLine = (1.2/*mm*/ / 25.4) * PrintDPI;
61 const qreal widthHairLine = widthMainLine/3.0;
62 
63 qreal sceneScale(QGraphicsScene *scene)
64 {
65  qreal scale = 1;
66 
67  if (scene)
68  {
69  const QList<QGraphicsView *> views = scene->views();
70  if (not views.isEmpty())
71  {
72  scale = views.first()->transform().m11();
73  }
74  }
75 
76  return scale;
77 }
78 
79 //---------------------------------------------------------------------------------------------------------------------
80 QColor correctColor(const QGraphicsItem *item, const QColor &color)
81 {
82  SCASSERT(item != nullptr)
83 
84  if (item->isEnabled())
85  {
86  return color;
87  }
88  else
89  {
90  return Qt::gray;
91  }
92 }
93 
94 //---------------------------------------------------------------------------------------------------------------------
95 QRectF PointRect(qreal radius)
96 {
97  QRectF rec = QRectF(0, 0, radius*2, radius*2);
98  rec.translate(-rec.center().x(), -rec.center().y());
99  return rec;
100 }
101 
102 //---------------------------------------------------------------------------------------------------------------------
103 qreal scaledRadius(qreal scale)
104 {
106  if (scale > 1)
107  {
109  }
110  return scaledRadius;
111 }
112 
113 //---------------------------------------------------------------------------------------------------------------------
114 void scaleCircleSize(QGraphicsEllipseItem *item, qreal scale)
115 {
116  SCASSERT(item != nullptr)
117 
118  item->setRect(PointRect(scaledRadius(scale)));
119 }
120 
121 //---------------------------------------------------------------------------------------------------------------------
122 void scaleRectSize(QGraphicsRectItem *item, qreal scale)
123 {
124  SCASSERT(item != nullptr)
125 
126  item->setRect(PointRect(scaledRadius(scale)));
127 }
128 
129 //---------------------------------------------------------------------------------------------------------------------
130 qreal scaleWidth(qreal width, qreal scale)
131 {
132  if (scale > 1)
133  {
134  width = qMax(0.01, width/scale);
135  }
136  return width;
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
140 QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen)
141 {
142  // We unfortunately need this hack as QPainterPathStroker will set a width of 1.0
143  // if we pass a value of 0.0 to QPainterPathStroker::setWidth()
144  const qreal penWidthZero = qreal(0.00000001);
145 
146  if (path == QPainterPath() || pen == Qt::NoPen)
147  {
148  return path;
149  }
150  QPainterPathStroker ps;
151  ps.setCapStyle(pen.capStyle());
152  if (pen.widthF() <= 0.0)
153  {
154  ps.setWidth(penWidthZero);
155  }
156  else
157  {
158  ps.setWidth(pen.widthF());
159  }
160  ps.setJoinStyle(pen.joinStyle());
161  ps.setMiterLimit(pen.miterLimit());
162  QPainterPath p = ps.createStroke(path);
163  p.addPath(path);
164  return p;
165 }
const qreal PrintDPI
Definition: def.cpp:228
#define SCASSERT(cond)
Definition: def.h:317
const qreal widthMainLine
Definition: global.cpp:60
qreal sceneScale(QGraphicsScene *scene)
Definition: global.cpp:63
const qreal defPointRadiusPixel
Definition: global.cpp:59
QRectF PointRect(qreal radius)
Definition: global.cpp:95
void scaleCircleSize(QGraphicsEllipseItem *item, qreal scale)
Definition: global.cpp:114
QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen)
Definition: global.cpp:140
qreal scaledRadius(qreal scale)
Definition: global.cpp:103
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130
QColor correctColor(const QGraphicsItem *item, const QColor &color)
Definition: global.cpp:80
void scaleRectSize(QGraphicsRectItem *item, qreal scale)
Definition: global.cpp:122
const qreal widthHairLine
Definition: global.cpp:61