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