📄 qbrush.cpp
字号:
QBrush::~QBrush(){ if (!d->ref.deref()) cleanUp(d);}void QBrush::cleanUp(QBrushData *x){ switch (x->style) { case Qt::TexturePattern: delete static_cast<QTexturedBrushData*>(x); break; case Qt::LinearGradientPattern: case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: delete static_cast<QGradientBrushData*>(x); break; default: delete x; }}void QBrush::detach(Qt::BrushStyle newStyle){ if (newStyle == d->style && d->ref == 1) return; QBrushData *x; switch(newStyle) { case Qt::TexturePattern: { QTexturedBrushData *tbd = new QTexturedBrushData; tbd->setPixmap(d->style == Qt::TexturePattern ? static_cast<QTexturedBrushData *>(d)->pixmap() : QPixmap()); x = tbd; break; } case Qt::LinearGradientPattern: case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: x = new QGradientBrushData; static_cast<QGradientBrushData *>(x)->gradient = static_cast<QGradientBrushData *>(d)->gradient; break; default: x = new QBrushData; break; } x->ref = 1; x->style = newStyle; x->color = d->color; x->transform = d->transform; x->hasTransform = d->hasTransform; x->forceTextureClamp = d->forceTextureClamp; x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) cleanUp(x);}/*! \fn QBrush &QBrush::operator=(const QBrush &brush) Assigns the given \a brush to \e this brush and returns a reference to \e this brush.*/QBrush &QBrush::operator=(const QBrush &b){ QBrushData *x = b.d; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) cleanUp(x); return *this;}/*! Returns the brush as a QVariant*/QBrush::operator QVariant() const{ return QVariant(QVariant::Brush, this);}/*! \fn Qt::BrushStyle QBrush::style() const Returns the brush style. \sa setStyle()*//*! Sets the brush style to \a style. \sa style()*/void QBrush::setStyle(Qt::BrushStyle style){ if (d->style == style) return; if (qbrush_check_type(style)) { detach(style); d->style = style; }}/*! \fn const QColor &QBrush::color() const Returns the brush color. \sa setColor()*//*! \fn void QBrush::setColor(const QColor &color) Sets the brush color to the given \a color. Note that 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. \sa color()*/void QBrush::setColor(const QColor &c){ detach(d->style); d->color = c;}/*! \fn void QBrush::setColor(Qt::GlobalColor color) \overload Sets the brush color to the given \a color.*/#ifdef QT3_SUPPORT/*! \fn void QBrush::setPixmap(const QPixmap &pixmap) \compat Sets a custom pattern for this brush. Use setTexture() instead.*//*! \fn QPixmap *QBrush::pixmap() const Returns a pointer to the custom brush pattern. Use texture() instead.*/QPixmap *QBrush::pixmap() const{ if (d->style != Qt::TexturePattern) return 0; QTexturedBrushData *data = static_cast<QTexturedBrushData*>(d); QPixmap &pixmap = data->pixmap(); return pixmap.isNull() ? 0 : &pixmap;}#endif/*! \fn QPixmap QBrush::texture() const Returns the custom brush pattern, or a null pixmap if no custom brush pattern has been set. \sa setTexture()*/QPixmap QBrush::texture() const{ return d->style == Qt::TexturePattern ? ((QTexturedBrushData*) d)->pixmap() : QPixmap();}/*! Sets the brush pixmap to \a pixmap. The style is set to Qt::TexturePattern. The current brush color will only have an effect for monochrome pixmaps, i.e. for QPixmap::depth() == 1 (\l {QBitmap}{QBitmaps}). \sa texture()*/void QBrush::setTexture(const QPixmap &pixmap){ if (!pixmap.isNull()) { detach(Qt::TexturePattern); QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d); data->setPixmap(pixmap); } else { detach(Qt::NoBrush); }}/*! \since 4.2 Returns the custom brush pattern, or a null image if no custom brush pattern has been set. If the texture was set as a QPixmap it will be converted to a QImage. \sa setTextureImage()*/QImage QBrush::textureImage() const{ return d->style == Qt::TexturePattern ? ((QTexturedBrushData *) d)->image() : QImage();}/*! \since 4.2 Sets the brush image to \a image. The style is set to Qt::TexturePattern. The current brush color will only have an effect for monochrome images, i.e. for QImage::depth() == 1. \sa textureImage()*/void QBrush::setTextureImage(const QImage &image){ if (!image.isNull()) { detach(Qt::TexturePattern); QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d); data->setImage(image); } else { detach(Qt::NoBrush); }}/*! Returns the gradient describing this brush.*/const QGradient *QBrush::gradient() const{ if (d->style == Qt::LinearGradientPattern || d->style == Qt::RadialGradientPattern || d->style == Qt::ConicalGradientPattern) { return &static_cast<const QGradientBrushData *>(d)->gradient; } return 0;}/*! Returns true if the brush is fully opaque otherwise false. A brush is considered opaque if: \list \i The alpha component of the color() is 255. \i Its texture() does not have an alpha channel and is not a QBitmap. \i The colors in the gradient() all have an alpha component that is 255. \endlist*/bool QBrush::isOpaque() const{ bool opaqueColor = d->color.alpha() == 255; // Test awfully simple case first if (d->style == Qt::SolidPattern) return opaqueColor; if (d->style == Qt::LinearGradientPattern || d->style == Qt::RadialGradientPattern || d->style == Qt::ConicalGradientPattern) { QGradientStops stops = gradient()->stops(); for (int i=0; i<stops.size(); ++i) if (stops.at(i).second.alpha() != 255) return false; return true; } else if (d->style == Qt::TexturePattern) { return !texture().hasAlpha(); } return false;}/*! \since 4.2 Sets \a matrix as an explicit transformation matrix on the current brush. The brush transformation matrix is merged with QPainter transformation matrix to produce the final result. \sa matrix()*/void QBrush::setMatrix(const QMatrix &matrix){ setTransform(QTransform(matrix));}/*! \since 4.3 Sets \a matrix as an explicit transformation matrix on the current brush. The brush transformation matrix is merged with QPainter transformation matrix to produce the final result. \sa transform()*/void QBrush::setTransform(const QTransform &matrix){ detach(d->style); d->transform = matrix; d->hasTransform = !matrix.isIdentity();}/*! \fn void QBrush::matrix() const \since 4.2 Returns the current transformation matrix for the brush. \sa setMatrix()*//*! \fn bool QBrush::operator!=(const QBrush &brush) const Returns true if the brush is different from the given \a brush; otherwise returns false. Two brushes are different if they have different styles, colors or pixmaps. \sa operator==()*//*! \fn bool QBrush::operator==(const QBrush &brush) const Returns true if the brush is equal to the given \a brush; otherwise returns false. Two brushes are equal if they have equal styles, colors and pixmaps. \sa operator!=()*/bool QBrush::operator==(const QBrush &b) const{ if (b.d == d) return true; if (b.d->style == d->style && b.d->color == d->color) { switch (d->style) { case Qt::TexturePattern: { QPixmap &us = ((QTexturedBrushData *) d)->pixmap(); QPixmap &them = ((QTexturedBrushData *) b.d)->pixmap(); return ((us.isNull() && them.isNull()) || us.cacheKey() == them.cacheKey()); } case Qt::LinearGradientPattern: case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: { QGradientBrushData *d1 = static_cast<QGradientBrushData *>(d); QGradientBrushData *d2 = static_cast<QGradientBrushData *>(b.d); return d1->gradient == d2->gradient; } default: return true; } } return false;}/*! \fn QBrush::operator const QColor&() const Returns the brush's color. Use color() instead.*/#ifndef QT_NO_DEBUG_STREAM/*! \internal*/QDebug operator<<(QDebug dbg, const QBrush &b){#ifndef Q_BROKEN_DEBUG_STREAM dbg.nospace() << "QBrush(" << b.color() << ',' << b.style() << ')'; return dbg.space();#else qWarning("This compiler doesn't support streaming QBrush to QDebug"); return dbg; Q_UNUSED(b);#endif}#endif/***************************************************************************** QBrush stream functions *****************************************************************************/#ifndef QT_NO_DATASTREAM/*! \fn QDataStream &operator<<(QDataStream &stream, const QBrush &brush) \relates QBrush Writes the given \a brush to the given \a stream and returns a reference to the \a stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &s, const QBrush &b){ s << (quint8)b.style() << b.color(); if (b.style() == Qt::TexturePattern) { s << b.texture(); } else if (b.style() == Qt::LinearGradientPattern || b.style() == Qt::RadialGradientPattern || b.style() == Qt::ConicalGradientPattern) { const QGradient *gradient = b.gradient(); int type_as_int = int(gradient->type()); s << type_as_int; if (s.version() >= QDataStream::Qt_4_3) { s << int(gradient->spread()); s << int(gradient->coordinateMode()); } if (sizeof(qreal) == sizeof(double)) { s << gradient->stops(); } else { // ensure that we write doubles here instead of streaming the stops // directly; otherwise, platforms that redefine qreal might generate // data that cannot be read on other platforms. QVector<QGradientStop> stops = gradient->stops(); s << quint32(stops.size()); for (int i = 0; i < stops.size(); ++i) { const QGradientStop &stop = stops.at(i); s << QPair<double, QColor>(double(stop.first), stop.second); } } if (gradient->type() == QGradient::LinearGradient) { s << static_cast<const QLinearGradient *>(gradient)->start(); s << static_cast<const QLinearGradient *>(gradient)->finalStop(); } else if (gradient->type() == QGradient::RadialGradient) { s << static_cast<const QRadialGradient *>(gradient)->center(); s << static_cast<const QRadialGradient *>(gradient)->focalPoint(); s << (double) static_cast<const QRadialGradient *>(gradient)->radius(); } else { // type == Conical s << static_cast<const QConicalGradient *>(gradient)->center(); s << (double) static_cast<const QConicalGradient *>(gradient)->angle(); } } if (s.version() >= QDataStream::Qt_4_3) s << b.transform(); return s;}/*! \fn QDataStream &operator>>(QDataStream &stream, QBrush &brush) \relates QBrush Reads the given \a brush from the given \a stream and returns a reference to the \a stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &s, QBrush &b){ quint8 style; QColor color; s >> style; s >> color; if (style == Qt::TexturePattern) { QPixmap pm; s >> pm; b = QBrush(color, pm); } else if (style == Qt::LinearGradientPattern || style == Qt::RadialGradientPattern || style == Qt::ConicalGradientPattern) { int type_as_int; QGradient::Type type; QGradientStops stops; QGradient::CoordinateMode cmode = QGradient::LogicalMode;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -