22 #include <QStyleOptionGraphicsItem>
27 #define M_DEG2RAD 0.0174532925
32 double rotToRad =
angle * M_PI / 180.0;
34 xRot = x * std::cos( rotToRad ) - y * std::sin( rotToRad );
35 yRot = x * std::sin( rotToRad ) + y * std::cos( rotToRad );
42 double clippedAngle =
angle;
43 if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
45 clippedAngle = std::fmod( clippedAngle, 360.0 );
47 if ( !allowNegative && clippedAngle < 0.0 )
49 clippedAngle += 360.0;
60 if ( clippedAngle >= 22.5 && clippedAngle < 67.5 )
64 else if ( clippedAngle >= 67.5 && clippedAngle < 112.5 )
68 else if ( clippedAngle >= 112.5 && clippedAngle < 157.5 )
72 else if ( clippedAngle >= 157.5 && clippedAngle < 202.5 )
76 else if ( clippedAngle >= 202.5 && clippedAngle < 247.5 )
80 else if ( clippedAngle >= 247.5 && clippedAngle < 292.5 )
84 else if ( clippedAngle >= 292.5 && clippedAngle < 337.5 )
100 if ( dpi < 0 && painter && painter->device() )
102 context.
setScaleFactor( painter->device()->logicalDpiX() / 25.4 );
119 dpi = ( painter && painter->device() ) ? painter->device()->logicalDpiX() : 88;
121 double dotsPerMM = dpi / 25.4;
125 QSizeF mapSizeLayoutUnits = map->rect().size();
154 double left =
relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
155 double right =
relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
156 double top =
relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
157 double bottom =
relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
159 rectToResize.setRect( left, top, right - left, bottom - top );
165 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
166 double c = afterMin - ( beforeMin * m );
169 return m * position +
c;
175 QFont scaledFont = font;
176 double pixelSize = pointsToMM( scaledFont.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
177 scaledFont.setPixelSize( pixelSize );
186 QFontMetricsF fontMetrics( metricsFont );
187 return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
195 QFontMetricsF fontMetrics( metricsFont );
196 return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
205 QFontMetricsF fontMetrics( metricsFont );
206 return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
215 QFontMetricsF fontMetrics( metricsFont );
216 return ( fontMetrics.boundingRect( character ).height() / FONT_WORKAROUND_SCALE );
224 QFontMetricsF fontMetrics( metricsFont );
225 return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
230 QStringList multiLineSplit = text.split(
'\n' );
231 int lines = multiLineSplit.size();
236 QFontMetricsF fontMetrics( metricsFont );
238 double fontHeight = fontMetrics.ascent() + fontMetrics.descent();
239 double textHeight = fontMetrics.ascent() +
static_cast< double >( ( lines - 1 ) * fontHeight * multiLineHeight );
241 return textHeight / FONT_WORKAROUND_SCALE;
256 painter->setFont( textFont );
257 if ( color.isValid() )
259 painter->setPen( color );
261 double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
262 painter->scale( scaleFactor, scaleFactor );
263 painter->drawText( position * FONT_WORKAROUND_SCALE, text );
267 void QgsLayoutUtils::drawText( QPainter *painter,
const QRectF &rect,
const QString &text,
const QFont &font,
const QColor &color,
const Qt::AlignmentFlag halignment,
const Qt::AlignmentFlag valignment,
const int flags )
278 QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
279 rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
282 painter->setFont( textFont );
283 if ( color.isValid() )
285 painter->setPen( color );
287 double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
288 painter->scale( scaleFactor, scaleFactor );
289 painter->drawText( scaledRect, halignment | valignment | flags, text );
295 double originalWidth = originalRect.width();
296 double originalHeight = originalRect.height();
297 double boundsWidth = boundsRect.width();
298 double boundsHeight = boundsRect.height();
299 double ratioBoundsRect = boundsWidth / boundsHeight;
309 rectScale = ( ( originalWidth / originalHeight ) > ratioBoundsRect ) ? boundsWidth / originalWidth : boundsHeight / originalHeight;
313 rectScale = ( ( originalHeight / originalWidth ) > ratioBoundsRect ) ? boundsWidth / originalHeight : boundsHeight / originalWidth;
315 double rectScaledWidth = rectScale * originalWidth;
316 double rectScaledHeight = rectScale * originalHeight;
320 return QRectF( ( boundsWidth - rectScaledWidth ) / 2.0, ( boundsHeight - rectScaledHeight ) / 2.0, rectScaledWidth, rectScaledHeight );
324 return QRectF( ( boundsWidth - rectScaledHeight ) / 2.0, ( boundsHeight - rectScaledWidth ) / 2.0, rectScaledWidth, rectScaledHeight );
329 double angleRad = -clippedRotation *
M_DEG2RAD;
330 double cosAngle = std::cos( angleRad );
331 double sinAngle = std::sin( angleRad );
334 double widthBoundsRotatedRect = originalWidth * std::fabs( cosAngle ) + originalHeight * std::fabs( sinAngle );
335 double heightBoundsRotatedRect = originalHeight * std::fabs( cosAngle ) + originalWidth * std::fabs( sinAngle );
339 double ratioBoundsRotatedRect = widthBoundsRotatedRect / heightBoundsRotatedRect;
340 double rectScale = ratioBoundsRotatedRect > ratioBoundsRect ? boundsWidth / widthBoundsRotatedRect : boundsHeight / heightBoundsRotatedRect;
341 double rectScaledWidth = rectScale * originalWidth;
342 double rectScaledHeight = rectScale * originalHeight;
346 double currentCornerX = 0;
348 currentCornerX += rectScaledWidth * cosAngle;
349 minX = minX < currentCornerX ? minX : currentCornerX;
350 currentCornerX += rectScaledHeight * sinAngle;
351 minX = minX < currentCornerX ? minX : currentCornerX;
352 currentCornerX -= rectScaledWidth * cosAngle;
353 minX = minX < currentCornerX ? minX : currentCornerX;
355 double currentCornerY = 0;
357 currentCornerY -= rectScaledWidth * sinAngle;
358 minY = minY < currentCornerY ? minY : currentCornerY;
359 currentCornerY += rectScaledHeight * cosAngle;
360 minY = minY < currentCornerY ? minY : currentCornerY;
361 currentCornerY += rectScaledWidth * sinAngle;
362 minY = minY < currentCornerY ? minY : currentCornerY;
365 double offsetX = ratioBoundsRotatedRect > ratioBoundsRect ? 0 : ( boundsWidth - rectScale * widthBoundsRotatedRect ) / 2.0;
366 offsetX += std::fabs( minX );
367 double offsetY = ratioBoundsRotatedRect > ratioBoundsRect ? ( boundsHeight - rectScale * heightBoundsRotatedRect ) / 2.0 : 0;
368 offsetY += std::fabs( minY );
370 return QRectF( offsetX, offsetY, rectScaledWidth, rectScaledHeight );
375 QString s =
string.trimmed();
376 if ( s.compare( QLatin1String(
"Portrait" ), Qt::CaseInsensitive ) == 0 )
381 else if ( s.compare( QLatin1String(
"Landscape" ), Qt::CaseInsensitive ) == 0 )
400 return !
qgsDoubleNear( style->matrix.m11(), 0.0 ) ? style->matrix.m11() : style->matrix.m12();
414 const auto layers = project->
mapLayers();
415 for (
auto it = layers.constBegin(); it != layers.constEnd(); ++it )
417 if ( it.value()->name().compare(
string, Qt::CaseInsensitive ) == 0 )
427 double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
428 return std::ceil( a / s ) * s;
434 double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
435 return std::floor( a / s ) * s;
440 if ( maximumSize < minimumSize )
454 while ( lowerNiceUnitsPerSeg > maximumSize && upperNiceUnitsPerSeg < minimumSize )
462 return upperNiceUnitsPerSeg < minimumSize ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
466 double QgsLayoutUtils::pointsToMM(
const double pointSize )
469 return ( pointSize * 0.3527 );
472 double QgsLayoutUtils::mmToPoints(
const double mmSize )
475 return ( mmSize / 0.3527 );