{
const int gridSize = 25;
if (backgroundCache.isNull()) {
backgroundCache
= QPixmap(gridSize, gridSize
);
const int middle = gridSize / 2;
QPainter backgroundPainter
(&backgroundCache
);
backgroundPainter.setRenderHints(painter->renderHints());
backgroundPainter.
fillRect(QRect(0,
0, gridSize, gridSize
),
QBrush(Qt
::white));
backgroundPainter.setPen(backgroundPen);
backgroundPainter.setBrush(backgroundBrush);
backgroundPainter.drawLine(0, middle, gridSize, middle);
backgroundPainter.drawLine(middle, 0, middle, gridSize);
}
const int realLeft = static_cast<int>(std::floor(rect.left()));
const int realRight = static_cast<int>(std::ceil(rect.right()));
const int realTop = static_cast<int>(std::floor(rect.top()));
const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
const int firstLeftGridLine = realLeft - (realLeft % gridSize);
const int firstTopGridLine = realTop - (realTop % gridSize);
for (int x = firstLeftGridLine; x < realRight; x += gridSize) {
for (int y = firstTopGridLine; y < realBottom; y += gridSize) {
painter->drawPixmap(x, y, backgroundCache);
}
}
}
void drawBackground(QPainter *painter, const QRectF &rect)
{
const int gridSize = 25;
if (backgroundCache.isNull()) {
backgroundCache = QPixmap(gridSize, gridSize);
const int middle = gridSize / 2;
QPainter backgroundPainter(&backgroundCache);
backgroundPainter.setRenderHints(painter->renderHints());
backgroundPainter.fillRect(QRect(0, 0, gridSize, gridSize), QBrush(Qt::white));
backgroundPainter.setPen(backgroundPen);
backgroundPainter.setBrush(backgroundBrush);
backgroundPainter.drawLine(0, middle, gridSize, middle);
backgroundPainter.drawLine(middle, 0, middle, gridSize);
}
const int realLeft = static_cast<int>(std::floor(rect.left()));
const int realRight = static_cast<int>(std::ceil(rect.right()));
const int realTop = static_cast<int>(std::floor(rect.top()));
const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
const int firstLeftGridLine = realLeft - (realLeft % gridSize);
const int firstTopGridLine = realTop - (realTop % gridSize);
QPainterPath background;
for (int x = firstLeftGridLine; x < realRight; x += gridSize) {
for (int y = firstTopGridLine; y < realBottom; y += gridSize) {
painter->drawPixmap(x, y, backgroundCache);
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks