// This is only used to draw borders.
void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
{
    if (paintingDisabled())
        return;

    FloatPoint p1 = point1;
    FloatPoint p2 = point2;

    QPainter *p = m_data->p();
    const bool antiAlias = p->testRenderHint(QPainter::Antialiasing);
    p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines);
    adjustLineToPixelBoundaries(p1, p2, strokeThickness(), strokeStyle());

    IntSize shadowSize;
    int shadowBlur;
    Color shadowColor;
    if (textDrawingMode() == cTextFill && getShadow(shadowSize, shadowBlur, shadowColor)) {
        p->save();
        p->translate(shadowSize.width(), shadowSize.height());
        p->setPen(QColor(shadowColor));
        p->drawLine(p1, p2);
        p->restore();
    }

    p->drawLine(p1, p2);

    p->setRenderHint(QPainter::Antialiasing, antiAlias);
}
Exemple #2
0
void Needle::Draw(
	QPainter& P, 		//! The painter object
	qreal fAngleDeg 	//! Needle angle given in degrees.
)
{
	P.save();
	
	P.rotate(-fAngleDeg);
	if(m_eType != tPlain && m_eType != tPlainLabel) {
		bool bActive = P.testRenderHint(QPainter::SmoothPixmapTransform);
		if(!bActive)
			P.setRenderHint(QPainter::SmoothPixmapTransform, true);
		P.drawPixmap(QPointF(0.0,-m_size.height()/2.0), m_px);
		if(!bActive)
			P.setRenderHint(QPainter::SmoothPixmapTransform, false);
	}
	else {
		P.setPen(QPen(Qt::white, m_size.height()));
		P.drawLine(m_iOffset, 0, m_size.width(), 0);
	}
	if(m_eType == tPlainLabel) {
		qreal fH = 9;
		P.setPen(QPen(Qt::white, 2));
		P.drawEllipse(0.5*m_size.width()-fH/2, -fH/2, fH,fH);
	}

	P.restore();
}
void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
{
    if (paintingDisabled() || strokeStyle() == NoStroke || strokeThickness() <= 0.0f || !strokeColor().alpha())
        return;

    QPainter *p = m_data->p();
    const bool antiAlias = p->testRenderHint(QPainter::Antialiasing);
    p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines);

    p->drawArc(rect, startAngle * 16, angleSpan * 16);

    p->setRenderHint(QPainter::Antialiasing, antiAlias);
}
// Draws a filled rectangle with a stroked border.
void GraphicsContext::drawRect(const IntRect& rect)
{
    if (paintingDisabled())
        return;

    QPainter *p = m_data->p();
    const bool antiAlias = p->testRenderHint(QPainter::Antialiasing);
    p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines);

    p->drawRect(rect);

    p->setRenderHint(QPainter::Antialiasing, antiAlias);
}
void KoShapeShadow::paint(KoShape *shape, QPainter &painter, const KoViewConverter &converter)
{
    if (! d->visible)
        return;

    // So the approach we are taking here is to draw into a buffer image the size of boundingRect
    // We offset by the shadow offset at the time we draw into the buffer
    // Then we filter the image and draw it at the position of the bounding rect on canvas

    //the boundingRect of the shape or the KoSelection boundingRect of the group
    QRectF shadowRect = shape->boundingRect();
    QRectF zoomedClipRegion = converter.documentToView(shadowRect);

    // Init the buffer image
    QImage sourceGraphic(zoomedClipRegion.size().toSize(), QImage::Format_ARGB32_Premultiplied);
    sourceGraphic.fill(qRgba(0,0,0,0));
    // Init the buffer painter
    QPainter imagePainter(&sourceGraphic);
    imagePainter.setPen(Qt::NoPen);
    imagePainter.setBrush(Qt::NoBrush);
    imagePainter.setRenderHint(QPainter::Antialiasing, painter.testRenderHint(QPainter::Antialiasing));
    // Since our imagebuffer and the canvas don't align we need to offset our drawings
    imagePainter.translate(-1.0f*converter.documentToView(shadowRect.topLeft()));

    // Handle the shadow offset
    imagePainter.translate(converter.documentToView(offset()));

    KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(shape);
    if (group) {
        d->paintGroupShadow(group, imagePainter, converter);
    } else {
        //apply shape's transformation
        imagePainter.setTransform(shape->absoluteTransformation(&converter), true);

        d->paintShadow(shape, imagePainter, converter);
    }
    imagePainter.end();

    // Blur the shadow (well the entire buffer)
    d->blurShadow(sourceGraphic, converter.documentToViewX(d->blur), d->color);

    // Paint the result
    painter.save();
    // The painter is initialized for us with canvas transform 'plus' shape transform
    // we are only interested in the canvas transform so 'subtract' the shape transform part
    painter.setTransform(shape->absoluteTransformation(&converter).inverted() * painter.transform());
    painter.drawImage(zoomedClipRegion.topLeft(), sourceGraphic);
    painter.restore();
}
// This is only used to draw borders.
void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
{
    if (paintingDisabled())
        return;

    FloatPoint p1 = point1;
    FloatPoint p2 = point2;

    QPainter *p = m_data->p();
    const bool antiAlias = p->testRenderHint(QPainter::Antialiasing);
    p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines);

    adjustLineToPixelBoundaries(p1, p2, strokeThickness(), strokeStyle());
    p->drawLine(p1, p2);

    p->setRenderHint(QPainter::Antialiasing, antiAlias);
}
void GraphicsContext::drawFocusRing(const Color& color)
{
    if (paintingDisabled())
        return;

    const Vector<IntRect>& rects = focusRingRects();
    unsigned rectCount = rects.size();

    if (rects.size() == 0)
        return;

    QPainter *p = m_data->p();
    const bool antiAlias = p->testRenderHint(QPainter::Antialiasing);
    p->setRenderHint(QPainter::Antialiasing, m_data->antiAliasingForRectsAndLines);

    const QPen oldPen = p->pen();
    const QBrush oldBrush = p->brush();

    QPen nPen = p->pen();
    nPen.setColor(color);
    p->setBrush(Qt::NoBrush);
    nPen.setStyle(Qt::DotLine);
    p->setPen(nPen);
#if 0
    // FIXME How do we do a bounding outline with Qt?
    QPainterPath path;
    for (int i = 0; i < rectCount; ++i)
        path.addRect(QRectF(rects[i]));
    QPainterPathStroker stroker;
    QPainterPath newPath = stroker.createStroke(path);
    p->strokePath(newPath, nPen);
#else
    for (int i = 0; i < rectCount; ++i)
        p->drawRect(QRectF(rects[i]));
#endif
    p->setPen(oldPen);
    p->setBrush(oldBrush);

    p->setRenderHint(QPainter::Antialiasing, antiAlias);
}
Exemple #8
0
void
PixmapRenderer::drawPixmap(
	QPainter& painter, QPixmap const& pixmap)
{
#if !defined(Q_WS_X11)
	drawPixmapNoXRender(painter, pixmap);
#else
	QPaintDevice* const dev = painter.device();
	
	QPoint offset; // Both x and y will be either zero or negative.
	QPaintDevice* const redir_dev = QPainter::redirected(painter.device(), &offset);
	QPaintDevice* const paint_dev = redir_dev ? redir_dev : dev;

#if defined(ENABLE_OPENGL)
	if (dynamic_cast<QGLWidget*>(paint_dev)) {
		drawPixmapNoXRender(painter, pixmap);
		return;
	}
#endif

	QRect const device_rect(
		QRect(0, 0, dev->width(), dev->height()).translated(-offset)
	);
	
	QRectF const src_rect(pixmap.rect());
	
	Display* const dpy = QX11Info::display();
	Picture const src_pict = pixmap.x11PictureHandle();
	Picture dst_pict = 0;
	if (QWidget* widget = dynamic_cast<QWidget*>(paint_dev)) {
		dst_pict = widget->x11PictureHandle();
	} else if (QPixmap* pixmap = dynamic_cast<QPixmap*>(paint_dev)) {
		dst_pict = pixmap->x11PictureHandle();
	}
	
	if (!dst_pict) {
		drawPixmapNoXRender(painter, pixmap);
		return;
	}
	
	// Note that device transform already accounts for offset
	// within a destination surface.
	QTransform const src_to_dst(painter.deviceTransform());
	QTransform const dst_to_src(src_to_dst.inverted());
	QPolygonF const dst_poly(src_to_dst.map(src_rect));
	
	XTransform xform = {{
		{
			XDoubleToFixed(dst_to_src.m11()),
			XDoubleToFixed(dst_to_src.m21()),
			XDoubleToFixed(dst_to_src.m31())
		},
		{
			XDoubleToFixed(dst_to_src.m12()),
			XDoubleToFixed(dst_to_src.m22()),
			XDoubleToFixed(dst_to_src.m32())
		},
		{
			XDoubleToFixed(dst_to_src.m13()),
			XDoubleToFixed(dst_to_src.m23()),
			XDoubleToFixed(dst_to_src.m33())
		}
	}};
	
	XRenderSetPictureTransform(dpy, src_pict, &xform);

	char const* filter = "fast";
	if (painter.testRenderHint(QPainter::SmoothPixmapTransform)) {
		filter = "good";
	}
	
	XRenderSetPictureFilter(dpy, src_pict, filter, 0, 0);
	
	QRectF const dst_rect_precise(dst_poly.boundingRect());
	QRect const dst_rect_fitting(
		QPoint(
			int(ceil(dst_rect_precise.left())),
			int(ceil(dst_rect_precise.top()))
		),
		QPoint(
			int(floor(dst_rect_precise.right())) - 1,
			int(floor(dst_rect_precise.bottom())) - 1
		)
	);
	QRect dst_bounding_rect(device_rect);
	if (painter.hasClipping()) {
		QRect const clip_rect(
			src_to_dst.map(painter.clipPath()).boundingRect().toRect()
		);
		dst_bounding_rect = dst_bounding_rect.intersected(clip_rect);
	}
	QRect const dst_rect(dst_rect_fitting.intersect(dst_bounding_rect));
	
	// Note that XRenderComposite() expects destination coordinates
	// everywhere, even for source picture origin.
	XRenderComposite(
		dpy, PictOpSrc,
		src_pict, 0, dst_pict, dst_rect.left(), dst_rect.top(), 0, 0,
		dst_rect.left(), dst_rect.top(), dst_rect.width(), dst_rect.height()
	);
#endif
}
Exemple #9
0
void QDeclarativeRectangle::drawRect(QPainter &p)
{
    Q_D(QDeclarativeRectangle);
    if ((d->gradient && d->gradient->gradient())
        || d->radius > width()/2 || d->radius > height()/2
        || width() < 3 || height() < 3) {
        // XXX This path is still slower than the image path
        // Image path won't work for gradients or invalid radius though
        bool oldAA = p.testRenderHint(QPainter::Antialiasing);
        if (d->smooth)
            p.setRenderHint(QPainter::Antialiasing);
        if (d->pen && d->pen->isValid()) {
            QPen pn(QColor(d->pen->color()), d->pen->width());
            pn.setJoinStyle(Qt::MiterJoin);
            p.setPen(pn);
        } else {
            p.setPen(Qt::NoPen);
        }
        if (d->gradient && d->gradient->gradient())
            p.setBrush(*d->gradient->gradient());
        else
            p.setBrush(d->color);
        const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
        QRectF rect;
        if (pw%2)
            rect = QRectF(0.5, 0.5, width()-1, height()-1);
        else
            rect = QRectF(0, 0, width(), height());
        qreal radius = d->radius;
        if (radius > width()/2 || radius > height()/2)
            radius = qMin(width()/2, height()/2);
        if (radius > 0.)
            p.drawRoundedRect(rect, radius, radius);
        else
            p.drawRect(rect);
        if (d->smooth)
            p.setRenderHint(QPainter::Antialiasing, oldAA);
    } else {
        bool oldAA = p.testRenderHint(QPainter::Antialiasing);
        bool oldSmooth = p.testRenderHint(QPainter::SmoothPixmapTransform);
        if (d->smooth)
            p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);

        const int pw = d->pen && d->pen->isValid() ? (d->pen->width()+1)/2*2 : 0;

        if (d->radius > 0)
            generateRoundedRect();
        else
            generateBorderedRect();

        int xOffset = (d->rectImage.width()-1)/2;
        int yOffset = (d->rectImage.height()-1)/2;
        Q_ASSERT(d->rectImage.width() == 2*xOffset + 1);
        Q_ASSERT(d->rectImage.height() == 2*yOffset + 1);

        // check whether we've eliminated the center completely
        if (2*xOffset > width()+pw)
            xOffset = (width()+pw)/2;
        if (2*yOffset > height()+pw)
            yOffset = (height()+pw)/2;

        QMargins margins(xOffset, yOffset, xOffset, yOffset);
        QTileRules rules(Qt::StretchTile, Qt::StretchTile);
        //NOTE: even though our item may have qreal-based width and height, qDrawBorderPixmap only supports QRects
        qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width()+pw, height()+pw), margins, d->rectImage, d->rectImage.rect(), margins, rules);

        if (d->smooth) {
            p.setRenderHint(QPainter::Antialiasing, oldAA);
            p.setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
        }
    }
}