qpen.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/painting/qpen.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtGui module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33#include "qpen.h"-
34#include "qpen_p.h"-
35#include "qdatastream.h"-
36#include "qvariant.h"-
37#include "qbrush.h"-
38-
39#include <qdebug.h>-
40-
41QT_BEGIN_NAMESPACE-
42-
43typedef QPenPrivate QPenData;-
44-
45/*!-
46 \class QPen-
47 \inmodule QtGui-
48 \ingroup painting-
49 \ingroup shared-
50-
51-
52 \brief The QPen class defines how a QPainter should draw lines and outlines-
53 of shapes.-
54-
55 A pen has a style(), width(), brush(), capStyle() and joinStyle().-
56-
57 The pen style defines the line type. The brush is used to fill-
58 strokes generated with the pen. Use the QBrush class to specify-
59 fill styles. The cap style determines the line end caps that can-
60 be drawn using QPainter, while the join style describes how joins-
61 between two lines are drawn. The pen width can be specified in-
62 both integer (width()) and floating point (widthF()) precision. A-
63 line width of zero indicates a cosmetic pen. This means that the-
64 pen width is always drawn one pixel wide, independent of the \l-
65 {QPainter#Coordinate Transformations}{transformation} set on the-
66 painter.-
67-
68 The various settings can easily be modified using the-
69 corresponding setStyle(), setWidth(), setBrush(), setCapStyle()-
70 and setJoinStyle() functions (note that the painter's pen must be-
71 reset when altering the pen's properties).-
72-
73 For example:-
74-
75 \snippet code/src_gui_painting_qpen.cpp 0-
76-
77 which is equivalent to-
78-
79 \snippet code/src_gui_painting_qpen.cpp 1-
80-
81 The default pen is a solid black brush with 1 width, square-
82 cap style (Qt::SquareCap), and bevel join style (Qt::BevelJoin).-
83-
84 In addition QPen provides the color() and setColor()-
85 convenience functions to extract and set the color of the pen's-
86 brush, respectively. Pens may also be compared and streamed.-
87-
88 For more information about painting in general, see the \l{Paint-
89 System} documentation.-
90-
91 \tableofcontents-
92-
93 \section1 Pen Style-
94-
95 Qt provides several built-in styles represented by the-
96 Qt::PenStyle enum:-
97-
98 \table-
99 \row-
100 \li \inlineimage qpen-solid.png-
101 \li \inlineimage qpen-dash.png-
102 \li \inlineimage qpen-dot.png-
103 \row-
104 \li Qt::SolidLine-
105 \li Qt::DashLine-
106 \li Qt::DotLine-
107 \row-
108 \li \inlineimage qpen-dashdot.png-
109 \li \inlineimage qpen-dashdotdot.png-
110 \li \inlineimage qpen-custom.png-
111 \row-
112 \li Qt::DashDotLine-
113 \li Qt::DashDotDotLine-
114 \li Qt::CustomDashLine-
115 \endtable-
116-
117 Simply use the setStyle() function to convert the pen style to-
118 either of the built-in styles, except the Qt::CustomDashLine style-
119 which we will come back to shortly. Setting the style to Qt::NoPen-
120 tells the painter to not draw lines or outlines. The default pen-
121 style is Qt::SolidLine.-
122-
123 Since Qt 4.1 it is also possible to specify a custom dash pattern-
124 using the setDashPattern() function which implicitly converts the-
125 style of the pen to Qt::CustomDashLine. The pattern argument, a-
126 QVector, must be specified as an even number of \l qreal entries-
127 where the entries 1, 3, 5... are the dashes and 2, 4, 6... are the-
128 spaces. For example, the custom pattern shown above is created-
129 using the following code:-
130-
131 \snippet code/src_gui_painting_qpen.cpp 2-
132-
133 Note that the dash pattern is specified in units of the pens-
134 width, e.g. a dash of length 5 in width 10 is 50 pixels long.-
135-
136 The currently set dash pattern can be retrieved using the-
137 dashPattern() function. Use the isSolid() function to determine-
138 whether the pen has a solid fill, or not.-
139-
140 \section1 Cap Style-
141-
142 The cap style defines how the end points of lines are drawn using-
143 QPainter. The cap style only apply to wide lines, i.e. when the-
144 width is 1 or greater. The Qt::PenCapStyle enum provides the-
145 following styles:-
146-
147 \table-
148 \row-
149 \li \inlineimage qpen-square.png-
150 \li \inlineimage qpen-flat.png-
151 \li \inlineimage qpen-roundcap.png-
152 \row-
153 \li Qt::SquareCap-
154 \li Qt::FlatCap-
155 \li Qt::RoundCap-
156 \endtable-
157-
158 The Qt::SquareCap style is a square line end that covers the end-
159 point and extends beyond it by half the line width. The-
160 Qt::FlatCap style is a square line end that does not cover the end-
161 point of the line. And the Qt::RoundCap style is a rounded line-
162 end covering the end point.-
163-
164 The default is Qt::SquareCap.-
165-
166 Whether or not end points are drawn when the pen width is 0 or 1-
167 depends on the cap style. Using Qt::SquareCap or Qt::RoundCap they-
168 are drawn, using Qt::FlatCap they are not drawn.-
169-
170 \section1 Join Style-
171-
172 The join style defines how joins between two connected lines can-
173 be drawn using QPainter. The join style only apply to wide lines,-
174 i.e. when the width is 1 or greater. The Qt::PenJoinStyle enum-
175 provides the following styles:-
176-
177 \table-
178 \row-
179 \li \inlineimage qpen-bevel.png-
180 \li \inlineimage qpen-miter.png-
181 \li \inlineimage qpen-roundjoin.png-
182 \row-
183 \li Qt::BevelJoin-
184 \li Qt::MiterJoin-
185 \li Qt::RoundJoin-
186 \endtable-
187-
188 The Qt::BevelJoin style fills the triangular notch between the two-
189 lines. The Qt::MiterJoin style extends the lines to meet at an-
190 angle. And the Qt::RoundJoin style fills a circular arc between-
191 the two lines.-
192-
193 The default is Qt::BevelJoin.-
194-
195 \image qpen-miterlimit.png-
196-
197 When the Qt::MiterJoin style is applied, it is possible to use the-
198 setMiterLimit() function to specify how far the miter join can-
199 extend from the join point. The miterLimit() is used to reduce-
200 artifacts between line joins where the lines are close to-
201 parallel.-
202-
203 The miterLimit() must be specified in units of the pens width,-
204 e.g. a miter limit of 5 in width 10 is 50 pixels long. The-
205 default miter limit is 2, i.e. twice the pen width in pixels.-
206-
207 \table 100%-
208 \row-
209 \li \inlineimage qpen-demo.png-
210 \li \b {\l {painting/pathstroke}{The Path Stroking Example}}-
211-
212 The Path Stroking example shows Qt's built-in dash patterns and shows-
213 how custom patterns can be used to extend the range of available-
214 patterns.-
215 \endtable-
216-
217 \sa QPainter, QBrush, {painting/pathstroke}{Path Stroking Example},-
218 {Scribble Example}-
219*/-
220-
221/*!-
222 \internal-
223*/-
224inline QPenPrivate::QPenPrivate(const QBrush &_brush, qreal _width, Qt::PenStyle penStyle,-
225 Qt::PenCapStyle _capStyle, Qt::PenJoinStyle _joinStyle, bool _defaultWidth)-
226 : ref(1), dashOffset(0), miterLimit(2),-
227 cosmetic(false), defaultWidth(_defaultWidth)-
228{-
229 width = _width;-
230 brush = _brush;-
231 style = penStyle;-
232 capStyle = _capStyle;-
233 joinStyle = _joinStyle;-
234}
never executed: end of block
0
235-
236static const Qt::PenCapStyle qpen_default_cap = Qt::SquareCap;-
237static const Qt::PenJoinStyle qpen_default_join = Qt::BevelJoin;-
238-
239class QPenDataHolder-
240{-
241public:-
242 QPenData *pen;-
243 QPenDataHolder(const QBrush &brush, qreal width, Qt::PenStyle penStyle,-
244 Qt::PenCapStyle penCapStyle, Qt::PenJoinStyle _joinStyle)-
245 : pen(new QPenData(brush, width, penStyle, penCapStyle, _joinStyle))-
246 { }
never executed: end of block
0
247 ~QPenDataHolder()-
248 {-
249 if (!pen->ref.deref())
!pen->ref.deref()Description
TRUEnever evaluated
FALSEnever evaluated
0
250 delete pen;
never executed: delete pen;
0
251 pen = 0;-
252 }
never executed: end of block
0
253};-
254-
255Q_GLOBAL_STATIC_WITH_ARGS(QPenDataHolder, defaultPenInstance,
never executed: end of block
never executed: guard.store(QtGlobalStatic::Destroyed);
never executed: return &holder.value;
guard.load() =...c::InitializedDescription
TRUEnever evaluated
FALSEnever evaluated
0
256 (Qt::black, 1, Qt::SolidLine, qpen_default_cap, qpen_default_join))-
257Q_GLOBAL_STATIC_WITH_ARGS(QPenDataHolder, nullPenInstance,
never executed: end of block
never executed: guard.store(QtGlobalStatic::Destroyed);
never executed: return &holder.value;
guard.load() =...c::InitializedDescription
TRUEnever evaluated
FALSEnever evaluated
0
258 (Qt::black, 1, Qt::NoPen, qpen_default_cap, qpen_default_join))-
259-
260/*!-
261 Constructs a default black solid line pen with 1 width.-
262*/-
263-
264QPen::QPen()-
265{-
266 d = defaultPenInstance()->pen;-
267 d->ref.ref();-
268}
never executed: end of block
0
269-
270/*!-
271 Constructs a black pen with 1 width and the given \a style.-
272-
273 \sa setStyle()-
274*/-
275-
276QPen::QPen(Qt::PenStyle style)-
277{-
278 if (style == Qt::NoPen) {
style == Qt::NoPenDescription
TRUEnever evaluated
FALSEnever evaluated
0
279 d = nullPenInstance()->pen;-
280 d->ref.ref();-
281 } else {
never executed: end of block
0
282 d = new QPenData(Qt::black, 1, style, qpen_default_cap, qpen_default_join);-
283 }
never executed: end of block
0
284}-
285-
286-
287/*!-
288 Constructs a solid line pen with 1 width and the given \a color.-
289-
290 \sa setBrush(), setColor()-
291*/-
292-
293QPen::QPen(const QColor &color)-
294{-
295 d = new QPenData(color, 1, Qt::SolidLine, qpen_default_cap, qpen_default_join);-
296}
never executed: end of block
0
297-
298-
299/*!-
300 \fn QPen::QPen(const QBrush &brush, qreal width, Qt::PenStyle style, Qt::PenCapStyle cap, Qt::PenJoinStyle join)-
301-
302 Constructs a pen with the specified \a brush, \a width, pen \a style,-
303 \a cap style and \a join style.-
304-
305 \sa setBrush(), setWidth(), setStyle(), setCapStyle(), setJoinStyle()-
306*/-
307-
308QPen::QPen(const QBrush &brush, qreal width, Qt::PenStyle s, Qt::PenCapStyle c, Qt::PenJoinStyle j)-
309{-
310 d = new QPenData(brush, width, s, c, j, false);-
311}
never executed: end of block
0
312-
313/*!-
314 \fn QPen::QPen(const QPen &pen)-
315-
316 Constructs a pen that is a copy of the given \a pen.-
317*/-
318-
319QPen::QPen(const QPen &p) Q_DECL_NOTHROW-
320{-
321 d = p.d;-
322 if (d)
dDescription
TRUEnever evaluated
FALSEnever evaluated
0
323 d->ref.ref();
never executed: d->ref.ref();
0
324}
never executed: end of block
0
325-
326-
327/*!-
328 \fn QPen::QPen(QPen &&pen)-
329 \since 5.4-
330-
331 Constructs a pen that is moved from the given \a pen.-
332-
333 The moved-from pen can only be assigned to, copied, or-
334 destroyed. Any other operation (prior to assignment) leads to-
335 undefined behavior.-
336*/-
337-
338/*!-
339 Destroys the pen.-
340*/-
341-
342QPen::~QPen()-
343{-
344 if (d && !d->ref.deref())
dDescription
TRUEnever evaluated
FALSEnever evaluated
!d->ref.deref()Description
TRUEnever evaluated
FALSEnever evaluated
0
345 delete d;
never executed: delete d;
0
346}
never executed: end of block
0
347-
348/*!-
349 \fn void QPen::detach()-
350 Detaches from shared pen data to make sure that this pen is the-
351 only one referring the data.-
352-
353 If multiple pens share common data, this pen dereferences the data-
354 and gets a copy of the data. Nothing is done if there is just a-
355 single reference.-
356*/-
357-
358void QPen::detach()-
359{-
360 if (d->ref.load() == 1)
d->ref.load() == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
361 return;
never executed: return;
0
362-
363 QPenData *x = new QPenData(*static_cast<QPenData *>(d));-
364 if (!d->ref.deref())
!d->ref.deref()Description
TRUEnever evaluated
FALSEnever evaluated
0
365 delete d;
never executed: delete d;
0
366 x->ref.store(1);-
367 d = x;-
368}
never executed: end of block
0
369-
370-
371/*!-
372 \fn QPen &QPen::operator=(const QPen &pen)-
373-
374 Assigns the given \a pen to this pen and returns a reference to-
375 this pen.-
376*/-
377-
378QPen &QPen::operator=(const QPen &p) Q_DECL_NOTHROW-
379{-
380 QPen(p).swap(*this);-
381 return *this;
never executed: return *this;
0
382}-
383-
384/*!-
385 \fn QPen &QPen::operator=(QPen &&other)-
386-
387 Move-assigns \a other to this QPen instance.-
388-
389 \since 5.2-
390*/-
391-
392/*!-
393 \fn void QPen::swap(QPen &other)-
394 \since 4.8-
395-
396 Swaps pen \a other with this pen. This operation is very-
397 fast and never fails.-
398*/-
399-
400/*!-
401 Returns the pen as a QVariant.-
402*/-
403QPen::operator QVariant() const-
404{-
405 return QVariant(QVariant::Pen, this);
never executed: return QVariant(QVariant::Pen, this);
0
406}-
407-
408/*!-
409 \fn Qt::PenStyle QPen::style() const-
410-
411 Returns the pen style.-
412-
413 \sa setStyle(), {QPen#Pen Style}{Pen Style}-
414*/-
415Qt::PenStyle QPen::style() const-
416{-
417 return d->style;
never executed: return d->style;
0
418}-
419/*!-
420 \fn void QPen::setStyle(Qt::PenStyle style)-
421-
422 Sets the pen style to the given \a style.-
423-
424 See the \l Qt::PenStyle documentation for a list of the available-
425 styles. Since Qt 4.1 it is also possible to specify a custom dash-
426 pattern using the setDashPattern() function which implicitly-
427 converts the style of the pen to Qt::CustomDashLine.-
428-
429 \note This function resets the dash offset to zero.-
430-
431 \sa style(), {QPen#Pen Style}{Pen Style}-
432*/-
433-
434void QPen::setStyle(Qt::PenStyle s)-
435{-
436 if (d->style == s)
d->style == sDescription
TRUEnever evaluated
FALSEnever evaluated
0
437 return;
never executed: return;
0
438 detach();-
439 d->style = s;-
440 QPenData *dd = static_cast<QPenData *>(d);-
441 dd->dashPattern.clear();-
442 dd->dashOffset = 0;-
443}
never executed: end of block
0
444-
445/*!-
446 Returns the dash pattern of this pen.-
447-
448 \sa style(), isSolid()-
449 */-
450QVector<qreal> QPen::dashPattern() const-
451{-
452 QPenData *dd = static_cast<QPenData *>(d);-
453 if (d->style == Qt::SolidLine || d->style == Qt::NoPen) {
d->style == Qt::SolidLineDescription
TRUEnever evaluated
FALSEnever evaluated
d->style == Qt::NoPenDescription
TRUEnever evaluated
FALSEnever evaluated
0
454 return QVector<qreal>();
never executed: return QVector<qreal>();
0
455 } else if (dd->dashPattern.isEmpty()) {
dd->dashPattern.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
456 const qreal space = 2;-
457 const qreal dot = 1;-
458 const qreal dash = 4;-
459-
460 switch (d->style) {-
461 case Qt::DashLine:
never executed: case Qt::DashLine:
0
462 dd->dashPattern.reserve(2);-
463 dd->dashPattern << dash << space;-
464 break;
never executed: break;
0
465 case Qt::DotLine:
never executed: case Qt::DotLine:
0
466 dd->dashPattern.reserve(2);-
467 dd->dashPattern << dot << space;-
468 break;
never executed: break;
0
469 case Qt::DashDotLine:
never executed: case Qt::DashDotLine:
0
470 dd->dashPattern.reserve(4);-
471 dd->dashPattern << dash << space << dot << space;-
472 break;
never executed: break;
0
473 case Qt::DashDotDotLine:
never executed: case Qt::DashDotDotLine:
0
474 dd->dashPattern.reserve(6);-
475 dd->dashPattern << dash << space << dot << space << dot << space;-
476 break;
never executed: break;
0
477 default:
never executed: default:
0
478 break;
never executed: break;
0
479 }-
480 }-
481 return dd->dashPattern;
never executed: return dd->dashPattern;
0
482}-
483-
484/*!-
485 Sets the dash pattern for this pen to the given \a pattern. This-
486 implicitly converts the style of the pen to Qt::CustomDashLine.-
487-
488 The pattern must be specified as an even number of positive entries-
489 where the entries 1, 3, 5... are the dashes and 2, 4, 6... are the-
490 spaces. For example:-
491-
492 \table 100%-
493 \row-
494 \li \inlineimage qpen-custom.png-
495 \li-
496 \snippet code/src_gui_painting_qpen.cpp 3-
497 \endtable-
498-
499 The dash pattern is specified in units of the pens width; e.g. a-
500 dash of length 5 in width 10 is 50 pixels long. Note that a pen-
501 with zero width is equivalent to a cosmetic pen with a width of 1-
502 pixel.-
503-
504 Each dash is also subject to cap styles so a dash of 1 with square-
505 cap set will extend 0.5 pixels out in each direction resulting in-
506 a total width of 2.-
507-
508 Note that the default cap style is Qt::SquareCap, meaning that a-
509 square line end covers the end point and extends beyond it by half-
510 the line width.-
511-
512 \sa setStyle(), dashPattern(), setCapStyle(), setCosmetic()-
513 */-
514void QPen::setDashPattern(const QVector<qreal> &pattern)-
515{-
516 if (pattern.isEmpty())
pattern.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
517 return;
never executed: return;
0
518 detach();-
519-
520 QPenData *dd = static_cast<QPenData *>(d);-
521 dd->dashPattern = pattern;-
522 d->style = Qt::CustomDashLine;-
523-
524 if ((dd->dashPattern.size() % 2) == 1) {
(dd->dashPatte...ze() % 2) == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
525 qWarning("QPen::setDashPattern: Pattern not of even length");-
526 dd->dashPattern << 1;-
527 }
never executed: end of block
0
528}
never executed: end of block
0
529-
530-
531/*!-
532 Returns the dash offset for the pen.-
533-
534 \sa setDashOffset()-
535*/-
536qreal QPen::dashOffset() const-
537{-
538 QPenData *dd = static_cast<QPenData *>(d);-
539 return dd->dashOffset;
never executed: return dd->dashOffset;
0
540}-
541/*!-
542 Sets the dash offset (the starting point on the dash pattern) for this pen-
543 to the \a offset specified. The offset is measured in terms of the units used-
544 to specify the dash pattern.-
545-
546 \table-
547 \row \li \inlineimage qpen-dashpattern.png-
548 \li For example, a pattern where each stroke is four units long, followed by a gap-
549 of two units, will begin with the stroke when drawn as a line.-
550-
551 However, if the dash offset is set to 4.0, any line drawn will begin with the gap.-
552 Values of the offset up to 4.0 will cause part of the stroke to be drawn first,-
553 and values of the offset between 4.0 and 6.0 will cause the line to begin with-
554 part of the gap.-
555 \endtable-
556-
557 \note This implicitly converts the style of the pen to Qt::CustomDashLine.-
558*/-
559void QPen::setDashOffset(qreal offset)-
560{-
561 if (qFuzzyCompare(offset, static_cast<QPenData *>(d)->dashOffset))
qFuzzyCompare(...)->dashOffset)Description
TRUEnever evaluated
FALSEnever evaluated
0
562 return;
never executed: return;
0
563 detach();-
564 QPenData *dd = static_cast<QPenData *>(d);-
565 dd->dashOffset = offset;-
566 if (d->style != Qt::CustomDashLine) {
d->style != Qt::CustomDashLineDescription
TRUEnever evaluated
FALSEnever evaluated
0
567 dd->dashPattern = dashPattern();-
568 d->style = Qt::CustomDashLine;-
569 }
never executed: end of block
0
570}
never executed: end of block
0
571-
572/*!-
573 Returns the miter limit of the pen. The miter limit is only-
574 relevant when the join style is set to Qt::MiterJoin.-
575-
576 \sa setMiterLimit(), {QPen#Join Style}{Join Style}-
577*/-
578qreal QPen::miterLimit() const-
579{-
580 const QPenData *dd = static_cast<QPenData *>(d);-
581 return dd->miterLimit;
never executed: return dd->miterLimit;
0
582}-
583-
584/*!-
585 Sets the miter limit of this pen to the given \a limit.-
586-
587 \image qpen-miterlimit.png-
588-
589 The miter limit describes how far a miter join can extend from the-
590 join point. This is used to reduce artifacts between line joins-
591 where the lines are close to parallel.-
592-
593 This value does only have effect when the pen style is set to-
594 Qt::MiterJoin. The value is specified in units of the pen's width,-
595 e.g. a miter limit of 5 in width 10 is 50 pixels long. The default-
596 miter limit is 2, i.e. twice the pen width in pixels.-
597-
598 \sa miterLimit(), setJoinStyle(), {QPen#Join Style}{Join Style}-
599*/-
600void QPen::setMiterLimit(qreal limit)-
601{-
602 detach();-
603 QPenData *dd = static_cast<QPenData *>(d);-
604 dd->miterLimit = limit;-
605}
never executed: end of block
0
606-
607-
608/*!-
609 \fn qreal QPen::width() const-
610-
611 Returns the pen width with integer precision.-
612-
613 \sa setWidth(), widthF()-
614*/-
615-
616int QPen::width() const-
617{-
618 return qRound(d->width);
never executed: return qRound(d->width);
0
619}-
620-
621/*!-
622 \fn qreal QPen::widthF() const-
623-
624 Returns the pen width with floating point precision.-
625-
626 \sa setWidthF(), width()-
627*/-
628qreal QPen::widthF() const-
629{-
630 return d->width;
never executed: return d->width;
0
631}-
632-
633/*!-
634 \fn QPen::setWidth(int width)-
635-
636 Sets the pen width to the given \a width in pixels with integer-
637 precision.-
638-
639 A line width of zero indicates a cosmetic pen. This means that the-
640 pen width is always drawn one pixel wide, independent of the \l-
641 {QPainter#Coordinate Transformations}{transformation} set on the-
642 painter.-
643-
644 Setting a pen width with a negative value is not supported.-
645-
646 \sa setWidthF(), width()-
647*/-
648void QPen::setWidth(int width)-
649{-
650 if (width < 0)
width < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
651 qWarning("QPen::setWidth: Setting a pen width with a negative value is not defined");
never executed: QMessageLogger(__FILE__, 651, __PRETTY_FUNCTION__).warning("QPen::setWidth: Setting a pen width with a negative value is not defined");
0
652 if ((qreal)width == d->width)
(qreal)width == d->widthDescription
TRUEnever evaluated
FALSEnever evaluated
0
653 return;
never executed: return;
0
654 detach();-
655 d->width = width;-
656}
never executed: end of block
0
657-
658/*!-
659 Sets the pen width to the given \a width in pixels with floating point-
660 precision.-
661-
662 A line width of zero indicates a cosmetic pen. This means that the-
663 pen width is always drawn one pixel wide, independent of the \l-
664 {QPainter#Coordinate Transformations}{transformation} on the-
665 painter.-
666-
667 Setting a pen width with a negative value is not supported.-
668-
669 \sa setWidth(), widthF()-
670*/-
671-
672void QPen::setWidthF(qreal width)-
673{-
674 if (width < 0.f) {
width < 0.fDescription
TRUEnever evaluated
FALSEnever evaluated
0
675 qWarning("QPen::setWidthF: Setting a pen width with a negative value is not defined");-
676 return;
never executed: return;
0
677 }-
678 if (qAbs(d->width - width) < 0.00000001f)
qAbs(d->width ... < 0.00000001fDescription
TRUEnever evaluated
FALSEnever evaluated
0
679 return;
never executed: return;
0
680 detach();-
681 d->width = width;-
682 d->defaultWidth = false;-
683}
never executed: end of block
0
684-
685-
686/*!-
687 Returns the pen's cap style.-
688-
689 \sa setCapStyle(), {QPen#Cap Style}{Cap Style}-
690*/-
691Qt::PenCapStyle QPen::capStyle() const-
692{-
693 return d->capStyle;
never executed: return d->capStyle;
0
694}-
695-
696/*!-
697 \fn void QPen::setCapStyle(Qt::PenCapStyle style)-
698-
699 Sets the pen's cap style to the given \a style. The default value-
700 is Qt::SquareCap.-
701-
702 \sa capStyle(), {QPen#Cap Style}{Cap Style}-
703*/-
704-
705void QPen::setCapStyle(Qt::PenCapStyle c)-
706{-
707 if (d->capStyle == c)
d->capStyle == cDescription
TRUEnever evaluated
FALSEnever evaluated
0
708 return;
never executed: return;
0
709 detach();-
710 d->capStyle = c;-
711}
never executed: end of block
0
712-
713/*!-
714 Returns the pen's join style.-
715-
716 \sa setJoinStyle(), {QPen#Join Style}{Join Style}-
717*/-
718Qt::PenJoinStyle QPen::joinStyle() const-
719{-
720 return d->joinStyle;
never executed: return d->joinStyle;
0
721}-
722-
723/*!-
724 \fn void QPen::setJoinStyle(Qt::PenJoinStyle style)-
725-
726 Sets the pen's join style to the given \a style. The default value-
727 is Qt::BevelJoin.-
728-
729 \sa joinStyle(), {QPen#Join Style}{Join Style}-
730*/-
731-
732void QPen::setJoinStyle(Qt::PenJoinStyle j)-
733{-
734 if (d->joinStyle == j)
d->joinStyle == jDescription
TRUEnever evaluated
FALSEnever evaluated
0
735 return;
never executed: return;
0
736 detach();-
737 d->joinStyle = j;-
738}
never executed: end of block
0
739-
740/*!-
741 \fn const QColor &QPen::color() const-
742-
743 Returns the color of this pen's brush.-
744-
745 \sa brush(), setColor()-
746*/-
747QColor QPen::color() const-
748{-
749 return d->brush.color();
never executed: return d->brush.color();
0
750}-
751-
752/*!-
753 \fn void QPen::setColor(const QColor &color)-
754-
755 Sets the color of this pen's brush to the given \a color.-
756-
757 \sa setBrush(), color()-
758*/-
759-
760void QPen::setColor(const QColor &c)-
761{-
762 detach();-
763 d->brush = QBrush(c);-
764}
never executed: end of block
0
765-
766-
767/*!-
768 Returns the brush used to fill strokes generated with this pen.-
769*/-
770QBrush QPen::brush() const-
771{-
772 return d->brush;
never executed: return d->brush;
0
773}-
774-
775/*!-
776 Sets the brush used to fill strokes generated with this pen to the given-
777 \a brush.-
778-
779 \sa brush(), setColor()-
780*/-
781void QPen::setBrush(const QBrush &brush)-
782{-
783 detach();-
784 d->brush = brush;-
785}
never executed: end of block
0
786-
787-
788/*!-
789 Returns \c true if the pen has a solid fill, otherwise false.-
790-
791 \sa style(), dashPattern()-
792*/-
793bool QPen::isSolid() const-
794{-
795 return d->brush.style() == Qt::SolidPattern;
never executed: return d->brush.style() == Qt::SolidPattern;
0
796}-
797-
798-
799/*!-
800 Returns \c true if the pen is cosmetic; otherwise returns \c false.-
801-
802 Cosmetic pens are used to draw strokes that have a constant width-
803 regardless of any transformations applied to the QPainter they are-
804 used with. Drawing a shape with a cosmetic pen ensures that its-
805 outline will have the same thickness at different scale factors.-
806-
807 A zero width pen is cosmetic by default.-
808-
809 \sa setCosmetic(), widthF()-
810*/-
811-
812bool QPen::isCosmetic() const-
813{-
814 QPenData *dd = static_cast<QPenData *>(d);-
815 return (dd->cosmetic == true) || d->width == 0;
never executed: return (dd->cosmetic == true) || d->width == 0;
(dd->cosmetic == true)Description
TRUEnever evaluated
FALSEnever evaluated
d->width == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
816}-
817-
818-
819/*!-
820 Sets this pen to cosmetic or non-cosmetic, depending on the value of-
821 \a cosmetic.-
822-
823 \sa isCosmetic()-
824*/-
825-
826void QPen::setCosmetic(bool cosmetic)-
827{-
828 detach();-
829 QPenData *dd = static_cast<QPenData *>(d);-
830 dd->cosmetic = cosmetic;-
831}
never executed: end of block
0
832-
833-
834-
835/*!-
836 \fn bool QPen::operator!=(const QPen &pen) const-
837-
838 Returns \c true if the pen is different from the given \a pen;-
839 otherwise false. Two pens are different if they have different-
840 styles, widths or colors.-
841-
842 \sa operator==()-
843*/-
844-
845/*!-
846 \fn bool QPen::operator==(const QPen &pen) const-
847-
848 Returns \c true if the pen is equal to the given \a pen; otherwise-
849 false. Two pens are equal if they have equal styles, widths and-
850 colors.-
851-
852 \sa operator!=()-
853*/-
854-
855bool QPen::operator==(const QPen &p) const-
856{-
857 QPenData *dd = static_cast<QPenData *>(d);-
858 QPenData *pdd = static_cast<QPenData *>(p.d);-
859 return (p.d == d)
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
(p.d == d)Description
TRUEnever evaluated
FALSEnever evaluated
0
860 || (p.d->style == d->style
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
p.d->style == d->styleDescription
TRUEnever evaluated
FALSEnever evaluated
0
861 && p.d->capStyle == d->capStyle
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
p.d->capStyle == d->capStyleDescription
TRUEnever evaluated
FALSEnever evaluated
0
862 && p.d->joinStyle == d->joinStyle
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
p.d->joinStyle == d->joinStyleDescription
TRUEnever evaluated
FALSEnever evaluated
0
863 && p.d->width == d->width
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
p.d->width == d->widthDescription
TRUEnever evaluated
FALSEnever evaluated
0
864 && pdd->miterLimit == dd->miterLimit
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
pdd->miterLimi...dd->miterLimitDescription
TRUEnever evaluated
FALSEnever evaluated
0
865 && (d->style != Qt::CustomDashLine
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
d->style != Qt::CustomDashLineDescription
TRUEnever evaluated
FALSEnever evaluated
0
866 || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) &&
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
qFuzzyCompare(...d->dashOffset)Description
TRUEnever evaluated
FALSEnever evaluated
0
867 pdd->dashPattern == dd->dashPattern))
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
pdd->dashPatte...d->dashPatternDescription
TRUEnever evaluated
FALSEnever evaluated
0
868 && p.d->brush == d->brush
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
p.d->brush == d->brushDescription
TRUEnever evaluated
FALSEnever evaluated
0
869 && pdd->cosmetic == dd->cosmetic
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
pdd->cosmetic == dd->cosmeticDescription
TRUEnever evaluated
FALSEnever evaluated
0
870 && pdd->defaultWidth == dd->defaultWidth);
never executed: return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && pdd->miterLimit == dd->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) && pdd->dashPattern == dd->dashPattern)) && p.d->brush == d->brush && pdd->cosmetic == dd->cosmetic && pdd->defaultWidth == dd->defaultWidth);
pdd->defaultWi...->defaultWidthDescription
TRUEnever evaluated
FALSEnever evaluated
0
871}-
872-
873-
874/*!-
875 \fn bool QPen::isDetached()-
876-
877 \internal-
878*/-
879-
880bool QPen::isDetached()-
881{-
882 return d->ref.load() == 1;
never executed: return d->ref.load() == 1;
0
883}-
884-
885-
886/*****************************************************************************-
887 QPen stream functions-
888 *****************************************************************************/-
889#ifndef QT_NO_DATASTREAM-
890/*!-
891 \fn QDataStream &operator<<(QDataStream &stream, const QPen &pen)-
892 \relates QPen-
893-
894 Writes the given \a pen to the given \a stream and returns a reference to-
895 the \a stream.-
896-
897 \sa {Serializing Qt Data Types}-
898*/-
899-
900QDataStream &operator<<(QDataStream &s, const QPen &p)-
901{-
902 QPenData *dd = static_cast<QPenData *>(p.d);-
903 if (s.version() < 3) {
s.version() < 3Description
TRUEnever evaluated
FALSEnever evaluated
0
904 s << (quint8)p.style();-
905 } else if (s.version() < QDataStream::Qt_4_3) {
never executed: end of block
s.version() < ...Stream::Qt_4_3Description
TRUEnever evaluated
FALSEnever evaluated
0
906 s << (quint8)(p.style() | p.capStyle() | p.joinStyle());-
907 } else {
never executed: end of block
0
908 s << (quint16)(p.style() | p.capStyle() | p.joinStyle());-
909 s << (bool)(dd->cosmetic);-
910 }
never executed: end of block
0
911-
912 if (s.version() < 7) {
s.version() < 7Description
TRUEnever evaluated
FALSEnever evaluated
0
913 s << (quint8)p.width();-
914 s << p.color();-
915 } else {
never executed: end of block
0
916 s << double(p.widthF());-
917 s << p.brush();-
918 s << double(p.miterLimit());-
919 if (sizeof(qreal) == sizeof(double)) {
sizeof(qreal) ...sizeof(double)Description
TRUEnever evaluated
FALSEnever evaluated
0
920 s << p.dashPattern();-
921 } else {
never executed: end of block
0
922 // ensure that we write doubles here instead of streaming the pattern-
923 // directly; otherwise, platforms that redefine qreal might generate-
924 // data that cannot be read on other platforms.-
925 QVector<qreal> pattern = p.dashPattern();-
926 s << quint32(pattern.size());-
927 for (int i = 0; i < pattern.size(); ++i)
i < pattern.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
928 s << double(pattern.at(i));
never executed: s << double(pattern.at(i));
0
929 }
never executed: end of block
0
930 if (s.version() >= 9)
s.version() >= 9Description
TRUEnever evaluated
FALSEnever evaluated
0
931 s << double(p.dashOffset());
never executed: s << double(p.dashOffset());
0
932 if (s.version() >= QDataStream::Qt_5_0)
s.version() >=...Stream::Qt_5_0Description
TRUEnever evaluated
FALSEnever evaluated
0
933 s << bool(dd->defaultWidth);
never executed: s << bool(dd->defaultWidth);
0
934 }
never executed: end of block
0
935 return s;
never executed: return s;
0
936}-
937-
938/*!-
939 \fn QDataStream &operator>>(QDataStream &stream, QPen &pen)-
940 \relates QPen-
941-
942 Reads a pen from the given \a stream into the given \a pen and-
943 returns a reference to the \a stream.-
944-
945 \sa {Serializing Qt Data Types}-
946*/-
947-
948QDataStream &operator>>(QDataStream &s, QPen &p)-
949{-
950 quint16 style;-
951 quint8 width8 = 0;-
952 double width = 0;-
953 QColor color;-
954 QBrush brush;-
955 double miterLimit = 2;-
956 QVector<qreal> dashPattern;-
957 double dashOffset = 0;-
958 bool cosmetic = false;-
959 bool defaultWidth = false;-
960 if (s.version() < QDataStream::Qt_4_3) {
s.version() < ...Stream::Qt_4_3Description
TRUEnever evaluated
FALSEnever evaluated
0
961 quint8 style8;-
962 s >> style8;-
963 style = style8;-
964 } else {
never executed: end of block
0
965 s >> style;-
966 s >> cosmetic;-
967 }
never executed: end of block
0
968 if (s.version() < 7) {
s.version() < 7Description
TRUEnever evaluated
FALSEnever evaluated
0
969 s >> width8;-
970 s >> color;-
971 brush = color;-
972 width = width8;-
973 } else {
never executed: end of block
0
974 s >> width;-
975 s >> brush;-
976 s >> miterLimit;-
977 if (sizeof(qreal) == sizeof(double)) {
sizeof(qreal) ...sizeof(double)Description
TRUEnever evaluated
FALSEnever evaluated
0
978 s >> dashPattern;-
979 } else {
never executed: end of block
0
980 quint32 numDashes;-
981 s >> numDashes;-
982 double dash;-
983 dashPattern.reserve(numDashes);-
984 for (quint32 i = 0; i < numDashes; ++i) {
i < numDashesDescription
TRUEnever evaluated
FALSEnever evaluated
0
985 s >> dash;-
986 dashPattern << dash;-
987 }
never executed: end of block
0
988 }
never executed: end of block
0
989 if (s.version() >= 9)
s.version() >= 9Description
TRUEnever evaluated
FALSEnever evaluated
0
990 s >> dashOffset;
never executed: s >> dashOffset;
0
991 }
never executed: end of block
0
992-
993 if (s.version() >= QDataStream::Qt_5_0) {
s.version() >=...Stream::Qt_5_0Description
TRUEnever evaluated
FALSEnever evaluated
0
994 s >> defaultWidth;-
995 } else {
never executed: end of block
0
996 // best we can do for legacy pens-
997 defaultWidth = qFuzzyIsNull(width);-
998 }
never executed: end of block
0
999-
1000 p.detach();-
1001 QPenData *dd = static_cast<QPenData *>(p.d);-
1002 dd->width = width;-
1003 dd->brush = brush;-
1004 dd->style = Qt::PenStyle(style & Qt::MPenStyle);-
1005 dd->capStyle = Qt::PenCapStyle(style & Qt::MPenCapStyle);-
1006 dd->joinStyle = Qt::PenJoinStyle(style & Qt::MPenJoinStyle);-
1007 dd->dashPattern = dashPattern;-
1008 dd->miterLimit = miterLimit;-
1009 dd->dashOffset = dashOffset;-
1010 dd->cosmetic = cosmetic;-
1011 dd->defaultWidth = defaultWidth;-
1012-
1013 return s;
never executed: return s;
0
1014}-
1015#endif //QT_NO_DATASTREAM-
1016-
1017#ifndef QT_NO_DEBUG_STREAM-
1018QDebug operator<<(QDebug dbg, const QPen &p)-
1019{-
1020 const char *PEN_STYLES[] = {-
1021 "NoPen",-
1022 "SolidLine",-
1023 "DashLine",-
1024 "DotLine",-
1025 "DashDotLine",-
1026 "DashDotDotLine",-
1027 "CustomDashLine"-
1028 };-
1029-
1030 QDebugStateSaver saver(dbg);-
1031 dbg.nospace() << "QPen(" << p.width() << ',' << p.brush()-
1032 << ',' << PEN_STYLES[p.style()] << ',' << int(p.capStyle())-
1033 << ',' << int(p.joinStyle()) << ',' << p.dashPattern()-
1034 << ',' << p.dashOffset()-
1035 << ',' << p.miterLimit() << ')';-
1036 return dbg;
never executed: return dbg;
0
1037}-
1038#endif-
1039-
1040/*!-
1041 \fn DataPtr &QPen::data_ptr()-
1042 \internal-
1043*/-
1044-
1045/*!-
1046 \typedef QPen::DataPtr-
1047-
1048 \internal-
1049*/-
1050-
1051QT_END_NAMESPACE-
1052-
1053#undef QT_COMPILING_QPEN-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9