silvansky programming stuff

5Apr/110

nine part pixmap

Код для отрисовки картинки из 9 частей (типа border-image). Принимает QImage, но может быть модифицирован для работы напрямую с QPixmap.

void drawNinePartImage(const QImage &image, QRectF paintRect, qreal borderLeft, qreal borderRight, qreal borderTop, qreal borderBottom, QPainter * painter)
{
	QPixmap bg = QPixmap::fromImage(image);
	// source size
	qreal w = bg.width();
	qreal h = bg.height();
	// target size
	qreal tw = paintRect.width();
	qreal th = paintRect.height();
/*
 
+-------+-------------------+-------+
|       |                   |       |
|   0   |         1         |   2   |
|       |                   |       |
+-------+-------------------+-------+
|       |                   |       |
|   3   |         4         |   5   |
|       |                   |       |
+-------+-------------------+-------+
|       |                   |       |
|   6   |         7         |   8   |
|       |                   |       |
+-------+-------------------+-------+
 
*/
	QPainter::PixmapFragment fragments[9]; // we'll draw 9-part pixmap
	qreal hborders = borderLeft + borderRight;
	qreal vborders = borderTop + borderBottom;
	qreal sx = (tw - hborders) / (w - hborders), sy = (th - vborders) / (h - vborders);
	qreal hbLeft = borderLeft / 2.0;
	qreal hbRight = borderRight / 2.0;
	qreal hbTop = borderTop / 2.0;
	qreal hbBottom = borderBottom / 2.0;
	fragments[0] = QPainter::PixmapFragment::create(QPointF(hbLeft, hbTop),
							QRectF(0, 0, borderLeft, borderTop));
	fragments[1] = QPainter::PixmapFragment::create(QPointF(borderLeft + (tw - hborders) / 2.0, hbTop),
							QRectF(borderLeft, 0, w - hborders, borderTop),
							sx);
	fragments[2] = QPainter::PixmapFragment::create(QPointF(tw - hbRight, hbTop),
							QRectF(w - borderRight, 0, borderRight, borderTop));
	fragments[3] = QPainter::PixmapFragment::create(QPointF(hbLeft, borderTop + (th - hborders) / 2.0),
							QRectF(0, borderTop, borderLeft, h - hborders),
							1.0,
							sy);
	fragments[4] = QPainter::PixmapFragment::create(QPointF(borderLeft + (tw - hborders) / 2.0, borderTop + (th - vborders) / 2.0),
							QRectF(borderLeft, borderTop, w - hborders, h - vborders),
							sx,
							sy);
	fragments[5] = QPainter::PixmapFragment::create(QPointF(tw - hbRight, borderTop + (th - vborders) / 2.0),
							QRectF(w - borderRight, borderTop, borderRight, h - vborders),
							1,
							sy);
	fragments[6] = QPainter::PixmapFragment::create(QPointF(hbLeft, th - hbBottom),
							QRectF(0, h - borderBottom, borderLeft, borderBottom));
	fragments[7] = QPainter::PixmapFragment::create(QPointF(borderLeft + (tw - hborders) / 2.0, th - hbBottom),
							QRectF(borderLeft, h - borderBottom, w - hborders, borderBottom),
							sx);
	fragments[8] = QPainter::PixmapFragment::create(QPointF(tw - hbRight, th - hbBottom),
							QRectF(w - borderRight, h - borderBottom, borderRight, borderBottom));
 
	painter->drawPixmapFragments(fragments, 9, bg);
}
 
Tagged as: , , , No Comments