📄 qbrush.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.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 GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qbrush.h"#include "qpixmap.h"#include "qbitmap.h"#include "qpixmapcache.h"#include "qdatastream.h"#include "qvariant.h"#include "qline.h"#include "qdebug.h"const uchar *qt_patternForBrush(int brushStyle, bool invert){ Q_ASSERT(brushStyle > Qt::SolidPattern && brushStyle < Qt::LinearGradientPattern); if(invert) { static const uchar dense1_pat[] = { 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff }; static const uchar dense2_pat[] = { 0x77, 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff }; static const uchar dense3_pat[] = { 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55, 0xee }; static const uchar dense4_pat[] = { 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 }; static const uchar dense5_pat[] = { 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11 }; static const uchar dense6_pat[] = { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 }; static const uchar dense7_pat[] = { 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00 }; static const uchar hor_pat[] = { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }; static const uchar ver_pat[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 }; static const uchar cross_pat[] = { 0x10, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10, 0x10 }; static const uchar bdiag_pat[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; static const uchar fdiag_pat[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; static const uchar dcross_pat[] = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }; static const uchar *const pat_tbl[] = { dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat, dense6_pat, dense7_pat, hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat }; return pat_tbl[brushStyle - Qt::Dense1Pattern]; } static const uchar dense1_pat[] = { 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00 }; static const uchar dense2_pat[] = { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 }; static const uchar dense3_pat[] = { 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11 }; static const uchar dense4_pat[] = { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa }; static const uchar dense5_pat[] = { 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55, 0xee }; static const uchar dense6_pat[] = { 0x77, 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff }; static const uchar dense7_pat[] = { 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff }; static const uchar hor_pat[] = { 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff }; static const uchar ver_pat[] = { 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef }; static const uchar cross_pat[] = { 0xef, 0xef, 0xef, 0x00, 0xef, 0xef, 0xef, 0xef }; static const uchar bdiag_pat[] = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe }; static const uchar fdiag_pat[] = { 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f }; static const uchar dcross_pat[] = { 0x7e, 0xbd, 0xdb, 0xe7, 0xe7, 0xdb, 0xbd, 0x7e }; static const uchar *const pat_tbl[] = { dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat, dense6_pat, dense7_pat, hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat }; return pat_tbl[brushStyle - Qt::Dense1Pattern];}QPixmap qt_pixmapForBrush(int brushStyle, bool invert){ QPixmap pm; QString key = QLatin1String("$qt-brush$") + QString::number(brushStyle) + QString::number((int)invert); if (!QPixmapCache::find(key, pm)) { pm = QBitmap::fromData(QSize(8, 8), qt_patternForBrush(brushStyle, invert), QImage::Format_MonoLSB); QPixmapCache::insert(key, pm); } return pm;}Q_GUI_EXPORTQImage qt_imageForBrush(int brushStyle, bool invert){ QImage image(8, 8, QImage::Format_MonoLSB); const uchar *pattern = qt_patternForBrush(brushStyle, invert); for (int y=0; y<8; ++y) *image.scanLine(y) = pattern[y]; return image;}struct QTexturedBrushData : public QBrushData{ QTexturedBrushData() { m_has_pixmap_texture = false; m_pixmap = 0; } ~QTexturedBrushData() { delete m_pixmap; } void setPixmap(const QPixmap &pm) { delete m_pixmap; if (pm.isNull()) { m_pixmap = 0; m_has_pixmap_texture = false; } else { m_pixmap = new QPixmap(pm); m_has_pixmap_texture = true; } m_image = QImage(); } void setImage(const QImage &image) { m_image = image; delete m_pixmap; m_pixmap = 0; m_has_pixmap_texture = false; } QPixmap &pixmap() { if (!m_pixmap) { m_pixmap = new QPixmap(QPixmap::fromImage(m_image)); } return *m_pixmap; } QImage &image() { if (m_image.isNull() && m_pixmap) m_image = m_pixmap->toImage(); return m_image; } QPixmap *m_pixmap; QImage m_image; bool m_has_pixmap_texture;};// returns true if the brush has a pixmap (or bitmap) set as the// brush texture, false otherwisebool hasPixmapTexture(const QBrush& brush){ QTexturedBrushData *tx_data = static_cast<QTexturedBrushData *>(brush.d); return tx_data->m_has_pixmap_texture;}struct QGradientBrushData : public QBrushData{ QGradient gradient;};/*! \class QBrush \ingroup multimedia \ingroup shared \brief The QBrush class defines the fill pattern of shapes drawn by QPainter. A brush has a style, a color, a gradient and a texture. The brush style() defines the fill pattern using the Qt::BrushStyle enum. The default brush style is Qt::NoBrush (depending on how you construct a brush). This style tells the painter to not fill shapes. The standard style for filling is Qt::SolidPattern. The style can be set when the brush is created using the appropriate constructor, and in addition the setStyle() function provides means for altering the style once the brush is constructed. \image brush-styles.png Brush Styles The brush color() defines the color of the fill pattern. The color can either be one of Qt's predefined colors, Qt::GlobalColor, or any other custom QColor. The currently set color can be retrieved and altered using the color() and setColor() functions, respectively. The gradient() defines the gradient fill used when the current style is either Qt::LinearGradientPattern, Qt::RadialGradientPattern or Qt::ConicalGradientPattern. Gradient brushes are created by giving a QGradient as a constructor argument when creating the QBrush. Qt provides three different gradients: QLinearGradient, QConicalGradient, and QRadialGradient - all of which inherit QGradient. \quotefromfile snippets/brush/gradientcreationsnippet.cpp \skipto QRadialGradient \printuntil QBrush The texture() defines the pixmap used when the current style is Qt::TexturePattern. You can create a brush with a texture by providing the pixmap when the brush is created or by using setTexture(). Note that applying setTexture() makes style() == Qt::TexturePattern, regardless of previous style settings. Also, calling setColor() will not make a difference if the style is a gradient. The same is the case if the style is Qt::TexturePattern style unless the current texture is a QBitmap. The isOpaque() function returns true if the brush is fully opaque otherwise false. A brush is considered opaque if: \list \o The alpha component of the color() is 255. \o Its texture() does not have an alpha channel and is not a QBitmap. \o The colors in the gradient() all have an alpha component that is 255. \endlist \table 100% \row \o \inlineimage brush-outline.png Outlines \o To specify the style and color of lines and outlines, use the QPainter's \l {QPen}{pen} combined with Qt::PenStyle and Qt::GlobalColor: \code QPainter painter(this); painter.setBrush(Qt::cyan); painter.setPen(Qt::darkCyan); painter.drawRect(0, 0, 100,100); painter.setBrush(Qt::NoBrush); painter.setPen(Qt::darkGreen); painter.drawRect(40, 40, 100, 100); \endcode Note that, by default, QPainter renders the outline (using the currently set pen) when drawing shapes. Use \l {Qt::NoPen}{\c painter.setPen(Qt::NoPen)} to disable this behavior. \endtable For more information about painting in general, see \l{The Paint System} documentation. \sa Qt::BrushStyle, QPainter, QColor*/class QBrushStatic{public: QBrushData *pointer; bool destroyed; inline QBrushStatic() : pointer(0), destroyed(false) { } inline ~QBrushStatic() { if (!pointer->ref.deref()) delete pointer; pointer = 0; destroyed = true; }};static QBrushData *nullBrushInstance(){ static QBrushStatic defaultBrush; if (!defaultBrush.pointer && !defaultBrush.destroyed) { QBrushData *x = new QBrushData; x->ref = 1; x->style = Qt::BrushStyle(0); x->color = Qt::black; x->hasTransform = false; x->forceTextureClamp = false; if (!q_atomic_test_and_set_ptr(&defaultBrush.pointer, 0, x)) delete x; } return defaultBrush.pointer;}static bool qbrush_check_type(Qt::BrushStyle style) { switch (style) { case Qt::TexturePattern: qWarning("QBrush: Incorrect use of TexturePattern"); break; case Qt::LinearGradientPattern: case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: qWarning("QBrush: Wrong use of a gradient pattern"); break; default: return true; } return false;}/*! \internal Initializes the brush.*/void QBrush::init(const QColor &color, Qt::BrushStyle style){ switch(style) { case Qt::NoBrush: d = nullBrushInstance(); d->ref.ref(); return; case Qt::TexturePattern: d = new QTexturedBrushData; static_cast<QTexturedBrushData *>(d)->setPixmap(QPixmap()); break; case Qt::LinearGradientPattern: case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: d = new QGradientBrushData; break; default: d = new QBrushData; break; } d->ref = 1; d->style = style; d->color = color; d->hasTransform = false; d->forceTextureClamp = false;}/*! Constructs a default black brush with the style Qt::NoBrush (i.e. this brush will not fill shapes).*/QBrush::QBrush(){ d = nullBrushInstance(); Q_ASSERT(d); d->ref.ref();}/*! Constructs a brush with a black color and a texture set to the given \a pixmap. The style is set to Qt::TexturePattern. \sa setTexture()*/QBrush::QBrush(const QPixmap &pixmap){ init(Qt::black, Qt::TexturePattern); setTexture(pixmap);}/*! Constructs a brush with a black color and a texture set to the given \a image. The style is set to Qt::TexturePattern. \sa setTextureImage()*/QBrush::QBrush(const QImage &image){ init(Qt::black, Qt::TexturePattern); setTextureImage(image);}/*! Constructs a black brush with the given \a style. \sa setStyle()*/QBrush::QBrush(Qt::BrushStyle style){ if (qbrush_check_type(style)) init(Qt::black, style); else { d = nullBrushInstance(); d->ref.ref(); }}/*! Constructs a brush with the given \a color and \a style. \sa setColor(), setStyle()*/QBrush::QBrush(const QColor &color, Qt::BrushStyle style){ if (qbrush_check_type(style)) init(color, style); else { d = nullBrushInstance(); d->ref.ref(); }}/*! \fn QBrush::QBrush(Qt::GlobalColor color, Qt::BrushStyle style) Constructs a brush with the given \a color and \a style. \sa setColor(), setStyle()*/QBrush::QBrush(Qt::GlobalColor color, Qt::BrushStyle style){ if (qbrush_check_type(style)) init(color, style); else { d = nullBrushInstance(); d->ref.ref(); }}/*! Constructs a brush with the given \a color and the custom pattern stored in \a pixmap. The style is set to Qt::TexturePattern. The color will only have an effect for QBitmaps. \sa setColor(), setPixmap()*/QBrush::QBrush(const QColor &color, const QPixmap &pixmap){ init(color, Qt::TexturePattern); setTexture(pixmap);}/*! Constructs a brush with the given \a color and the custom pattern stored in \a pixmap. The style is set to Qt::TexturePattern. The color will only have an effect for QBitmaps. \sa setColor(), setPixmap()*/QBrush::QBrush(Qt::GlobalColor color, const QPixmap &pixmap){ init(color, Qt::TexturePattern); setTexture(pixmap);}/*! Constructs a copy of \a other.*/QBrush::QBrush(const QBrush &other){ d = other.d; d->ref.ref();}/*! Constructs a brush based on the given \a gradient. The brush style is set to the corresponding gradient style (either Qt::LinearGradientPattern, Qt::RadialGradientPattern or Qt::ConicalGradientPattern).*/QBrush::QBrush(const QGradient &gradient){ Q_ASSERT_X(gradient.type() != QGradient::NoGradient, "QBrush::QBrush", "QGradient should not be used directly, use the linear, radial\n" "or conical gradients instead"); const Qt::BrushStyle enum_table[] = { Qt::LinearGradientPattern, Qt::RadialGradientPattern, Qt::ConicalGradientPattern }; init(QColor(), enum_table[gradient.type()]); QGradientBrushData *grad = static_cast<QGradientBrushData *>(d); grad->gradient = gradient;}/*! Destroys the brush.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -