QElectroTech  0.70
qetgraphicshandlerutility.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <QPainterPath>
20 
21 
36 QVector<QPointF> QetGraphicsHandlerUtility::pointsForRect(const QRectF &rect)
37 {
38  QVector<QPointF> vector;
39  QPointF point;
40  vector << rect.topLeft();//*****Top left
41  point = rect.center();
42  point.setY(rect.top());
43  vector << point;//**************Middle top
44  vector << rect.topRight();//****Top right
45  point = rect.center();
46  point.setX(rect.left());
47  vector << point;//**************Middle left
48  point.setX(rect.right());
49  vector << point;//**************Middle right
50  vector << rect.bottomLeft();//**Bottom left
51  point = rect.center();
52  point.setY(rect.bottom());
53  vector << point;//*************Middle bottom
54  vector << rect.bottomRight();//*Bottom right
55  return vector;
56 }
57 
65 QVector<QPointF> QetGraphicsHandlerUtility::pointsForLine(const QLineF &line) {
66  return (QVector<QPointF> {line.p1(), line.p2()});
67 }
68 
78 QVector<QPointF> QetGraphicsHandlerUtility::pointsForArc(const QRectF &rect, qreal start_angle, qreal span_angle)
79 {
80  QVector<QPointF> vector;
81  QPainterPath path;
82  path.arcTo(rect, start_angle, 0);
83  vector.append(path.currentPosition());
84  path.arcTo(rect, start_angle, span_angle);
85  vector.append(path.currentPosition());
86  return vector;
87 
88 }
89 
99 QRectF QetGraphicsHandlerUtility::rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
100 {
101  if (index < 0 || index > 7) return old_rect;
102 
103  QRectF rect = old_rect;
104  if (index == 0) rect.setTopLeft(pos);
105  else if (index == 1) rect.setTop(pos.y());
106  else if (index == 2) rect.setTopRight(pos);
107  else if (index == 3) rect.setLeft(pos.x());
108  else if (index == 4) rect.setRight(pos.x());
109  else if (index == 5) rect.setBottomLeft(pos);
110  else if (index == 6) rect.setBottom(pos.y());
111  else if (index == 7) rect.setBottomRight(pos);
112 
113  return rect;
114 }
115 
126 QRectF QetGraphicsHandlerUtility::mirrorRectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
127 {
128  if (index < 0 || index > 7) return old_rect;
129 
130  QRectF rect = old_rect;
131  QPointF center = rect.center();
132 
133  if (index == 0) {
134  qreal x = pos.x() + (pos.x() - rect.topLeft().x());
135  qreal y = pos.y() + (pos.y() - rect.topLeft().y());
136  rect.setTopLeft(QPointF(x,y));
137  }
138  else if (index == 1) {
139  qreal y = pos.y() + (pos.y() - rect.topLeft().y());
140  rect.setTop(y);
141  }
142  else if (index == 2) {
143  qreal x = pos.x() + (pos.x() - rect.topRight().x());
144  qreal y = pos.y() + (pos.y() - rect.topLeft().y());
145  rect.setTopRight(QPointF(x,y));
146  }
147  else if (index == 3) {
148  qreal x = pos.x() + (pos.x() - rect.left());
149  rect.setLeft(x);
150  }
151  else if (index == 4) {
152  qreal x = pos.x() + (pos.x() - rect.right());
153  rect.setRight(x);
154  }
155  else if (index == 5) {
156  qreal x = pos.x() + (pos.x() - rect.bottomLeft().x());
157  qreal y = pos.y() + (pos.y() - rect.bottomLeft().y());
158  rect.setBottomLeft(QPointF(x,y));
159  }
160  else if (index == 6) {
161  qreal y = pos.y() + (pos.y() - rect.bottom());
162  rect.setBottom(y);
163  }
164  else if (index == 7) {
165  qreal x = pos.x() + (pos.x() - rect.bottomRight().x());
166  qreal y = pos.y() + (pos.y() - rect.bottomRight().y());
167  rect.setBottomRight(QPointF(x,y));
168  }
169 
170  rect.moveCenter(center);
171  return rect;
172 }
173 
182 QLineF QetGraphicsHandlerUtility::lineForPosAtIndex(const QLineF &old_line, const QPointF &pos, int index) {
183  QLineF line = old_line;
184  index == 0 ? line.setP1(pos) : line.setP2(pos);
185  return line;
186 }
187 
195 QPolygonF QetGraphicsHandlerUtility::polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos)
196 {
197  qreal max_angle = 0;
198  int index = 0;
199 
200  for (int i=1 ; i<old_polygon.size() ; i++)
201  {
202  QPointF A = old_polygon.at(i-1);
203  QPointF B = old_polygon.at(i);
204  QLineF line_a(A, pos);
205  QLineF line_b(pos, B);
206  qreal angle = line_a.angleTo(line_b);
207  if(angle<180)
208  angle = 360-angle;
209 
210  if (i==1)
211  {
212  max_angle = angle;
213  index=i;
214  }
215  if (angle > max_angle)
216  {
217  max_angle = angle;
218  index=i;
219  }
220  }
221  //Special case when polygon is close
222  if (closed)
223  {
224  QLineF line_a(old_polygon.last(), pos);
225  QLineF line_b(pos, old_polygon.first());
226 
227  qreal angle = line_a.angleTo(line_b);
228  if (angle<180)
229  angle = 360-angle;
230 
231  if (angle > max_angle)
232  {
233  max_angle = angle;
234  index=old_polygon.size();
235  }
236  }
237 
238  QPolygonF polygon = old_polygon;
239  polygon.insert(index, pos);
240  return polygon;
241 }
242 
253 QVector<QPointF> QetGraphicsHandlerUtility::pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
254 {
255  QVector<QPointF> v;
256 
257  if(mode == Qt::AbsoluteSize)
258  {
259  QPointF X = rect.topRight();
260  X.rx() -= xRadius;
261  v << X;
262 
263  QPointF Y = rect.topRight();
264  Y.ry() += yRadius;
265  v << Y;
266  }
267  else
268  {
269  qreal half_width = rect.width()/2;
270  qreal x_percent = std::min(xRadius, 100.00)/100;
271  QPointF X(rect.right() - half_width*x_percent,
272  rect.top());
273  v << X;
274 
275  qreal half_height = rect.height()/2;
276  qreal y_percent = std::min(yRadius, 100.00)/100;
277  QPointF Y(rect.right(),
278  rect.top()+ half_height*y_percent);
279  v << Y;
280  }
281 
282  return v;
283 }
284 
293 qreal QetGraphicsHandlerUtility::radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode)
294 {
295  if (mode == Qt::AbsoluteSize)
296  {
297  if (index == 0)
298  {
299  QPointF tr = rect.topRight();
300  qreal x = tr.x() - pos.x();
301  if (x < 0) {
302  x = 0;
303  }
304  else if (x > rect.width()/2) {
305  x = rect.width()/2;
306  }
307 
308  return x;
309  }
310  else if (index == 1)
311  {
312  QPointF tr = rect.topRight();
313  qreal y = pos.y() - tr.y();
314  if (y < 0) {
315  y = 0;
316  }
317  else if (y > rect.height()/2) {
318  y = rect.height()/2;
319  }
320 
321  return y;
322  }
323  else {
324  return 0;
325  }
326  }
327  else
328  {
329  if(index == 0) //X
330  {
331  if (pos.x() < rect.center().x()) {
332  return 100;
333  }
334  else if (pos.x() > rect.right()) {
335  return 0;
336  }
337  else {
338  return (100 - percentageInRange(rect.center().x(), rect.right(), pos.x()));
339  }
340  }
341  else if (index == 1) //Y
342  {
343  if (pos.y() < rect.top()) {
344  return 0;
345  }
346  else if (pos.y() > rect.center().y()) {
347  return 100;
348  }
349  else {
350  return percentageInRange(rect.top(), rect.center().y(), pos.y());
351  }
352  }
353  else {
354  return 0;
355  }
356  }
357 }
358 
359 qreal QetGraphicsHandlerUtility::percentageInRange(qreal min, qreal max, qreal value) {
360  return ((value - min) * 100) / (max - min);
361 }
362 
static QVector< QPointF > pointsForArc(const QRectF &rect, qreal start_angle, qreal span_angle)
QetGraphicsHandlerUtility::pointsForArc Return the points for the given arc. The first value in the v...
static QVector< QPointF > pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
QetGraphicsHandlerUtility::pointForRadiusRect.
static qreal radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode=Qt::AbsoluteSize)
QetGraphicsHandlerUtility::radiusForPosAtIndex.
static QLineF lineForPosAtIndex(const QLineF &old_line, const QPointF &pos, int index)
QetGraphicsHandlerUtility::lineForPosAtIndex Return a line after modification of at index of ...
static QPolygonF polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos)
QetGraphicsHandlerUtility::polygonForInsertPoint.
static QRectF mirrorRectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
QetGraphicsHandlerUtility::mirrorRectForPosAtIndex Return a rectangle after modification of the point...
static QRectF rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
QetGraphicsHandlerUtility::rectForPosAtIndex Return a rectangle after modification of the point &#39;&#39; at...
QIcon tr
Definition: qeticons.cpp:204
static QVector< QPointF > pointsForRect(const QRectF &rect)
QetGraphicsHandlerUtility::pointsForRect Return the keys points of the rectangle, stored in a vector...
static QVector< QPointF > pointsForLine(const QLineF &line)
QetGraphicsHandlerUtility::pointsForLine The point that define a line in a QVector. there is two points.
static qreal percentageInRange(qreal min, qreal max, qreal value)