48 if (!
m_d->config.showGrid())
return;
55 const int minWidgetSize = 3;
56 const int effectiveSize = qMin(
m_d->config.spacing().x(),
m_d->config.spacing().y());
59 quint32 subdivision =
m_d->config.subdivision();
61 while (qFloor(scale * scaleCoeff * effectiveSize) <= minWidgetSize) {
62 if (subdivision > 1) {
63 scaleCoeff = subdivision;
69 if (scaleCoeff > 32768) {
70 qWarning() <<
"WARNING: Grid Scale Coeff is too high! That is surely a bug!";
75 QPen mainPen =
m_d->config.penMain();
76 QPen subdivisionPen =
m_d->config.penSubdivision();
82 gc.setTransform(transform);
85 const QRectF updateRectInImagePixels =
87 imageRectInImagePixels;
94 gc.setRenderHints(QPainter::Antialiasing,
false);
96 updateRectInImagePixels.getCoords(&x1, &y1, &x2, &y2);
103 if (
m_d->config.xSpacingActive()) {
106 if (
m_d->config.offsetActive()) {
107 offset =
m_d->config.offset().x();
109 const int step = scaleCoeff *
m_d->config.spacing().x();
110 const int lineIndexFirst = qCeil((x1 - offset) / step);
111 const int lineIndexLast = qFloor((x2 - offset) / step);
113 if (mainPen.style() != Qt::SolidLine) {
114 mainPen.setDashOffset(y1 * scale);
116 if (subdivisionPen.style() != Qt::SolidLine) {
117 subdivisionPen.setDashOffset(y1 * scale);
120 for (
int i = lineIndexFirst; i <= lineIndexLast; i++) {
121 int w = offset + i * step;
123 gc.setPen(i % subdivision == 0 ? mainPen : subdivisionPen);
125 gc.drawLine(QPointF(w, y1),QPointF(w, qMin(y2, qreal(imageRectInImagePixels.bottom() + 1))));
129 if (
m_d->config.ySpacingActive()) {
132 if (
m_d->config.offsetActive()) {
133 offset =
m_d->config.offset().y();
135 const int step = scaleCoeff *
m_d->config.spacing().y();
136 const int lineIndexFirst = qCeil((y1 - offset) / step);
137 const int lineIndexLast = qFloor((y2 - offset) / step);
139 if (mainPen.style() != Qt::SolidLine) {
140 mainPen.setDashOffset(x1 * scale);
142 if (subdivisionPen.style() != Qt::SolidLine) {
143 subdivisionPen.setDashOffset(x1 * scale);
146 for (
int i = lineIndexFirst; i <= lineIndexLast; i++) {
147 int w = offset + i * step;
149 gc.setPen(i % subdivision == 0 ? mainPen : subdivisionPen);
151 gc.drawLine(QPointF(x1, w),QPointF(qMin(x2, qreal(imageRectInImagePixels.right() + 1)), w));
155 qreal x1, y1, x2, y2;
159 trueImageRect.getCoords(&x1, &y1, &x2, &y2);
168 if (
m_d->config.offsetActive()) {
169 offset =
m_d->config.offset().x();
170 offsetY =
m_d->config.offset().y();
172 const int cellSpacing =
m_d->config.cellSpacing();
174 gc.setClipping(
true);
175 gc.setClipRect(updateRectInImagePixels, Qt::IntersectClip);
178 if (
m_d->config.angleLeftActive()) {
179 const qreal gridXAngle =
m_d->config.angleLeft();
180 const qreal bottomRightOfImageY = y2;
185 qreal correctedAngleSpacing = cellSpacing;
186 if (gridXAngle > 0.0) {
187 correctedAngleSpacing = cellSpacing / qCos(qDegreesToRadians(gridXAngle));
188 gc.setRenderHints(QPainter::Antialiasing,
true);
191 gc.setRenderHints(QPainter::Antialiasing,
false);
194 qreal counter = qFloor((-(offset + offsetY)) / correctedAngleSpacing);
196 while (finalY < bottomRightOfImageY) {
198 const qreal w = (counter * correctedAngleSpacing) + offsetY + offset;
202 const qreal startingY = w;
203 const qreal horizontalDistance = x2;
206 const qreal length2 = qTan(qDegreesToRadians(gridXAngle)) * x2;
208 finalY = startingY - length2;
210 gc.drawLine(QPointF(x1, w), QPointF(horizontalDistance, finalY));
212 counter = counter + 1.0;
217 if (
m_d->config.angleRightActive()) {
218 const qreal gridXAngle =
m_d->config.angleRight();
219 const qreal bottomLeftOfImageY = y2;
222 qreal correctedAngleSpacing = cellSpacing;
223 if (gridXAngle > 0.0) {
224 correctedAngleSpacing = cellSpacing / qCos(qDegreesToRadians(gridXAngle));
225 gc.setRenderHints(QPainter::Antialiasing,
true);
228 gc.setRenderHints(QPainter::Antialiasing,
false);
232 const qreal horizontalDistance = x2;
234 const qreal length2 = qTan(qDegreesToRadians(gridXAngle)) * horizontalDistance;
237 const qreal yLower = 0.0;
238 const qreal yHigher = yLower - length2;
240 const qreal yLeftFirst = qCeil(yHigher / correctedAngleSpacing) * correctedAngleSpacing;
241 const qreal additionalOffset = yLeftFirst - yHigher;
243 qreal counter = qFloor((-(offsetY - offset)) / correctedAngleSpacing);
245 while (finalY < bottomLeftOfImageY) {
247 const qreal w = (counter * correctedAngleSpacing) + offsetY - offset + additionalOffset;
251 const qreal startingY = w;
253 finalY = startingY - length2;
255 gc.drawLine(QPointF(x2, w), QPointF(0.0, finalY));
257 counter = counter + 1.0;
261 qreal x1, y1, xRight, yBottom;
265 trueImageRect.getCoords(&x1, &y1, &xRight, &yBottom);
276 if (
m_d->config.offsetActive()) {
278 offsetY =
m_d->config.offset().y();
281 gc.setClipping(
true);
282 gc.setClipRect(updateRectInImagePixels, Qt::IntersectClip);
286 if (
m_d->config.angleLeft() > 0.0) {
287 gc.setRenderHints(QPainter::Antialiasing,
true);
290 gc.setRenderHints(QPainter::Antialiasing,
false);
296 const qreal yLower = 0.0;
297 const qreal yHigher = yLower - length2;
300 const qreal additionalOffset = yLeftFirst - yHigher;
307 qreal startingY = yLeftFirst + offsetY + offsetX + additionalOffset;
309 while (finalY < yBottom) {
311 finalY = startingY - length2;
313 gc.setPen(qAbs(subdivisionIndex) % subdivision == 0 ? mainPen : subdivisionPen);
314 gc.drawLine(QPointF(0.0, startingY), QPointF(xRight, finalY));
323 if (
m_d->config.angleRight() > 0.0) {
324 gc.setRenderHints(QPainter::Antialiasing,
true);
327 gc.setRenderHints(QPainter::Antialiasing,
false);
333 const qreal yLower = 0.0;
334 const qreal yHigher = yLower - length2;
337 const qreal additionalOffset = yLeftFirst - yHigher;
344 qreal startingY = yLeftFirst + offsetY - offsetX + additionalOffset;
346 while (finalY < yBottom) {
348 finalY = startingY - length2;
350 gc.setPen(qAbs(subdivisionIndex) % subdivision == 0 ? mainPen : subdivisionPen);
351 gc.drawLine(QPointF(xRight, startingY), QPointF(0.0, finalY));
360 QPen verticalPen =
m_d->config.penVertical();
361 if (verticalPen.style() != Qt::SolidLine) {
362 verticalPen.setDashOffset(updateRectInImagePixels.top() * scale);
364 gc.setPen(verticalPen);
365 gc.setRenderHints(QPainter::Antialiasing,
false);
368 if (
m_d->config.offsetActive()) {
369 offset =
m_d->config.offset().x();
376 while(pX <= updateRectInImagePixels.right()) {
377 if (pX > updateRectInImagePixels.left()) {
378 gc.drawLine(QPointF(pX, updateRectInImagePixels.top()),QPointF(pX, qMin(yBottom, qreal(updateRectInImagePixels.bottom() + 1))));