35 #include <QGraphicsScene>
36 #include <QGraphicsView>
43 :
QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
44 mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), \
45 mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ),
46 mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
53 QList<const QgsComposerMap*>::const_iterator mapIt = mapList.constBegin();
54 for ( ; mapIt != mapList.constEnd(); ++mapIt )
56 if (( *mapIt )->id() > maxId )
58 maxId = ( *mapIt )->id();
83 setToolTip( tr(
"Map %1" ).arg(
mId ) );
88 :
QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
89 mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), \
90 mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ),
91 mMapCanvas( 0 ), mDrawCanvasItems( true )
105 setToolTip( tr(
"Map %1" ).arg(
mId ) );
106 mGridPen.setCapStyle( Qt::FlatCap );
115 draw( painter, extent, QSizeF( size.width(), size.height() ), dpi );
153 if ( settings.value(
"/qgis/enable_anti_aliasing",
true ).toBool() )
155 painter->setRenderHint( QPainter::Antialiasing );
159 if ( theRendererContext )
169 double bk_scale = theMapRenderer.
scale();
174 bool bkLayerCaching = s.value(
"/qgis/enable_render_caching",
false ).toBool();
175 s.setValue(
"/qgis/enable_render_caching",
false );
177 theMapRenderer.
render( painter );
178 s.setValue(
"/qgis/enable_render_caching", bkLayerCaching );
180 theMapRenderer.
setScale( bk_scale );
202 if ( horizontalVScaleFactor < 0 )
220 mCacheImage = QImage( w, h, QImage::Format_ARGB32 );
243 QRectF thisPaintRect = QRectF( 0, 0, QGraphicsRectItem::rect().width(), QGraphicsRectItem::rect().height() );
245 painter->setClipRect( thisPaintRect );
251 QFont messageFont(
"", 12 );
252 painter->setFont( messageFont );
253 painter->setPen( QColor( 0, 0, 0 ) );
254 painter->drawText( thisPaintRect, tr(
"Map will be printed here" ) );
266 if ( horizontalVScaleFactor < 0 )
272 double scale = rect().width() / imagePixelWidth;
286 painter->translate( xTopLeftShift, yTopLeftShift );
288 painter->translate( xShiftMM, -yShiftMM );
289 painter->scale( scale, scale );
307 QPaintDevice* thePaintDevice = painter->device();
308 if ( !thePaintDevice )
328 painter->translate( xTopLeftShift, yTopLeftShift );
330 painter->translate( xShiftMM, -yShiftMM );
331 draw( painter, requestRectangle, theSize, 25.4 );
342 painter->setClipRect( thisPaintRect , Qt::NoClip );
368 QGraphicsRectItem::update();
388 calculator.
setDpi( 25.4 );
395 QRectF currentRect = rect();
396 QRectF newSceneRect = QRectF( transform().dx(), transform().dy(), currentRect.width() + dx, currentRect.height() + dy );
427 int zoomMode = settings.value(
"/qgis/wheel_action", 2 ).toInt();
433 double zoomFactor = settings.value(
"/qgis/zoom_factor", 2.0 ).toDouble();
449 else if ( zoomMode == 2 )
451 centerX = mapMouseX + ( centerX - mapMouseX ) * ( 1.0 / zoomFactor );
452 centerY = mapMouseY + ( centerY - mapMouseY ) * ( 1.0 / zoomFactor );
456 double newIntervalX, newIntervalY;
463 else if ( delta < 0 )
486 double w = rectangle.width();
487 double h = rectangle.height();
516 QRectF currentRect = rect();
518 double newHeight = currentRect.width() * extent.
height() / extent.
width();
520 setSceneRect( QRectF( transform().dx(), transform().dy(), currentRect.width(), newHeight ) );
525 double currentScaleDenominator =
scale();
527 if ( scaleDenominator == currentScaleDenominator )
532 double scaleRatio = scaleDenominator / currentScaleDenominator;
562 QStringList::const_iterator layer_it = layers.constBegin();
565 for ( ; layer_it != layers.constEnd(); ++layer_it )
571 if ( currentRasterLayer )
574 if (( rasterProvider = currentRasterLayer->
dataProvider() ) )
576 if ( rasterProvider->
name() ==
"wms" )
593 connect( layerRegistry, SIGNAL( layerWillBeRemoved( QString ) ),
this, SLOT(
updateCachedImage() ) );
605 QDomElement composerMapElem = doc.createElement(
"ComposerMap" );
606 composerMapElem.setAttribute(
"id",
mId );
611 composerMapElem.setAttribute(
"previewMode",
"Cache" );
615 composerMapElem.setAttribute(
"previewMode",
"Render" );
619 composerMapElem.setAttribute(
"previewMode",
"Rectangle" );
624 composerMapElem.setAttribute(
"keepLayerSet",
"true" );
628 composerMapElem.setAttribute(
"keepLayerSet",
"false" );
633 composerMapElem.setAttribute(
"drawCanvasItems",
"true" );
637 composerMapElem.setAttribute(
"drawCanvasItems",
"false" );
641 QDomElement extentElem = doc.createElement(
"Extent" );
646 composerMapElem.appendChild( extentElem );
649 QDomElement layerSetElem = doc.createElement(
"LayerSet" );
650 QStringList::const_iterator layerIt =
mLayerSet.constBegin();
651 for ( ; layerIt !=
mLayerSet.constEnd(); ++layerIt )
653 QDomElement layerElem = doc.createElement(
"Layer" );
654 QDomText layerIdText = doc.createTextNode( *layerIt );
655 layerElem.appendChild( layerIdText );
656 layerSetElem.appendChild( layerElem );
658 composerMapElem.appendChild( layerSetElem );
661 QDomElement gridElem = doc.createElement(
"Grid" );
663 gridElem.setAttribute(
"gridStyle",
mGridStyle );
668 gridElem.setAttribute(
"penWidth",
mGridPen.widthF() );
669 gridElem.setAttribute(
"penColorRed",
mGridPen.color().red() );
670 gridElem.setAttribute(
"penColorGreen",
mGridPen.color().green() );
671 gridElem.setAttribute(
"penColorBlue",
mGridPen.color().blue() );
675 QDomElement annotationElem = doc.createElement(
"Annotation" );
683 gridElem.appendChild( annotationElem );
684 composerMapElem.appendChild( gridElem );
686 elem.appendChild( composerMapElem );
687 return _writeXML( composerMapElem, doc );
692 if ( itemElem.isNull() )
697 QString idRead = itemElem.attribute(
"id",
"not found" );
698 if ( idRead !=
"not found" )
700 mId = idRead.toInt();
705 QString
previewMode = itemElem.attribute(
"previewMode" );
706 if ( previewMode ==
"Cache" )
710 else if ( previewMode ==
"Render" )
720 QDomNodeList extentNodeList = itemElem.elementsByTagName(
"Extent" );
721 if ( extentNodeList.size() > 0 )
723 QDomElement extentElem = extentNodeList.at( 0 ).toElement();
724 double xmin, xmax, ymin, ymax;
725 xmin = extentElem.attribute(
"xmin" ).toDouble();
726 xmax = extentElem.attribute(
"xmax" ).toDouble();
727 ymin = extentElem.attribute(
"ymin" ).toDouble();
728 ymax = extentElem.attribute(
"ymax" ).toDouble();
734 QString keepLayerSetFlag = itemElem.attribute(
"keepLayerSet" );
735 if ( keepLayerSetFlag.compare(
"true", Qt::CaseInsensitive ) == 0 )
744 QString drawCanvasItemsFlag = itemElem.attribute(
"drawCanvasItems" );
745 if ( drawCanvasItemsFlag.compare(
"true", Qt::CaseInsensitive ) == 0 )
755 QDomNodeList layerSetNodeList = itemElem.elementsByTagName(
"LayerSet" );
757 if ( layerSetNodeList.size() > 0 )
759 QDomElement layerSetElem = layerSetNodeList.at( 0 ).toElement();
760 QDomNodeList layerIdNodeList = layerSetElem.elementsByTagName(
"Layer" );
761 for (
int i = 0; i < layerIdNodeList.size(); ++i )
763 layerSet << layerIdNodeList.at( i ).toElement().text();
773 QDomNodeList gridNodeList = itemElem.elementsByTagName(
"Grid" );
774 if ( gridNodeList.size() > 0 )
776 QDomElement gridElem = gridNodeList.at( 0 ).toElement();
777 mGridEnabled = ( gridElem.attribute(
"show",
"0" ) !=
"0" );
779 mGridIntervalX = gridElem.attribute(
"intervalX",
"0" ).toDouble();
780 mGridIntervalY = gridElem.attribute(
"intervalY",
"0" ).toDouble();
781 mGridOffsetX = gridElem.attribute(
"offsetX",
"0" ).toDouble();
782 mGridOffsetY = gridElem.attribute(
"offsetY",
"0" ).toDouble();
783 mGridPen.setWidthF( gridElem.attribute(
"penWidth",
"0" ).toDouble() );
784 mGridPen.setColor( QColor( gridElem.attribute(
"penColorRed",
"0" ).toInt(), \
785 gridElem.attribute(
"penColorGreen",
"0" ).toInt(), \
786 gridElem.attribute(
"penColorBlue",
"0" ).toInt() ) );
787 mCrossLength = gridElem.attribute(
"crossLength",
"3" ).toDouble();
789 QDomNodeList annotationNodeList = gridElem.elementsByTagName(
"Annotation" );
790 if ( annotationNodeList.size() > 0 )
792 QDomElement annotationElem = annotationNodeList.at( 0 ).toElement();
803 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
804 if ( composerItemList.size() > 0 )
806 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
831 QStringList currentLayerSet;
841 for (
int i =
mLayerSet.size() - 1; i >= 0; --i )
843 if ( !currentLayerSet.contains(
mLayerSet.at( i ) ) )
854 QList< QPair< double, QLineF > > verticalLines;
856 QList< QPair< double, QLineF > >::const_iterator vIt = verticalLines.constBegin();
857 QList< QPair< double, QLineF > > horizontalLines;
859 QList< QPair< double, QLineF > >::const_iterator hIt = horizontalLines.constBegin();
861 QRectF thisPaintRect = QRectF( 0, 0, QGraphicsRectItem::rect().width(), QGraphicsRectItem::rect().height() );
862 p->setClipRect( thisPaintRect );
867 for ( ; vIt != verticalLines.constEnd(); ++vIt )
869 p->drawLine( vIt->second );
872 for ( ; hIt != horizontalLines.constEnd(); ++hIt )
874 p->drawLine( hIt->second );
879 QPointF intersectionPoint, crossEnd1, crossEnd2;
880 for ( ; vIt != verticalLines.constEnd(); ++vIt )
884 p->drawLine( vIt->second.p1(), crossEnd1 );
887 hIt = horizontalLines.constBegin();
888 for ( ; hIt != horizontalLines.constEnd(); ++hIt )
890 if ( hIt->second.intersect( vIt->second, &intersectionPoint ) == QLineF::BoundedIntersection )
894 p->drawLine( crossEnd1, crossEnd2 );
899 p->drawLine( vIt->second.p2(), crossEnd2 );
902 hIt = horizontalLines.constBegin();
903 for ( ; hIt != horizontalLines.constEnd(); ++hIt )
907 p->drawLine( hIt->second.p1(), crossEnd1 );
909 vIt = verticalLines.constBegin();
910 for ( ; vIt != verticalLines.constEnd(); ++vIt )
912 if ( vIt->second.intersect( hIt->second, &intersectionPoint ) == QLineF::BoundedIntersection )
916 p->drawLine( crossEnd1, crossEnd2 );
921 p->drawLine( hIt->second.p2(), crossEnd1 );
927 p->setClipRect( thisPaintRect , Qt::NoClip );
943 QString currentAnnotationString;
944 QList< QPair< double, QLineF > >::const_iterator it = hLines.constBegin();
945 for ( ; it != hLines.constEnd(); ++it )
952 it = vLines.constBegin();
953 for ( ; it != vLines.constEnd(); ++it )
967 double xpos = pos.x();
968 double ypos = pos.y();
971 if ( frameBorder ==
Left )
979 ypos += textWidth / 2.0;
985 ypos += textHeight / 2.0;
993 ypos += textWidth / 2.0;
999 ypos += textHeight / 2.0;
1004 else if ( frameBorder ==
Right )
1011 ypos += textWidth / 2.0;
1017 ypos += textHeight / 2.0;
1025 ypos += textWidth / 2.0;
1031 ypos += textHeight / 2.0;
1035 else if ( frameBorder ==
Bottom )
1042 xpos -= textWidth / 2.0;
1046 xpos += textHeight / 2.0;
1056 xpos -= textWidth / 2.0;
1060 xpos += textHeight / 2.0;
1072 xpos -= textWidth / 2.0;
1077 xpos += textHeight / 2.0;
1086 xpos -= textWidth / 2.0;
1091 xpos += textHeight / 2.0;
1098 drawAnnotation( p, QPointF( xpos, ypos ), rotation, annotationString );
1104 p->translate( pos );
1105 p->rotate( rotation );
1106 p->setPen( QColor( 0, 0, 0 ) );
1121 QRectF mapBoundingRect = mapPolygon.boundingRect();
1124 double roundCorrection = mapBoundingRect.top() > 0 ? 1.0 : 0.0;
1131 double yCanvasCoord;
1133 while ( currentLevel <= mapBoundingRect.bottom() )
1135 yCanvasCoord = rect().height() * ( 1 - ( currentLevel - mapBoundingRect.top() ) / mapBoundingRect.height() );
1136 lines.push_back( qMakePair( currentLevel, QLineF( 0, yCanvasCoord, rect().width(), yCanvasCoord ) ) );
1142 QVector<QLineF> borderLines;
1143 borderLines << QLineF( mapPolygon.at( 0 ), mapPolygon.at( 1 ) );
1144 borderLines << QLineF( mapPolygon.at( 1 ), mapPolygon.at( 2 ) );
1145 borderLines << QLineF( mapPolygon.at( 2 ), mapPolygon.at( 3 ) );
1146 borderLines << QLineF( mapPolygon.at( 3 ), mapPolygon.at( 0 ) );
1148 QList<QPointF> intersectionList;
1150 while ( currentLevel <= mapBoundingRect.bottom() )
1152 intersectionList.clear();
1153 QLineF gridLine( mapBoundingRect.left(), currentLevel, mapBoundingRect.right(), currentLevel );
1155 QVector<QLineF>::const_iterator it = borderLines.constBegin();
1156 for ( ; it != borderLines.constEnd(); ++it )
1158 QPointF intersectionPoint;
1159 if ( it->intersect( gridLine, &intersectionPoint ) == QLineF::BoundedIntersection )
1161 intersectionList.push_back( intersectionPoint );
1162 if ( intersectionList.size() >= 2 )
1169 if ( intersectionList.size() >= 2 )
1171 lines.push_back( qMakePair( currentLevel, QLineF(
mapToItemCoords( intersectionList.at( 0 ) ),
mapToItemCoords( intersectionList.at( 1 ) ) ) ) );
1189 QRectF mapBoundingRect = mapPolygon.boundingRect();
1192 double roundCorrection = mapBoundingRect.left() > 0 ? 1.0 : 0.0;
1198 double xCanvasCoord;
1200 while ( currentLevel <= mapBoundingRect.right() )
1202 xCanvasCoord = rect().width() * ( currentLevel - mapBoundingRect.left() ) / mapBoundingRect.width();
1203 lines.push_back( qMakePair( currentLevel, QLineF( xCanvasCoord, 0, xCanvasCoord, rect().height() ) ) );
1209 QVector<QLineF> borderLines;
1210 borderLines << QLineF( mapPolygon.at( 0 ), mapPolygon.at( 1 ) );
1211 borderLines << QLineF( mapPolygon.at( 1 ), mapPolygon.at( 2 ) );
1212 borderLines << QLineF( mapPolygon.at( 2 ), mapPolygon.at( 3 ) );
1213 borderLines << QLineF( mapPolygon.at( 3 ), mapPolygon.at( 0 ) );
1215 QList<QPointF> intersectionList;
1217 while ( currentLevel <= mapBoundingRect.right() )
1219 intersectionList.clear();
1220 QLineF gridLine( currentLevel, mapBoundingRect.bottom(), currentLevel, mapBoundingRect.top() );
1222 QVector<QLineF>::const_iterator it = borderLines.constBegin();
1223 for ( ; it != borderLines.constEnd(); ++it )
1225 QPointF intersectionPoint;
1226 if ( it->intersect( gridLine, &intersectionPoint ) == QLineF::BoundedIntersection )
1228 intersectionList.push_back( intersectionPoint );
1229 if ( intersectionList.size() >= 2 )
1236 if ( intersectionList.size() >= 2 )
1238 lines.push_back( qMakePair( currentLevel, QLineF(
mapToItemCoords( intersectionList.at( 0 ) ),
mapToItemCoords( intersectionList.at( 1 ) ) ) ) );
1263 QRectF rectangle = rect();
1265 rectangle.setLeft( rectangle.left() - extension );
1266 rectangle.setRight( rectangle.right() + extension );
1267 rectangle.setTop( rectangle.top() - extension );
1268 rectangle.setBottom( rectangle.bottom() + extension );
1271 prepareGeometryChange();
1297 poly.translate( -dx, -dy );
1308 QList< QPair< double, QLineF > > xLines;
1309 QList< QPair< double, QLineF > > yLines;
1322 double currentExtension = 0;
1323 QString currentAnnotationString;
1325 QList< QPair< double, QLineF > >::const_iterator it = xLines.constBegin();
1326 for ( ; it != xLines.constEnd(); ++it )
1330 maxExtension = qMax( maxExtension, currentExtension );
1333 it = yLines.constBegin();
1334 for ( ; it != yLines.constEnd(); ++it )
1338 maxExtension = qMax( maxExtension, currentExtension );
1364 poly << QPointF( rotationPoint.
x() + dx, rotationPoint.
y() + dy );
1370 poly << QPointF( rotationPoint.
x() + dx, rotationPoint.
y() + dy );
1376 poly << QPointF( rotationPoint.
x() + dx, rotationPoint.
y() + dy );
1382 poly << QPointF( rotationPoint.
x() + dx, rotationPoint.
y() + dy );
1395 QRectF bRect = poly.boundingRect();
1406 if ( extentWidth <= 0 )
1410 return rect().width() / extentWidth;
1416 double dxScaled = xShift * mmToMapUnits;
1417 double dyScaled = - yShift * mmToMapUnits;
1428 if ( mapPoly.size() < 1 )
1430 return QPointF( 0, 0 );
1435 double dx = mapCoords.x() - rotationPoint.
x();
1436 double dy = mapCoords.y() - rotationPoint.
y();
1438 QgsPoint backRotatedCoords( rotationPoint.
x() + dx, rotationPoint.
y() + dy );
1441 double xItem = rect().width() * ( backRotatedCoords.x() - unrotatedExtent.xMinimum() ) / unrotatedExtent.width();
1442 double yItem = rect().height() * ( 1 - ( backRotatedCoords.y() - unrotatedExtent.yMinimum() ) / unrotatedExtent.height() );
1443 return QPointF( xItem, yItem );
1448 if ( p.x() <= pen().widthF() )
1452 else if ( p.x() >= ( rect().width() - pen().widthF() ) )
1456 else if ( p.y() <= pen().widthF() )
1473 QList<QGraphicsItem*> itemList =
mMapCanvas->items();
1474 if ( itemList.size() < 1 )
1478 QGraphicsItem* currentItem = 0;
1480 #if QT_VERSION >= 0x40600 //Qt 4.6 provides the items in visibility order
1481 for (
int i = itemList.size() - 1; i >= 0; --i )
1483 currentItem = itemList.at( i );
1485 if ( !currentItem || currentItem->zValue() == -10 )
1491 #else //Qt <4.6 provides the items in random order
1492 QMultiMap<int, QGraphicsItem*> topLevelItems;
1493 QMultiMap<QGraphicsItem*, QGraphicsItem*> childItems;
1495 for (
int i = 0; i < itemList.size(); ++i )
1497 currentItem = itemList.at( i );
1499 if ( !currentItem || currentItem->zValue() == -10 )
1503 if ( currentItem->parentItem() )
1505 childItems.insert( currentItem->parentItem(), currentItem );
1509 topLevelItems.insert( currentItem->zValue(), currentItem );
1513 QMultiMap<int, QGraphicsItem*>::iterator topLevelIt = topLevelItems.begin();
1514 for ( ; topLevelIt != topLevelItems.end(); ++topLevelIt )
1519 QMap<QGraphicsItem*, QGraphicsItem*>::iterator childIt = childItems.find( topLevelIt.value() );
1520 while ( childIt != childItems.end() && childIt.key() == topLevelIt.value() )
1542 double scaleFactor = 1.0 /
mMapCanvas->logicalDpiX() * 25.4;
1544 double itemX, itemY;
1545 QGraphicsItem* parent = item->parentItem();
1554 QPointF itemScenePos = item->scenePos();
1555 QPointF parentScenePos = parent->scenePos();
1559 itemX = mapPos.x() + ( itemScenePos.x() - parentScenePos.x() ) * scaleFactor;
1560 itemY = mapPos.y() + ( itemScenePos.y() - parentScenePos.y() ) * scaleFactor;
1562 painter->translate( itemX, itemY );
1565 painter->scale( scaleFactor, scaleFactor );
1568 item->setData( 0,
"composer" );
1569 item->paint( painter, itemStyle, 0 );
1570 item->setData( 0,
"" );
1578 return QPointF( 0, 0 );
1583 return QPointF( 0, 0 );
1586 QRectF graphicsSceneRect =
mMapCanvas->sceneRect();
1587 QPointF itemScenePos = item->scenePos();
1590 double mapX = itemScenePos.x() / graphicsSceneRect.
width() * mapRendererExtent.
width() + mapRendererExtent.
xMinimum();
1591 double mapY = mapRendererExtent.
yMaximum() - itemScenePos.y() / graphicsSceneRect.height() * mapRendererExtent.
height();