[QGIS Commit] r13154 - in trunk/qgis/src: app/composer core/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Mar 24 12:37:41 EDT 2010


Author: mhugent
Date: 2010-03-24 12:37:40 -0400 (Wed, 24 Mar 2010)
New Revision: 13154

Modified:
   trunk/qgis/src/app/composer/qgscomposer.cpp
   trunk/qgis/src/core/composer/qgscomposermap.cpp
   trunk/qgis/src/core/composer/qgscomposermap.h
Log:
Code for drawing items in composer map. Not yet enabled

Modified: trunk/qgis/src/app/composer/qgscomposer.cpp
===================================================================
--- trunk/qgis/src/app/composer/qgscomposer.cpp	2010-03-24 15:46:13 UTC (rev 13153)
+++ trunk/qgis/src/app/composer/qgscomposer.cpp	2010-03-24 16:37:40 UTC (rev 13154)
@@ -1315,6 +1315,7 @@
     return;
   }
 
+  map->setMapCanvas( mapCanvas() ); //set canvas to composer map to have the possibility to draw canvas items
   QgsComposerMapWidget* mapWidget = new QgsComposerMapWidget( map );
   connect( this, SIGNAL( zoomLevelChanged() ), map, SLOT( renderModeUpdateCachedImage() ) );
   mItemWidgetMap.insert( map, mapWidget );

Modified: trunk/qgis/src/core/composer/qgscomposermap.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposermap.cpp	2010-03-24 15:46:13 UTC (rev 13153)
+++ trunk/qgis/src/core/composer/qgscomposermap.cpp	2010-03-24 16:37:40 UTC (rev 13154)
@@ -34,6 +34,7 @@
 #include "qgslabelattributes.h"
 
 #include <QGraphicsScene>
+#include <QGraphicsView>
 #include <QPainter>
 #include <QSettings>
 #include <iostream>
@@ -42,7 +43,7 @@
 QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
     : QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
     mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), \
-    mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 )
+    mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ), mMapCanvas( 0 )
 {
   mComposition = composition;
   mId = mComposition->composerMapItems().size();
@@ -73,7 +74,7 @@
 QgsComposerMap::QgsComposerMap( QgsComposition *composition )
     : QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
     mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), \
-    mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 )
+    mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ), mMapCanvas( 0 )
 {
   //Offset
   mXOffset = 0.0;
@@ -257,14 +258,19 @@
     double yTopLeftShift = ( mExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
 
     painter->save();
-    //painter->scale( scale, scale );
+
     painter->translate( mXOffset, mYOffset );
     painter->translate( xTopLeftShift, yTopLeftShift );
     painter->rotate( mRotation );
     painter->translate( xShiftMM, -yShiftMM );
     painter->scale( scale, scale );
     painter->drawImage( 0, 0, mCacheImage );
+
+    //restore rotation
     painter->restore();
+
+    //draw canvas items
+    //drawCanvasItems( painter, itemStyle );
   }
   else if ( mComposition->plotStyle() == QgsComposition::Print ||
             mComposition->plotStyle() == QgsComposition::Postscript )
@@ -300,8 +306,13 @@
     painter->rotate( mRotation );
     painter->translate( xShiftMM, -yShiftMM );
     draw( painter, requestRectangle, theSize, 25.4 ); //scene coordinates seem to be in mm
+
+    //restore rotation
     painter->restore();
 
+    //draw canvas items
+    //drawCanvasItems( painter, itemStyle );
+
     mDrawing = false;
   }
 
@@ -1388,3 +1399,93 @@
     return Bottom;
   }
 }
+
+void QgsComposerMap::drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle )
+{
+  if ( !mMapCanvas )
+  {
+    return;
+  }
+
+  QList<QGraphicsItem*> itemList = mMapCanvas->items();
+
+  int nItems = itemList.size();
+
+  QList<QGraphicsItem*>::iterator itemIt = itemList.begin();
+  for ( ; itemIt != itemList.end(); ++itemIt )
+  {
+    //don't draw mapcanvasmap (has z value -10)
+    if ( !( *itemIt ) || ( *itemIt )->zValue() == -10 )
+    {
+      continue;
+    }
+    drawCanvasItem( *itemIt, painter, itemStyle );
+  }
+}
+
+void QgsComposerMap::drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle )
+{
+  if ( !item || !mMapCanvas || !mMapRenderer )
+  {
+    return;
+  }
+
+  painter->save();
+
+  //only for debugging
+  double itemPosX = item->scenePos().x();
+  double itemPosY = item->scenePos().y();
+  double cWidth = mMapCanvas->width();
+  double cHeight = mMapCanvas->height();
+  QgsRectangle rendererExtent = mMapRenderer->extent();
+  QgsRectangle composerMapExtent = mExtent;
+
+  //determine scale factor according to graphics view dpi
+  double scaleFactor = 1.0 / mMapCanvas->logicalDpiX() * 25.4;
+
+  double itemX, itemY;
+  QGraphicsItem* parent = item->parentItem();
+  if ( !parent )
+  {
+    QPointF mapPos = composerMapPosForItem( item );
+    itemX = mapPos.x();
+    itemY = mapPos.y();
+  }
+  else //place item relative to the parent item
+  {
+    QPointF itemScenePos = item->scenePos();
+    QPointF parentScenePos = parent->scenePos();
+
+    QPointF mapPos = composerMapPosForItem( parent );
+
+    itemX = mapPos.x() + ( itemScenePos.x() - parentScenePos.x() ) * scaleFactor;
+    itemY = mapPos.y() + ( itemScenePos.y() - parentScenePos.y() ) * scaleFactor;
+  }
+  painter->translate( itemX, itemY );
+
+
+  painter->scale( scaleFactor, scaleFactor );
+
+  item->paint( painter, itemStyle, 0 );
+  painter->restore();
+}
+
+QPointF QgsComposerMap::composerMapPosForItem( const QGraphicsItem* item ) const
+{
+  if ( !item || !mMapCanvas || !mMapRenderer )
+  {
+    return QPointF( 0, 0 );
+  }
+
+  if ( mExtent.height() <= 0 || mExtent.width() <= 0 || mMapCanvas->width() <= 0 || mMapCanvas->height() <= 0 )
+  {
+    return QPointF( 0, 0 );
+  }
+
+  double mapX = item->scenePos().x() / mMapCanvas->width() * mMapRenderer->extent().width() + mMapRenderer->extent().xMinimum();
+  double mapY = mMapRenderer->extent().yMaximum() - item->scenePos().y() / mMapCanvas->height() * mMapRenderer->extent().height();
+
+  double itemX = rect().width() * ( mapX - mExtent.xMinimum() ) / mExtent.width();
+  double itemY = rect().height() * ( mExtent.yMaximum() - mapY ) / mExtent.height();
+  return QPointF( itemX, itemY );
+}

Modified: trunk/qgis/src/core/composer/qgscomposermap.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposermap.h	2010-03-24 15:46:13 UTC (rev 13153)
+++ trunk/qgis/src/core/composer/qgscomposermap.h	2010-03-24 16:37:40 UTC (rev 13154)
@@ -27,6 +27,7 @@
 class QgsMapToPixel;
 class QDomNode;
 class QDomDocument;
+class QGraphicsView;
 class QPainter;
 
 /** \ingroup MapComposer
@@ -245,6 +246,9 @@
 
     void setMapRotation( double r );
 
+    /**Sets canvas pointer (necessary to query and draw map canvas items)*/
+    void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
+
   public slots:
 
     /**Called if map canvas has changed*/
@@ -341,6 +345,7 @@
     QRectF mCurrentRectangle;
     /**The length of the cross sides for mGridStyle Cross*/
     double mCrossLength;
+    QGraphicsView* mMapCanvas;
 
     /**Draws the map grid*/
     void drawGrid( QPainter* p );
@@ -381,6 +386,10 @@
     QPointF mapToItemCoords( const QPointF& mapCoords ) const;
     /**Returns the item border of a point (in item coordinates)*/
     Border borderForLineCoord( const QPointF& p ) const;
+
+    void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
+    void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
+    QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
 };
 
 #endif



More information about the QGIS-commit mailing list