Make QPen default to 1-width non-cosmetic.
[profile/ivi/qtbase.git] / src / gui / painting / qpen.cpp
index 3654c6f..6b71c38 100644 (file)
@@ -1,38 +1,38 @@
 /****************************************************************************
 **
-** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the QtGui module of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
 ** GNU Lesser General Public License Usage
-** This file may be used under the terms of the GNU Lesser General Public
-** License version 2.1 as published by the Free Software Foundation and
-** appearing in the file LICENSE.LGPL included in the packaging of this
-** file. Please review the following information to ensure the GNU Lesser
-** General Public License version 2.1 requirements will be met:
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
-** In addition, as a special exception, Nokia gives you certain additional
-** rights. These rights are described in the Nokia Qt LGPL Exception
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU General
-** Public License version 3.0 as published by the Free Software Foundation
-** and appearing in the file LICENSE.GPL included in the packaging of this
-** file. Please review the following information to ensure the GNU General
-** Public License version 3.0 requirements will be met:
-** http://www.gnu.org/copyleft/gpl.html.
-**
-** Other Usage
-** Alternatively, this file may be used in accordance with the terms and
-** conditions contained in a signed written agreement between you and Nokia.
-**
-**
-**
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
 **
 **
 ** $QT_END_LICENSE$
@@ -52,6 +52,7 @@ typedef QPenPrivate QPenData;
 
 /*!
     \class QPen
+    \inmodule QtGui
     \ingroup painting
     \ingroup shared
 
@@ -79,13 +80,13 @@ typedef QPenPrivate QPenData;
 
     For example:
 
-    \snippet doc/src/snippets/code/src_gui_painting_qpen.cpp 0
+    \snippet code/src_gui_painting_qpen.cpp 0
 
     which is equivalent to
 
-    \snippet doc/src/snippets/code/src_gui_painting_qpen.cpp 1
+    \snippet code/src_gui_painting_qpen.cpp 1
 
-    The default pen is a solid black brush with 0 width, square
+    The default pen is a solid black brush with 1 width, square
     cap style (Qt::SquareCap), and  bevel join style (Qt::BevelJoin).
 
     In addition QPen provides the color() and setColor()
@@ -104,21 +105,21 @@ typedef QPenPrivate QPenData;
 
     \table
     \row
-    \o \inlineimage qpen-solid.png
-    \o \inlineimage qpen-dash.png
-    \o \inlineimage qpen-dot.png
+    \li \inlineimage qpen-solid.png
+    \li \inlineimage qpen-dash.png
+    \li \inlineimage qpen-dot.png
     \row
-    \o Qt::SolidLine
-    \o Qt::DashLine
-    \o Qt::DotLine
+    \li Qt::SolidLine
+    \li Qt::DashLine
+    \li Qt::DotLine
     \row
-    \o \inlineimage qpen-dashdot.png
-    \o \inlineimage qpen-dashdotdot.png
-    \o \inlineimage qpen-custom.png
+    \li \inlineimage qpen-dashdot.png
+    \li \inlineimage qpen-dashdotdot.png
+    \li \inlineimage qpen-custom.png
     \row
-    \o Qt::DashDotLine
-    \o Qt::DashDotDotLine
-    \o Qt::CustomDashLine
+    \li Qt::DashDotLine
+    \li Qt::DashDotDotLine
+    \li Qt::CustomDashLine
     \endtable
 
     Simply use the setStyle() function to convert the pen style to
@@ -135,7 +136,7 @@ typedef QPenPrivate QPenData;
     spaces. For example, the custom pattern shown above is created
     using the following code:
 
-    \snippet doc/src/snippets/code/src_gui_painting_qpen.cpp 2
+    \snippet code/src_gui_painting_qpen.cpp 2
 
     Note that the dash pattern is specified in units of the pens
     width, e.g. a dash of length 5 in width 10 is 50 pixels long.
@@ -153,13 +154,13 @@ typedef QPenPrivate QPenData;
 
     \table
     \row
-    \o \inlineimage qpen-square.png
-    \o \inlineimage qpen-flat.png
-    \o \inlineimage qpen-roundcap.png
+    \li \inlineimage qpen-square.png
+    \li \inlineimage qpen-flat.png
+    \li \inlineimage qpen-roundcap.png
     \row
-    \o Qt::SquareCap
-    \o Qt::FlatCap
-    \o Qt::RoundCap
+    \li Qt::SquareCap
+    \li Qt::FlatCap
+    \li Qt::RoundCap
     \endtable
 
     The Qt::SquareCap style is a square line end that covers the end
@@ -183,13 +184,13 @@ typedef QPenPrivate QPenData;
 
     \table
     \row
-    \o \inlineimage qpen-bevel.png
-    \o \inlineimage qpen-miter.png
-    \o \inlineimage qpen-roundjoin.png
+    \li \inlineimage qpen-bevel.png
+    \li \inlineimage qpen-miter.png
+    \li \inlineimage qpen-roundjoin.png
     \row
-    \o Qt::BevelJoin
-    \o Qt::MiterJoin
-    \o Qt::RoundJoin
+    \li Qt::BevelJoin
+    \li Qt::MiterJoin
+    \li Qt::RoundJoin
     \endtable
 
     The Qt::BevelJoin style fills the triangular notch between the two
@@ -213,8 +214,8 @@ typedef QPenPrivate QPenData;
 
     \table 100%
     \row
-    \o \inlineimage qpen-demo.png
-    \o \bold {\l {painting/pathstroke}{The Path Stroking Example}}
+    \li \inlineimage qpen-demo.png
+    \li \b {\l {painting/pathstroke}{The Path Stroking Example}}
 
     The Path Stroking example shows Qt's built-in dash patterns and shows
     how custom patterns can be used to extend the range of available
@@ -229,9 +230,9 @@ typedef QPenPrivate QPenData;
   \internal
 */
 inline QPenPrivate::QPenPrivate(const QBrush &_brush, qreal _width, Qt::PenStyle penStyle,
-                                Qt::PenCapStyle _capStyle, Qt::PenJoinStyle _joinStyle)
+                                Qt::PenCapStyle _capStyle, Qt::PenJoinStyle _joinStyle, bool _defaultWidth)
     : ref(1), dashOffset(0), miterLimit(2),
-      cosmetic(false)
+      cosmetic(false), defaultWidth(_defaultWidth)
 {
     width = _width;
     brush = _brush;
@@ -260,12 +261,12 @@ public:
 };
 
 Q_GLOBAL_STATIC_WITH_ARGS(QPenDataHolder, defaultPenInstance,
-                          (Qt::black, 0, Qt::SolidLine, qpen_default_cap, qpen_default_join))
+                          (Qt::black, 1, Qt::SolidLine, qpen_default_cap, qpen_default_join))
 Q_GLOBAL_STATIC_WITH_ARGS(QPenDataHolder, nullPenInstance,
-                          (Qt::black, 0, Qt::NoPen, qpen_default_cap, qpen_default_join))
+                          (Qt::black, 1, Qt::NoPen, qpen_default_cap, qpen_default_join))
 
 /*!
-    Constructs a default black solid line pen with 0 width.
+    Constructs a default black solid line pen with 1 width.
 */
 
 QPen::QPen()
@@ -275,7 +276,7 @@ QPen::QPen()
 }
 
 /*!
-    Constructs a black pen with 0 width and the given \a style.
+    Constructs a black pen with 1 width and the given \a style.
 
     \sa setStyle()
 */
@@ -286,20 +287,20 @@ QPen::QPen(Qt::PenStyle style)
         d = nullPenInstance()->pen;
         d->ref.ref();
     } else {
-        d = new QPenData(Qt::black, 0, style, qpen_default_cap, qpen_default_join);
+        d = new QPenData(Qt::black, 1, style, qpen_default_cap, qpen_default_join);
     }
 }
 
 
 /*!
-    Constructs a solid line pen with 0 width and the given \a color.
+    Constructs a solid line pen with 1 width and the given \a color.
 
     \sa setBrush(), setColor()
 */
 
 QPen::QPen(const QColor &color)
 {
-    d = new QPenData(color, 0, Qt::SolidLine, qpen_default_cap, qpen_default_join);
+    d = new QPenData(color, 1, Qt::SolidLine, qpen_default_cap, qpen_default_join);
 }
 
 
@@ -309,12 +310,12 @@ QPen::QPen(const QColor &color)
     Constructs a pen with the specified \a brush, \a width, pen \a style,
     \a cap style and \a join style.
 
-    \sa setBrush(), setWidth(), setStyle(),  setCapStyle(), setJoinStyle()
+    \sa setBrush(), setWidth(), setStyle(), setCapStyle(), setJoinStyle()
 */
 
 QPen::QPen(const QBrush &brush, qreal width, Qt::PenStyle s, Qt::PenCapStyle c, Qt::PenJoinStyle j)
 {
-    d = new QPenData(brush, width, s, c, j);
+    d = new QPenData(brush, width, s, c, j, false);
 }
 
 /*!
@@ -474,9 +475,9 @@ QVector<qreal> QPen::dashPattern() const
 
     \table 100%
     \row
-    \o \inlineimage qpen-custom.png
-    \o
-    \snippet doc/src/snippets/code/src_gui_painting_qpen.cpp 3
+    \li \inlineimage qpen-custom.png
+    \li
+    \snippet code/src_gui_painting_qpen.cpp 3
     \endtable
 
     The dash pattern is specified in units of the pens width; e.g. a
@@ -527,8 +528,8 @@ qreal QPen::dashOffset() const
     to specify the dash pattern.
 
     \table
-    \row \o \inlineimage qpen-dashpattern.png
-    \o For example, a pattern where each stroke is four units long, followed by a gap
+    \row \li \inlineimage qpen-dashpattern.png
+    \li For example, a pattern where each stroke is four units long, followed by a gap
     of two units, will begin with the stroke when drawn as a line.
 
     However, if the dash offset is set to 4.0, any line drawn will begin with the gap.
@@ -556,7 +557,7 @@ void QPen::setDashOffset(qreal offset)
     Returns the miter limit of the pen. The miter limit is only
     relevant when the join style is set to Qt::MiterJoin.
 
-    \sa setMiterLimit(),  {QPen#Join Style}{Join Style}
+    \sa setMiterLimit(), {QPen#Join Style}{Join Style}
 */
 qreal QPen::miterLimit() const
 {
@@ -606,7 +607,7 @@ int QPen::width() const
 
     Returns the pen width with floating point precision.
 
-    \sa setWidthF() width()
+    \sa setWidthF(), width()
 */
 qreal QPen::widthF() const
 {
@@ -649,17 +650,20 @@ void QPen::setWidth(int width)
 
     Setting a pen width with a negative value is not supported.
 
-    \sa setWidth() widthF()
+    \sa setWidth(), widthF()
 */
 
 void QPen::setWidthF(qreal width)
 {
-    if (width < 0.f)
+    if (width < 0.f) {
         qWarning("QPen::setWidthF: Setting a pen width with a negative value is not defined");
+        return;
+    }
     if (qAbs(d->width - width) < 0.00000001f)
         return;
     detach();
     d->width = width;
+    d->defaultWidth = false;
 }
 
 
@@ -693,7 +697,7 @@ void QPen::setCapStyle(Qt::PenCapStyle c)
 /*!
     Returns the pen's join style.
 
-    \sa setJoinStyle(),  {QPen#Join Style}{Join Style}
+    \sa setJoinStyle(), {QPen#Join Style}{Join Style}
 */
 Qt::PenJoinStyle QPen::joinStyle() const
 {
@@ -784,8 +788,7 @@ bool QPen::isSolid() const
     used with. Drawing a shape with a cosmetic pen ensures that its
     outline will have the same thickness at different scale factors.
 
-    A zero width pen is cosmetic by default; pens with a non-zero width
-    are non-cosmetic.
+    A zero width pen is cosmetic by default.
 
     \sa setCosmetic(), widthF()
 */
@@ -847,7 +850,8 @@ bool QPen::operator==(const QPen &p) const
                 || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) &&
                     pdd->dashPattern == dd->dashPattern))
             && p.d->brush == d->brush
-            && pdd->cosmetic == dd->cosmetic);
+            && pdd->cosmetic == dd->cosmetic
+            && pdd->defaultWidth == dd->defaultWidth);
 }
 
 
@@ -909,6 +913,8 @@ QDataStream &operator<<(QDataStream &s, const QPen &p)
         }
         if (s.version() >= 9)
             s << double(p.dashOffset());
+        if (s.version() >= QDataStream::Qt_5_0)
+            s << bool(dd->defaultWidth);
     }
     return s;
 }
@@ -934,6 +940,7 @@ QDataStream &operator>>(QDataStream &s, QPen &p)
     QVector<qreal> dashPattern;
     double dashOffset = 0;
     bool cosmetic = false;
+    bool defaultWidth = false;
     if (s.version() < QDataStream::Qt_4_3) {
         quint8 style8;
         s >> style8;
@@ -966,6 +973,13 @@ QDataStream &operator>>(QDataStream &s, QPen &p)
             s >> dashOffset;
     }
 
+    if (s.version() >= QDataStream::Qt_5_0) {
+        s >> defaultWidth;
+    } else {
+        // best we can do for legacy pens
+        defaultWidth = qFuzzyIsNull(width);
+    }
+
     p.detach();
     QPenData *dd = static_cast<QPenData *>(p.d);
     dd->width = width;
@@ -977,6 +991,7 @@ QDataStream &operator>>(QDataStream &s, QPen &p)
     dd->miterLimit = miterLimit;
     dd->dashOffset = dashOffset;
     dd->cosmetic = cosmetic;
+    dd->defaultWidth = defaultWidth;
 
     return s;
 }
@@ -985,7 +1000,6 @@ QDataStream &operator>>(QDataStream &s, QPen &p)
 #ifndef QT_NO_DEBUG_STREAM
 QDebug operator<<(QDebug dbg, const QPen &p)
 {
-#ifndef Q_BROKEN_DEBUG_STREAM
     const char *PEN_STYLES[] = {
         "NoPen",
         "SolidLine",
@@ -1002,11 +1016,6 @@ QDebug operator<<(QDebug dbg, const QPen &p)
                   << ',' << p.dashOffset()
                   << ',' << p.miterLimit() << ')';
     return dbg.space();
-#else
-    qWarning("This compiler doesn't support streaming QPen to QDebug");
-    return dbg;
-    Q_UNUSED(p);
-#endif
 }
 #endif