1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8                           pageitem.cpp  -  description
9                              -------------------
10     begin                : Sat Apr 7 2001
11     copyright            : (C) 2001 by Franz Schmid
12     email                : Franz.Schmid@altmuehlnet.de
13  ***************************************************************************/
14 
15 /***************************************************************************
16  *                                                                         *
17  *   This program is free software; you can redistribute it and/or modify  *
18  *   it under the terms of the GNU General Public License as published by  *
19  *   the Free Software Foundation; either version 2 of the License, or     *
20  *   (at your option) any later version.                                   *
21  *                                                                         *
22  ***************************************************************************/
23 
24 #if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
25 #define _USE_MATH_DEFINES
26 #endif
27 #include <cmath>
28 #include <cassert>
29 
30 #include "scconfig.h"
31 
32 #include "commonstrings.h"
33 #include "pageitem.h"
34 #include "pageitem_line.h"
35 #include "prefsmanager.h"
36 #include "scpage.h"
37 #include "scpainter.h"
38 #include "scpaths.h"
39 
40 #include "scribusstructs.h"
41 #include "scribusdoc.h"
42 #include "scribusview.h"
43 #include "undomanager.h"
44 #include "undostate.h"
45 #include "util.h"
46 #include "util_math.h"
47 
48 using namespace std;
49 
PageItem_Line(ScribusDoc * pa,double x,double y,double w,double h,double w2,const QString & fill,const QString & outline)50 PageItem_Line::PageItem_Line(ScribusDoc *pa, double x, double y, double w, double h, double w2, const QString& fill, const QString& outline)
51 	: PageItem(pa, PageItem::Line, x, y, w, h, w2, fill, outline)
52 {
53 }
54 
DrawObj_Item(ScPainter * p,QRectF)55 void PageItem_Line::DrawObj_Item(ScPainter *p, QRectF /*e*/)
56 {
57 	if (m_Doc->RePos)
58 		return;
59 
60 	if (m_Doc->layerOutline(m_layerID))
61 		p->drawLine(FPoint(0, 0), FPoint(m_width, 0));
62 	else
63 	{
64 		if (NamedLStyle.isEmpty())
65 		{
66 			ScPattern *strokePattern = m_Doc->checkedPattern(patternStrokeVal);
67 			if (strokePattern)
68 			{
69 				if (patternStrokePath)
70 				{
71 					QPainterPath guidePath;
72 					guidePath.moveTo(0, 0);
73 					guidePath.lineTo(m_width, 0);
74 					DrawStrokePattern(p, guidePath);
75 				}
76 				else
77 				{
78 					p->setPattern(strokePattern, patternStrokeScaleX, patternStrokeScaleY, patternStrokeOffsetX, patternStrokeOffsetY, patternStrokeRotation, patternStrokeSkewX, patternStrokeSkewY, patternStrokeMirrorX, patternStrokeMirrorY);
79 					p->setStrokeMode(ScPainter::Pattern);
80 					p->drawLine(FPoint(0, 0), FPoint(m_width, 0));
81 				}
82 			}
83 			else if (GrTypeStroke > 0)
84 			{
85 				if ((!gradientStrokeVal.isEmpty()) && (!m_Doc->docGradients.contains(gradientStrokeVal)))
86 					gradientStrokeVal.clear();
87 				if (!(gradientStrokeVal.isEmpty()) && (m_Doc->docGradients.contains(gradientStrokeVal)))
88 					stroke_gradient = m_Doc->docGradients[gradientStrokeVal];
89 				if (stroke_gradient.stops() < 2) // fall back to solid stroking if there are not enough colorstops in the gradient.
90 				{
91 					if (lineColor() != CommonStrings::None)
92 					{
93 						p->setBrush(m_strokeQColor);
94 						p->setStrokeMode(ScPainter::Solid);
95 					}
96 					else
97 					{
98 						no_stroke = true;
99 						p->setStrokeMode(ScPainter::None);
100 					}
101 				}
102 				else
103 				{
104 					p->setStrokeMode(ScPainter::Gradient);
105 					p->stroke_gradient = stroke_gradient;
106 					if (GrTypeStroke == Gradient_Linear)
107 						p->setGradient(VGradient::linear, FPoint(GrStrokeStartX, GrStrokeStartY), FPoint(GrStrokeEndX, GrStrokeEndY), FPoint(GrStrokeStartX, GrStrokeStartY), GrStrokeScale, GrStrokeSkew);
108 					else
109 						p->setGradient(VGradient::radial, FPoint(GrStrokeStartX, GrStrokeStartY), FPoint(GrStrokeEndX, GrStrokeEndY), FPoint(GrStrokeFocalX, GrStrokeFocalY), GrStrokeScale, GrStrokeSkew);
110 				}
111 				p->drawLine(FPoint(0, 0), FPoint(m_width, 0));
112 			}
113 			else if (lineColor() != CommonStrings::None)
114 			{
115 				p->setStrokeMode(ScPainter::Solid);
116 				p->drawLine(FPoint(0, 0), FPoint(m_width, 0));
117 			}
118 			else
119 				no_stroke = true;
120 		}
121 		else
122 		{
123 			p->setStrokeMode(ScPainter::Solid);
124 			multiLine ml = m_Doc->docLineStyles[NamedLStyle];
125 			QColor tmp;
126 			for (int it = ml.size()-1; it > -1; it--)
127 			{
128 				if (ml[it].Color != CommonStrings::None) // && (ml[it].Width != 0))
129 				{
130 					SetQColor(&tmp, ml[it].Color, ml[it].Shade);
131 					p->setPen(tmp, ml[it].Width, static_cast<Qt::PenStyle>(ml[it].Dash), static_cast<Qt::PenCapStyle>(ml[it].LineEnd), static_cast<Qt::PenJoinStyle>(ml[it].LineJoin));
132 					p->drawLine(FPoint(0, 0), FPoint(m_width, 0));
133 				}
134 			}
135 		}
136 	}
137 	if (m_startArrowIndex != 0)
138 	{
139 		QTransform arrowTrans;
140 		arrowTrans.translate(0, 0);
141 		arrowTrans.scale(-1,1);
142 		arrowTrans.scale(m_startArrowScale / 100.0, m_startArrowScale / 100.0);
143 		drawArrow(p, arrowTrans, m_startArrowIndex);
144 	}
145 	if (m_endArrowIndex != 0)
146 	{
147 		QTransform arrowTrans;
148 		arrowTrans.translate(m_width, 0);
149 		arrowTrans.scale(m_endArrowScale / 100.0, m_endArrowScale / 100.0);
150 		drawArrow(p, arrowTrans, m_endArrowIndex);
151 	}
152 }
153 
applicableActions(QStringList & actionList)154 void PageItem_Line::applicableActions(QStringList & actionList)
155 {
156 	actionList << "itemConvertToBezierCurve";
157 }
158 
infoDescription() const159 QString PageItem_Line::infoDescription() const
160 {
161 	return QString();
162 }
163 
startPoint() const164 QPointF PageItem_Line::startPoint() const
165 {
166 	return QPointF(m_xPos, m_yPos);
167 }
168 
endPoint() const169 QPointF PageItem_Line::endPoint() const
170 {
171 	double rot = this->rotation();
172 	double x = m_xPos + m_width * cos(rot * M_PI / 180.0);
173 	double y = m_yPos + m_width * sin(rot * M_PI / 180.0);
174 	return QPointF(x, y);
175 }
176 
setLineClip()177 void PageItem_Line::setLineClip()
178 {
179 	// Big fat warning : QPolygon::setPoints is a variadic function,
180 	// only pass ints as arguments, or expect strange results
181 	int ph = static_cast<int>(qMax(1.0, this->visualLineWidth() / 2.0));
182 	Clip.setPoints(4, -1, -ph,
183 	                  static_cast<int>(width() + 1.0), -ph,
184 	                  static_cast<int>(width() + 1.0), ph,
185 	                  -1, ph);
186 }
187 
getBoundingRect(double * x1,double * y1,double * x2,double * y2) const188 void PageItem_Line::getBoundingRect(double *x1, double *y1, double *x2, double *y2) const
189 {
190 	double minx =  std::numeric_limits<double>::max();
191 	double miny =  std::numeric_limits<double>::max();
192 	double maxx = -std::numeric_limits<double>::max();
193 	double maxy = -std::numeric_limits<double>::max();
194 	if (m_rotation != 0)
195 	{
196 		FPointArray pb;
197 		pb.resize(0);
198 		pb.addPoint(FPoint(0,       - m_lineWidth / 2.0, m_xPos, m_yPos, m_rotation, 1.0, 1.0));
199 		pb.addPoint(FPoint(m_width, - m_lineWidth / 2.0, m_xPos, m_yPos, m_rotation, 1.0, 1.0));
200 		pb.addPoint(FPoint(m_width, + m_lineWidth / 2.0, m_xPos, m_yPos, m_rotation, 1.0, 1.0));
201 		pb.addPoint(FPoint(0,       + m_lineWidth / 2.0, m_xPos, m_yPos, m_rotation, 1.0, 1.0));
202 		for (uint pc = 0; pc < 4; ++pc)
203 		{
204 			minx = qMin(minx, pb.point(pc).x());
205 			miny = qMin(miny, pb.point(pc).y());
206 			maxx = qMax(maxx, pb.point(pc).x());
207 			maxy = qMax(maxy, pb.point(pc).y());
208 		}
209 		*x1 = minx;
210 		*y1 = miny;
211 		*x2 = maxx;
212 		*y2 = maxy;
213 	}
214 	else
215 	{
216 		*x1 = m_xPos;
217 		*y1 = m_yPos - qMax(1.0, m_lineWidth) / 2.0;
218 		*x2 = m_xPos + m_width;
219 		*y2 = m_yPos + qMax(1.0, m_lineWidth) / 2.0;
220 	}
221 
222 	QRectF totalRect = QRectF(QPointF(*x1, *y1), QPointF(*x2, *y2));
223 	if (m_startArrowIndex != 0)
224 	{
225 		QRectF arrowRect = getStartArrowBoundingRect();
226 		totalRect = totalRect.united(arrowRect);
227 	}
228 	if (m_endArrowIndex != 0)
229 	{
230 		QRectF arrowRect = getEndArrowBoundingRect();
231 		totalRect = totalRect.united(arrowRect);
232 	}
233 	totalRect.getCoords(x1, y1, x2, y2);
234 }
235 
getOldBoundingRect(double * x1,double * y1,double * x2,double * y2) const236 void PageItem_Line::getOldBoundingRect(double *x1, double *y1, double *x2, double *y2) const
237 {
238 	double minx =  std::numeric_limits<double>::max();
239 	double miny =  std::numeric_limits<double>::max();
240 	double maxx = -std::numeric_limits<double>::max();
241 	double maxy = -std::numeric_limits<double>::max();
242 	if (oldRot != 0)
243 	{
244 		FPointArray pb;
245 		pb.resize(0);
246 		pb.addPoint(FPoint(0,        - m_oldLineWidth / 2.0, oldXpos, oldYpos, oldRot, 1.0, 1.0));
247 		pb.addPoint(FPoint(oldWidth, - m_oldLineWidth / 2.0, oldXpos, oldYpos, oldRot, 1.0, 1.0));
248 		pb.addPoint(FPoint(oldWidth, + m_oldLineWidth / 2.0, oldXpos, oldYpos, oldRot, 1.0, 1.0));
249 		pb.addPoint(FPoint(0,        + m_oldLineWidth / 2.0, oldXpos, oldYpos, oldRot, 1.0, 1.0));
250 		for (uint pc = 0; pc < 4; ++pc)
251 		{
252 			minx = qMin(minx, pb.point(pc).x());
253 			miny = qMin(miny, pb.point(pc).y());
254 			maxx = qMax(maxx, pb.point(pc).x());
255 			maxy = qMax(maxy, pb.point(pc).y());
256 		}
257 		*x1 = minx;
258 		*y1 = miny;
259 		*x2 = maxx;
260 		*y2 = maxy;
261 	}
262 	else
263 	{
264 		*x1 = oldXpos;
265 		*y1 = oldYpos - qMax(1.0, m_oldLineWidth) / 2.0;
266 		*x2 = oldXpos + oldWidth;
267 		*y2 = oldYpos + qMax(1.0, m_oldLineWidth) / 2.0;
268 	}
269 
270 	QRectF totalRect = QRectF(QPointF(*x1, *y1), QPointF(*x2, *y2));
271 	if (m_startArrowIndex != 0)
272 	{
273 		QRectF arrowRect = getStartArrowOldBoundingRect();
274 		totalRect = totalRect.united(arrowRect);
275 	}
276 	if (m_endArrowIndex != 0)
277 	{
278 		QRectF arrowRect = getEndArrowOldBoundingRect();
279 		totalRect = totalRect.united(arrowRect);
280 	}
281 	totalRect.getCoords(x1, y1, x2, y2);
282 }
283 
getVisualBoundingRect(double * x1,double * y1,double * x2,double * y2) const284 void PageItem_Line::getVisualBoundingRect(double * x1, double * y1, double * x2, double * y2) const
285 {
286 	double minx =  std::numeric_limits<double>::max();
287 	double miny =  std::numeric_limits<double>::max();
288 	double maxx = -std::numeric_limits<double>::max();
289 	double maxy = -std::numeric_limits<double>::max();
290 	double extraSpace = visualLineWidth() / 2.0;
291 	if (m_rotation != 0)
292 	{
293 		FPointArray pb;
294 		pb.resize(0);
295 		pb.addPoint(FPoint(0.0,           -extraSpace, xPos(), yPos(), m_rotation, 1.0, 1.0));
296 		pb.addPoint(FPoint(visualWidth(), -extraSpace, xPos(), yPos(), m_rotation, 1.0, 1.0));
297 		pb.addPoint(FPoint(visualWidth(), +extraSpace, xPos(), yPos(), m_rotation, 1.0, 1.0));
298 		pb.addPoint(FPoint(0.0,           +extraSpace, xPos(), yPos(), m_rotation, 1.0, 1.0));
299 		for (uint pc = 0; pc < 4; ++pc)
300 		{
301 			minx = qMin(minx, pb.point(pc).x());
302 			miny = qMin(miny, pb.point(pc).y());
303 			maxx = qMax(maxx, pb.point(pc).x());
304 			maxy = qMax(maxy, pb.point(pc).y());
305 		}
306 		*x1 = minx;
307 		*y1 = miny;
308 		*x2 = maxx;
309 		*y2 = maxy;
310 	}
311 	else
312 	{
313 		*x1 = m_xPos;
314 		*y1 = m_yPos - extraSpace;
315 		*x2 = m_xPos + visualWidth();
316 		*y2 = m_yPos + extraSpace;
317 	}
318 
319 	QRectF totalRect(QPointF(*x1, *y1), QPointF(*x2, *y2));
320 	if (m_startArrowIndex != 0)
321 	{
322 		QRectF arrowRect = getStartArrowBoundingRect();
323 		totalRect = totalRect.united(arrowRect);
324 	}
325 	if (m_endArrowIndex != 0)
326 	{
327 		QRectF arrowRect = getEndArrowBoundingRect();
328 		totalRect = totalRect.united(arrowRect);
329 	}
330 	totalRect.getCoords(x1, y1, x2, y2);
331 }
332 
getStartArrowBoundingRect() const333 QRectF PageItem_Line::getStartArrowBoundingRect() const
334 {
335 	QRectF arrowRect;
336 	if (m_startArrowIndex != 0)
337 	{
338 		QTransform arrowTrans;
339 		FPointArray arrow = m_Doc->arrowStyles().at(m_startArrowIndex - 1).points.copy();
340 		arrowTrans.translate(m_xPos, m_yPos);
341 		arrowTrans.rotate(m_rotation);
342 		arrowTrans.translate(0, 0);
343 		arrowTrans.scale(m_startArrowScale / 100.0, m_startArrowScale / 100.0);
344 		if (NamedLStyle.isEmpty())
345 		{
346 			if (m_lineWidth != 0.0)
347 				arrowTrans.scale(m_lineWidth, m_lineWidth);
348 		}
349 		else
350 		{
351 			const multiLine ml = m_Doc->docLineStyles[NamedLStyle];
352 			const SingleLine& sl = ml.last();
353 			if (sl.Width != 0.0)
354 				arrowTrans.scale(sl.Width, sl.Width);
355 		}
356 		arrowTrans.scale(-1, 1);
357 		arrow.map(arrowTrans);
358 		FPoint minAr = getMinClipF(&arrow);
359 		FPoint maxAr = getMaxClipF(&arrow);
360 		arrowRect = QRectF(QPointF(minAr.x(), minAr.y()), QPointF(maxAr.x(), maxAr.y()));
361 	}
362 
363 	return arrowRect;
364 }
365 
getStartArrowOldBoundingRect() const366 QRectF PageItem_Line::getStartArrowOldBoundingRect() const
367 {
368 	QRectF arrowRect;
369 	if (m_startArrowIndex != 0)
370 	{
371 		QTransform arrowTrans;
372 		FPointArray arrow = m_Doc->arrowStyles().at(m_startArrowIndex - 1).points.copy();
373 		arrowTrans.translate(oldXpos, oldYpos);
374 		arrowTrans.rotate(oldRot);
375 		arrowTrans.translate(0, 0);
376 		arrowTrans.scale(m_startArrowScale / 100.0, m_startArrowScale / 100.0);
377 		if (NamedLStyle.isEmpty())
378 		{
379 			if (m_oldLineWidth != 0.0)
380 				arrowTrans.scale(m_oldLineWidth, m_oldLineWidth);
381 		}
382 		else
383 		{
384 			const multiLine ml = m_Doc->docLineStyles[NamedLStyle];
385 			const SingleLine& sl = ml.last();
386 			if (sl.Width != 0.0)
387 				arrowTrans.scale(sl.Width, sl.Width);
388 		}
389 		arrowTrans.scale(-1, 1);
390 		arrow.map(arrowTrans);
391 		FPoint minAr = getMinClipF(&arrow);
392 		FPoint maxAr = getMaxClipF(&arrow);
393 		arrowRect = QRectF(QPointF(minAr.x(), minAr.y()), QPointF(maxAr.x(), maxAr.y()));
394 	}
395 
396 	return arrowRect;
397 }
398 
getEndArrowBoundingRect() const399 QRectF PageItem_Line::getEndArrowBoundingRect() const
400 {
401 	QRectF arrowRect;
402 	if (m_endArrowIndex != 0)
403 	{
404 		QTransform arrowTrans;
405 		FPointArray arrow = m_Doc->arrowStyles().at(m_endArrowIndex - 1).points.copy();
406 		arrowTrans.translate(m_xPos, m_yPos);
407 		arrowTrans.rotate(m_rotation);
408 		arrowTrans.translate(m_width, 0);
409 		arrowTrans.scale(m_endArrowScale / 100.0, m_endArrowScale / 100.0);
410 		if (NamedLStyle.isEmpty())
411 		{
412 			if (m_lineWidth != 0.0)
413 				arrowTrans.scale(m_lineWidth, m_lineWidth);
414 		}
415 		else
416 		{
417 			const multiLine ml = m_Doc->docLineStyles[NamedLStyle];
418 			const SingleLine& sl = ml.last();
419 			if (sl.Width != 0.0)
420 				arrowTrans.scale(sl.Width, sl.Width);
421 		}
422 		arrow.map(arrowTrans);
423 		FPoint minAr = getMinClipF(&arrow);
424 		FPoint maxAr = getMaxClipF(&arrow);
425 		arrowRect = QRectF(QPointF(minAr.x(), minAr.y()), QPointF(maxAr.x(), maxAr.y()));
426 	}
427 
428 	return arrowRect;
429 }
430 
getEndArrowOldBoundingRect() const431 QRectF PageItem_Line::getEndArrowOldBoundingRect() const
432 {
433 	QRectF arrowRect;
434 	if (m_endArrowIndex != 0)
435 	{
436 		QTransform arrowTrans;
437 		FPointArray arrow = m_Doc->arrowStyles().at(m_endArrowIndex - 1).points.copy();
438 		arrowTrans.translate(oldXpos, oldYpos);
439 		arrowTrans.rotate(oldRot);
440 		arrowTrans.translate(oldWidth, 0);
441 		arrowTrans.scale(m_endArrowScale / 100.0, m_endArrowScale / 100.0);
442 		if (NamedLStyle.isEmpty())
443 		{
444 			if (m_oldLineWidth != 0.0)
445 				arrowTrans.scale(m_oldLineWidth, m_oldLineWidth);
446 		}
447 		else
448 		{
449 			const multiLine ml = m_Doc->docLineStyles[NamedLStyle];
450 			const SingleLine& sl = ml.last();
451 			if (sl.Width != 0.0)
452 				arrowTrans.scale(sl.Width, sl.Width);
453 		}
454 		arrow.map(arrowTrans);
455 		FPoint minAr = getMinClipF(&arrow);
456 		FPoint maxAr = getMaxClipF(&arrow);
457 		arrowRect = QRectF(QPointF(minAr.x(), minAr.y()), QPointF(maxAr.x(), maxAr.y()));
458 	}
459 
460 	return arrowRect;
461 }
462 
visualXPos() const463 double PageItem_Line::visualXPos() const
464 {
465 	return m_xPos;
466 }
467 
visualYPos() const468 double PageItem_Line::visualYPos() const
469 {
470 	double extraSpace = visualLineWidth() / 2.0;
471 	return m_yPos - extraSpace;
472 }
473 
visualWidth() const474 double PageItem_Line::visualWidth() const
475 {
476 	return m_width;
477 }
478 
visualHeight() const479 double PageItem_Line::visualHeight() const
480 {
481 	double extraSpace = visualLineWidth();
482 	return extraSpace;
483 }
484