|
34 | 34 | #include "qgslabelattributes.h"
|
35 | 35 |
|
36 | 36 | #include <QGraphicsScene>
|
| 37 | +#include <QGraphicsView> |
37 | 38 | #include <QPainter>
|
38 | 39 | #include <QSettings>
|
39 | 40 | #include <iostream>
|
|
42 | 43 | QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
|
43 | 44 | : QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
|
44 | 45 | mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), \
|
45 |
| - mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ) |
| 46 | + mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ), mMapCanvas( 0 ) |
46 | 47 | {
|
47 | 48 | mComposition = composition;
|
48 | 49 | mId = mComposition->composerMapItems().size();
|
@@ -73,7 +74,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
|
73 | 74 | QgsComposerMap::QgsComposerMap( QgsComposition *composition )
|
74 | 75 | : QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
|
75 | 76 | mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), \
|
76 |
| - mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ) |
| 77 | + mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ), mMapCanvas( 0 ) |
77 | 78 | {
|
78 | 79 | //Offset
|
79 | 80 | mXOffset = 0.0;
|
@@ -257,14 +258,19 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
|
257 | 258 | double yTopLeftShift = ( mExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
|
258 | 259 |
|
259 | 260 | painter->save();
|
260 |
| - //painter->scale( scale, scale ); |
| 261 | + |
261 | 262 | painter->translate( mXOffset, mYOffset );
|
262 | 263 | painter->translate( xTopLeftShift, yTopLeftShift );
|
263 | 264 | painter->rotate( mRotation );
|
264 | 265 | painter->translate( xShiftMM, -yShiftMM );
|
265 | 266 | painter->scale( scale, scale );
|
266 | 267 | painter->drawImage( 0, 0, mCacheImage );
|
| 268 | + |
| 269 | + //restore rotation |
267 | 270 | painter->restore();
|
| 271 | + |
| 272 | + //draw canvas items |
| 273 | + //drawCanvasItems( painter, itemStyle ); |
268 | 274 | }
|
269 | 275 | else if ( mComposition->plotStyle() == QgsComposition::Print ||
|
270 | 276 | mComposition->plotStyle() == QgsComposition::Postscript )
|
@@ -300,8 +306,13 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
|
300 | 306 | painter->rotate( mRotation );
|
301 | 307 | painter->translate( xShiftMM, -yShiftMM );
|
302 | 308 | draw( painter, requestRectangle, theSize, 25.4 ); //scene coordinates seem to be in mm
|
| 309 | + |
| 310 | + //restore rotation |
303 | 311 | painter->restore();
|
304 | 312 |
|
| 313 | + //draw canvas items |
| 314 | + //drawCanvasItems( painter, itemStyle ); |
| 315 | + |
305 | 316 | mDrawing = false;
|
306 | 317 | }
|
307 | 318 |
|
@@ -1388,3 +1399,93 @@ QgsComposerMap::Border QgsComposerMap::borderForLineCoord( const QPointF& p ) co
|
1388 | 1399 | return Bottom;
|
1389 | 1400 | }
|
1390 | 1401 | }
|
| 1402 | + |
| 1403 | +void QgsComposerMap::drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle ) |
| 1404 | +{ |
| 1405 | + if ( !mMapCanvas ) |
| 1406 | + { |
| 1407 | + return; |
| 1408 | + } |
| 1409 | + |
| 1410 | + QList<QGraphicsItem*> itemList = mMapCanvas->items(); |
| 1411 | + |
| 1412 | + int nItems = itemList.size(); |
| 1413 | + |
| 1414 | + QList<QGraphicsItem*>::iterator itemIt = itemList.begin(); |
| 1415 | + for ( ; itemIt != itemList.end(); ++itemIt ) |
| 1416 | + { |
| 1417 | + //don't draw mapcanvasmap (has z value -10) |
| 1418 | + if ( !( *itemIt ) || ( *itemIt )->zValue() == -10 ) |
| 1419 | + { |
| 1420 | + continue; |
| 1421 | + } |
| 1422 | + drawCanvasItem( *itemIt, painter, itemStyle ); |
| 1423 | + } |
| 1424 | +} |
| 1425 | + |
| 1426 | +void QgsComposerMap::drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle ) |
| 1427 | +{ |
| 1428 | + if ( !item || !mMapCanvas || !mMapRenderer ) |
| 1429 | + { |
| 1430 | + return; |
| 1431 | + } |
| 1432 | + |
| 1433 | + painter->save(); |
| 1434 | + |
| 1435 | + //only for debugging |
| 1436 | + double itemPosX = item->scenePos().x(); |
| 1437 | + double itemPosY = item->scenePos().y(); |
| 1438 | + double cWidth = mMapCanvas->width(); |
| 1439 | + double cHeight = mMapCanvas->height(); |
| 1440 | + QgsRectangle rendererExtent = mMapRenderer->extent(); |
| 1441 | + QgsRectangle composerMapExtent = mExtent; |
| 1442 | + |
| 1443 | + //determine scale factor according to graphics view dpi |
| 1444 | + double scaleFactor = 1.0 / mMapCanvas->logicalDpiX() * 25.4; |
| 1445 | + |
| 1446 | + double itemX, itemY; |
| 1447 | + QGraphicsItem* parent = item->parentItem(); |
| 1448 | + if ( !parent ) |
| 1449 | + { |
| 1450 | + QPointF mapPos = composerMapPosForItem( item ); |
| 1451 | + itemX = mapPos.x(); |
| 1452 | + itemY = mapPos.y(); |
| 1453 | + } |
| 1454 | + else //place item relative to the parent item |
| 1455 | + { |
| 1456 | + QPointF itemScenePos = item->scenePos(); |
| 1457 | + QPointF parentScenePos = parent->scenePos(); |
| 1458 | + |
| 1459 | + QPointF mapPos = composerMapPosForItem( parent ); |
| 1460 | + |
| 1461 | + itemX = mapPos.x() + ( itemScenePos.x() - parentScenePos.x() ) * scaleFactor; |
| 1462 | + itemY = mapPos.y() + ( itemScenePos.y() - parentScenePos.y() ) * scaleFactor; |
| 1463 | + } |
| 1464 | + painter->translate( itemX, itemY ); |
| 1465 | + |
| 1466 | + |
| 1467 | + painter->scale( scaleFactor, scaleFactor ); |
| 1468 | + |
| 1469 | + item->paint( painter, itemStyle, 0 ); |
| 1470 | + painter->restore(); |
| 1471 | +} |
| 1472 | + |
| 1473 | +QPointF QgsComposerMap::composerMapPosForItem( const QGraphicsItem* item ) const |
| 1474 | +{ |
| 1475 | + if ( !item || !mMapCanvas || !mMapRenderer ) |
| 1476 | + { |
| 1477 | + return QPointF( 0, 0 ); |
| 1478 | + } |
| 1479 | + |
| 1480 | + if ( mExtent.height() <= 0 || mExtent.width() <= 0 || mMapCanvas->width() <= 0 || mMapCanvas->height() <= 0 ) |
| 1481 | + { |
| 1482 | + return QPointF( 0, 0 ); |
| 1483 | + } |
| 1484 | + |
| 1485 | + double mapX = item->scenePos().x() / mMapCanvas->width() * mMapRenderer->extent().width() + mMapRenderer->extent().xMinimum(); |
| 1486 | + double mapY = mMapRenderer->extent().yMaximum() - item->scenePos().y() / mMapCanvas->height() * mMapRenderer->extent().height(); |
| 1487 | + |
| 1488 | + double itemX = rect().width() * ( mapX - mExtent.xMinimum() ) / mExtent.width(); |
| 1489 | + double itemY = rect().height() * ( mExtent.yMaximum() - mapY ) / mExtent.height(); |
| 1490 | + return QPointF( itemX, itemY ); |
| 1491 | +} |
0 commit comments