45{
46 if (!
m_d->config.showGrid())
return;
47
48 Q_UNUSED(canvas);
49
51
53 const int minWidgetSize = 3;
54 const int effectiveSize = qMin(
m_d->config.spacing().x(),
m_d->config.spacing().y());
55
56 int scaleCoeff = 1;
57 quint32 subdivision =
m_d->config.subdivision();
58
59 while (qFloor(scale * scaleCoeff * effectiveSize) <= minWidgetSize) {
60 if (subdivision > 1) {
61 scaleCoeff = subdivision;
62 subdivision = 1;
63 } else {
64 scaleCoeff *= 2;
65 }
66
67 if (scaleCoeff > 32768) {
68 qWarning() << "WARNING: Grid Scale Coeff is too high! That is surely a bug!";
69 return;
70 }
71 }
72
73 QPen mainPen =
m_d->config.penMain();
74 QPen subdivisionPen =
m_d->config.penSubdivision();
75
76 gc.save();
77 gc.setTransform(transform);
78
80 const QRectF updateRectInImagePixels =
82 imageRectInImagePixels;
83
84
85
87
89 gc.setRenderHints(QPainter::Antialiasing, false);
90 qreal x1, y1, x2, y2;
91 updateRectInImagePixels.getCoords(&x1, &y1, &x2, &y2);
92
93
94
95 x2++;
96 y2++;
97
98 if (
m_d->config.xSpacingActive()) {
99
100 int offset = 0;
101 if (
m_d->config.offsetActive()) {
102 offset =
m_d->config.offset().x();
103 }
104 const int step = scaleCoeff *
m_d->config.spacing().x();
105 const int lineIndexFirst = qCeil((x1 - offset) / step);
106 const int lineIndexLast = qFloor((x2 - offset) / step);
107
108 if (mainPen.style() != Qt::SolidLine) {
109 mainPen.setDashOffset(y1 * scale);
110 }
111 if (subdivisionPen.style() != Qt::SolidLine) {
112 subdivisionPen.setDashOffset(y1 * scale);
113 }
114
115 for (int i = lineIndexFirst; i <= lineIndexLast; i++) {
116 int w = offset + i * step;
117
118 gc.setPen(i % subdivision == 0 ? mainPen : subdivisionPen);
119
120 gc.drawLine(QPointF(w, y1),QPointF(w, qMin(y2, qreal(imageRectInImagePixels.bottom() + 1))));
121 }
122 }
123
124 if (
m_d->config.ySpacingActive()) {
125
126 int offset = 0;
127 if (
m_d->config.offsetActive()) {
128 offset =
m_d->config.offset().y();
129 }
130 const int step = scaleCoeff *
m_d->config.spacing().y();
131 const int lineIndexFirst = qCeil((y1 - offset) / step);
132 const int lineIndexLast = qFloor((y2 - offset) / step);
133
134 if (mainPen.style() != Qt::SolidLine) {
135 mainPen.setDashOffset(x1 * scale);
136 }
137 if (subdivisionPen.style() != Qt::SolidLine) {
138 subdivisionPen.setDashOffset(x1 * scale);
139 }
140
141 for (int i = lineIndexFirst; i <= lineIndexLast; i++) {
142 int w = offset + i * step;
143
144 gc.setPen(i % subdivision == 0 ? mainPen : subdivisionPen);
145
146 gc.drawLine(QPointF(x1, w),QPointF(qMin(x2, qreal(imageRectInImagePixels.right() + 1)), w));
147 }
148 }
150 qreal x1, y1, x2, y2;
151
152
154 trueImageRect.getCoords(&x1, &y1, &x2, &y2);
155
156
157
158 x2++;
159 y2++;
160
161 int offset = 0;
162 int offsetY = 0;
163 if (
m_d->config.offsetActive()) {
164 offset =
m_d->config.offset().x();
165 offsetY =
m_d->config.offset().y();
166 }
167 const int cellSpacing =
m_d->config.cellSpacing();
168
169 gc.setClipping(true);
170 gc.setClipRect(updateRectInImagePixels, Qt::IntersectClip);
171
172
173 if (
m_d->config.angleLeftActive()) {
174 const qreal gridXAngle =
m_d->config.angleLeft();
175 const qreal bottomRightOfImageY = y2;
176 qreal finalY = 0.0;
177
178
179
180 qreal correctedAngleSpacing = cellSpacing;
181 if (gridXAngle > 0.0) {
182 correctedAngleSpacing = cellSpacing / qCos(qDegreesToRadians(gridXAngle));
183 gc.setRenderHints(QPainter::Antialiasing, true);
184 } else {
185
186 gc.setRenderHints(QPainter::Antialiasing, false);
187 }
188
189 qreal counter = qFloor((-(offset + offsetY)) / correctedAngleSpacing);
190
191 while (finalY < bottomRightOfImageY) {
192
193 const qreal
w = (counter * correctedAngleSpacing) + offsetY + offset;
194 gc.setPen(mainPen);
195
196
197 const qreal startingY =
w;
198 const qreal horizontalDistance = x2;
199
200
201 const qreal length2 = qTan(qDegreesToRadians(gridXAngle)) * x2;
202
203 finalY = startingY - length2;
204
205 gc.drawLine(QPointF(x1, w), QPointF(horizontalDistance, finalY));
206
207 counter = counter + 1.0;
208 }
209 }
210
211
212 if (
m_d->config.angleRightActive()) {
213 const qreal gridXAngle =
m_d->config.angleRight();
214 const qreal bottomLeftOfImageY = y2;
215
216
217 qreal correctedAngleSpacing = cellSpacing;
218 if (gridXAngle > 0.0) {
219 correctedAngleSpacing = cellSpacing / qCos(qDegreesToRadians(gridXAngle));
220 gc.setRenderHints(QPainter::Antialiasing, true);
221 } else {
222
223 gc.setRenderHints(QPainter::Antialiasing, false);
224 }
225
226
227 const qreal horizontalDistance = x2;
228
229 const qreal length2 = qTan(qDegreesToRadians(gridXAngle)) * horizontalDistance;
230
231
232 const qreal yLower = 0.0;
233 const qreal yHigher = yLower - length2;
234
235 const qreal yLeftFirst = qCeil(yHigher / correctedAngleSpacing) * correctedAngleSpacing;
236 const qreal additionalOffset = yLeftFirst - yHigher;
237 qreal finalY = 0.0;
238 qreal counter = qFloor((-(offsetY - offset)) / correctedAngleSpacing);
239
240 while (finalY < bottomLeftOfImageY) {
241
242 const qreal
w = (counter * correctedAngleSpacing) + offsetY - offset + additionalOffset;
243 gc.setPen(mainPen);
244
245
246 const qreal startingY =
w;
247
248 finalY = startingY - length2;
249
250 gc.drawLine(QPointF(x2, w), QPointF(0.0, finalY));
251
252 counter = counter + 1.0;
253 }
254 }
256 qreal x1, y1, xRight, yBottom;
257
258
260 trueImageRect.getCoords(&x1, &y1, &xRight, &yBottom);
261
262
263
264 xRight++;
265 yBottom++;
266
268
269 int offsetX = 0;
270 int offsetY = 0;
271 if (
m_d->config.offsetActive()) {
273 offsetY =
m_d->config.offset().y();
274 }
275
276 gc.setClipping(true);
277 gc.setClipRect(updateRectInImagePixels, Qt::IntersectClip);
278
279
281 if (
m_d->config.angleLeft() > 0.0) {
282 gc.setRenderHints(QPainter::Antialiasing, true);
283 } else {
284
285 gc.setRenderHints(QPainter::Antialiasing, false);
286 }
287
289
290
291 const qreal yLower = 0.0;
292 const qreal yHigher = yLower - length2;
293
295 const qreal additionalOffset = yLeftFirst - yHigher;
296
297 qreal finalY = 0.0;
298
299
301
302 qreal startingY = yLeftFirst + offsetY + offsetX + additionalOffset;
303
304 while (finalY < yBottom) {
305
306 finalY = startingY - length2;
307
308 gc.setPen(qAbs(subdivisionIndex) % subdivision == 0 ? mainPen : subdivisionPen);
309 gc.drawLine(QPointF(0.0, startingY), QPointF(xRight, finalY));
310
311 subdivisionIndex++;
313 }
314 }
315
316
318 if (
m_d->config.angleRight() > 0.0) {
319 gc.setRenderHints(QPainter::Antialiasing, true);
320 } else {
321
322 gc.setRenderHints(QPainter::Antialiasing, false);
323 }
324
326
327
328 const qreal yLower = 0.0;
329 const qreal yHigher = yLower - length2;
330
332 const qreal additionalOffset = yLeftFirst - yHigher;
333
334 qreal finalY = 0.0;
335
336
338
339 qreal startingY = yLeftFirst + offsetY - offsetX + additionalOffset;
340
341 while (finalY < yBottom) {
342
343 finalY = startingY - length2;
344
345 gc.setPen(qAbs(subdivisionIndex) % subdivision == 0 ? mainPen : subdivisionPen);
346 gc.drawLine(QPointF(xRight, startingY), QPointF(0.0, finalY));
347
348 subdivisionIndex++;
350 }
351 }
352
353
355 QPen verticalPen =
m_d->config.penVertical();
356 if (verticalPen.style() != Qt::SolidLine) {
357 verticalPen.setDashOffset(updateRectInImagePixels.top() * scale);
358 }
359 gc.setPen(verticalPen);
360 gc.setRenderHints(QPainter::Antialiasing, false);
361
362 int offset = 0;
363 if (
m_d->config.offsetActive()) {
364 offset =
m_d->config.offset().x();
365 }
366
370
371 while(pX <= updateRectInImagePixels.right()) {
372 if (pX > updateRectInImagePixels.left()) {
373 gc.drawLine(QPointF(pX, updateRectInImagePixels.top()),QPointF(pX, qMin(yBottom, qreal(updateRectInImagePixels.bottom() + 1))));
374 }
376 }
377 }
378 }
379
380 gc.restore();
381}
QTransform imageToWidgetTransform() const
_Private::Traits< T >::Result documentToImage(const T &obj) const
QRect imageRectInImagePixels() const
static qreal approxTransformScale(const QTransform &t)
qreal correctedAngleLeftCellSize
qreal correctedAngleRightOffsetX
qreal correctedAngleRightCellSize