Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgsmapcanvasitem.h - base class for map canvas items 00003 ---------------------- 00004 begin : February 2006 00005 copyright : (C) 2006 by Martin Dobias 00006 email : wonder.sk at gmail dot com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 /* $Id$ */ 00016 00017 00018 #include "qgsmapcanvasitem.h" 00019 #include "qgsmapcanvas.h" 00020 #include "qgsmaprenderer.h" 00021 #include "qgsmaptopixel.h" 00022 #include "qgsrendercontext.h" 00023 #include <QGraphicsScene> 00024 #include <QRect> 00025 #include <QPen> 00026 #include <QBrush> 00027 #include <QPainter> 00028 #include "qgslogger.h" 00029 00030 QgsMapCanvasItem::QgsMapCanvasItem( QgsMapCanvas* mapCanvas ) 00031 : QGraphicsItem( 0, mapCanvas->scene() ), mMapCanvas( mapCanvas ), 00032 mPanningOffset( 0, 0 ), mItemSize( 0, 0 ) 00033 { 00034 } 00035 00036 QgsMapCanvasItem::~QgsMapCanvasItem() 00037 { 00038 update(); // schedule redraw of canvas 00039 } 00040 00041 void QgsMapCanvasItem::paint( QPainter * painter, 00042 const QStyleOptionGraphicsItem * option, 00043 QWidget * widget ) 00044 { 00045 if ( mMapCanvas->antiAliasingEnabled() ) 00046 { 00047 painter->setRenderHint( QPainter::Antialiasing ); 00048 } 00049 paint( painter ); // call the derived item's drawing routines 00050 } 00051 00052 QgsPoint QgsMapCanvasItem::toMapCoordinates( const QPoint& point ) 00053 { 00054 return mMapCanvas->getCoordinateTransform()->toMapCoordinates( point - mPanningOffset ); 00055 } 00056 00057 00058 QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point ) 00059 { 00060 double x = point.x(), y = point.y(); 00061 mMapCanvas->getCoordinateTransform()->transformInPlace( x, y ); 00062 return QPointF( x, y ) + mPanningOffset; 00063 } 00064 00065 00066 QgsRectangle QgsMapCanvasItem::rect() const 00067 { 00068 return mRect; 00069 } 00070 00071 00072 void QgsMapCanvasItem::setRect( const QgsRectangle& rect ) 00073 { 00074 mRect = rect; 00075 //updatePosition(); 00076 00077 QRectF r; // empty rect by default 00078 if ( !mRect.isEmpty() ) 00079 { 00080 r.setTopLeft( toCanvasCoordinates( QgsPoint( mRect.xMinimum(), mRect.yMinimum() ) ) ); 00081 r.setBottomRight( toCanvasCoordinates( QgsPoint( mRect.xMaximum(), mRect.yMaximum() ) ) ); 00082 r = r.normalized(); 00083 } 00084 00085 // set position in canvas where the item will have coordinate (0,0) 00086 prepareGeometryChange(); 00087 setPos( r.topLeft() ); 00088 mItemSize = QSizeF( r.width() + 2, r.height() + 2 ); 00089 00090 // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height())); 00091 00092 update(); 00093 } 00094 00095 QRectF QgsMapCanvasItem::boundingRect() const 00096 { 00097 return QRectF( QPointF( -1, -1 ), mItemSize ); 00098 } 00099 00100 00101 void QgsMapCanvasItem::updateCanvas() 00102 { 00103 update(); 00104 // porting: update below should not be needed anymore 00105 //mMapCanvas->scene()->update(); //Contents(); 00106 } 00107 00108 bool QgsMapCanvasItem::setRenderContextVariables( QPainter* p, QgsRenderContext& context ) const 00109 { 00110 if ( !mMapCanvas || !p ) 00111 { 00112 return false; 00113 } 00114 QgsMapRenderer* mapRenderer = mMapCanvas->mapRenderer(); 00115 if ( !mapRenderer ) 00116 { 00117 return false; 00118 } 00119 00120 context.setPainter( p ); 00121 context.setRendererScale( mMapCanvas->scale() ); 00122 00123 int dpi = mapRenderer->outputDpi(); 00124 int painterDpi = p->device()->logicalDpiX(); 00125 double scaleFactor = 1.0; 00126 double rasterScaleFactor = 1.0; 00127 00128 //little trick to find out if painting origines from composer or main map canvas 00129 if ( data( 0 ).toString() == "composer" ) 00130 { 00131 rasterScaleFactor = painterDpi / 25.4; 00132 scaleFactor = dpi / 25.4; 00133 } 00134 else 00135 { 00136 if ( mapRenderer->outputUnits() == QgsMapRenderer::Millimeters ) 00137 { 00138 scaleFactor = dpi / 25.4; 00139 } 00140 } 00141 context.setScaleFactor( scaleFactor ); 00142 context.setRasterScaleFactor( rasterScaleFactor ); 00143 return true; 00144 } 00145 00146 void QgsMapCanvasItem::updatePosition() 00147 { 00148 // default implementation: recalculate position of the item 00149 setRect( mRect ); 00150 } 00151 00152 00153 void QgsMapCanvasItem::setPanningOffset( const QPoint& point ) 00154 { 00155 mPanningOffset = point; 00156 }