Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qpen.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#include "qpen.h"
4#include "qpen_p.h"
5#include "qdatastream.h"
6#include "qvariant.h"
7#include "qbrush.h"
8
9#include <qdebug.h>
10
12
192QPenPrivate::QPenPrivate(const QBrush &_brush, qreal _width, Qt::PenStyle penStyle,
193 Qt::PenCapStyle _capStyle, Qt::PenJoinStyle _joinStyle)
194 : dashOffset(0), miterLimit(2),
195 cosmetic(false)
196{
197 width = _width;
198 brush = _brush;
199 style = penStyle;
200 capStyle = _capStyle;
201 joinStyle = _joinStyle;
202}
203
206
208{
209public:
212 Qt::PenCapStyle penCapStyle, Qt::PenJoinStyle _joinStyle)
213 : pen(new QPenPrivate(brush, width, penStyle, penCapStyle, _joinStyle))
214 { }
215 ~QPenDataHolder() = default;
216 Q_DISABLE_COPY_MOVE(QPenDataHolder)
217};
218
223
229{
230 d = defaultPenInstance()->pen;
231}
232
240{
241 if (style == Qt::NoPen) {
242 d = nullPenInstance()->pen;
243 } else {
245 }
246}
247
248
259
260
274
281QPen::QPen(const QPen &p) noexcept
282 : d(p.d)
283{
284}
285
286
302QPen::~QPen() = default;
303
305
306
316void QPen::detach()
317{
318 d.detach();
319}
320
321
329QPen &QPen::operator=(const QPen &p) noexcept
330{
331 QPen(p).swap(*this);
332 return *this;
333}
334
354QPen::operator QVariant() const
355{
356 return QVariant::fromValue(*this);
357}
358
367{
368 return d->style;
369}
386{
387 if (d->style == s)
388 return;
389 detach();
390 d->style = s;
391 d->dashPattern.clear();
392 d->dashOffset = 0;
393}
394
400QList<qreal> QPen::dashPattern() const
401{
402 if (d->style == Qt::SolidLine || d->style == Qt::NoPen) {
403 return QList<qreal>();
404 } else if (d->dashPattern.isEmpty()) {
405 const qreal space = 2;
406 const qreal dot = 1;
407 const qreal dash = 4;
408
409 switch (d->style) {
410 case Qt::DashLine:
411 d->dashPattern.reserve(2);
412 d->dashPattern << dash << space;
413 break;
414 case Qt::DotLine:
415 d->dashPattern.reserve(2);
416 d->dashPattern << dot << space;
417 break;
418 case Qt::DashDotLine:
419 d->dashPattern.reserve(4);
420 d->dashPattern << dash << space << dot << space;
421 break;
423 d->dashPattern.reserve(6);
424 d->dashPattern << dash << space << dot << space << dot << space;
425 break;
426 default:
427 break;
428 }
429 }
430 return d->dashPattern;
431}
432
463void QPen::setDashPattern(const QList<qreal> &pattern)
464{
465 if (pattern.isEmpty())
466 return;
467 detach();
468
469 d->dashPattern = pattern;
471
472 if ((d->dashPattern.size() % 2) == 1) {
473 qWarning("QPen::setDashPattern: Pattern not of even length");
474 d->dashPattern << 1;
475 }
476}
477
478
485{
486 return d->dashOffset;
487}
507{
509 return;
510 detach();
511 d->dashOffset = offset;
512 if (d->style != Qt::CustomDashLine) {
515 }
516}
517
525{
526 return d->miterLimit;
527}
528
546{
547 detach();
548 d->miterLimit = limit;
549}
550
551
560int QPen::width() const
561{
562 return qRound(d->width);
563}
564
573{
574 return d->width;
575}
576
593{
594 if (width < 0 || width >= (1 << 15)) {
595 qWarning("QPen::setWidth: Setting a pen width that is out of range");
596 return;
597 }
598 if ((qreal)width == d->width)
599 return;
600 detach();
601 d->width = width;
602}
603
619{
620 if (width < 0.f || width >= (1 << 15)) {
621 qWarning("QPen::setWidthF: Setting a pen width that is out of range");
622 return;
623 }
624 if (qAbs(d->width - width) < 0.00000001f)
625 return;
626 detach();
627 d->width = width;
628}
629
630
637{
638 return d->capStyle;
639}
640
651{
652 if (d->capStyle == c)
653 return;
654 detach();
655 d->capStyle = c;
656}
657
664{
665 return d->joinStyle;
666}
667
678{
679 if (d->joinStyle == j)
680 return;
681 detach();
682 d->joinStyle = j;
683}
684
693{
694 return d->brush.color();
695}
696
706{
707 detach();
708 d->brush = QBrush(c);
709}
710
711
716{
717 return d->brush;
718}
719
727{
728 detach();
729 d->brush = brush;
730}
731
732
738bool QPen::isSolid() const
739{
740 return d->brush.style() == Qt::SolidPattern;
741}
742
743
758{
759 return (d->cosmetic == true) || d->width == 0;
760}
761
762
770void QPen::setCosmetic(bool cosmetic)
771{
772 detach();
773 d->cosmetic = cosmetic;
774}
775
776
777
798bool QPen::operator==(const QPen &p) const
799{
800 return (p.d == d)
801 || (p.d->style == d->style
802 && p.d->capStyle == d->capStyle
803 && p.d->joinStyle == d->joinStyle
804 && p.d->width == d->width
805 && p.d->miterLimit == d->miterLimit
806 && (d->style != Qt::CustomDashLine
807 || (qFuzzyCompare(p.d->dashOffset, d->dashOffset) &&
808 p.d->dashPattern == d->dashPattern))
809 && p.d->brush == d->brush
810 && p.d->cosmetic == d->cosmetic);
811}
812
813
821{
822 return d->ref.loadRelaxed() == 1;
823}
824
825
826/*****************************************************************************
827 QPen stream functions
828 *****************************************************************************/
829#ifndef QT_NO_DATASTREAM
841{
842 if (s.version() < 3) {
843 s << (quint8)p.style();
844 } else if (s.version() < QDataStream::Qt_4_3) {
845 s << (quint8)(uint(p.style()) | uint(p.capStyle()) | uint(p.joinStyle()));
846 } else {
847 s << (quint16)(uint(p.style()) | uint(p.capStyle()) | uint(p.joinStyle()));
848 s << (bool)(p.d->cosmetic);
849 }
850
851 if (s.version() < 7) {
852 s << (quint8)p.width();
853 s << p.color();
854 } else {
855 s << double(p.widthF());
856 s << p.brush();
857 s << double(p.miterLimit());
858 if (sizeof(qreal) == sizeof(double)) {
859 s << p.dashPattern();
860 } else {
861 // ensure that we write doubles here instead of streaming the pattern
862 // directly; otherwise, platforms that redefine qreal might generate
863 // data that cannot be read on other platforms.
864 QList<qreal> pattern = p.dashPattern();
865 s << quint32(pattern.size());
866 for (int i = 0; i < pattern.size(); ++i)
867 s << double(pattern.at(i));
868 }
869 if (s.version() >= 9)
870 s << double(p.dashOffset());
871 if (s.version() >= QDataStream::Qt_5_0)
872 s << bool(qFuzzyIsNull(p.widthF()));
873 }
874 return s;
875}
876
888{
889 quint16 style;
890 quint8 width8 = 0;
891 double width = 0;
894 double miterLimit = 2;
895 QList<qreal> dashPattern;
896 double dashOffset = 0;
897 bool cosmetic = false;
898 bool defaultWidth;
899 if (s.version() < QDataStream::Qt_4_3) {
900 quint8 style8;
901 s >> style8;
902 style = style8;
903 } else {
904 s >> style;
905 s >> cosmetic;
906 }
907 if (s.version() < 7) {
908 s >> width8;
909 s >> color;
910 brush = color;
911 width = width8;
912 } else {
913 s >> width;
914 s >> brush;
915 s >> miterLimit;
916 if (sizeof(qreal) == sizeof(double)) {
917 s >> dashPattern;
918 } else {
919 quint32 numDashes;
920 s >> numDashes;
921 double dash;
922 dashPattern.reserve(numDashes);
923 for (quint32 i = 0; i < numDashes; ++i) {
924 s >> dash;
925 dashPattern << dash;
926 }
927 }
928 if (s.version() >= 9)
929 s >> dashOffset;
930 }
931
932 if (s.version() >= QDataStream::Qt_5_0) {
933 s >> defaultWidth;
934 }
935
936 p.detach();
937 p.d->width = width;
938 p.d->brush = brush;
939 p.d->style = Qt::PenStyle(style & Qt::MPenStyle);
940 p.d->capStyle = Qt::PenCapStyle(style & Qt::MPenCapStyle);
941 p.d->joinStyle = Qt::PenJoinStyle(style & Qt::MPenJoinStyle);
942 p.d->dashPattern = dashPattern;
943 p.d->miterLimit = miterLimit;
944 p.d->dashOffset = dashOffset;
945 p.d->cosmetic = cosmetic;
946
947 return s;
948}
949#endif //QT_NO_DATASTREAM
950
951#ifndef QT_NO_DEBUG_STREAM
953{
954 const char *PEN_STYLES[] = {
955 "NoPen",
956 "SolidLine",
957 "DashLine",
958 "DotLine",
959 "DashDotLine",
960 "DashDotDotLine",
961 "CustomDashLine"
962 };
963
964 QDebugStateSaver saver(dbg);
965 dbg.nospace() << "QPen(" << p.width() << ',' << p.brush()
966 << ',' << PEN_STYLES[p.style()] << ',' << int(p.capStyle())
967 << ',' << int(p.joinStyle()) << ',' << p.dashPattern()
968 << ',' << p.dashOffset()
969 << ',' << p.miterLimit() << ')';
970 return dbg;
971}
972#endif
973
986
987#undef QT_COMPILING_QPEN
T loadRelaxed() const noexcept
\inmodule QtGui
Definition qbrush.h:30
const QColor & color() const
Returns the brush color.
Definition qbrush.h:121
Qt::BrushStyle style() const
Returns the brush style.
Definition qbrush.h:120
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
void reserve(qsizetype size)
Definition qlist.h:753
void clear()
Definition qlist.h:434
QPenDataHolder(const QBrush &brush, qreal width, Qt::PenStyle penStyle, Qt::PenCapStyle penCapStyle, Qt::PenJoinStyle _joinStyle)
Definition qpen.cpp:211
QPen::DataPtr pen
Definition qpen.cpp:210
~QPenDataHolder()=default
qreal dashOffset
Definition qpen_p.h:34
qreal width
Definition qpen_p.h:28
Qt::PenStyle style
Definition qpen_p.h:30
QPenPrivate(const QBrush &brush, qreal width, Qt::PenStyle, Qt::PenCapStyle, Qt::PenJoinStyle _joinStyle)
Definition qpen.cpp:192
Qt::PenCapStyle capStyle
Definition qpen_p.h:31
QBrush brush
Definition qpen_p.h:29
QList< qreal > dashPattern
Definition qpen_p.h:33
Qt::PenJoinStyle joinStyle
Definition qpen_p.h:32
uint cosmetic
Definition qpen_p.h:36
qreal miterLimit
Definition qpen_p.h:35
\inmodule QtGui
Definition qpen.h:28
void setCapStyle(Qt::PenCapStyle pcs)
Sets the pen's cap style to the given style.
Definition qpen.cpp:650
qreal widthF() const
Returns the pen width with floating point precision.
Definition qpen.cpp:572
void setStyle(Qt::PenStyle)
[0]
QList< qreal > dashPattern() const
Returns the dash pattern of this pen.
Definition qpen.cpp:400
QPen()
Constructs a default black solid line pen with 1 width.
Definition qpen.cpp:228
bool isCosmetic() const
Returns true if the pen is cosmetic; otherwise returns false.
Definition qpen.cpp:757
void setWidth(int width)
Sets the pen width to the given width in pixels with integer precision.
Definition qpen.cpp:592
QColor color() const
Returns the color of this pen's brush.
Definition qpen.cpp:692
~QPen()
Destroys the pen.
void setWidthF(qreal width)
Sets the pen width to the given width in pixels with floating point precision.
Definition qpen.cpp:618
int width() const
Returns the pen width with integer precision.
Definition qpen.cpp:560
Qt::PenCapStyle capStyle() const
Returns the pen's cap style.
Definition qpen.cpp:636
void swap(QPen &other) noexcept
Definition qpen.h:42
void setColor(const QColor &color)
Sets the color of this pen's brush to the given color.
Definition qpen.cpp:705
void setBrush(const QBrush &brush)
Sets the brush used to fill strokes generated with this pen to the given brush.
Definition qpen.cpp:726
void setJoinStyle(Qt::PenJoinStyle pcs)
Sets the pen's join style to the given style.
Definition qpen.cpp:677
void setDashOffset(qreal doffset)
Sets the dash offset (the starting point on the dash pattern) for this pen to the offset specified.
Definition qpen.cpp:506
bool isDetached()
Definition qpen.cpp:820
void setMiterLimit(qreal limit)
Sets the miter limit of this pen to the given limit.
Definition qpen.cpp:545
QPen & operator=(const QPen &pen) noexcept
Assigns the given pen to this pen and returns a reference to this pen.
Definition qpen.cpp:329
bool operator==(const QPen &p) const
Returns true if the pen is equal to the given pen; otherwise false.
Definition qpen.cpp:798
bool isSolid() const
Returns true if the pen has a solid fill, otherwise false.
Definition qpen.cpp:738
Qt::PenJoinStyle joinStyle() const
Returns the pen's join style.
Definition qpen.cpp:663
void setCosmetic(bool cosmetic)
Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic.
Definition qpen.cpp:770
void setDashPattern(const QList< qreal > &pattern)
Sets the dash pattern for this pen to the given pattern.
Definition qpen.cpp:463
qreal miterLimit() const
Returns the miter limit of the pen.
Definition qpen.cpp:524
QBrush brush() const
Returns the brush used to fill strokes generated with this pen.
Definition qpen.cpp:715
qreal dashOffset() const
Returns the dash offset for the pen.
Definition qpen.cpp:484
Qt::PenStyle style() const
Returns the pen style.
Definition qpen.cpp:366
QAtomicInt ref
Definition qshareddata.h:21
\inmodule QtCore
Definition qvariant.h:65
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
Combined button and popup list for selecting options.
Definition qcompare.h:63
@ black
Definition qnamespace.h:30
@ CustomDashLine
@ DashDotDotLine
@ DotLine
@ SolidLine
@ MPenStyle
@ DashDotLine
@ DashLine
@ NoPen
PenJoinStyle
@ BevelJoin
@ MPenJoinStyle
@ SolidPattern
PenCapStyle
@ SquareCap
@ MPenCapStyle
Definition brush.cpp:5
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
#define qWarning
Definition qlogging.h:166
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLint GLsizei width
GLuint color
[2]
GLenum GLuint GLintptr offset
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLint limit
GLfloat GLfloat p
[1]
GLubyte * pattern
static qreal dot(const QPointF &a, const QPointF &b)
static const Qt::PenCapStyle qpen_default_cap
Definition qpen.cpp:204
static const Qt::PenJoinStyle qpen_default_join
Definition qpen.cpp:205
QDataStream & operator>>(QDataStream &s, QPen &p)
Definition qpen.cpp:887
nullPenInstance
Definition qpen.cpp:221
QDataStream & operator<<(QDataStream &s, const QPen &p)
Definition qpen.cpp:840
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
unsigned int quint32
Definition qtypes.h:50
unsigned short quint16
Definition qtypes.h:48
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
unsigned char quint8
Definition qtypes.h:46
QGraphicsSvgItem * black