46 if (!
m_d->config.showGrid())
return;
53 const int minWidgetSize = 3;
54 const int effectiveSize = qMin(
m_d->config.spacing().x(),
m_d->config.spacing().y());
57 quint32 subdivision =
m_d->config.subdivision();
59 while (qFloor(scale * scaleCoeff * effectiveSize) <= minWidgetSize) {
60 if (subdivision > 1) {
61 scaleCoeff = subdivision;
67 if (scaleCoeff > 32768) {
68 qWarning() <<
"WARNING: Grid Scale Coeff is too high! That is surely a bug!";
73 QPen mainPen =
m_d->config.penMain();
74 QPen subdivisionPen =
m_d->config.penSubdivision();
77 gc.setTransform(transform);
80 const QRectF updateRectInImagePixels =
82 imageRectInImagePixels;
89 gc.setRenderHints(QPainter::Antialiasing,
false);
91 updateRectInImagePixels.getCoords(&x1, &y1, &x2, &y2);
98 if (
m_d->config.xSpacingActive()) {
101 if (
m_d->config.offsetActive()) {
102 offset =
m_d->config.offset().x();
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);
108 if (mainPen.style() != Qt::SolidLine) {
109 mainPen.setDashOffset(y1 * scale);
111 if (subdivisionPen.style() != Qt::SolidLine) {
112 subdivisionPen.setDashOffset(y1 * scale);
115 for (
int i = lineIndexFirst; i <= lineIndexLast; i++) {
116 int w = offset + i * step;
118 gc.setPen(i % subdivision == 0 ? mainPen : subdivisionPen);
120 gc.drawLine(QPointF(w, y1),QPointF(w, qMin(y2, qreal(imageRectInImagePixels.bottom() + 1))));
124 if (
m_d->config.ySpacingActive()) {
127 if (
m_d->config.offsetActive()) {
128 offset =
m_d->config.offset().y();
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);
134 if (mainPen.style() != Qt::SolidLine) {
135 mainPen.setDashOffset(x1 * scale);
137 if (subdivisionPen.style() != Qt::SolidLine) {
138 subdivisionPen.setDashOffset(x1 * scale);
141 for (
int i = lineIndexFirst; i <= lineIndexLast; i++) {
142 int w = offset + i * step;
144 gc.setPen(i % subdivision == 0 ? mainPen : subdivisionPen);
146 gc.drawLine(QPointF(x1, w),QPointF(qMin(x2, qreal(imageRectInImagePixels.right() + 1)), w));
150 qreal x1, y1, x2, y2;
154 trueImageRect.getCoords(&x1, &y1, &x2, &y2);
163 if (
m_d->config.offsetActive()) {
164 offset =
m_d->config.offset().x();
165 offsetY =
m_d->config.offset().y();
167 const int cellSpacing =
m_d->config.cellSpacing();
169 gc.setClipping(
true);
170 gc.setClipRect(updateRectInImagePixels, Qt::IntersectClip);
173 if (
m_d->config.angleLeftActive()) {
174 const qreal gridXAngle =
m_d->config.angleLeft();
175 const qreal bottomRightOfImageY = y2;
180 qreal correctedAngleSpacing = cellSpacing;
181 if (gridXAngle > 0.0) {
182 correctedAngleSpacing = cellSpacing / qCos(qDegreesToRadians(gridXAngle));
183 gc.setRenderHints(QPainter::Antialiasing,
true);
186 gc.setRenderHints(QPainter::Antialiasing,
false);
189 qreal counter = qFloor((-(offset + offsetY)) / correctedAngleSpacing);
191 while (finalY < bottomRightOfImageY) {
193 const qreal w = (counter * correctedAngleSpacing) + offsetY + offset;
197 const qreal startingY = w;
198 const qreal horizontalDistance = x2;
201 const qreal length2 = qTan(qDegreesToRadians(gridXAngle)) * x2;
203 finalY = startingY - length2;
205 gc.drawLine(QPointF(x1, w), QPointF(horizontalDistance, finalY));
207 counter = counter + 1.0;
212 if (
m_d->config.angleRightActive()) {
213 const qreal gridXAngle =
m_d->config.angleRight();
214 const qreal bottomLeftOfImageY = y2;
217 qreal correctedAngleSpacing = cellSpacing;
218 if (gridXAngle > 0.0) {
219 correctedAngleSpacing = cellSpacing / qCos(qDegreesToRadians(gridXAngle));
220 gc.setRenderHints(QPainter::Antialiasing,
true);
223 gc.setRenderHints(QPainter::Antialiasing,
false);
227 const qreal horizontalDistance = x2;
229 const qreal length2 = qTan(qDegreesToRadians(gridXAngle)) * horizontalDistance;
232 const qreal yLower = 0.0;
233 const qreal yHigher = yLower - length2;
235 const qreal yLeftFirst = qCeil(yHigher / correctedAngleSpacing) * correctedAngleSpacing;
236 const qreal additionalOffset = yLeftFirst - yHigher;
238 qreal counter = qFloor((-(offsetY - offset)) / correctedAngleSpacing);
240 while (finalY < bottomLeftOfImageY) {
242 const qreal w = (counter * correctedAngleSpacing) + offsetY - offset + additionalOffset;
246 const qreal startingY = w;
248 finalY = startingY - length2;
250 gc.drawLine(QPointF(x2, w), QPointF(0.0, finalY));
252 counter = counter + 1.0;
256 qreal x1, y1, xRight, yBottom;
260 trueImageRect.getCoords(&x1, &y1, &xRight, &yBottom);
271 if (
m_d->config.offsetActive()) {
273 offsetY =
m_d->config.offset().y();
276 gc.setClipping(
true);
277 gc.setClipRect(updateRectInImagePixels, Qt::IntersectClip);
281 if (
m_d->config.angleLeft() > 0.0) {
282 gc.setRenderHints(QPainter::Antialiasing,
true);
285 gc.setRenderHints(QPainter::Antialiasing,
false);
291 const qreal yLower = 0.0;
292 const qreal yHigher = yLower - length2;
295 const qreal additionalOffset = yLeftFirst - yHigher;
302 qreal startingY = yLeftFirst + offsetY + offsetX + additionalOffset;
304 while (finalY < yBottom) {
306 finalY = startingY - length2;
308 gc.setPen(qAbs(subdivisionIndex) % subdivision == 0 ? mainPen : subdivisionPen);
309 gc.drawLine(QPointF(0.0, startingY), QPointF(xRight, finalY));
318 if (
m_d->config.angleRight() > 0.0) {
319 gc.setRenderHints(QPainter::Antialiasing,
true);
322 gc.setRenderHints(QPainter::Antialiasing,
false);
328 const qreal yLower = 0.0;
329 const qreal yHigher = yLower - length2;
332 const qreal additionalOffset = yLeftFirst - yHigher;
339 qreal startingY = yLeftFirst + offsetY - offsetX + additionalOffset;
341 while (finalY < yBottom) {
343 finalY = startingY - length2;
345 gc.setPen(qAbs(subdivisionIndex) % subdivision == 0 ? mainPen : subdivisionPen);
346 gc.drawLine(QPointF(xRight, startingY), QPointF(0.0, finalY));
355 QPen verticalPen =
m_d->config.penVertical();
356 if (verticalPen.style() != Qt::SolidLine) {
357 verticalPen.setDashOffset(updateRectInImagePixels.top() * scale);
359 gc.setPen(verticalPen);
360 gc.setRenderHints(QPainter::Antialiasing,
false);
363 if (
m_d->config.offsetActive()) {
364 offset =
m_d->config.offset().x();
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))));