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