23 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
26 #define M_DEG2RAD 0.0174532925
36 double angleRad = angle / 180.0 *
M_PI;
37 QPointF middlePoint( x, y );
39 QPointF p1 = QPointF( -arrowHeadWidth / 2.0, arrowHeadWidth );
40 QPointF p2 = QPointF( arrowHeadWidth / 2.0, arrowHeadWidth );
42 QPointF p1Rotated, p2Rotated;
43 p1Rotated.setX( p1.x() * cos( angleRad ) + p1.y() * -sin( angleRad ) );
44 p1Rotated.setY( p1.x() * sin( angleRad ) + p1.y() * cos( angleRad ) );
45 p2Rotated.setX( p2.x() * cos( angleRad ) + p2.y() * -sin( angleRad ) );
46 p2Rotated.setY( p2.x() * sin( angleRad ) + p2.y() * cos( angleRad ) );
48 QPolygonF arrowHeadPoly;
49 arrowHeadPoly << middlePoint;
50 arrowHeadPoly << QPointF( middlePoint.x() + p1Rotated.x(), middlePoint.y() + p1Rotated.y() );
51 arrowHeadPoly << QPointF( middlePoint.x() + p2Rotated.x(), middlePoint.y() + p2Rotated.y() );
55 QPen arrowPen = p->pen();
56 arrowPen.setJoinStyle( Qt::RoundJoin );
57 QBrush arrowBrush = p->brush();
58 arrowBrush.setStyle( Qt::SolidPattern );
59 p->setPen( arrowPen );
60 p->setBrush( arrowBrush );
61 arrowBrush.setStyle( Qt::SolidPattern );
62 p->drawPolygon( arrowHeadPoly );
69 double xDiff = p2.x() - p1.x();
70 double yDiff = p2.y() - p1.y();
71 double length = sqrt( xDiff * xDiff + yDiff * yDiff );
77 double angle = acos(( -yDiff * length ) / ( length * length ) ) * 180 /
M_PI;
80 return ( 360 - angle );
87 double rotToRad = angle *
M_PI / 180.0;
89 xRot = x * cos( rotToRad ) - y * sin( rotToRad );
90 yRot = x * sin( rotToRad ) + y * cos( rotToRad );
97 double clippedAngle =
angle;
98 if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
100 clippedAngle = fmod( clippedAngle, 360.0 );
102 if ( clippedAngle < 0.0 )
104 clippedAngle += 360.0;
115 if ( clippedAngle >= 22.5 && clippedAngle < 67.5 )
119 else if ( clippedAngle >= 67.5 && clippedAngle < 112.5 )
123 else if ( clippedAngle >= 112.5 && clippedAngle < 157.5 )
127 else if ( clippedAngle >= 157.5 && clippedAngle < 202.5 )
131 else if ( clippedAngle >= 202.5 && clippedAngle < 247.5 )
135 else if ( clippedAngle >= 247.5 && clippedAngle < 292.5 )
139 else if ( clippedAngle >= 292.5 && clippedAngle < 337.5 )
151 double originalWidth = originalRect.width();
152 double originalHeight = originalRect.height();
153 double boundsWidth = boundsRect.width();
154 double boundsHeight = boundsRect.height();
155 double ratioBoundsRect = boundsWidth / boundsHeight;
160 if ( clippedRotation == 0 || clippedRotation == 90 || clippedRotation == 180 || clippedRotation == 270 )
163 if ( clippedRotation == 0 || clippedRotation == 180 )
165 rectScale = (( originalWidth / originalHeight ) > ratioBoundsRect ) ? boundsWidth / originalWidth : boundsHeight / originalHeight;
169 rectScale = (( originalHeight / originalWidth ) > ratioBoundsRect ) ? boundsWidth / originalHeight : boundsHeight / originalWidth;
171 double rectScaledWidth = rectScale * originalWidth;
172 double rectScaledHeight = rectScale * originalHeight;
174 if ( clippedRotation == 0 || clippedRotation == 180 )
176 return QRectF(( boundsWidth - rectScaledWidth ) / 2.0, ( boundsHeight - rectScaledHeight ) / 2.0, rectScaledWidth, rectScaledHeight );
180 return QRectF(( boundsWidth - rectScaledHeight ) / 2.0, ( boundsHeight - rectScaledWidth ) / 2.0, rectScaledWidth, rectScaledHeight );
185 double angleRad = -clippedRotation *
M_DEG2RAD;
186 double cosAngle = cos( angleRad );
187 double sinAngle = sin( angleRad );
190 double widthBoundsRotatedRect = originalWidth * fabs( cosAngle ) + originalHeight * fabs( sinAngle );
191 double heightBoundsRotatedRect = originalHeight * fabs( cosAngle ) + originalWidth * fabs( sinAngle );
195 double ratioBoundsRotatedRect = widthBoundsRotatedRect / heightBoundsRotatedRect;
196 double rectScale = ratioBoundsRotatedRect > ratioBoundsRect ? boundsWidth / widthBoundsRotatedRect : boundsHeight / heightBoundsRotatedRect;
197 double rectScaledWidth = rectScale * originalWidth;
198 double rectScaledHeight = rectScale * originalHeight;
202 double currentCornerX = 0;
204 currentCornerX += rectScaledWidth * cosAngle;
205 minX = minX < currentCornerX ? minX : currentCornerX;
206 currentCornerX += rectScaledHeight * sinAngle;
207 minX = minX < currentCornerX ? minX : currentCornerX;
208 currentCornerX -= rectScaledWidth * cosAngle;
209 minX = minX < currentCornerX ? minX : currentCornerX;
211 double currentCornerY = 0;
213 currentCornerY -= rectScaledWidth * sinAngle;
214 minY = minY < currentCornerY ? minY : currentCornerY;
215 currentCornerY += rectScaledHeight * cosAngle;
216 minY = minY < currentCornerY ? minY : currentCornerY;
217 currentCornerY += rectScaledWidth * sinAngle;
218 minY = minY < currentCornerY ? minY : currentCornerY;
221 double offsetX = ratioBoundsRotatedRect > ratioBoundsRect ? 0 : ( boundsWidth - rectScale * widthBoundsRotatedRect ) / 2.0;
222 offsetX += fabs( minX );
223 double offsetY = ratioBoundsRotatedRect > ratioBoundsRect ? ( boundsHeight - rectScale * heightBoundsRotatedRect ) / 2.0 : 0;
224 offsetY += fabs( minY );
226 return QRectF( offsetX, offsetY, rectScaledWidth, rectScaledHeight );
232 return ( pointSize * 0.3527 );
238 return ( mmSize / 0.3527 );
244 double left =
relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
245 double right =
relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
246 double top =
relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
247 double bottom =
relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
249 rectToResize.setRect( left, top, right - left, bottom - top );
255 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
256 double c = afterMin - ( beforeMin * m );
259 return m * position + c;
264 if ( orientationString.compare(
"Portrait", Qt::CaseInsensitive ) == 0 )
269 if ( orientationString.compare(
"Landscape", Qt::CaseInsensitive ) == 0 )
280 QList< QPair< QString, QSizeF > > presets;
281 presets << qMakePair( QString(
"A5" ), QSizeF( 148, 210 ) );
282 presets << qMakePair( QString(
"A4" ), QSizeF( 210, 297 ) );
283 presets << qMakePair( QString(
"A3" ), QSizeF( 297, 420 ) );
284 presets << qMakePair( QString(
"A2" ), QSizeF( 420, 594 ) );
285 presets << qMakePair( QString(
"A1" ), QSizeF( 594, 841 ) );
286 presets << qMakePair( QString(
"A0" ), QSizeF( 841, 1189 ) );
287 presets << qMakePair( QString(
"B5" ), QSizeF( 176, 250 ) );
288 presets << qMakePair( QString(
"B4" ), QSizeF( 250, 353 ) );
289 presets << qMakePair( QString(
"B3" ), QSizeF( 353, 500 ) );
290 presets << qMakePair( QString(
"B2" ), QSizeF( 500, 707 ) );
291 presets << qMakePair( QString(
"B1" ), QSizeF( 707, 1000 ) );
292 presets << qMakePair( QString(
"B0" ), QSizeF( 1000, 1414 ) );
294 presets << qMakePair( QString(
"Legal" ), QSizeF( 215.9, 355.6 ) );
295 presets << qMakePair( QString(
"Letter" ), QSizeF( 215.9, 279.4 ) );
296 presets << qMakePair( QString(
"ANSI A" ), QSizeF( 215.9, 279.4 ) );
297 presets << qMakePair( QString(
"ANSI B" ), QSizeF( 279.4, 431.8 ) );
298 presets << qMakePair( QString(
"ANSI C" ), QSizeF( 431.8, 558.8 ) );
299 presets << qMakePair( QString(
"ANSI D" ), QSizeF( 558.8, 863.6 ) );
300 presets << qMakePair( QString(
"ANSI E" ), QSizeF( 863.6, 1117.6 ) );
301 presets << qMakePair( QString(
"Arch A" ), QSizeF( 228.6, 304.8 ) );
302 presets << qMakePair( QString(
"Arch B" ), QSizeF( 304.8, 457.2 ) );
303 presets << qMakePair( QString(
"Arch C" ), QSizeF( 457.2, 609.6 ) );
304 presets << qMakePair( QString(
"Arch D" ), QSizeF( 609.6, 914.4 ) );
305 presets << qMakePair( QString(
"Arch E" ), QSizeF( 914.4, 1219.2 ) );
306 presets << qMakePair( QString(
"Arch E1" ), QSizeF( 762, 1066.8 ) );
308 QList< QPair< QString, QSizeF > >::const_iterator presetIt = presets.constBegin();
309 for ( ;presetIt != presets.constEnd(); ++presetIt )
311 if ( presetString.compare(( *presetIt ).first, Qt::CaseInsensitive ) == 0 )
313 width = ( *presetIt ).second.width();
314 height = ( *presetIt ).second.height();
321 void QgsComposerUtils::readDataDefinedPropertyMap(
const QDomElement &itemElem, QMap<QgsComposerObject::DataDefinedProperty, QString> *dataDefinedNames, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties )
323 QMap<QgsComposerObject::DataDefinedProperty, QString>::const_iterator i = dataDefinedNames->constBegin();
324 for ( ; i != dataDefinedNames->constEnd(); ++i )
326 QString elemName = i.value();
327 QDomNodeList ddNodeList = itemElem.elementsByTagName( elemName );
328 if ( ddNodeList.size() > 0 )
330 QDomElement ddElem = ddNodeList.at( 0 ).toElement();
344 QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >::const_iterator it = dataDefinedProperties->constFind( property );
347 if ( it != dataDefinedProperties->constEnd() )
355 dataDefinedProperties->insert( property, dd );
359 QString active = ddElem.attribute(
"active" );
360 if ( active.compare(
"true", Qt::CaseInsensitive ) == 0 )
368 QString useExpr = ddElem.attribute(
"useExpr" );
369 if ( useExpr.compare(
"true", Qt::CaseInsensitive ) == 0 )
377 dd->
setField( ddElem.attribute(
"field" ) );
381 void QgsComposerUtils::writeDataDefinedPropertyMap( QDomElement &itemElem, QDomDocument &doc,
const QMap<QgsComposerObject::DataDefinedProperty, QString> *dataDefinedNames,
const QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties )
383 QMap<QgsComposerObject::DataDefinedProperty, QString >::const_iterator i = dataDefinedNames->constBegin();
384 for ( ; i != dataDefinedNames->constEnd(); ++i )
386 QString newElemName = i.value();
388 QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >::const_iterator it = dataDefinedProperties->find( i.key() );
389 if ( it != dataDefinedProperties->constEnd() )
397 QString field = dd->
field();
399 bool defaultVals = ( !active && !useExpr && expr.isEmpty() && field.isEmpty() );
403 QDomElement ddElem = doc.createElement( newElemName );
406 ddElem.setAttribute(
"active",
"true" );
410 ddElem.setAttribute(
"active",
"false" );
414 ddElem.setAttribute(
"useExpr",
"true" );
418 ddElem.setAttribute(
"useExpr",
"false" );
420 ddElem.setAttribute(
"expr", expr );
421 ddElem.setAttribute(
"field", field );
422 itemElem.appendChild( ddElem );
433 QFont scaledFont = font;
435 scaledFont.setPixelSize( pixelSize );
444 QFontMetricsF fontMetrics( metricsFont );
453 QFontMetricsF fontMetrics( metricsFont );
462 QFontMetricsF fontMetrics( metricsFont );
471 QFontMetricsF fontMetrics( metricsFont );
480 QFontMetricsF fontMetrics( metricsFont );
496 painter->setFont( textFont );
497 if ( color.isValid() )
499 painter->setPen( color );
502 painter->scale( scaleFactor, scaleFactor );
507 void QgsComposerUtils::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 )
522 painter->setFont( textFont );
523 if ( color.isValid() )
525 painter->setPen( color );
528 painter->scale( scaleFactor, scaleFactor );
529 painter->drawText( scaledRect, halignment | valignment | flags, text );