Skip to content

Commit 101c4c9

Browse files
author
mhugent
committed
Code for drawing items in composer map. Not yet enabled
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13154 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c280b15 commit 101c4c9

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

src/app/composer/qgscomposer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ void QgsComposer::addComposerMap( QgsComposerMap* map )
13151315
return;
13161316
}
13171317

1318+
map->setMapCanvas( mapCanvas() ); //set canvas to composer map to have the possibility to draw canvas items
13181319
QgsComposerMapWidget* mapWidget = new QgsComposerMapWidget( map );
13191320
connect( this, SIGNAL( zoomLevelChanged() ), map, SLOT( renderModeUpdateCachedImage() ) );
13201321
mItemWidgetMap.insert( map, mapWidget );

src/core/composer/qgscomposermap.cpp

+104-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "qgslabelattributes.h"
3535

3636
#include <QGraphicsScene>
37+
#include <QGraphicsView>
3738
#include <QPainter>
3839
#include <QSettings>
3940
#include <iostream>
@@ -42,7 +43,7 @@
4243
QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
4344
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
4445
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 )
4647
{
4748
mComposition = composition;
4849
mId = mComposition->composerMapItems().size();
@@ -73,7 +74,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
7374
QgsComposerMap::QgsComposerMap( QgsComposition *composition )
7475
: QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
7576
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 )
7778
{
7879
//Offset
7980
mXOffset = 0.0;
@@ -257,14 +258,19 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
257258
double yTopLeftShift = ( mExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
258259

259260
painter->save();
260-
//painter->scale( scale, scale );
261+
261262
painter->translate( mXOffset, mYOffset );
262263
painter->translate( xTopLeftShift, yTopLeftShift );
263264
painter->rotate( mRotation );
264265
painter->translate( xShiftMM, -yShiftMM );
265266
painter->scale( scale, scale );
266267
painter->drawImage( 0, 0, mCacheImage );
268+
269+
//restore rotation
267270
painter->restore();
271+
272+
//draw canvas items
273+
//drawCanvasItems( painter, itemStyle );
268274
}
269275
else if ( mComposition->plotStyle() == QgsComposition::Print ||
270276
mComposition->plotStyle() == QgsComposition::Postscript )
@@ -300,8 +306,13 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
300306
painter->rotate( mRotation );
301307
painter->translate( xShiftMM, -yShiftMM );
302308
draw( painter, requestRectangle, theSize, 25.4 ); //scene coordinates seem to be in mm
309+
310+
//restore rotation
303311
painter->restore();
304312

313+
//draw canvas items
314+
//drawCanvasItems( painter, itemStyle );
315+
305316
mDrawing = false;
306317
}
307318

@@ -1388,3 +1399,93 @@ QgsComposerMap::Border QgsComposerMap::borderForLineCoord( const QPointF& p ) co
13881399
return Bottom;
13891400
}
13901401
}
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+
}

src/core/composer/qgscomposermap.h

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class QgsMapRenderer;
2727
class QgsMapToPixel;
2828
class QDomNode;
2929
class QDomDocument;
30+
class QGraphicsView;
3031
class QPainter;
3132

3233
/** \ingroup MapComposer
@@ -245,6 +246,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
245246

246247
void setMapRotation( double r );
247248

249+
/**Sets canvas pointer (necessary to query and draw map canvas items)*/
250+
void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
251+
248252
public slots:
249253

250254
/**Called if map canvas has changed*/
@@ -341,6 +345,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
341345
QRectF mCurrentRectangle;
342346
/**The length of the cross sides for mGridStyle Cross*/
343347
double mCrossLength;
348+
QGraphicsView* mMapCanvas;
344349

345350
/**Draws the map grid*/
346351
void drawGrid( QPainter* p );
@@ -381,6 +386,10 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
381386
QPointF mapToItemCoords( const QPointF& mapCoords ) const;
382387
/**Returns the item border of a point (in item coordinates)*/
383388
Border borderForLineCoord( const QPointF& p ) const;
389+
390+
void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
391+
void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
392+
QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
384393
};
385394

386395
#endif

0 commit comments

Comments
 (0)