📄 qbrush.cpp
字号:
QGradient::Spread spread = QGradient::PadSpread; s >> type_as_int; type = QGradient::Type(type_as_int); if (s.version() >= QDataStream::Qt_4_3) { s >> type_as_int; spread = QGradient::Spread(type_as_int); s >> type_as_int; cmode = QGradient::CoordinateMode(type_as_int); } if (sizeof(qreal) == sizeof(double)) { s >> stops; } else { quint32 numStops; double n; QColor c; s >> numStops; for (quint32 i = 0; i < numStops; ++i) { s >> n >> c; stops << QPair<qreal, QColor>(n, c); } } if (type == QGradient::LinearGradient) { QPointF p1, p2; s >> p1; s >> p2; QLinearGradient lg(p1, p2); lg.setStops(stops); lg.setSpread(spread); lg.setCoordinateMode(cmode); b = QBrush(lg); } else if (type == QGradient::RadialGradient) { QPointF center, focal; double radius; s >> center; s >> focal; s >> radius; QRadialGradient rg(center, radius, focal); rg.setStops(stops); rg.setSpread(spread); rg.setCoordinateMode(cmode); b = QBrush(rg); } else { // type == QGradient::ConicalGradient QPointF center; double angle; s >> center; s >> angle; QConicalGradient cg(center, angle); cg.setStops(stops); cg.setSpread(spread); cg.setCoordinateMode(cmode); b = QBrush(cg); } } else { b = QBrush(color, (Qt::BrushStyle)style); } if (s.version() >= QDataStream::Qt_4_3) { QTransform transform; s >> transform; b.setTransform(transform); } return s;}#endif // QT_NO_DATASTREAM/******************************************************************************* * QGradient implementations *//*! \class QGradient \ingroup multimedia \ingroup shared \brief The QGradient class is used in combination with QBrush to specify gradient fills. Qt currently supports three types of gradient fills: \list \o \e Linear gradients interpolate colors between start and end points. \o \e Radial gradients interpolate colors between a focal point and end points on a circle surrounding it. \o \e Conical gradients interpolate colors around a center point. \endlist A gradient's type can be retrieved using the type() function. Each of the types is represented by a subclass of QGradient: \table \row \o \inlineimage qgradient-linear.png \o \inlineimage qgradient-radial.png \o \inlineimage qgradient-conical.png \header \o QLinearGradient \o QRadialGradient \o QConicalGradient \endtable The colors in a gradient is defined using stop points of the QGradientStop type, i.e. a position and a color. Use the setColorAt() function to define a single stop point. Alternatively, use the setStops() function to define several stop points in one go. Note that the latter function \e replaces the current set of stop points. It is the gradient's complete set of stop points (accessible through the stops() function) that describes how the gradient area should be filled. If no stop points have been specified, a gradient of black at 0 to white at 1 is used. A diagonal linear gradient from black at (100, 100) to white at (200, 200) could be specified like this: \quotefromfile snippets/brush/brush.cpp \skipto LINEAR \skipto QLinearGradient \printuntil Qt::white A gradient can have an arbitrary number of stop points. The following would create a radial gradient starting with red in the center, blue and then green on the edges: \quotefromfile snippets/brush/brush.cpp \skipto RADIAL \skipto QRadialGradient \printuntil Qt::green It is possible to repeat or reflect the gradient outside its area by specifiying the \l {QGradient::Spread}{spread method} using the setSpread() function. The default is to pad the outside area with the color at the closest stop point. The currently set \l {QGradient::Spread}{spread method} can be retrieved using the spread() function. The QGradient::Spread enum defines three different methods: \table \row \o \inlineimage qradialgradient-pad.png \o \inlineimage qradialgradient-repeat.png \o \inlineimage qradialgradient-reflect.png \row \o \l {QGradient::PadSpread}{PadSpread} \o \l {QGradient::RepeatSpread}{RepeatSpread} \o \l {QGradient::ReflectSpread}{ReflectSpread} \endtable Note that the setSpread() function only has effect for linear and radial gradients. The reason is that the conical gradient is closed by definition, i.e. the \e conical gradient fills the entire circle from 0 - 360 degrees, while the boundary of a radial or a linear gradient can be specified through its radius or final stop points, respectively. \sa {demos/gradients}{The Gradients Demo}, QBrush*//*! \internal*/QGradient::QGradient() : m_type(NoGradient), dummy(0){}/*! \enum QGradient::Type Specifies the type of gradient. \value LinearGradient Interpolates colors between start and end points (QLinearGradient). \value RadialGradient Interpolate colors between a focal point and end points on a circle surrounding it (QRadialGradient). \value ConicalGradient Interpolate colors around a center point (QConicalGradient). \value NoGradient No gradient is used. \sa type()*//*! \enum QGradient::Spread Specifies how the area outside the gradient area should be filled. \value PadSpread The area is filled with the closest stop color. This is the default. \value RepeatSpread The gradient is repeated outside the gradient area. \value ReflectSpread The gradient is reflected outside the gradient area. \sa spread(), setSpread()*//*! \fn void QGradient::setSpread(Spread method) Specifies the spread \a method that should be used for this gradient. Note that this function only has effect for linear and radial gradients. \sa spread()*//*! \fn QGradient::Spread QGradient::spread() const Returns the spread method use by this gradient. The default is PadSpread. \sa setSpread()*//*! \fn QGradient::Type QGradient::type() const Returns the type of gradient.*//*! \fn void QGradient::setColorAt(qreal position, const QColor &color) Creates a stop point at the given \a position with the given \a color. The given \a position must be in the range 0 to 1. \sa setStops(), stops()*/void QGradient::setColorAt(qreal pos, const QColor &color){ if (pos > 1 || pos < 0) { qWarning("QGradient::setColorAt: Color position must be specified in the range 0 to 1"); return; } int index = 0; while (index < m_stops.size() && m_stops.at(index).first <= pos) ++index; m_stops.insert(index, QGradientStop(pos, color));}/*! \fn void QGradient::setStops(const QGradientStops &stopPoints) Replaces the current set of stop points with the given \a stopPoints. The positions of the points must be in the range 0 to 1, and must be sorted with the lowest point first. \sa setColorAt(), stops()*/void QGradient::setStops(const QGradientStops &stops){ m_stops.clear(); for (int i=0; i<stops.size(); ++i) setColorAt(stops.at(i).first, stops.at(i).second);}/*! Returns the stop points for this gradient. If no stop points have been specified, a gradient of black at 0 to white at 1 is used. \sa setStops(), setColorAt()*/QGradientStops QGradient::stops() const{ if (m_stops.isEmpty()) { QGradientStops tmp; tmp << QGradientStop(0, Qt::black) << QGradientStop(1, Qt::white); return tmp; } return m_stops;}/*! \enum QGradient::CoordinateMode \internal This enum specifies how gradient coordinates map to the paint device on which the gradient is used. \value LogicalMode \value StretchToDeviceMode \value ObjectBoundingMode*//*! \internal Returns the coordinate mode of this gradient. The default mode is LogicalMode.*/QGradient::CoordinateMode QGradient::coordinateMode() const{ if (dummy == 0) return LogicalMode; else if (dummy == (void*)1) return StretchToDeviceMode; else return ObjectBoundingMode;}/*! \internal Sets the coordinate mode of this gradient to \a mode. The default mode is LogicalMode.*/void QGradient::setCoordinateMode(CoordinateMode mode){ if (mode == LogicalMode) dummy = 0; else if (mode == StretchToDeviceMode) dummy = (void *) 1; else dummy = (void *) 2;}/*! \fn bool QGradient::operator!=(const QGradient &gradient) const \since 4.2 Returns true if the gradient is the same as the other \a gradient specified; otherwise returns false. \sa operator==()*//*! Returns true if the gradient is the same as the other \a gradient specified; otherwise returns false. \sa operator!=()*/bool QGradient::operator==(const QGradient &gradient) const{ if (gradient.m_type != m_type || gradient.m_spread != m_spread || gradient.dummy != dummy) return false; if (m_type == LinearGradient) { if (m_data.linear.x1 != gradient.m_data.linear.x1 || m_data.linear.y1 != gradient.m_data.linear.y1 || m_data.linear.x2 != gradient.m_data.linear.x2 || m_data.linear.y2 != gradient.m_data.linear.y2) return false; } else if (m_type == RadialGradient) { if (m_data.radial.cx != gradient.m_data.radial.cx || m_data.radial.cy != gradient.m_data.radial.cy || m_data.radial.fx != gradient.m_data.radial.fx || m_data.radial.fy != gradient.m_data.radial.fy || m_data.radial.radius != gradient.m_data.radial.radius) return false; } else { // m_type == ConicalGradient if (m_data.conical.cx != gradient.m_data.conical.cx || m_data.conical.cy != gradient.m_data.conical.cy || m_data.conical.angle != gradient.m_data.conical.angle) return false; } return stops() == gradient.stops();}/*! \internal*/bool QGradient::operator==(const QGradient &gradient){ return const_cast<const QGradient *>(this)->operator==(gradient);}/*! \class QLinearGradient \ingroup multimedia \brief The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush. Linear gradients interpolate colors between start and end points. Outside these points the gradient is either padded, reflected or repeated depending on the currently set \l {QGradient::Spread}{spread} method: \table \row \o \inlineimage qlineargradient-pad.png \o \inlineimage qlineargradient-reflect.png \o \inlineimage qlineargradient-repeat.png \row \o \l {QGradient::PadSpread}{PadSpread} (default) \o \l {QGradient::ReflectSpread}{ReflectSpread} \o \l {QGradient::RepeatSpread}{RepeatSpread} \endtable The colors in a gradient is defined using stop points of the QGradientStop type, i.e. a position and a color. Use the QGradient::setColorAt() or the QGradient::setStops() function to define the stop points. It is the gradient's complete set of stop points that describes how the gradient area should be filled. If no stop points have been specified, a gradient of black at 0 to white at 1 is used. In addition to the functions inherited from QGradient, the QLinearGradient class provides the finalStop() function which returns the final stop point of the gradient, and the start() function returning the start point of the gradient. \sa QRadialGradient, QConicalGradient, {demos/gradients}{The Gradients Demo}*//*! Constructs a default linear gradient with interpolation area between (0, 0) and (1, 1). \sa QGradient::setColorAt(), setStart(), setFinalStop()*/QLinearGradient::QLinearGradient(){ m_type = LinearGradient; m_spread = PadSpread; m_data.linear.x1 = 0; m_data.linear.y1 = 0; m_data.linear.x2 = 1; m_data.linear.y2 = 1;}/*! Constructs a linear gradient with interpolation area between the given \a start point and \a finalStop. \sa QGradient::setColorAt(), QGradient::setStops()*/QLinearGradient::QLinearGradient(const QPointF &start, const QPointF &finalStop){ m_type = LinearGradient; m_spread = PadSpread; m_data.linear.x1 = start.x(); m_data.linear.y1 = start.y(); m_data.linear.x2 = finalStop.x(); m_data.linear.y2 = finalStop.y();}/*! \fn QLinearGradient::QLinearGradient(qreal x1, qreal y1, qreal x2, qreal y2) Constructs a linear gradient with interpolation area between (\a x1, \a y1) and (\a x2, \a y2). \sa QGradient::setColorAt(), QGradient::setStops()*/QLinearGradient::QLinearGradient(qreal xStart, qreal yStart, qreal xFinalStop, qreal yFinalStop){ m_type = LinearGradient; m_spread = PadSpread; m_data.linear.x1 = xStart; m_data.linear.y1 = yStart; m_data.linear.x2 = xFinalStop; m_data.linear.y2 = yFinalStop;}/*! Returns the start point of this linear gradient in logical coordinates. \sa QGradient::stops()*/QPointF QLinearGradient::start() const{ Q_ASSERT(m_type == LinearGradient); return QPointF(m_data.linear.x1, m_data.linear.y1);}/*! \fn void QLinearGradient::setStart(qreal x, qreal y) \overload \since 4.2 Sets the start point of this linear gradient in logical coordinates to \a x, \a y. \sa start()*//*! \since 4.2 Sets the start point of this linear gradient in logical coordinates to \a start.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -