17#include "tiledbuffer.h"
19#include <QPainterPath>
28TiledBuffer::~TiledBuffer()
33Tile* TiledBuffer::getTileFromIndex(
const TileIndex& tileIndex)
35 Tile* selectedTile = mTiles.
value(tileIndex,
nullptr);
39 const QPoint& tilePos (getTilePos(tileIndex));
40 selectedTile =
new Tile(tilePos,
QSize(UNIFORM_TILE_SIZE, UNIFORM_TILE_SIZE));
41 mTiles.
insert(tileIndex, selectedTile);
43 emit this->tileCreated(
this, selectedTile);
45 emit this->tileUpdated(
this, selectedTile);
52 const float tileSize = UNIFORM_TILE_SIZE;
55 const int xLeft = qFloor((qFloor(point.
x() - brushWidth)) / tileSize);
56 const int xRight = qFloor((qFloor(point.
x() + brushWidth)) / tileSize);
57 const int yTop = qFloor(qFloor(point.
y() - brushWidth) / tileSize);
58 const int yBottom = qFloor(qFloor(point.
y() + brushWidth) / tileSize);
61 bool drawPoint =
false;
62 if (!antialiasing && brushWidth < 1.42) {
64 const QPointF nearestPixelCenter(qRound(point.
x()+0.5)-0.5, qRound(point.
y()+0.5)-0.5);
65 const qreal distanceToNearest =
QLineF(point, nearestPixelCenter).
length();
66 if (distanceToNearest >= brushWidth/2) {
69 point =
QPointF(nearestPixelCenter.
x() - 0.5, nearestPixelCenter.
y() - 0.5);
73 const QRectF brushRect(point.
x() - 0.5 * brushWidth, point.
y() - 0.5 * brushWidth, brushWidth, brushWidth);
75 for (
int tileY = yTop; tileY <= yBottom; tileY++) {
76 for (
int tileX = xLeft; tileX <= xRight; tileX++) {
78 Tile* tile = getTileFromIndex({tileX, tileY});
82 painter.translate(-tile->pos());
85 painter.setBrush(brush);
86 painter.setCompositionMode(cm);
88 painter.drawPoint(point);
90 painter.drawEllipse(brushRect);
94 mTileBounds.extend(tile->bounds());
100 const float tileSize = UNIFORM_TILE_SIZE;
101 const float imageXRad = image.
width();
102 const float imageYRad = image.
height();
104 const int xLeft = qFloor((qFloor(imageBounds.
left() - imageXRad)) / tileSize);
105 const int xRight = qFloor((qFloor(imageBounds.
right() + imageXRad)) / tileSize);
106 const int yTop = qFloor(qFloor(imageBounds.
top() - imageYRad) / tileSize);
107 const int yBottom = qFloor(qFloor(imageBounds.
bottom() + imageYRad) / tileSize);
109 for (
int tileY = yTop; tileY <= yBottom; tileY++) {
110 for (
int tileX = xLeft; tileX <= xRight; tileX++) {
112 Tile* tile = getTileFromIndex({tileX, tileY});
116 painter.translate(-tile->pos());
118 painter.setCompositionMode(cm);
119 painter.drawImage(imageBounds.
topLeft(), image);
122 mTileBounds.extend(tile->bounds());
131 const qreal width = pen.
widthF();
132 const float tileSize = UNIFORM_TILE_SIZE;
136 const int xLeft = qFloor((qFloor(pathRect.
left() - width)) / tileSize);
137 const int xRight = qFloor((qFloor(pathRect.
right() + width)) / tileSize);
138 const int yTop = qFloor(qFloor(pathRect.
top() - width) / tileSize);
139 const int yBottom = qFloor(qFloor(pathRect.
bottom() + width) / tileSize);
141 for (
int tileY = yTop; tileY <= yBottom; tileY++) {
142 for (
int tileX = xLeft; tileX <= xRight; tileX++) {
144 Tile* tile = getTileFromIndex({tileX, tileY});
148 painter.translate(-tile->pos());
151 painter.setBrush(brush);
152 painter.setCompositionMode(cm);
153 painter.drawPath(path);
156 mTileBounds.extend(tile->bounds());
180 return QPoint { qRound(UNIFORM_TILE_SIZE*
static_cast<qreal
>(index.x)),
181 qRound(UNIFORM_TILE_SIZE*
static_cast<qreal
>(index.y)) };
void drawPath(QPainterPath path, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing)
Draws a path with the specified parameters to the tiled buffer.
void clear()
Clears the content of the tiled buffer.
void drawImage(const QImage &image, const QRect &imageBounds, QPainter::CompositionMode cm, bool antialiasing)
Draws a image with the specified parameters to the tiled buffer.
void drawBrush(QPointF point, qreal brushWidth, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing)
Draws a brush with the specified parameters to the tiled buffer.
QHash::iterator insert(const Key &key, const T &value)
int remove(const Key &key)
const T value(const Key &key) const const
bool hasNext() const const
const Key & key() const const
QHashIterator::Item next()
const T & value() const const
qreal length() const const
QRectF boundingRect() const const
qreal widthF() const const
QPoint topLeft() const const
qreal bottom() const const
qreal right() const const