Example #1
0
QImage createRectGradient(int width, int height, const QGradient &gradient)
{
    QImage::Format format = QImage::Format_ARGB32_Premultiplied;
    QImage buffer(width, height, format);
    buffer.fill( qRgba(255, 255,255, 255) );

    QPainter painter(&buffer);
    painter.setPen(Qt::NoPen);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);

    // Divide the rectangle into four triangles, each facing North, South,
    // East, and West. Every triangle will have its bounding box and linear
    // gradient.
    QLinearGradient linearGradient;

    // from center going North
    linearGradient = QLinearGradient(0, 0, width, height / 2);
    linearGradient.setStops( gradient.stops() );
    linearGradient.setStart(width / 2, height / 2);
    linearGradient.setFinalStop(width / 2, 0);
    painter.setBrush(linearGradient);
    painter.drawRect(0, 0, width, height / 2);

    // from center going South
    linearGradient = QLinearGradient(0, 0, width, height / 2);
    linearGradient.setStops( gradient.stops() );
    linearGradient.setStart(width / 2, height / 2);
    linearGradient.setFinalStop(width / 2, height);
    painter.setBrush(linearGradient);
    painter.drawRect(0, height / 2, width, height / 2);

    // clip the East and West portion
    QPainterPath clip;
    clip.moveTo(width, 0);
    clip.lineTo(width, height);
    clip.lineTo(0, 0);
    clip.lineTo(0, height);
    clip.closeSubpath();
    painter.setClipPath(clip);

    // from center going East
    linearGradient = QLinearGradient(0, 0, width / 2, height);
    linearGradient.setStops( gradient.stops() );
    linearGradient.setStart(width / 2, height / 2);
    linearGradient.setFinalStop(width, height / 2);
    painter.setBrush(linearGradient);
    painter.drawRect(width / 2, 0, width, height);

    // from center going West
    linearGradient = QLinearGradient(0, 0, width / 2, height);
    linearGradient.setStops( gradient.stops() );
    linearGradient.setStart(width / 2, height / 2);
    linearGradient.setFinalStop(0, height / 2);
    painter.setBrush(linearGradient);
    painter.drawRect(0, 0, width / 2, height);

    painter.end();
    return buffer;
}
Example #2
0
QGradient* KoStopGradient::toQGradient() const
{
    QGradient* gradient;

    switch (type()) {
    case QGradient::LinearGradient: {
        gradient = new QLinearGradient(m_start, m_stop);
        break;
    }
    case QGradient::RadialGradient: {
        QPointF diff = m_stop - m_start;
        qreal radius = sqrt(diff.x() * diff.x() + diff.y() * diff.y());
        gradient = new QRadialGradient(m_start, radius, m_focalPoint);
        break;
    }
    case QGradient::ConicalGradient: {
        qreal angle = atan2(m_start.y(), m_start.x()) * 180.0 / M_PI;
        if (angle < 0.0)
            angle += 360.0;
        gradient = new QConicalGradient(m_start, angle);
        break;
    }
    default:
        return 0;
    }
    QColor color;
    for (QList<KoGradientStop>::const_iterator i = m_stops.begin(); i != m_stops.end(); ++i) {
        i->second.toQColor(&color);
        gradient->setColorAt(i->first , color);
    }
    return gradient;
}
Example #3
0
 void ShadowWindow::setGradientColor(QGradient& gradient)
 {
     gradient.setColorAt(0, QColor(0, 0, 0, 50));
     gradient.setColorAt(0.2, QColor(0, 0, 0, IsActive_ ? 20 : 10));
     gradient.setColorAt(0.6, IsActive_ ? QColor(0, 0, 0, 5) : Qt::transparent);
     gradient.setColorAt(1, Qt::transparent);
 }
void GradientRangeEditor::setGradient( const QGradient &g )
{
	gradientType = g.type();
	stops = g.stops();
	spreadType = g.spread();
	switch( gradientType )
	{
		case QGradient::LinearGradient:
			setStartPoint( ( ( QLinearGradient& )g ).start() );
			setEndPoint( ( ( QLinearGradient& )g ).finalStop() );
			break;

		case QGradient::RadialGradient:
			setStartPoint( ( ( QRadialGradient& )g ).center() );
			setEndPoint( ( ( QRadialGradient& )g ).focalPoint() );
			break;

		default:
			setStartPoint( ( ( QConicalGradient& )g ).center() );
			qreal a = ( ( QConicalGradient& )g ).angle();
			QLineF l( QLineF( 0.0, 0.0, 0.5, 0.0 ) *
							QMatrix().rotate( a ) );
			l.translate( _startPoint );
			setEndPoint( l.p2() );
			break;
	}
}
Example #5
0
void KoResourcePopupAction::indexChanged(const QModelIndex &modelIndex)
{
    if (! modelIndex.isValid()) {
        return;
    }

    d->menu->hide();

    KoResource *resource = static_cast<KoResource*>(modelIndex.internalPointer());
    if(resource) {
        KoAbstractGradient *gradient = dynamic_cast<KoAbstractGradient*>(resource);
        KoPattern *pattern = dynamic_cast<KoPattern*>(resource);
        if (gradient) {
            QGradient *qg = gradient->toQGradient();
            qg->setCoordinateMode(QGradient::ObjectBoundingMode);
            d->background = QSharedPointer<KoShapeBackground>(new KoGradientBackground(qg));
        } else if (pattern) {
            KoImageCollection *collection = new KoImageCollection();
            d->background = QSharedPointer<KoShapeBackground>(new KoPatternBackground(collection));
            qSharedPointerDynamicCast<KoPatternBackground>(d->background)->setPattern(pattern->pattern());
        }

        emit resourceSelected(d->background);

        updateIcon();
    }
}
GLuint QOpenGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity)
{
    quint64 hash_val = 0;

    QGradientStops stops = gradient.stops();
    for (int i = 0; i < stops.size() && i <= 2; i++)
        hash_val += stops[i].second.rgba();

    const QMutexLocker lock(&m_mutex);
    QOpenGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val);

    if (it == cache.constEnd())
        return addCacheElement(hash_val, gradient, opacity);
    else {
        do {
            const CacheInfo &cache_info = it.value();
            if (cache_info.stops == stops && cache_info.opacity == opacity
                && cache_info.interpolationMode == gradient.interpolationMode())
            {
                return cache_info.texId;
            }
            ++it;
        } while (it != cache.constEnd() && it.key() == hash_val);
        // an exact match for these stops and opacity was not found, create new cache
        return addCacheElement(hash_val, gradient, opacity);
    }
}
QGradient GradientRangeEditor::gradient() const
{
	QGradient g;

	switch( gradientType )
	{
		case QGradient::LinearGradient:
			g = QLinearGradient( _startPoint, _endPoint );
			break;

		case QGradient::RadialGradient:
		{
			QLineF l( _startPoint, _endPoint );
			if( l.length() > 0.5 )
				l.setLength( 0.5 - 0.001 );
			g = QRadialGradient( l.p1(), 0.5, l.p2() );
			break;
		}

		default:
			g = QConicalGradient( _startPoint, angle() );
			break;
	}

	g.setStops( stops );
	g.setSpread( spreadType );
	return g;
}
Example #8
0
static PyObject *meth_QGradient_setColorAt(PyObject *sipSelf, PyObject *sipArgs)
{
    PyObject *sipParseErr = NULL;

    {
        qreal a0;
        const QColor* a1;
        int a1State = 0;
        QGradient *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "BdJ1", &sipSelf, sipType_QGradient, &sipCpp, &a0, sipType_QColor, &a1, &a1State))
        {
            sipCpp->setColorAt(a0,*a1);
            sipReleaseType(const_cast<QColor *>(a1),sipType_QColor,a1State);

            Py_INCREF(Py_None);
            return Py_None;
        }
    }

    /* Raise an exception if the arguments couldn't be parsed. */
    sipNoMethod(sipParseErr, sipName_QGradient, sipName_setColorAt, doc_QGradient_setColorAt);

    return NULL;
}
Example #9
0
void GradientRenderer::paint(QPainter *p)
{
    QPolygonF pts = m_hoverPoints->points();

    QGradient g;

    if (m_gradientType == Qt::LinearGradientPattern) {
        g = QLinearGradient(pts.at(0), pts.at(1));

    } else if (m_gradientType == Qt::RadialGradientPattern) {
        g = QRadialGradient(pts.at(0), qMin(width(), height()) / 3.0, pts.at(1));

    } else {
        QLineF l(pts.at(0), pts.at(1));
        qreal angle = l.angle(QLineF(0, 0, 1, 0));
        if (l.dy() > 0)
            angle = 360 - angle;
        g = QConicalGradient(pts.at(0), angle);
    }

    for (int i=0; i<m_stops.size(); ++i)
        g.setColorAt(m_stops.at(i).first, m_stops.at(i).second);

    g.setSpread(m_spread);

    p->setBrush(g);
    p->setPen(Qt::NoPen);

    p->drawRect(rect());

}
QPixmap QtGradientUtils::gradientPixmap(const QGradient &gradient, const QSize &size, bool checkeredBackground)
{
    QImage image(size, QImage::Format_ARGB32);
    QPainter p(&image);
    p.setCompositionMode(QPainter::CompositionMode_Source);

    if (checkeredBackground) {
        int pixSize = 20;
        QPixmap pm(2 * pixSize, 2 * pixSize);

        QPainter pmp(&pm);
        pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
        pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
        pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
        pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);

        p.setBrushOrigin((size.width() % pixSize + pixSize) / 2, (size.height() % pixSize + pixSize) / 2);
        p.fillRect(0, 0, size.width(), size.height(), pm);
        p.setBrushOrigin(0, 0);
        p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    }

    const qreal scaleFactor = 0.999999;
    p.scale(scaleFactor, scaleFactor);
    QGradient grad = gradient;
    grad.setCoordinateMode(QGradient::StretchToDeviceMode);
    p.fillRect(QRect(0, 0, size.width(), size.height()), grad);
    p.drawRect(QRect(0, 0, size.width() - 1, size.height() - 1));

    return QPixmap::fromImage(image);
}
Example #11
0
void KoGradientEditWidget::setGradient(const QGradient & gradient)
{
    m_stops = gradient.stops();
    m_type = gradient.type();
    m_spread = gradient.spread();

    updateUI();
}
QGradient* KarbonGradientHelper::convertGradient(const QGradient * gradient, QGradient::Type newType)
{
    QPointF start, stop;
    // try to preserve gradient positions
    switch (gradient->type()) {
    case QGradient::LinearGradient: {
        const QLinearGradient *g = static_cast<const QLinearGradient*>(gradient);
        start = g->start();
        stop = g->finalStop();
        break;
    }
    case QGradient::RadialGradient: {
        const QRadialGradient *g = static_cast<const QRadialGradient*>(gradient);
        start = g->center();
        stop = QPointF(g->radius(), 0.0);
        break;
    }
    case QGradient::ConicalGradient: {
        const QConicalGradient *g = static_cast<const QConicalGradient*>(gradient);
        start = g->center();
        qreal radAngle = g->angle() * M_PI / 180.0;
        stop = QPointF(0.5 * cos(radAngle), 0.5 * sin(radAngle));
        break;
    }
    default:
        start = QPointF(0.0, 0.0);
        stop = QPointF(0.5, 0.5);
    }

    QGradient *newGradient = 0;
    switch (newType) {
    case QGradient::LinearGradient:
        newGradient = new QLinearGradient(start, stop);
        break;
    case QGradient::RadialGradient: {
        QPointF diff(stop - start);
        qreal radius = sqrt(diff.x()*diff.x() + diff.y()*diff.y());
        newGradient = new QRadialGradient(start, radius, start);
        break;
    }
    case QGradient::ConicalGradient: {
        QPointF diff(stop - start);
        qreal angle = atan2(diff.y(), diff.x());
        if (angle < 0.0)
            angle += 2 * M_PI;
        newGradient = new QConicalGradient(start, angle * 180/M_PI);
        break;
    }
    default:
        return 0;
    }
    newGradient->setCoordinateMode(QGradient::ObjectBoundingMode);
    newGradient->setSpread(gradient->spread());
    newGradient->setStops(gradient->stops());

    return newGradient;
}
Example #13
0
void grSetup (sgradientParam prm,QGradient &g)
{
	g.setColorAt(prm.pos1/100.,prm.color1);
	if(prm.pos2<=prm.pos1) return ;
	g.setColorAt(prm.pos2/100.,prm.color2);
	if(prm.pos3<=prm.pos2) return ;
	g.setColorAt(prm.pos3/100.,prm.color3);
	if(prm.pos4<=prm.pos3) return ;
	g.setColorAt(prm.pos4/100.,prm.color4);
}
static QStringList styleSheetParameters(const QGradient &gradient)
{
    QStringList result;

    if (gradient.type() != QGradient::ConicalGradient) {
        QString spread;
        switch (gradient.spread()) {
            case QGradient::PadSpread:
                spread = QLatin1String("pad");
                break;
            case QGradient::ReflectSpread:
                spread = QLatin1String("reflect");
                break;
            case QGradient::RepeatSpread:
                spread = QLatin1String("repeat");
                break;
            default:
                qWarning() << "QtGradientUtils::styleSheetParameters(): gradient spread" << gradient.spread() << "not supported!";
                break;
        }
        result << QLatin1String("spread:") + spread;
    }

    switch (gradient.type()) {
        case QGradient::LinearGradient: {
            const QLinearGradient *linearGradient = static_cast<const QLinearGradient*>(&gradient);
            result << QLatin1String("x1:") + QString::number(linearGradient->start().x())
                << QLatin1String("y1:")    + QString::number(linearGradient->start().y())
                << QLatin1String("x2:")    + QString::number(linearGradient->finalStop().x())
                << QLatin1String("y2:")    + QString::number(linearGradient->finalStop().y());
            break;
        }
        case QGradient::RadialGradient: {
            const QRadialGradient *radialGradient = static_cast<const QRadialGradient*>(&gradient);
            result << QLatin1String("cx:")  + QString::number(radialGradient->center().x())
                << QLatin1String("cy:")     + QString::number(radialGradient->center().y())
                << QLatin1String("radius:") + QString::number(radialGradient->radius())
                << QLatin1String("fx:")     + QString::number(radialGradient->focalPoint().x())
                << QLatin1String("fy:")     + QString::number(radialGradient->focalPoint().y());
            break;
        }
        case QGradient::ConicalGradient: {
            const QConicalGradient *conicalGradient = static_cast<const QConicalGradient*>(&gradient);
            result << QLatin1String("cx:") + QString::number(conicalGradient->center().x())
                << QLatin1String("cy:")    + QString::number(conicalGradient->center().y())
                << QLatin1String("angle:") + QString::number(conicalGradient->angle());
            break;
        }
        default:
            qWarning() << "QtGradientUtils::styleSheetParameters(): gradient type" << gradient.type() << "not supported!";
            break;
    }

    return result;
}
//TODO: Let GL generate the texture using an FBO
void QOpenGL2GradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const
{
    int pos = 0;
    QGradientStops s = gradient.stops();
    QVector<uint> colors(s.size());

    for (int i = 0; i < s.size(); ++i)
        colors[i] = s[i].second.rgba(); // Qt LIES! It returns ARGB (on little-endian AND on big-endian)

    bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation);

    uint alpha = qRound(opacity * 256);
    uint current_color = ARGB_COMBINE_ALPHA(colors[0], alpha);
    qreal incr = 1.0 / qreal(size);
    qreal fpos = 1.5 * incr;
    colorTable[pos++] = ARGB2RGBA(qPremultiply(current_color));

    while (fpos <= s.first().first) {
        colorTable[pos] = colorTable[pos - 1];
        pos++;
        fpos += incr;
    }

    if (colorInterpolation)
        current_color = qPremultiply(current_color);

    for (int i = 0; i < s.size() - 1; ++i) {
        qreal delta = 1/(s[i+1].first - s[i].first);
        uint next_color = ARGB_COMBINE_ALPHA(colors[i+1], alpha);
        if (colorInterpolation)
            next_color = qPremultiply(next_color);

        while (fpos < s[i+1].first && pos < size) {
            int dist = int(256 * ((fpos - s[i].first) * delta));
            int idist = 256 - dist;
            if (colorInterpolation)
                colorTable[pos] = ARGB2RGBA(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist));
            else
                colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)));
            ++pos;
            fpos += incr;
        }
        current_color = next_color;
    }

    Q_ASSERT(s.size() > 0);

    uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(colors[s.size() - 1], alpha)));
    for (;pos < size; ++pos)
        colorTable[pos] = last_color;

    // Make sure the last color stop is represented at the end of the table
    colorTable[size-1] = last_color;
}
Example #16
0
bool SVGPaintServerGradient::setup(QPainter* painter, QPainterPath* painterPath, const RenderObject* object,
                                   SVGPaintTargetType type, bool isPaintingText) const
{
    Q_UNUSED(isPaintingText);
    kDebug() << "!!!!!!!" << endl;
    m_ownerElement->buildGradient();

    /*QPainter* painter(context ? context->platformContext() : 0);
    Q_ASSERT(painter);*/

    /*QPainterPath* path(context ? context->currentPath() : 0);
    Q_ASSERT(path);*/

    RenderStyle* renderStyle = object->style();

    QGradient gradient = setupGradient(painter, painterPath, object);

    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::NoBrush);

    if (spreadMethod() == SPREADMETHOD_REPEAT)
        gradient.setSpread(QGradient::RepeatSpread);
    else if (spreadMethod() == SPREADMETHOD_REFLECT)
        gradient.setSpread(QGradient::ReflectSpread);
    else
        gradient.setSpread(QGradient::PadSpread);    
    double opacity = 1.0;

    kDebug() << "type: " << type << (type & ApplyToFillTargetType) << endl;
    if ((type & ApplyToFillTargetType) && renderStyle->svgStyle()->hasFill()) {
        fillColorArray(gradient, gradientStops(), opacity);

        QBrush brush(gradient);
        brush.setMatrix(gradientTransform());

        painter->setBrush(brush);
        /* FIXME khtml context->setFillRule(renderStyle->svgStyle()->fillRule());*/
    }

    if ((type & ApplyToStrokeTargetType) && renderStyle->svgStyle()->hasStroke()) {
        fillColorArray(gradient, gradientStops(), opacity);

        QPen pen;
        QBrush brush(gradient);
        brush.setMatrix(gradientTransform());

        setPenProperties(object, renderStyle, pen);
        pen.setBrush(brush);

        painter->setPen(pen);
    }

    return true;
}
Example #17
0
void KoResourceItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    if( ! index.isValid() )
        return;

    KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
    if (!resource)
        return;

    painter->save();

    if (option.state & QStyle::State_Selected)
        painter->fillRect( option.rect, option.palette.highlight() );

    QRect innerRect = option.rect.adjusted( 2, 1, -2, -1 );

    KoAbstractGradient * gradient = dynamic_cast<KoAbstractGradient*>( resource );
    if (gradient) {
        QGradient * g = gradient->toQGradient();

        QLinearGradient paintGradient;
        paintGradient.setStops( g->stops() );
        paintGradient.setStart( innerRect.topLeft() );
        paintGradient.setFinalStop( innerRect.topRight() );

        m_checkerPainter.paint( *painter, innerRect );
        painter->fillRect( innerRect, QBrush( paintGradient ) );

        delete g;
    }
    else {
        QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();

        QSize imageSize = thumbnail.size();

        if(imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) {
            qreal scaleW = static_cast<qreal>( innerRect.width() ) / static_cast<qreal>( imageSize.width() );
            qreal scaleH = static_cast<qreal>( innerRect.height() ) / static_cast<qreal>( imageSize.height() );

            qreal scale = qMin( scaleW, scaleH );

            int thumbW = static_cast<int>( imageSize.width() * scale );
            int thumbH = static_cast<int>( imageSize.height() * scale );
            thumbnail = thumbnail.scaled( thumbW, thumbH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
        }
        painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
        if (thumbnail.hasAlphaChannel()) {
            painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.
        }
        painter->fillRect( innerRect, QBrush(thumbnail) );
    }
    painter->restore();
}
void KTPaletteDocument::addGradient(const QGradient &gradient)
{
    QDomElement element = createElement("Gradient");
    
    element.setAttribute("type", gradient.type());
    element.setAttribute("spread", gradient.spread());
    const QGradient *gr = &gradient;
    switch (gradient.type()) {
            case QGradient::LinearGradient:
            {
                 element.setAttribute("startX", static_cast<const QLinearGradient *>(gr)->start().x());
                 element.setAttribute("startY", static_cast<const QLinearGradient *>(gr)->start().y());
                
                 element.setAttribute("finalX", static_cast<const QLinearGradient *>(gr)->finalStop().x());
                 element.setAttribute("finalY", static_cast<const QLinearGradient *>(gr)->finalStop().y());
            }
            break;
            case QGradient::RadialGradient:
            {
                 element.setAttribute("centerX", static_cast<const QRadialGradient *>(gr)->center().x());
                 element.setAttribute("centerY", static_cast<const QRadialGradient *>(gr)->center().y());
                
                 element.setAttribute("focalX", static_cast<const QRadialGradient *>(gr)->focalPoint().x());
                 element.setAttribute("focalY", static_cast<const QRadialGradient *>(gr)->focalPoint().y());
                
                 element.setAttribute("radius", static_cast<const QRadialGradient *>(gr)->radius());
            }
            break;
            case QGradient::ConicalGradient:
            {
                 element.setAttribute("centerX", static_cast<const QRadialGradient *>(gr)->center().x());
                 element.setAttribute("centerY", static_cast<const QRadialGradient *>(gr)->center().y());
                
                 element.setAttribute("angle", static_cast<const QConicalGradient *>(gr)->angle());
            }
            break;
            case QGradient::NoGradient:
            {
            }
            break;
    }
    
    QGradientStops stops = gradient.stops();
    
    foreach (QGradientStop stop, stops) {
             QDomElement stopElement = createElement("Stop");
             stopElement.setAttribute("value", stop.first);
             stopElement.setAttribute("colorName", stop.second.name());
             stopElement.setAttribute("alpha", stop.second.alpha());
        
             element.appendChild(stopElement);
    }
Example #19
0
bool SVGPaintServerGradient::setup(GraphicsContext*& context, const RenderObject* object,
                                   SVGPaintTargetType type, bool isPaintingText) const
{
    m_ownerElement->buildGradient();

    QPainter* painter(context ? context->platformContext() : 0);
    Q_ASSERT(painter);

    QPainterPath* path(context ? context->currentPath() : 0);
    Q_ASSERT(path);

    const SVGRenderStyle* svgStyle = object->style()->svgStyle();
    RenderStyle* style = object->style();

    QGradient gradient = setupGradient(context, object);

    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::NoBrush);

    if (spreadMethod() == SpreadMethodRepeat)
        gradient.setSpread(QGradient::RepeatSpread);
    else if (spreadMethod() == SpreadMethodReflect)
        gradient.setSpread(QGradient::ReflectSpread);
    else
        gradient.setSpread(QGradient::PadSpread);    
    double opacity = 1.0;

    if ((type & ApplyToFillTargetType) && svgStyle->hasFill()) {
        fillColorArray(gradient, gradientStops(), opacity);

        QBrush brush(gradient);
        brush.setMatrix(gradientTransform());

        painter->setBrush(brush);
        context->setFillRule(svgStyle->fillRule());
    }

    if ((type & ApplyToStrokeTargetType) && svgStyle->hasStroke()) {
        fillColorArray(gradient, gradientStops(), opacity);

        QPen pen;
        QBrush brush(gradient);
        brush.setMatrix(gradientTransform());
        pen.setBrush(brush);
        painter->setPen(pen);

        applyStrokeStyleToContext(context, style, object);
    }

    return true;
}
Example #20
0
KoResourcePopupAction::KoResourcePopupAction(QSharedPointer<KoAbstractResourceServerAdapter>resourceAdapter, QObject *parent)
:  KAction(parent)
, d(new Private())
{
    Q_ASSERT(resourceAdapter);

    d->menu = new QMenu();
    QWidget *widget = new QWidget(d->menu);
    QWidgetAction *wdgAction = new QWidgetAction(widget);

    d->resourceList = new KoResourceItemView(widget);
    d->resourceList->setModel(new KoResourceModel(resourceAdapter, widget));
    d->resourceList->setItemDelegate(new KoResourceItemDelegate(widget));
    KoResourceModel * resourceModel = qobject_cast<KoResourceModel*>(d->resourceList->model());
    if (resourceModel) {
        resourceModel->setColumnCount(1);
    }

    KoResource *resource = 0;
    if (resourceAdapter->resources().count() > 0) {
        resource = resourceAdapter->resources().at(0);
    }

    KoAbstractGradient *gradient = dynamic_cast<KoAbstractGradient*>(resource);
    KoPattern *pattern = dynamic_cast<KoPattern*>(resource);
    if (gradient) {
        QGradient *qg = gradient->toQGradient();
        qg->setCoordinateMode(QGradient::ObjectBoundingMode);
        d->background = QSharedPointer<KoShapeBackground>(new KoGradientBackground(qg));
    } else if (pattern) {
        KoImageCollection *collection = new KoImageCollection();
        d->background = QSharedPointer<KoShapeBackground>(new KoPatternBackground(collection));
        static_cast<KoPatternBackground*>(d->background.data())->setPattern(pattern->pattern());
    }

    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->addWidget(d->resourceList);
    widget->setLayout(layout);

    wdgAction->setDefaultWidget(widget);
    d->menu->addAction(wdgAction);
    setMenu(d->menu);
    new QHBoxLayout(d->menu);
    d->menu->layout()->addWidget(widget);
    d->menu->layout()->setMargin(0);

    connect(d->resourceList, SIGNAL(clicked(QModelIndex)), this, SLOT(indexChanged(QModelIndex)));

    updateIcon();
}
static inline QGradient applySpreadMethod(QGradient gradient, GradientSpreadMethod spreadMethod)
{
    switch (spreadMethod) {
        case SpreadMethodPad:
            gradient.setSpread(QGradient::PadSpread);
           break;
        case SpreadMethodReflect:
            gradient.setSpread(QGradient::ReflectSpread);
            break;
        case SpreadMethodRepeat:
            gradient.setSpread(QGradient::RepeatSpread);
            break;
    }
    return gradient;
}
Example #22
0
QPixmap PostEffect::progressivePixmap(const QPixmap &pm)
{
    int maxSize = qMax( pm.width(),pm.height() );
    float factor = (maxSize - 2 * progressiveBorderSize) / (float)maxSize;
    QGradient gradient;
    gradient.setColorAt( 1, QColor(0,0,0,0) );
    gradient.setColorAt( factor, QColor(255,255,255,255) );
    gradient.setColorAt( 0, QColor(255,255,255,255) );
    QPixmap res = QPixmap::fromImage( createRectGradient(pm.width(),pm.height(), gradient) );

    QPainter painter(&res);
    painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
    painter.drawPixmap(0,0,pm);
    painter.end();
    return res;
}
Example #23
0
//-----------------------------------------------------------------------------
//! 
//-----------------------------------------------------------------------------
void tBoatWidget::DrawThrustArrow( QPainter& painter, int value, int yPos )
{
    painter.save();
    painter.translate( QPointF(width()/2, yPos+0.5) );

    int minValue = 10;
    int headLength = 24;
    int valuePix = value/2;
    if( valuePix < 0 )
        valuePix = -valuePix;
   
    int s = (value > 0) ? 1 : -1;
    QColor arrowColor = (value > 0) ? QColor(0,188,0) : Qt::red;

    QColor grad1Color = QColor(7,131,255,255);
    QColor grad2Color = QColor(7,131,255,0);

    if( tSystemSettings::Instance()->NightMode() )
    {
        arrowColor = arrowColor.darker();
        grad1Color = grad1Color.darker();
    }

    // Draw arrow
    QPainterPath path;
    path.moveTo( 0, -10 );
    path.lineTo( s*(minValue + valuePix), -10);
    path.lineTo( s*(minValue + valuePix), -15 );
    path.lineTo( s*(minValue + valuePix + headLength), 0 );
    path.lineTo( s*(minValue + valuePix), 15 );
    path.lineTo( s*(minValue + valuePix), 10 );
    path.lineTo( 0, 10 );
    painter.fillPath( path, arrowColor );

    // Draw vacuum
    QGradient grad = QLinearGradient( QPointF(0,0), QPointF(-s*40,0) );
    grad.setColorAt(0.0, grad1Color );
    grad.setColorAt(1.0, grad2Color );
    QPainterPath p2;
    p2.moveTo( 0, -12 );
    p2.lineTo( -s*40, -21 );
    p2.lineTo( -s*40, 21 );
    p2.lineTo( 0, 12 );
    painter.fillPath( p2, grad );

    painter.restore();
}
Example #24
0
void GradientDialog::setGradient( const QGradient &gradient )
{
    d->m_gradient.clear();
    d->ui->stopSelector->clear();
    const QGradientStops stops = gradient.stops();
    Q_FOREACH( const QGradientStop &stop, stops )
    {
        d->m_gradient.append( stop );
    }
static PyObject *meth_QGradient_type(PyObject *sipSelf, PyObject *sipArgs)
{
    PyObject *sipParseErr = NULL;

    {
        QGradient *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QGradient, &sipCpp))
        {
            QGradient::Type sipRes;

            Py_BEGIN_ALLOW_THREADS
            sipRes = sipCpp->type();
            Py_END_ALLOW_THREADS

            return sipConvertFromEnum(sipRes,sipType_QGradient_Type);
        }
    }
static QGradient loadGradient(const QDomElement &elem)
{
    if (elem.tagName() != QLatin1String("gradientData"))
        return QLinearGradient();

    const QGradient::Type type = stringToGradientType(elem.attribute(QLatin1String("type")));
    const QGradient::Spread spread = stringToGradientSpread(elem.attribute(QLatin1String("spread")));
    const QGradient::CoordinateMode mode = stringToGradientCoordinateMode(elem.attribute(QLatin1String("coordinateMode")));

    QGradient gradient = QLinearGradient();

    if (type == QGradient::LinearGradient) {
        QLinearGradient g;
        g.setStart(elem.attribute(QLatin1String("startX")).toDouble(), elem.attribute(QLatin1String("startY")).toDouble());
        g.setFinalStop(elem.attribute(QLatin1String("endX")).toDouble(), elem.attribute(QLatin1String("endY")).toDouble());
        gradient = g;
    } else if (type == QGradient::RadialGradient) {
        QRadialGradient g;
        g.setCenter(elem.attribute(QLatin1String("centerX")).toDouble(), elem.attribute(QLatin1String("centerY")).toDouble());
        g.setFocalPoint(elem.attribute(QLatin1String("focalX")).toDouble(), elem.attribute(QLatin1String("focalY")).toDouble());
        g.setRadius(elem.attribute(QLatin1String("radius")).toDouble());
        gradient = g;
    } else if (type == QGradient::ConicalGradient) {
        QConicalGradient g;
        g.setCenter(elem.attribute(QLatin1String("centerX")).toDouble(), elem.attribute(QLatin1String("centerY")).toDouble());
        g.setAngle(elem.attribute(QLatin1String("angle")).toDouble());
        gradient = g;
    }

    QDomElement stopElem = elem.firstChildElement();
    while (!stopElem.isNull()) {
        QGradientStop stop = loadGradientStop(stopElem);

        gradient.setColorAt(stop.first, stop.second);

        stopElem = stopElem.nextSiblingElement();
    }

    gradient.setSpread(spread);
    gradient.setCoordinateMode(mode);

    return gradient;
}
Example #27
0
void MythYUVAPainter::DrawRect(const QRect &area, const QBrush &fillBrush,
                               const QPen &linePen, int alpha)
{
    QBrush brush(fillBrush);

    switch (fillBrush.style())
    {
    case Qt::LinearGradientPattern:
    case Qt::RadialGradientPattern:
    case Qt::ConicalGradientPattern:
        {
        QGradient gradient = *fillBrush.gradient();
        QGradientStops stops = gradient.stops();
        for (QGradientStops::iterator it = stops.begin(); it != stops.end(); ++it)
        {
            it->second = rgb_to_yuv(it->second);
            it->second.setAlpha(alpha);
        }
        gradient.setStops(stops);
        brush = gradient;
        }
        break;
    default:
        brush.setColor(rgb_to_yuv(brush.color()));
        break;
    }

    QPen pen(linePen);
    pen.setColor(rgb_to_yuv(pen.color()));

    // We pull an image here, in the hopes that when DrawRect
    // pulls an image this will still be in the cache and have
    // the right properties.
    MythImage *im = GetImageFromRect(area, 0, 0, brush, pen);
    if (im)
    {
        im->SetToYUV();
        im->DecrRef();
        im = NULL;
    }

    MythQImagePainter::DrawRect(area, brush, pen, alpha);
}
static QString styleSheetFillName(const QGradient &gradient)
{
    QString result;

    switch (gradient.type()) {
        case QGradient::LinearGradient:
            result += QLatin1String("qlineargradient");
            break;
        case QGradient::RadialGradient:
            result += QLatin1String("qradialgradient");
            break;
        case QGradient::ConicalGradient:
            result += QLatin1String("qconicalgradient");
            break;
        default:
            qWarning() << "QtGradientUtils::styleSheetFillName(): gradient type" << gradient.type() << "not supported!";
            break;
    }

    return result;
}
Example #29
0
void QEmulationPaintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem)
{
    if (state()->bgMode == Qt::OpaqueMode) {
        const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
        QRectF rect(p.x(), p.y() - ti.ascent.toReal(), ti.width.toReal(), (ti.ascent + ti.descent + 1).toReal());
        fillBGRect(rect);
    }

    QPainterState *s = state();
    Qt::BrushStyle style = qbrush_style(s->pen.brush());
    if (style >= Qt::LinearGradientPattern && style <= Qt::ConicalGradientPattern)
    {
        QPen savedPen = s->pen;
        QGradient g = *s->pen.brush().gradient();

        if (g.coordinateMode() > QGradient::LogicalMode) {
            QTransform mat = s->pen.brush().transform();
            if (g.coordinateMode() == QGradient::StretchToDeviceMode) {
                mat.scale(real_engine->painter()->device()->width(), real_engine->painter()->device()->height());
            } else if (g.coordinateMode() == QGradient::ObjectBoundingMode) {
                const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
                QRectF r(p.x(), p.y() - ti.ascent.toReal(), ti.width.toReal(), (ti.ascent + ti.descent + 1).toReal());
                mat.translate(r.x(), r.y());
                mat.scale(r.width(), r.height());
            }
            g.setCoordinateMode(QGradient::LogicalMode);
            QBrush brush(g);
            brush.setTransform(mat);
            s->pen.setBrush(brush);
            penChanged();
            real_engine->drawTextItem(p, textItem);
            s->pen = savedPen;
            penChanged();
            return;
        }
    }

    real_engine->drawTextItem(p, textItem);
}
QGradient* KarbonGradientHelper::defaultGradient(QGradient::Type type, QGradient::Spread spread, const QGradientStops &stops)
{
    QGradient *gradient = 0;
    switch (type) {
    case QGradient::LinearGradient:
        gradient = new QLinearGradient(QPointF(0.0, 0.5), QPointF(1, 0.5));
        break;
    case QGradient::RadialGradient:
        gradient = new QRadialGradient(QPointF(0.5, 0.5), sqrt(0.5));
        break;
    case QGradient::ConicalGradient:
        gradient = new QConicalGradient(QPointF(0.5, 0.5), 0.0);
        break;
    default:
        return 0;
    }
    gradient->setCoordinateMode(QGradient::ObjectBoundingMode);
    gradient->setSpread(spread);
    gradient->setStops(stops);

    return gradient;
}