QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgscomposermap.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposermap.cpp
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : blazek@itc.it
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgscomposermap.h"
19 #include "qgscomposermapgrid.h"
20 #include "qgscomposermapoverview.h"
21 #include "qgscomposition.h"
22 #include "qgscomposerutils.h"
23 #include "qgscoordinatetransform.h"
24 #include "qgslogger.h"
25 #include "qgsmaprenderer.h"
27 #include "qgsmaplayerregistry.h"
28 #include "qgsmaptopixel.h"
29 #include "qgsproject.h"
30 #include "qgsrasterlayer.h"
31 #include "qgsrendercontext.h"
32 #include "qgsscalecalculator.h"
33 #include "qgsvectorlayer.h"
34 #include "qgspallabeling.h"
35 #include "qgsexpression.h"
36 
37 #include "qgslabel.h"
38 #include "qgslabelattributes.h"
39 #include "qgssymbollayerv2utils.h" //for pointOnLineWithDistance
40 
41 #include <QGraphicsScene>
42 #include <QGraphicsView>
43 #include <QPainter>
44 #include <QSettings>
45 #include <cmath>
46 
47 QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
48  : QgsComposerItem( x, y, width, height, composition )
49  , mGridStack( 0 )
50  , mOverviewStack( 0 )
51  , mMapRotation( 0 )
52  , mEvaluatedMapRotation( 0 )
53  , mKeepLayerSet( false )
54  , mUpdatesEnabled( true )
55  , mMapCanvas( 0 )
56  , mDrawCanvasItems( true )
57  , mAtlasDriven( false )
58  , mAtlasScalingMode( Auto )
59  , mAtlasMargin( 0.10 )
60 {
62 
63  mId = 0;
64  assignFreeId();
65 
66  mPreviewMode = QgsComposerMap::Rectangle;
67  mCurrentRectangle = rect();
68 
69  // Cache
70  mCacheUpdated = false;
71  mDrawing = false;
72 
73  //Offset
74  mXOffset = 0.0;
75  mYOffset = 0.0;
76 
77  //get the color for map canvas background and set map background color accordingly
78  int bgRedInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorRedPart", 255 );
79  int bgGreenInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorGreenPart", 255 );
80  int bgBlueInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorBluePart", 255 );
81  setBackgroundColor( QColor( bgRedInt, bgGreenInt, bgBlueInt ) );
82 
83  //calculate mExtent based on width/height ratio and map canvas extent
84  mExtent = mComposition->mapSettings().visibleExtent();
85 
86  init();
87 
88  setSceneRect( QRectF( x, y, width, height ) );
89 }
90 
92  : QgsComposerItem( 0, 0, 10, 10, composition )
93  , mGridStack( 0 )
94  , mOverviewStack( 0 )
95  , mMapRotation( 0 )
96  , mEvaluatedMapRotation( 0 )
97  , mKeepLayerSet( false )
98  , mUpdatesEnabled( true )
99  , mMapCanvas( 0 )
100  , mDrawCanvasItems( true )
101  , mAtlasDriven( false )
102  , mAtlasScalingMode( Auto )
103  , mAtlasMargin( 0.10 )
104 {
105  //Offset
106  mXOffset = 0.0;
107  mYOffset = 0.0;
108 
110  mId = mComposition->composerMapItems().size();
111  mPreviewMode = QgsComposerMap::Rectangle;
112  mCurrentRectangle = rect();
113 
114  init();
115  updateToolTip();
116 }
117 
118 void QgsComposerMap::init()
119 {
120  mGridStack = new QgsComposerMapGridStack( this );
121  mOverviewStack = new QgsComposerMapOverviewStack( this );
122  connectUpdateSlot();
123 
124  // data defined strings
125  mDataDefinedNames.insert( QgsComposerObject::MapRotation, QString( "dataDefinedMapRotation" ) );
126  mDataDefinedNames.insert( QgsComposerObject::MapScale, QString( "dataDefinedMapScale" ) );
127  mDataDefinedNames.insert( QgsComposerObject::MapXMin, QString( "dataDefinedMapXMin" ) );
128  mDataDefinedNames.insert( QgsComposerObject::MapYMin, QString( "dataDefinedMapYMin" ) );
129  mDataDefinedNames.insert( QgsComposerObject::MapXMax, QString( "dataDefinedMapXMax" ) );
130  mDataDefinedNames.insert( QgsComposerObject::MapYMax, QString( "dataDefinedMapYMax" ) );
131  mDataDefinedNames.insert( QgsComposerObject::MapAtlasMargin, QString( "dataDefinedMapAtlasMargin" ) );
132 }
133 
134 void QgsComposerMap::updateToolTip()
135 {
136  setToolTip( tr( "Map %1" ).arg( mId ) );
137 }
138 
139 void QgsComposerMap::adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const
140 {
141  double itemWidthHeightRatio = itemWidth / itemHeight;
142  double newWidthHeightRatio = extent.width() / extent.height();
143 
144  if ( itemWidthHeightRatio <= newWidthHeightRatio )
145  {
146  //enlarge height of new extent, ensuring the map center stays the same
147  double newHeight = extent.width() / itemWidthHeightRatio;
148  double deltaHeight = newHeight - extent.height();
149  extent.setYMinimum( extent.yMinimum() - deltaHeight / 2 );
150  extent.setYMaximum( extent.yMaximum() + deltaHeight / 2 );
151  }
152  else
153  {
154  //enlarge width of new extent, ensuring the map center stays the same
155  double newWidth = itemWidthHeightRatio * extent.height();
156  double deltaWidth = newWidth - extent.width();
157  extent.setXMinimum( extent.xMinimum() - deltaWidth / 2 );
158  extent.setXMaximum( extent.xMaximum() + deltaWidth / 2 );
159  }
160 }
161 
163 {
164  delete mOverviewStack;
165  delete mGridStack;
166 }
167 
168 /* This function is called by paint() and cache() to render the map. It does not override any functions
169 from QGraphicsItem. */
170 void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale )
171 {
172  Q_UNUSED( forceWidthScale );
173 
174  if ( !painter )
175  {
176  return;
177  }
178  if ( size.width() == 0 || size.height() == 0 )
179  {
180  //don't attempt to draw if size is invalid
181  return;
182  }
183 
184 
185  // render
186  QgsMapRendererCustomPainterJob job( mapSettings( extent, size, dpi ), painter );
187  // Render the map in this thread. This is done because of problems
188  // with printing to printer on Windows (printing to PDF is fine though).
189  // Raster images were not displayed - see #10599
190  job.renderSynchronously();
191 }
192 
193 QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QSizeF& size, int dpi ) const
194 {
195  const QgsMapSettings& ms = mComposition->mapSettings();
196 
197  QgsMapSettings jobMapSettings;
198  jobMapSettings.setExtent( extent );
199  jobMapSettings.setOutputSize( size.toSize() );
200  jobMapSettings.setOutputDpi( dpi );
201  jobMapSettings.setMapUnits( ms.mapUnits() );
202  jobMapSettings.setBackgroundColor( Qt::transparent );
203  jobMapSettings.setOutputImageFormat( ms.outputImageFormat() );
204 
205  //set layers to render
206  QStringList theLayerSet = layersToRender();
207  if ( -1 != mCurrentExportLayer )
208  {
209  //exporting with separate layers (eg, to svg layers), so we only want to render a single map layer
210  const int layerIdx = mCurrentExportLayer - ( hasBackground() ? 1 : 0 );
211  theLayerSet =
212  ( layerIdx >= 0 && layerIdx < theLayerSet.length() )
213  ? QStringList( theLayerSet[ theLayerSet.length() - layerIdx - 1 ] )
214  : QStringList(); //exporting decorations such as map frame/grid/overview, so no map layers required
215  }
216  jobMapSettings.setLayers( theLayerSet );
217  jobMapSettings.setDestinationCrs( ms.destinationCrs() );
218  jobMapSettings.setCrsTransformEnabled( ms.hasCrsTransformEnabled() );
219  jobMapSettings.setFlags( ms.flags() );
220  jobMapSettings.setFlag( QgsMapSettings::DrawSelection, false );
221 
224  {
225  //if outputing composer, disable optimisations like layer simplification
226  jobMapSettings.setFlag( QgsMapSettings::UseRenderingOptimization, false );
227  }
228 
229  //update $map variable. Use QgsComposerItem's id since that is user-definable
231 
232  // composer-specific overrides of flags
233  jobMapSettings.setFlag( QgsMapSettings::ForceVectorOutput ); // force vector output (no caching of marker images etc.)
234  jobMapSettings.setFlag( QgsMapSettings::DrawEditingInfo, false );
235  jobMapSettings.setFlag( QgsMapSettings::UseAdvancedEffects, mComposition->useAdvancedEffects() ); // respect the composition's useAdvancedEffects flag
236 
237  return jobMapSettings;
238 }
239 
241 {
242  if ( mPreviewMode == Rectangle )
243  {
244  return;
245  }
246 
247  if ( mDrawing )
248  {
249  return;
250  }
251 
252  mDrawing = true;
253 
254  //in case of rotation, we need to request a larger rectangle and create a larger cache image
255  QgsRectangle requestExtent;
256  requestedExtent( requestExtent );
257 
258  double horizontalVScaleFactor = horizontalViewScaleFactor();
259  if ( horizontalVScaleFactor < 0 )
260  {
261  //make sure scale factor is positive
262  horizontalVScaleFactor = mLastValidViewScaleFactor > 0 ? mLastValidViewScaleFactor : 1;
263  }
264 
265  double widthMM = requestExtent.width() * mapUnitsToMM();
266  double heightMM = requestExtent.height() * mapUnitsToMM();
267 
268  int w = widthMM * horizontalVScaleFactor;
269  int h = heightMM * horizontalVScaleFactor;
270 
271  if ( w > 5000 ) //limit size of image for better performance
272  {
273  w = 5000;
274  }
275 
276  if ( h > 5000 )
277  {
278  h = 5000;
279  }
280 
281  mCacheImage = QImage( w, h, QImage::Format_ARGB32 );
282 
283  // set DPI of the image
284  mCacheImage.setDotsPerMeterX( 1000 * w / widthMM );
285  mCacheImage.setDotsPerMeterY( 1000 * h / heightMM );
286 
287  if ( hasBackground() )
288  {
289  //Initially fill image with specified background color. This ensures that layers with blend modes will
290  //preview correctly
291  mCacheImage.fill( backgroundColor().rgba() );
292  }
293  else
294  {
295  //no background, but start with empty fill to avoid artifacts
296  mCacheImage.fill( QColor( 255, 255, 255, 0 ).rgba() );
297  }
298 
299  QPainter p( &mCacheImage );
300 
301  draw( &p, requestExtent, QSizeF( w, h ), mCacheImage.logicalDpiX() );
302  p.end();
303  mCacheUpdated = true;
304 
305  mDrawing = false;
306 }
307 
308 void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
309 {
310  Q_UNUSED( pWidget );
311 
312  if ( !mComposition || !painter )
313  {
314  return;
315  }
316  if ( !shouldDrawItem() )
317  {
318  return;
319  }
320 
321  QRectF thisPaintRect = QRectF( 0, 0, QGraphicsRectItem::rect().width(), QGraphicsRectItem::rect().height() );
322  painter->save();
323  painter->setClipRect( thisPaintRect );
324 
325  if ( mComposition->plotStyle() == QgsComposition::Preview && mPreviewMode == Rectangle )
326  {
327  // Fill with background color
328  drawBackground( painter );
329  QFont messageFont( "", 12 );
330  painter->setFont( messageFont );
331  painter->setPen( QColor( 0, 0, 0, 125 ) );
332  painter->drawText( thisPaintRect, tr( "Map will be printed here" ) );
333  }
335  {
336  //draw cached pixmap. This function does not call cache() any more because
337  //Qt 4.4.0 and 4.4.1 have problems with recursive paintings
338  //QgsComposerMap::cache() and QgsComposerMap::update() need to be called by
339  //client functions
340 
341  //Background color is already included in cached image, so no need to draw
342 
343  QgsRectangle requestRectangle;
344  requestedExtent( requestRectangle );
345 
346  QgsRectangle cExtent = *currentMapExtent();
347 
348  double imagePixelWidth = cExtent.width() / requestRectangle.width() * mCacheImage.width() ; //how many pixels of the image are for the map extent?
349  double scale = rect().width() / imagePixelWidth;
350  QgsPoint rotationPoint = QgsPoint(( cExtent.xMaximum() + cExtent.xMinimum() ) / 2.0, ( cExtent.yMaximum() + cExtent.yMinimum() ) / 2.0 );
351 
352  //shift such that rotation point is at 0/0 point in the coordinate system
353  double yShiftMM = ( requestRectangle.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
354  double xShiftMM = ( requestRectangle.xMinimum() - rotationPoint.x() ) * mapUnitsToMM();
355 
356  //shift such that top left point of the extent at point 0/0 in item coordinate system
357  double xTopLeftShift = ( rotationPoint.x() - cExtent.xMinimum() ) * mapUnitsToMM();
358  double yTopLeftShift = ( cExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
359 
360  painter->save();
361 
362  painter->translate( mXOffset, mYOffset );
363  painter->translate( xTopLeftShift, yTopLeftShift );
364  painter->rotate( mEvaluatedMapRotation );
365  painter->translate( xShiftMM, -yShiftMM );
366  painter->scale( scale, scale );
367  painter->drawImage( 0, 0, mCacheImage );
368 
369  //restore rotation
370  painter->restore();
371 
372  //draw canvas items
373  drawCanvasItems( painter, itemStyle );
374  }
375  else if ( mComposition->plotStyle() == QgsComposition::Print ||
377  {
378  if ( mDrawing )
379  {
380  return;
381  }
382 
383  mDrawing = true;
384  QPaintDevice* thePaintDevice = painter->device();
385  if ( !thePaintDevice )
386  {
387  return;
388  }
389 
390  // Fill with background color
391  if ( shouldDrawPart( Background ) )
392  {
393  drawBackground( painter );
394  }
395 
396  QgsRectangle requestRectangle;
397  requestedExtent( requestRectangle );
398 
399  QgsRectangle cExtent = *currentMapExtent();
400 
401  QSizeF theSize( requestRectangle.width() * mapUnitsToMM(), requestRectangle.height() * mapUnitsToMM() );
402 
403  QgsPoint rotationPoint = QgsPoint(( cExtent.xMaximum() + cExtent.xMinimum() ) / 2.0, ( cExtent.yMaximum() + cExtent.yMinimum() ) / 2.0 );
404 
405  //shift such that rotation point is at 0/0 point in the coordinate system
406  double yShiftMM = ( requestRectangle.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
407  double xShiftMM = ( requestRectangle.xMinimum() - rotationPoint.x() ) * mapUnitsToMM();
408 
409  //shift such that top left point of the extent at point 0/0 in item coordinate system
410  double xTopLeftShift = ( rotationPoint.x() - cExtent.xMinimum() ) * mapUnitsToMM();
411  double yTopLeftShift = ( cExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
412  painter->save();
413  painter->translate( mXOffset, mYOffset );
414  painter->translate( xTopLeftShift, yTopLeftShift );
415  painter->rotate( mEvaluatedMapRotation );
416  painter->translate( xShiftMM, -yShiftMM );
417 
418  double dotsPerMM = thePaintDevice->logicalDpiX() / 25.4;
419  theSize *= dotsPerMM; // output size will be in dots (pixels)
420  painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
421  draw( painter, requestRectangle, theSize, thePaintDevice->logicalDpiX() );
422 
423  //restore rotation
424  painter->restore();
425 
426  //draw canvas items
427  drawCanvasItems( painter, itemStyle );
428 
429  mDrawing = false;
430  }
431 
432  painter->setClipRect( thisPaintRect, Qt::NoClip );
433  if ( shouldDrawPart( OverviewMapExtent ) &&
434  ( mComposition->plotStyle() != QgsComposition::Preview || mPreviewMode != Rectangle ) )
435  {
436  mOverviewStack->drawItems( painter );
437  }
438  if ( shouldDrawPart( Grid ) &&
439  ( mComposition->plotStyle() != QgsComposition::Preview || mPreviewMode != Rectangle ) )
440  {
441  mGridStack->drawItems( painter );
442  }
443  if ( shouldDrawPart( Frame ) )
444  {
445  drawFrame( painter );
446  }
447  if ( isSelected() && shouldDrawPart( SelectionBoxes ) )
448  {
449  drawSelectionBoxes( painter );
450  }
451 
452  painter->restore();
453 }
454 
456 {
457  return
458  ( hasBackground() ? 1 : 0 )
459  + layersToRender().length()
460  + 1 // for grids, if they exist
461  + 1 // for overviews, if they exist
462  + ( hasFrame() ? 1 : 0 )
463  + ( isSelected() ? 1 : 0 )
464  ;
465 }
466 
467 bool QgsComposerMap::shouldDrawPart( PartType part ) const
468 {
469  if ( -1 == mCurrentExportLayer )
470  {
471  //all parts of the composer map are visible
472  return true;
473  }
474 
475  int idx = numberExportLayers();
476  if ( isSelected() )
477  {
478  --idx;
479  if ( SelectionBoxes == part )
480  {
481  return mCurrentExportLayer == idx;
482  }
483  }
484 
485  if ( hasFrame() )
486  {
487  --idx;
488  if ( Frame == part )
489  {
490  return mCurrentExportLayer == idx;
491  }
492  }
493  --idx;
494  if ( OverviewMapExtent == part )
495  {
496  return mCurrentExportLayer == idx;
497  }
498  --idx;
499  if ( Grid == part )
500  {
501  return mCurrentExportLayer == idx;
502  }
503  if ( hasBackground() )
504  {
505  if ( Background == part )
506  {
507  return mCurrentExportLayer == 0;
508  }
509  }
510 
511  return true; // for Layer
512 }
513 
515 {
516  syncLayerSet(); //layer list may have changed
517  mCacheUpdated = false;
518  cache();
519  QGraphicsRectItem::update();
520 }
521 
523 {
524  if ( mPreviewMode == Render )
525  {
527  }
528 }
529 
531 {
532  mCacheUpdated = u;
533 }
534 
536 {
538  return mComposition->mapRenderer();
540 }
541 
542 QStringList QgsComposerMap::layersToRender() const
543 {
544  //use stored layer set or read current set from main canvas
545  QStringList renderLayerSet;
546  if ( mKeepLayerSet )
547  {
548  renderLayerSet = mLayerSet;
549  }
550  else
551  {
552  renderLayerSet = mComposition->mapSettings().layers();
553  }
554 
555  //remove atlas coverage layer if required
556  //TODO - move setting for hiding coverage layer to map item properties
558  {
560  {
561  //hiding coverage layer
562  int removeAt = renderLayerSet.indexOf( mComposition->atlasComposition().coverageLayer()->id() );
563  if ( removeAt != -1 )
564  {
565  renderLayerSet.removeAt( removeAt );
566  }
567  }
568  }
569 
570  return renderLayerSet;
571 }
572 
573 double QgsComposerMap::scale() const
574 {
575  QgsScaleCalculator calculator;
576  calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
577  calculator.setDpi( 25.4 ); //QGraphicsView units are mm
578  return calculator.calculate( *currentMapExtent(), rect().width() );
579 }
580 
581 void QgsComposerMap::resize( double dx, double dy )
582 {
583  //setRect
584  QRectF currentRect = rect();
585  QRectF newSceneRect = QRectF( pos().x(), pos().y(), currentRect.width() + dx, currentRect.height() + dy );
586  setSceneRect( newSceneRect );
587  updateItem();
588 }
589 
590 void QgsComposerMap::moveContent( double dx, double dy )
591 {
592  if ( !mDrawing )
593  {
594  transformShift( dx, dy );
595  currentMapExtent()->setXMinimum( currentMapExtent()->xMinimum() + dx );
596  currentMapExtent()->setXMaximum( currentMapExtent()->xMaximum() + dx );
597  currentMapExtent()->setYMinimum( currentMapExtent()->yMinimum() + dy );
598  currentMapExtent()->setYMaximum( currentMapExtent()->yMaximum() + dy );
599 
600  //in case data defined extents are set, these override the calculated values
601  refreshMapExtents();
602 
603  cache();
604  update();
605  emit itemChanged();
606  emit extentChanged();
607  }
608 }
609 
610 void QgsComposerMap::zoomContent( int delta, double x, double y )
611 {
612  QSettings settings;
613 
614  //read zoom mode
615  QgsComposerItem::ZoomMode zoomMode = ( QgsComposerItem::ZoomMode )settings.value( "/qgis/wheel_action", 2 ).toInt();
616  if ( zoomMode == QgsComposerItem::NoZoom )
617  {
618  //do nothing
619  return;
620  }
621 
622  double zoomFactor = settings.value( "/qgis/zoom_factor", 2.0 ).toDouble();
623  zoomFactor = delta > 0 ? zoomFactor : 1 / zoomFactor;
624 
625  zoomContent( zoomFactor, QPointF( x, y ), zoomMode );
626 }
627 
628 void QgsComposerMap::zoomContent( const double factor, const QPointF point, const ZoomMode mode )
629 {
630  if ( mDrawing )
631  {
632  return;
633  }
634 
635  if ( mode == QgsComposerItem::NoZoom )
636  {
637  //do nothing
638  return;
639  }
640 
641  //find out map coordinates of position
642  double mapX = currentMapExtent()->xMinimum() + ( point.x() / rect().width() ) * ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() );
643  double mapY = currentMapExtent()->yMinimum() + ( 1 - ( point.y() / rect().height() ) ) * ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() );
644 
645  //find out new center point
646  double centerX = ( currentMapExtent()->xMaximum() + currentMapExtent()->xMinimum() ) / 2;
647  double centerY = ( currentMapExtent()->yMaximum() + currentMapExtent()->yMinimum() ) / 2;
648 
649  if ( mode != QgsComposerItem::Zoom )
650  {
651  if ( mode == QgsComposerItem::ZoomRecenter )
652  {
653  centerX = mapX;
654  centerY = mapY;
655  }
656  else if ( mode == QgsComposerItem::ZoomToPoint )
657  {
658  centerX = mapX + ( centerX - mapX ) * ( 1.0 / factor );
659  centerY = mapY + ( centerY - mapY ) * ( 1.0 / factor );
660  }
661  }
662 
663  double newIntervalX, newIntervalY;
664 
665  if ( factor > 0 )
666  {
667  newIntervalX = ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() ) / factor;
668  newIntervalY = ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() ) / factor;
669  }
670  else //no need to zoom
671  {
672  return;
673  }
674 
675  currentMapExtent()->setXMaximum( centerX + newIntervalX / 2 );
676  currentMapExtent()->setXMinimum( centerX - newIntervalX / 2 );
677  currentMapExtent()->setYMaximum( centerY + newIntervalY / 2 );
678  currentMapExtent()->setYMinimum( centerY - newIntervalY / 2 );
679 
680  if ( mAtlasDriven && mAtlasScalingMode == Fixed && mComposition->atlasMode() != QgsComposition::AtlasOff )
681  {
682  //if map is atlas controlled and set to fixed scaling mode, then scale changes should be treated as permanant
683  //and also apply to the map's original extent (see #9602)
684  //we can't use the scaleRatio calculated earlier, as the scale can vary depending on extent for geographic coordinate systems
685  QgsScaleCalculator calculator;
686  calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
687  calculator.setDpi( 25.4 ); //QGraphicsView units are mm
688  double scaleRatio = scale() / calculator.calculate( mExtent, rect().width() );
689  mExtent.scale( scaleRatio );
690  }
691 
692  //recalculate data defined scale and extents, since that may override zoom
693  refreshMapExtents();
694 
695  cache();
696  update();
697  emit itemChanged();
698  emit extentChanged();
699 }
700 
701 void QgsComposerMap::setSceneRect( const QRectF& rectangle )
702 {
703  double w = rectangle.width();
704  double h = rectangle.height();
705  //prepareGeometryChange();
706 
707  QgsComposerItem::setSceneRect( rectangle );
708 
709  //QGraphicsRectItem::update();
710  double newHeight = mExtent.width() * h / w;
711  mExtent = QgsRectangle( mExtent.xMinimum(), mExtent.yMinimum(), mExtent.xMaximum(), mExtent.yMinimum() + newHeight );
712 
713  //recalculate data defined scale and extents
714  refreshMapExtents();
715  mCacheUpdated = false;
716 
718  update();
719  emit itemChanged();
720  emit extentChanged();
721 }
722 
724 {
725  if ( *currentMapExtent() == extent )
726  {
727  return;
728  }
730 
731  //recalculate data defined scale and extents, since that may override extent
732  refreshMapExtents();
733 
734  //adjust height
735  QRectF currentRect = rect();
736 
737  double newHeight = currentRect.width() * currentMapExtent()->height() / currentMapExtent()->width();
738 
739  setSceneRect( QRectF( pos().x(), pos().y(), currentRect.width(), newHeight ) );
740  updateItem();
741 }
742 
744 {
745  QgsRectangle newExtent = extent;
746  //Make sure the width/height ratio is the same as the current composer map extent.
747  //This is to keep the map item frame size fixed
748  double currentWidthHeightRatio = currentMapExtent()->width() / currentMapExtent()->height();
749  double newWidthHeightRatio = newExtent.width() / newExtent.height();
750 
751  if ( currentWidthHeightRatio < newWidthHeightRatio )
752  {
753  //enlarge height of new extent, ensuring the map center stays the same
754  double newHeight = newExtent.width() / currentWidthHeightRatio;
755  double deltaHeight = newHeight - newExtent.height();
756  newExtent.setYMinimum( newExtent.yMinimum() - deltaHeight / 2 );
757  newExtent.setYMaximum( newExtent.yMaximum() + deltaHeight / 2 );
758  }
759  else
760  {
761  //enlarge width of new extent, ensuring the map center stays the same
762  double newWidth = currentWidthHeightRatio * newExtent.height();
763  double deltaWidth = newWidth - newExtent.width();
764  newExtent.setXMinimum( newExtent.xMinimum() - deltaWidth / 2 );
765  newExtent.setXMaximum( newExtent.xMaximum() + deltaWidth / 2 );
766  }
767 
768  if ( *currentMapExtent() == newExtent )
769  {
770  return;
771  }
772  *currentMapExtent() = newExtent;
773 
774  //recalculate data defined scale and extents, since that may override extent
775  refreshMapExtents();
776 
777  mCacheUpdated = false;
778  updateItem();
779  emit itemChanged();
780  emit extentChanged();
781 }
782 
784 {
785  if ( mAtlasFeatureExtent != extent )
786  {
787  //don't adjust size of item, instead adjust size of bounds to fit
788  QgsRectangle newExtent = extent;
789 
790  //Make sure the width/height ratio is the same as the map item size
791  double currentWidthHeightRatio = rect().width() / rect().height();
792  double newWidthHeightRatio = newExtent.width() / newExtent.height();
793 
794  if ( currentWidthHeightRatio < newWidthHeightRatio )
795  {
796  //enlarge height of new extent, ensuring the map center stays the same
797  double newHeight = newExtent.width() / currentWidthHeightRatio;
798  double deltaHeight = newHeight - newExtent.height();
799  newExtent.setYMinimum( extent.yMinimum() - deltaHeight / 2 );
800  newExtent.setYMaximum( extent.yMaximum() + deltaHeight / 2 );
801  }
802  else if ( currentWidthHeightRatio >= newWidthHeightRatio )
803  {
804  //enlarge width of new extent, ensuring the map center stays the same
805  double newWidth = currentWidthHeightRatio * newExtent.height();
806  double deltaWidth = newWidth - newExtent.width();
807  newExtent.setXMinimum( extent.xMinimum() - deltaWidth / 2 );
808  newExtent.setXMaximum( extent.xMaximum() + deltaWidth / 2 );
809  }
810 
811  mAtlasFeatureExtent = newExtent;
812  }
813 
814  //recalculate data defined scale and extents, since that may override extents
815  refreshMapExtents();
816 
817  mCacheUpdated = false;
818  emit preparedForAtlas();
819  updateItem();
820  emit itemChanged();
821  emit extentChanged();
822 }
823 
825 {
826  //non-const version
827  if ( mAtlasDriven && mComposition->atlasMode() != QgsComposition::AtlasOff )
828  {
829  //if atlas is enabled, and we are either exporting the composition or previewing the atlas, then
830  //return the current temporary atlas feature extent
831  return &mAtlasFeatureExtent;
832  }
833  else
834  {
835  //otherwise return permenant user set extent
836  return &mExtent;
837  }
838 }
839 
841 {
842  //const version
843  if ( mAtlasDriven && mComposition->atlasMode() != QgsComposition::AtlasOff )
844  {
845  //if atlas is enabled, and we are either exporting the composition or previewing the atlas, then
846  //return the current temporary atlas feature extent
847  return &mAtlasFeatureExtent;
848  }
849  else
850  {
851  //otherwise return permenant user set extent
852  return &mExtent;
853  }
854 }
855 
856 void QgsComposerMap::setNewScale( double scaleDenominator, bool forceUpdate )
857 {
858  double currentScaleDenominator = scale();
859 
860  if ( scaleDenominator == currentScaleDenominator || scaleDenominator == 0 )
861  {
862  return;
863  }
864 
865  double scaleRatio = scaleDenominator / currentScaleDenominator;
866  currentMapExtent()->scale( scaleRatio );
867 
868  if ( mAtlasDriven && mAtlasScalingMode == Fixed && mComposition->atlasMode() != QgsComposition::AtlasOff )
869  {
870  //if map is atlas controlled and set to fixed scaling mode, then scale changes should be treated as permanant
871  //and also apply to the map's original extent (see #9602)
872  //we can't use the scaleRatio calculated earlier, as the scale can vary depending on extent for geographic coordinate systems
873  QgsScaleCalculator calculator;
874  calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
875  calculator.setDpi( 25.4 ); //QGraphicsView units are mm
876  scaleRatio = scaleDenominator / calculator.calculate( mExtent, rect().width() );
877  mExtent.scale( scaleRatio );
878  }
879 
880  mCacheUpdated = false;
881  if ( forceUpdate )
882  {
883  cache();
884  update();
885  emit itemChanged();
886  }
887  emit extentChanged();
888 }
889 
891 {
892  mPreviewMode = m;
893  emit itemChanged();
894 }
895 
896 void QgsComposerMap::setOffset( double xOffset, double yOffset )
897 {
898  mXOffset = xOffset;
899  mYOffset = yOffset;
900 }
901 
903 {
904  //kept for api compatibility with QGIS 2.0
905  setMapRotation( r );
906 }
907 
909 {
910  mMapRotation = r;
911  mEvaluatedMapRotation = mMapRotation;
912  emit mapRotationChanged( r );
913  emit itemChanged();
914  update();
915 }
916 
918 {
919  return valueType == QgsComposerObject::EvaluatedValue ? mEvaluatedMapRotation : mMapRotation;
920 }
921 
922 void QgsComposerMap::refreshMapExtents()
923 {
924  //data defined map extents set?
925  QVariant exprVal;
926 
927  QgsRectangle newExtent = *currentMapExtent();
928  bool useDdXMin = false;
929  bool useDdXMax = false;
930  bool useDdYMin = false;
931  bool useDdYMax = false;
932  double minXD = 0;
933  double minYD = 0;
934  double maxXD = 0;
935  double maxYD = 0;
936 
938  {
939  bool ok;
940  minXD = exprVal.toDouble( &ok );
941  QgsDebugMsg( QString( "exprVal Map XMin:%1" ).arg( minXD ) );
942  if ( ok && !exprVal.isNull() )
943  {
944  useDdXMin = true;
945  newExtent.setXMinimum( minXD );
946  }
947  }
949  {
950  bool ok;
951  minYD = exprVal.toDouble( &ok );
952  QgsDebugMsg( QString( "exprVal Map YMin:%1" ).arg( minYD ) );
953  if ( ok && !exprVal.isNull() )
954  {
955  useDdYMin = true;
956  newExtent.setYMinimum( minYD );
957  }
958  }
960  {
961  bool ok;
962  maxXD = exprVal.toDouble( &ok );
963  QgsDebugMsg( QString( "exprVal Map XMax:%1" ).arg( maxXD ) );
964  if ( ok && !exprVal.isNull() )
965  {
966  useDdXMax = true;
967  newExtent.setXMaximum( maxXD );
968  }
969  }
971  {
972  bool ok;
973  maxYD = exprVal.toDouble( &ok );
974  QgsDebugMsg( QString( "exprVal Map YMax:%1" ).arg( maxYD ) );
975  if ( ok && !exprVal.isNull() )
976  {
977  useDdYMax = true;
978  newExtent.setYMaximum( maxYD );
979  }
980  }
981 
982  if ( newExtent != *currentMapExtent() )
983  {
984  //calculate new extents to fit data defined extents
985 
986  //Make sure the width/height ratio is the same as in current map extent.
987  //This is to keep the map item frame and the page layout fixed
988  double currentWidthHeightRatio = currentMapExtent()->width() / currentMapExtent()->height();
989  double newWidthHeightRatio = newExtent.width() / newExtent.height();
990 
991  if ( currentWidthHeightRatio < newWidthHeightRatio )
992  {
993  //enlarge height of new extent, ensuring the map center stays the same
994  double newHeight = newExtent.width() / currentWidthHeightRatio;
995  double deltaHeight = newHeight - newExtent.height();
996  newExtent.setYMinimum( newExtent.yMinimum() - deltaHeight / 2 );
997  newExtent.setYMaximum( newExtent.yMaximum() + deltaHeight / 2 );
998  }
999  else
1000  {
1001  //enlarge width of new extent, ensuring the map center stays the same
1002  double newWidth = currentWidthHeightRatio * newExtent.height();
1003  double deltaWidth = newWidth - newExtent.width();
1004  newExtent.setXMinimum( newExtent.xMinimum() - deltaWidth / 2 );
1005  newExtent.setXMaximum( newExtent.xMaximum() + deltaWidth / 2 );
1006  }
1007 
1008  *currentMapExtent() = newExtent;
1009  }
1010 
1011  //now refresh scale, as this potentially overrides extents
1012 
1013  //data defined map scale set?
1015  {
1016  bool ok;
1017  double scaleD = exprVal.toDouble( &ok );
1018  QgsDebugMsg( QString( "exprVal Map Scale:%1" ).arg( scaleD ) );
1019  if ( ok && !exprVal.isNull() )
1020  {
1021  setNewScale( scaleD, false );
1022  newExtent = *currentMapExtent();
1023  }
1024  }
1025 
1026  if ( useDdXMax || useDdXMin || useDdYMax || useDdYMin )
1027  {
1028  //if only one of min/max was set for either x or y, then make sure our extent is locked on that value
1029  //as we can do this without altering the scale
1030  if ( useDdXMin && !useDdXMax )
1031  {
1032  double xMax = currentMapExtent()->xMaximum() - ( currentMapExtent()->xMinimum() - minXD );
1033  newExtent.setXMinimum( minXD );
1034  newExtent.setXMaximum( xMax );
1035  }
1036  else if ( !useDdXMin && useDdXMax )
1037  {
1038  double xMin = currentMapExtent()->xMinimum() - ( currentMapExtent()->xMaximum() - maxXD );
1039  newExtent.setXMinimum( xMin );
1040  newExtent.setXMaximum( maxXD );
1041  }
1042  if ( useDdYMin && !useDdYMax )
1043  {
1044  double yMax = currentMapExtent()->yMaximum() - ( currentMapExtent()->yMinimum() - minYD );
1045  newExtent.setYMinimum( minYD );
1046  newExtent.setYMaximum( yMax );
1047  }
1048  else if ( !useDdYMin && useDdYMax )
1049  {
1050  double yMin = currentMapExtent()->yMinimum() - ( currentMapExtent()->yMaximum() - maxYD );
1051  newExtent.setYMinimum( yMin );
1052  newExtent.setYMaximum( maxYD );
1053  }
1054 
1055  if ( newExtent != *currentMapExtent() )
1056  {
1057  *currentMapExtent() = newExtent;
1058  }
1059  }
1060 
1061  //lastly, map rotation overrides all
1062  double mapRotation = mMapRotation;
1063 
1064  //data defined map rotation set?
1066  {
1067  bool ok;
1068  double rotationD = exprVal.toDouble( &ok );
1069  QgsDebugMsg( QString( "exprVal Map Rotation:%1" ).arg( rotationD ) );
1070  if ( ok && !exprVal.isNull() )
1071  {
1072  mapRotation = rotationD;
1073  }
1074  }
1075 
1076  if ( mEvaluatedMapRotation != mapRotation )
1077  {
1078  mEvaluatedMapRotation = mapRotation;
1079  emit mapRotationChanged( mapRotation );
1080  }
1081 
1082 }
1083 
1085 {
1086  if ( !mUpdatesEnabled )
1087  {
1088  return;
1089  }
1090 
1091  if ( mPreviewMode != QgsComposerMap::Rectangle && !mCacheUpdated )
1092  {
1093  cache();
1094  }
1096 }
1097 
1099 {
1100  QStringList layers = mComposition->mapSettings().layers();
1101 
1102  QStringList::const_iterator layer_it = layers.constBegin();
1103  QgsMapLayer* currentLayer = 0;
1104 
1105  for ( ; layer_it != layers.constEnd(); ++layer_it )
1106  {
1107  currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *layer_it );
1108  if ( currentLayer )
1109  {
1110  QgsRasterLayer* currentRasterLayer = qobject_cast<QgsRasterLayer *>( currentLayer );
1111  if ( currentRasterLayer )
1112  {
1113  const QgsRasterDataProvider* rasterProvider = 0;
1114  if (( rasterProvider = currentRasterLayer->dataProvider() ) )
1115  {
1116  if ( rasterProvider->name() == "wms" )
1117  {
1118  return true;
1119  }
1120  }
1121  }
1122  }
1123  }
1124  return false;
1125 }
1126 
1128 {
1129  //check easy things first
1130 
1131  //overviews
1132  if ( mOverviewStack->containsAdvancedEffects() )
1133  {
1134  return true;
1135  }
1136 
1137  //grids
1138  if ( mGridStack->containsAdvancedEffects() )
1139  {
1140  return true;
1141  }
1142 
1143  // check if map contains advanced effects like blend modes, or flattened layers for transparency
1144 
1145  QStringList layers = mComposition->mapSettings().layers();
1146 
1147  QStringList::const_iterator layer_it = layers.constBegin();
1148  QgsMapLayer* currentLayer = 0;
1149 
1150  for ( ; layer_it != layers.constEnd(); ++layer_it )
1151  {
1152  currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *layer_it );
1153  if ( currentLayer )
1154  {
1155  if ( currentLayer->blendMode() != QPainter::CompositionMode_SourceOver )
1156  {
1157  return true;
1158  }
1159  // if vector layer, check labels and feature blend mode
1160  QgsVectorLayer* currentVectorLayer = qobject_cast<QgsVectorLayer *>( currentLayer );
1161  if ( currentVectorLayer )
1162  {
1163  if ( currentVectorLayer->layerTransparency() != 0 )
1164  {
1165  return true;
1166  }
1167  if ( currentVectorLayer->featureBlendMode() != QPainter::CompositionMode_SourceOver )
1168  {
1169  return true;
1170  }
1171  // check label blend modes
1172  if ( QgsPalLabeling::staticWillUseLayer( currentVectorLayer ) )
1173  {
1174  // Check all label blending properties
1175  QgsPalLayerSettings layerSettings = QgsPalLayerSettings::fromLayer( currentVectorLayer );
1176  if (( layerSettings.blendMode != QPainter::CompositionMode_SourceOver ) ||
1177  ( layerSettings.bufferSize != 0 && layerSettings.bufferBlendMode != QPainter::CompositionMode_SourceOver ) ||
1178  ( layerSettings.shadowDraw && layerSettings.shadowBlendMode != QPainter::CompositionMode_SourceOver ) ||
1179  ( layerSettings.shapeDraw && layerSettings.shapeBlendMode != QPainter::CompositionMode_SourceOver ) )
1180  {
1181  return true;
1182  }
1183  }
1184  }
1185  }
1186  }
1187 
1188  return false;
1189 }
1190 
1191 void QgsComposerMap::connectUpdateSlot()
1192 {
1193  //connect signal from layer registry to update in case of new or deleted layers
1195  if ( layerRegistry )
1196  {
1197  connect( layerRegistry, SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( updateCachedImage() ) );
1198  connect( layerRegistry, SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( updateCachedImage() ) );
1199  }
1200 }
1201 
1202 bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
1203 {
1204  if ( elem.isNull() )
1205  {
1206  return false;
1207  }
1208 
1209  QDomElement composerMapElem = doc.createElement( "ComposerMap" );
1210  composerMapElem.setAttribute( "id", mId );
1211 
1212  //previewMode
1213  if ( mPreviewMode == Cache )
1214  {
1215  composerMapElem.setAttribute( "previewMode", "Cache" );
1216  }
1217  else if ( mPreviewMode == Render )
1218  {
1219  composerMapElem.setAttribute( "previewMode", "Render" );
1220  }
1221  else //rectangle
1222  {
1223  composerMapElem.setAttribute( "previewMode", "Rectangle" );
1224  }
1225 
1226  if ( mKeepLayerSet )
1227  {
1228  composerMapElem.setAttribute( "keepLayerSet", "true" );
1229  }
1230  else
1231  {
1232  composerMapElem.setAttribute( "keepLayerSet", "false" );
1233  }
1234 
1235  if ( mDrawCanvasItems )
1236  {
1237  composerMapElem.setAttribute( "drawCanvasItems", "true" );
1238  }
1239  else
1240  {
1241  composerMapElem.setAttribute( "drawCanvasItems", "false" );
1242  }
1243 
1244  //extent
1245  QDomElement extentElem = doc.createElement( "Extent" );
1246  extentElem.setAttribute( "xmin", qgsDoubleToString( mExtent.xMinimum() ) );
1247  extentElem.setAttribute( "xmax", qgsDoubleToString( mExtent.xMaximum() ) );
1248  extentElem.setAttribute( "ymin", qgsDoubleToString( mExtent.yMinimum() ) );
1249  extentElem.setAttribute( "ymax", qgsDoubleToString( mExtent.yMaximum() ) );
1250  composerMapElem.appendChild( extentElem );
1251 
1252  //map rotation
1253  composerMapElem.setAttribute( "mapRotation", QString::number( mMapRotation ) );
1254 
1255  //layer set
1256  QDomElement layerSetElem = doc.createElement( "LayerSet" );
1257  QStringList::const_iterator layerIt = mLayerSet.constBegin();
1258  for ( ; layerIt != mLayerSet.constEnd(); ++layerIt )
1259  {
1260  QDomElement layerElem = doc.createElement( "Layer" );
1261  QDomText layerIdText = doc.createTextNode( *layerIt );
1262  layerElem.appendChild( layerIdText );
1263  layerSetElem.appendChild( layerElem );
1264  }
1265  composerMapElem.appendChild( layerSetElem );
1266 
1267  //write a dummy "Grid" element to prevent crashes on pre 2.5 versions (refs #10905)
1268  QDomElement gridElem = doc.createElement( "Grid" );
1269  composerMapElem.appendChild( gridElem );
1270 
1271  //grids
1272  mGridStack->writeXML( composerMapElem, doc );
1273 
1274  //overviews
1275  mOverviewStack->writeXML( composerMapElem, doc );
1276 
1277  //atlas
1278  QDomElement atlasElem = doc.createElement( "AtlasMap" );
1279  atlasElem.setAttribute( "atlasDriven", mAtlasDriven );
1280  atlasElem.setAttribute( "scalingMode", mAtlasScalingMode );
1281  atlasElem.setAttribute( "margin", qgsDoubleToString( mAtlasMargin ) );
1282  composerMapElem.appendChild( atlasElem );
1283 
1284  elem.appendChild( composerMapElem );
1285  return _writeXML( composerMapElem, doc );
1286 }
1287 
1288 bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& doc )
1289 {
1290  if ( itemElem.isNull() )
1291  {
1292  return false;
1293  }
1294 
1295  QString idRead = itemElem.attribute( "id", "not found" );
1296  if ( idRead != "not found" )
1297  {
1298  mId = idRead.toInt();
1299  updateToolTip();
1300  }
1301  mPreviewMode = Rectangle;
1302 
1303  //previewMode
1304  QString previewMode = itemElem.attribute( "previewMode" );
1305  if ( previewMode == "Cache" )
1306  {
1307  mPreviewMode = Cache;
1308  }
1309  else if ( previewMode == "Render" )
1310  {
1311  mPreviewMode = Render;
1312  }
1313  else
1314  {
1315  mPreviewMode = Rectangle;
1316  }
1317 
1318  //extent
1319  QDomNodeList extentNodeList = itemElem.elementsByTagName( "Extent" );
1320  if ( extentNodeList.size() > 0 )
1321  {
1322  QDomElement extentElem = extentNodeList.at( 0 ).toElement();
1323  double xmin, xmax, ymin, ymax;
1324  xmin = extentElem.attribute( "xmin" ).toDouble();
1325  xmax = extentElem.attribute( "xmax" ).toDouble();
1326  ymin = extentElem.attribute( "ymin" ).toDouble();
1327  ymax = extentElem.attribute( "ymax" ).toDouble();
1328  setNewExtent( QgsRectangle( xmin, ymin, xmax, ymax ) );
1329  }
1330 
1331  //map rotation
1332  if ( itemElem.attribute( "mapRotation", "0" ).toDouble() != 0 )
1333  {
1334  mMapRotation = itemElem.attribute( "mapRotation", "0" ).toDouble();
1335  }
1336 
1337  //mKeepLayerSet flag
1338  QString keepLayerSetFlag = itemElem.attribute( "keepLayerSet" );
1339  if ( keepLayerSetFlag.compare( "true", Qt::CaseInsensitive ) == 0 )
1340  {
1341  mKeepLayerSet = true;
1342  }
1343  else
1344  {
1345  mKeepLayerSet = false;
1346  }
1347 
1348  QString drawCanvasItemsFlag = itemElem.attribute( "drawCanvasItems", "true" );
1349  if ( drawCanvasItemsFlag.compare( "true", Qt::CaseInsensitive ) == 0 )
1350  {
1351  mDrawCanvasItems = true;
1352  }
1353  else
1354  {
1355  mDrawCanvasItems = false;
1356  }
1357 
1358  //mLayerSet
1359  QDomNodeList layerSetNodeList = itemElem.elementsByTagName( "LayerSet" );
1360  QStringList layerSet;
1361  if ( layerSetNodeList.size() > 0 )
1362  {
1363  QDomElement layerSetElem = layerSetNodeList.at( 0 ).toElement();
1364  QDomNodeList layerIdNodeList = layerSetElem.elementsByTagName( "Layer" );
1365  for ( int i = 0; i < layerIdNodeList.size(); ++i )
1366  {
1367  layerSet << layerIdNodeList.at( i ).toElement().text();
1368  }
1369  }
1370  mLayerSet = layerSet;
1371 
1372  mDrawing = false;
1373  mNumCachedLayers = 0;
1374  mCacheUpdated = false;
1375 
1376  //overviews
1377  mOverviewStack->readXML( itemElem, doc );
1378 
1379  //grids
1380  mGridStack->readXML( itemElem, doc );
1381 
1382  //load grid / grid annotation in old xml format
1383  //only do this if the grid stack didn't load any grids, otherwise this will
1384  //be the dummy element created by QGIS >= 2.5 (refs #10905)
1385  QDomNodeList gridNodeList = itemElem.elementsByTagName( "Grid" );
1386  if ( mGridStack->size() == 0 && gridNodeList.size() > 0 )
1387  {
1388  QDomElement gridElem = gridNodeList.at( 0 ).toElement();
1389  QgsComposerMapGrid* mapGrid = new QgsComposerMapGrid( tr( "Grid %1" ).arg( 1 ), this );
1390  mapGrid->setEnabled( gridElem.attribute( "show", "0" ) != "0" );
1391  mapGrid->setStyle( QgsComposerMapGrid::GridStyle( gridElem.attribute( "gridStyle", "0" ).toInt() ) );
1392  mapGrid->setIntervalX( gridElem.attribute( "intervalX", "0" ).toDouble() );
1393  mapGrid->setIntervalY( gridElem.attribute( "intervalY", "0" ).toDouble() );
1394  mapGrid->setOffsetX( gridElem.attribute( "offsetX", "0" ).toDouble() );
1395  mapGrid->setOffsetY( gridElem.attribute( "offsetY", "0" ).toDouble() );
1396  mapGrid->setCrossLength( gridElem.attribute( "crossLength", "3" ).toDouble() );
1397  mapGrid->setFrameStyle(( QgsComposerMapGrid::FrameStyle )gridElem.attribute( "gridFrameStyle", "0" ).toInt() );
1398  mapGrid->setFrameWidth( gridElem.attribute( "gridFrameWidth", "2.0" ).toDouble() );
1399  mapGrid->setFramePenSize( gridElem.attribute( "gridFramePenThickness", "0.5" ).toDouble() );
1400  mapGrid->setFramePenColor( QgsSymbolLayerV2Utils::decodeColor( gridElem.attribute( "framePenColor", "0,0,0" ) ) );
1401  mapGrid->setFrameFillColor1( QgsSymbolLayerV2Utils::decodeColor( gridElem.attribute( "frameFillColor1", "255,255,255,255" ) ) );
1402  mapGrid->setFrameFillColor2( QgsSymbolLayerV2Utils::decodeColor( gridElem.attribute( "frameFillColor2", "0,0,0,255" ) ) );
1403  mapGrid->setBlendMode( QgsMapRenderer::getCompositionMode(( QgsMapRenderer::BlendMode ) itemElem.attribute( "gridBlendMode", "0" ).toUInt() ) );
1404  QDomElement gridSymbolElem = gridElem.firstChildElement( "symbol" );
1405  QgsLineSymbolV2* lineSymbol = 0;
1406  if ( gridSymbolElem.isNull() )
1407  {
1408  //old project file, read penWidth /penColorRed, penColorGreen, penColorBlue
1409  lineSymbol = QgsLineSymbolV2::createSimple( QgsStringMap() );
1410  lineSymbol->setWidth( gridElem.attribute( "penWidth", "0" ).toDouble() );
1411  lineSymbol->setColor( QColor( gridElem.attribute( "penColorRed", "0" ).toInt(),
1412  gridElem.attribute( "penColorGreen", "0" ).toInt(),
1413  gridElem.attribute( "penColorBlue", "0" ).toInt() ) );
1414  }
1415  else
1416  {
1417  lineSymbol = dynamic_cast<QgsLineSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( gridSymbolElem ) );
1418  }
1419  mapGrid->setLineSymbol( lineSymbol );
1420 
1421  //annotation
1422  QDomNodeList annotationNodeList = gridElem.elementsByTagName( "Annotation" );
1423  if ( annotationNodeList.size() > 0 )
1424  {
1425  QDomElement annotationElem = annotationNodeList.at( 0 ).toElement();
1426  mapGrid->setAnnotationEnabled( annotationElem.attribute( "show", "0" ) != "0" );
1427  mapGrid->setAnnotationFormat( QgsComposerMapGrid::AnnotationFormat( annotationElem.attribute( "format", "0" ).toInt() ) );
1428  mapGrid->setAnnotationPosition( QgsComposerMapGrid::AnnotationPosition( annotationElem.attribute( "leftPosition", "0" ).toInt() ), QgsComposerMapGrid::Left );
1429  mapGrid->setAnnotationPosition( QgsComposerMapGrid::AnnotationPosition( annotationElem.attribute( "rightPosition", "0" ).toInt() ), QgsComposerMapGrid::Right );
1430  mapGrid->setAnnotationPosition( QgsComposerMapGrid::AnnotationPosition( annotationElem.attribute( "topPosition", "0" ).toInt() ), QgsComposerMapGrid::Top );
1431  mapGrid->setAnnotationPosition( QgsComposerMapGrid::AnnotationPosition( annotationElem.attribute( "bottomPosition", "0" ).toInt() ), QgsComposerMapGrid::Bottom );
1432  mapGrid->setAnnotationDirection( QgsComposerMapGrid::AnnotationDirection( annotationElem.attribute( "leftDirection", "0" ).toInt() ), QgsComposerMapGrid::Left );
1433  mapGrid->setAnnotationDirection( QgsComposerMapGrid::AnnotationDirection( annotationElem.attribute( "rightDirection", "0" ).toInt() ), QgsComposerMapGrid::Right );
1434  mapGrid->setAnnotationDirection( QgsComposerMapGrid::AnnotationDirection( annotationElem.attribute( "topDirection", "0" ).toInt() ), QgsComposerMapGrid::Top );
1435  mapGrid->setAnnotationDirection( QgsComposerMapGrid::AnnotationDirection( annotationElem.attribute( "bottomDirection", "0" ).toInt() ), QgsComposerMapGrid::Bottom );
1436  mapGrid->setAnnotationFrameDistance( annotationElem.attribute( "frameDistance", "0" ).toDouble() );
1437  QFont annotationFont;
1438  annotationFont.fromString( annotationElem.attribute( "font", "" ) );
1439  mapGrid->setAnnotationFont( annotationFont );
1440  mapGrid->setAnnotationFontColor( QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "fontColor", "0,0,0,255" ) ) );
1441 
1442  mapGrid->setAnnotationPrecision( annotationElem.attribute( "precision", "3" ).toInt() );
1443  }
1444  mGridStack->addGrid( mapGrid );
1445  }
1446 
1447  //load overview in old xml format
1448  QDomElement overviewFrameElem = itemElem.firstChildElement( "overviewFrame" );
1449  if ( !overviewFrameElem.isNull() )
1450  {
1451  QgsComposerMapOverview* mapOverview = new QgsComposerMapOverview( tr( "Overview %1" ).arg( mOverviewStack->size() + 1 ), this );
1452 
1453  mapOverview->setFrameMap( overviewFrameElem.attribute( "overviewFrameMap", "-1" ).toInt() );
1454  mapOverview->setBlendMode( QgsMapRenderer::getCompositionMode(( QgsMapRenderer::BlendMode ) overviewFrameElem.attribute( "overviewBlendMode", "0" ).toUInt() ) );
1455  mapOverview->setInverted( overviewFrameElem.attribute( "overviewInverted" ).compare( "true", Qt::CaseInsensitive ) == 0 );
1456  mapOverview->setCentered( overviewFrameElem.attribute( "overviewCentered" ).compare( "true", Qt::CaseInsensitive ) == 0 );
1457 
1458  QgsFillSymbolV2* fillSymbol = 0;
1459  QDomElement overviewFrameSymbolElem = overviewFrameElem.firstChildElement( "symbol" );
1460  if ( !overviewFrameSymbolElem.isNull() )
1461  {
1462  fillSymbol = dynamic_cast<QgsFillSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( overviewFrameSymbolElem ) );
1463  mapOverview->setFrameSymbol( fillSymbol );
1464  }
1465  mOverviewStack->addOverview( mapOverview );
1466  }
1467 
1468  //atlas
1469  QDomNodeList atlasNodeList = itemElem.elementsByTagName( "AtlasMap" );
1470  if ( atlasNodeList.size() > 0 )
1471  {
1472  QDomElement atlasElem = atlasNodeList.at( 0 ).toElement();
1473  mAtlasDriven = ( atlasElem.attribute( "atlasDriven", "0" ) != "0" );
1474  if ( atlasElem.hasAttribute( "fixedScale" ) ) // deprecated XML
1475  {
1476  mAtlasScalingMode = ( atlasElem.attribute( "fixedScale", "0" ) != "0" ) ? Fixed : Auto;
1477  }
1478  else if ( atlasElem.hasAttribute( "scalingMode" ) )
1479  {
1480  mAtlasScalingMode = static_cast<AtlasScalingMode>( atlasElem.attribute( "scalingMode" ).toInt() );
1481  }
1482  mAtlasMargin = atlasElem.attribute( "margin", "0.1" ).toDouble();
1483  }
1484 
1485  //restore general composer item properties
1486  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
1487  if ( composerItemList.size() > 0 )
1488  {
1489  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
1490 
1491  if ( composerItemElem.attribute( "rotation", "0" ).toDouble() != 0 )
1492  {
1493  //in versions prior to 2.1 map rotation was stored in the rotation attribute
1494  mMapRotation = composerItemElem.attribute( "rotation", "0" ).toDouble();
1495  }
1496 
1497  _readXML( composerItemElem, doc );
1498  }
1499 
1501  emit itemChanged();
1502  return true;
1503 }
1504 
1506 {
1507  mLayerSet = mComposition->mapSettings().layers();
1508 }
1509 
1510 void QgsComposerMap::syncLayerSet()
1511 {
1512  if ( mLayerSet.size() < 1 )
1513  {
1514  return;
1515  }
1516 
1517  //if layer set is fixed, do a lookup in the layer registry to also find the non-visible layers
1518  QStringList currentLayerSet;
1519  if ( mKeepLayerSet )
1520  {
1521  currentLayerSet = QgsMapLayerRegistry::instance()->mapLayers().uniqueKeys();
1522  }
1523  else //only consider layers visible in the map
1524  {
1525  currentLayerSet = mComposition->mapSettings().layers();
1526  }
1527 
1528  for ( int i = mLayerSet.size() - 1; i >= 0; --i )
1529  {
1530  if ( !currentLayerSet.contains( mLayerSet.at( i ) ) )
1531  {
1532  mLayerSet.removeAt( i );
1533  }
1534  }
1535 }
1536 
1538 {
1539  if ( mGridStack->size() < 1 )
1540  {
1541  QgsComposerMapGrid* grid = new QgsComposerMapGrid( tr( "Grid %1" ).arg( 1 ), this );
1542  mGridStack->addGrid( grid );
1543  }
1544  return mGridStack->grid( 0 );
1545 }
1546 
1547 const QgsComposerMapGrid* QgsComposerMap::constFirstMapGrid() const
1548 {
1549  return const_cast<QgsComposerMap*>( this )->grid();
1550 }
1551 
1553 {
1554  QgsComposerMapGrid* g = grid();
1555  g->setStyle( QgsComposerMapGrid::GridStyle( style ) );
1556 }
1557 
1559 {
1560  const QgsComposerMapGrid* g = constFirstMapGrid();
1561  return ( QgsComposerMap::GridStyle )g->style();
1562 }
1563 
1564 void QgsComposerMap::setGridIntervalX( double interval )
1565 {
1566  QgsComposerMapGrid* g = grid();
1567  g->setIntervalX( interval );
1568 }
1569 
1571 {
1572  const QgsComposerMapGrid* g = constFirstMapGrid();
1573  return g->intervalX();
1574 }
1575 
1576 void QgsComposerMap::setGridIntervalY( double interval )
1577 {
1578  QgsComposerMapGrid* g = grid();
1579  g->setIntervalY( interval );
1580 }
1581 
1583 {
1584  const QgsComposerMapGrid* g = constFirstMapGrid();
1585  return g->intervalY();
1586 }
1587 
1588 void QgsComposerMap::setGridOffsetX( double offset )
1589 {
1590  QgsComposerMapGrid* g = grid();
1591  g->setOffsetX( offset );
1592 }
1593 
1595 {
1596  const QgsComposerMapGrid* g = constFirstMapGrid();
1597  return g->offsetX();
1598 }
1599 
1600 void QgsComposerMap::setGridOffsetY( double offset )
1601 {
1602  QgsComposerMapGrid* g = grid();
1603  g->setOffsetY( offset );
1604 }
1605 
1607 {
1608  const QgsComposerMapGrid* g = constFirstMapGrid();
1609  return g->offsetY();
1610 }
1611 
1613 {
1614  QgsComposerMapGrid* g = grid();
1615  g->setGridLineWidth( w );
1616 }
1617 
1618 void QgsComposerMap::setGridPenColor( const QColor& c )
1619 {
1620  QgsComposerMapGrid* g = grid();
1621  g->setGridLineColor( c );
1622 }
1623 
1624 void QgsComposerMap::setGridPen( const QPen& p )
1625 {
1626  QgsComposerMapGrid* g = grid();
1627  g->setGridLineWidth( p.widthF() );
1628  g->setGridLineColor( p.color() );
1629 }
1630 
1632 {
1633  const QgsComposerMapGrid* g = constFirstMapGrid();
1634  QPen p;
1635  if ( g->lineSymbol() )
1636  {
1637  QgsLineSymbolV2* line = dynamic_cast<QgsLineSymbolV2*>( g->lineSymbol()->clone() );
1638  p.setWidthF( line->width() );
1639  p.setColor( line->color() );
1640  p.setCapStyle( Qt::FlatCap );
1641  delete line;
1642  }
1643  return p;
1644 }
1645 
1647 {
1648  QgsComposerMapGrid* g = grid();
1649  g->setAnnotationFont( f );
1650 }
1651 
1653 {
1654  const QgsComposerMapGrid* g = constFirstMapGrid();
1655  return g->annotationFont();
1656 }
1657 
1659 {
1660  QgsComposerMapGrid* g = grid();
1661  g->setAnnotationFontColor( c );
1662 }
1663 
1665 {
1666  const QgsComposerMapGrid* g = constFirstMapGrid();
1667  return g->annotationFontColor();
1668 }
1669 
1671 {
1672  QgsComposerMapGrid* g = grid();
1673  g->setAnnotationPrecision( p );
1674 }
1675 
1677 {
1678  const QgsComposerMapGrid* g = constFirstMapGrid();
1679  return g->annotationPrecision();
1680 }
1681 
1683 {
1684  QgsComposerMapGrid* g = grid();
1685  g->setAnnotationEnabled( show );
1686 }
1687 
1689 {
1690  const QgsComposerMapGrid* g = constFirstMapGrid();
1691  return g->annotationEnabled();
1692 }
1693 
1695 {
1696  QgsComposerMapGrid* g = grid();
1697  if ( p != QgsComposerMap::Disabled )
1698  {
1700  }
1701  else
1702  {
1704  }
1705 }
1706 
1708 {
1709  const QgsComposerMapGrid* g = constFirstMapGrid();
1711 }
1712 
1714 {
1715  QgsComposerMapGrid* g = grid();
1717 }
1718 
1720 {
1721  const QgsComposerMapGrid* g = constFirstMapGrid();
1722  return g->annotationFrameDistance();
1723 }
1724 
1726 {
1727  QgsComposerMapGrid* g = grid();
1728  //map grid direction to QgsComposerMapGrid direction (values are different)
1730  switch ( d )
1731  {
1733  gridDirection = QgsComposerMapGrid::Horizontal;
1734  break;
1736  gridDirection = QgsComposerMapGrid::Vertical;
1737  break;
1739  gridDirection = QgsComposerMapGrid::BoundaryDirection;
1740  break;
1741  default:
1742  gridDirection = QgsComposerMapGrid::Horizontal;
1743  }
1744  g->setAnnotationDirection( gridDirection, ( QgsComposerMapGrid::BorderSide )border );
1745 
1746 }
1747 
1749 {
1750  const QgsComposerMapGrid* g = constFirstMapGrid();
1752 }
1753 
1755 {
1756  QgsComposerMapGrid* g = grid();
1758 }
1759 
1761 {
1762  const QgsComposerMapGrid* g = constFirstMapGrid();
1764 }
1765 
1767 {
1768  QgsComposerMapGrid* g = grid();
1770 }
1771 
1773 {
1774  const QgsComposerMapGrid* g = constFirstMapGrid();
1776 }
1777 
1779 {
1780  QgsComposerMapGrid* g = grid();
1781  g->setFrameWidth( w );
1782 }
1783 
1785 {
1786  const QgsComposerMapGrid* g = constFirstMapGrid();
1787  return g->frameWidth();
1788 }
1789 
1791 {
1792  QgsComposerMapGrid* g = grid();
1793  g->setFramePenSize( w );
1794 }
1795 
1797 {
1798  const QgsComposerMapGrid* g = constFirstMapGrid();
1799  return g->framePenSize();
1800 }
1801 
1803 {
1804  QgsComposerMapGrid* g = grid();
1805  g->setFramePenColor( c );
1806 }
1807 
1809 {
1810  const QgsComposerMapGrid* g = constFirstMapGrid();
1811  return g->framePenColor();
1812 }
1813 
1815 {
1816  QgsComposerMapGrid* g = grid();
1817  g->setFrameFillColor1( c );
1818 }
1819 
1821 {
1822  const QgsComposerMapGrid* g = constFirstMapGrid();
1823  return g->frameFillColor1();
1824 }
1825 
1827 {
1828  QgsComposerMapGrid* g = grid();
1829  g->setFrameFillColor2( c );
1830 }
1831 
1833 {
1834  const QgsComposerMapGrid* g = constFirstMapGrid();
1835  return g->frameFillColor2();
1836 }
1837 
1839 {
1840  QgsComposerMapGrid* g = grid();
1841  g->setCrossLength( l );
1842 }
1843 
1845 {
1846  const QgsComposerMapGrid* g = constFirstMapGrid();
1847  return g->crossLength();
1848 }
1849 
1851 {
1852  if ( mOverviewStack->size() < 1 )
1853  {
1854  QgsComposerMapOverview* overview = new QgsComposerMapOverview( tr( "Overview %1" ).arg( 1 ), this );
1855  mOverviewStack->addOverview( overview );
1856  }
1857  return mOverviewStack->overview( 0 );
1858 }
1859 
1860 const QgsComposerMapOverview *QgsComposerMap::constFirstMapOverview() const
1861 {
1862  return const_cast<QgsComposerMap*>( this )->overview();
1863 }
1864 
1865 void QgsComposerMap::setGridBlendMode( QPainter::CompositionMode blendMode )
1866 {
1867  QgsComposerMapGrid* g = grid();
1868  g->setBlendMode( blendMode );
1869 }
1870 
1871 QPainter::CompositionMode QgsComposerMap::gridBlendMode() const
1872 {
1873  const QgsComposerMapGrid* g = constFirstMapGrid();
1874  return g->blendMode();
1875 }
1876 
1878 {
1879  return mCurrentRectangle;
1880 }
1881 
1883 {
1884  QRectF rectangle = rect();
1885  double frameExtension = mFrame ? pen().widthF() / 2.0 : 0.0;
1886  double maxGridExtension = mGridStack ? mGridStack->maxGridExtension() : 0;
1887 
1888  double maxExtension = qMax( frameExtension, maxGridExtension );
1889 
1890  rectangle.setLeft( rectangle.left() - maxExtension );
1891  rectangle.setRight( rectangle.right() + maxExtension );
1892  rectangle.setTop( rectangle.top() - maxExtension );
1893  rectangle.setBottom( rectangle.bottom() + maxExtension );
1894  if ( rectangle != mCurrentRectangle )
1895  {
1896  prepareGeometryChange();
1897  mCurrentRectangle = rectangle;
1898  }
1899 }
1900 
1902 {
1903  QgsComposerItem::setFrameOutlineWidth( outlineWidth );
1905 }
1906 
1907 QgsRectangle QgsComposerMap::transformedExtent() const
1908 {
1909  double dx = mXOffset;
1910  double dy = mYOffset;
1911  transformShift( dx, dy );
1912  return QgsRectangle( currentMapExtent()->xMinimum() - dx, currentMapExtent()->yMinimum() - dy, currentMapExtent()->xMaximum() - dx, currentMapExtent()->yMaximum() - dy );
1913 }
1914 
1916 {
1917  double dx = mXOffset;
1918  double dy = mYOffset;
1919  //qWarning("offset");
1920  //qWarning(QString::number(dx).toLocal8Bit().data());
1921  //qWarning(QString::number(dy).toLocal8Bit().data());
1922  transformShift( dx, dy );
1923  //qWarning("transformed:");
1924  //qWarning(QString::number(dx).toLocal8Bit().data());
1925  //qWarning(QString::number(dy).toLocal8Bit().data());
1926  QPolygonF poly = visibleExtentPolygon();
1927  poly.translate( -dx, -dy );
1928  return poly;
1929 }
1930 
1931 void QgsComposerMap::mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const
1932 {
1933  poly.clear();
1934  if ( mEvaluatedMapRotation == 0 )
1935  {
1936  poly << QPointF( extent.xMinimum(), extent.yMaximum() );
1937  poly << QPointF( extent.xMaximum(), extent.yMaximum() );
1938  poly << QPointF( extent.xMaximum(), extent.yMinimum() );
1939  poly << QPointF( extent.xMinimum(), extent.yMinimum() );
1940  //ensure polygon is closed by readding first point
1941  poly << QPointF( poly.at( 0 ) );
1942  return;
1943  }
1944 
1945  //there is rotation
1946  QgsPoint rotationPoint(( extent.xMaximum() + extent.xMinimum() ) / 2.0, ( extent.yMaximum() + extent.yMinimum() ) / 2.0 );
1947  double dx, dy; //x-, y- shift from rotation point to corner point
1948 
1949  //top left point
1950  dx = rotationPoint.x() - extent.xMinimum();
1951  dy = rotationPoint.y() - extent.yMaximum();
1952  QgsComposerUtils::rotate( mEvaluatedMapRotation, dx, dy );
1953  poly << QPointF( rotationPoint.x() - dx, rotationPoint.y() - dy );
1954 
1955  //top right point
1956  dx = rotationPoint.x() - extent.xMaximum();
1957  dy = rotationPoint.y() - extent.yMaximum();
1958  QgsComposerUtils::rotate( mEvaluatedMapRotation, dx, dy );
1959  poly << QPointF( rotationPoint.x() - dx, rotationPoint.y() - dy );
1960 
1961  //bottom right point
1962  dx = rotationPoint.x() - extent.xMaximum();
1963  dy = rotationPoint.y() - extent.yMinimum();
1964  QgsComposerUtils::rotate( mEvaluatedMapRotation, dx, dy );
1965  poly << QPointF( rotationPoint.x() - dx, rotationPoint.y() - dy );
1966 
1967  //bottom left point
1968  dx = rotationPoint.x() - extent.xMinimum();
1969  dy = rotationPoint.y() - extent.yMinimum();
1970  QgsComposerUtils::rotate( mEvaluatedMapRotation, dx, dy );
1971  poly << QPointF( rotationPoint.x() - dx, rotationPoint.y() - dy );
1972 
1973  //ensure polygon is closed by readding first point
1974  poly << QPointF( poly.at( 0 ) );
1975 }
1976 
1978 {
1979  QPolygonF poly;
1980  mapPolygon( *currentMapExtent(), poly );
1981  return poly;
1982 }
1983 
1985 {
1986  if ( !QgsComposerItem::id().isEmpty() )
1987  {
1988  return QgsComposerItem::id();
1989  }
1990 
1991  return tr( "Map %1" ).arg( mId );
1992 }
1993 
1995 {
1996  QgsRectangle newExtent = *currentMapExtent();
1997  if ( mEvaluatedMapRotation == 0 )
1998  {
1999  extent = newExtent;
2000  }
2001  else
2002  {
2003  QPolygonF poly;
2004  mapPolygon( newExtent, poly );
2005  QRectF bRect = poly.boundingRect();
2006  extent.setXMinimum( bRect.left() );
2007  extent.setXMaximum( bRect.right() );
2008  extent.setYMinimum( bRect.top() );
2009  extent.setYMaximum( bRect.bottom() );
2010  }
2011 }
2012 
2014 {
2015  double extentWidth = currentMapExtent()->width();
2016  if ( extentWidth <= 0 )
2017  {
2018  return 1;
2019  }
2020  return rect().width() / extentWidth;
2021 }
2022 
2024 {
2026  o->setFrameMap( mapId );
2027 }
2028 
2030 {
2031  const QgsComposerMapOverview* o = constFirstMapOverview();
2032  return o->frameMapId();
2033 }
2034 
2036 {
2037  //updates data defined properties and redraws item to match
2038  if ( property == QgsComposerObject::MapRotation || property == QgsComposerObject::MapScale ||
2039  property == QgsComposerObject::MapXMin || property == QgsComposerObject::MapYMin ||
2040  property == QgsComposerObject::MapXMax || property == QgsComposerObject::MapYMax ||
2041  property == QgsComposerObject::MapAtlasMargin ||
2042  property == QgsComposerObject::AllProperties )
2043  {
2044  QgsRectangle beforeExtent = *currentMapExtent();
2045  refreshMapExtents();
2046  emit itemChanged();
2047  if ( *currentMapExtent() != beforeExtent )
2048  {
2049  emit extentChanged();
2050  }
2051  }
2052 
2053  //force redraw
2054  mCacheUpdated = false;
2055 
2057 }
2058 
2060 {
2062  o->setFrameSymbol( symbol );
2063 }
2064 
2066 {
2068  return o->frameSymbol();
2069 }
2070 
2071 QPainter::CompositionMode QgsComposerMap::overviewBlendMode() const
2072 {
2073  const QgsComposerMapOverview* o = constFirstMapOverview();
2074  return o->blendMode();
2075 }
2076 
2077 void QgsComposerMap::setOverviewBlendMode( QPainter::CompositionMode blendMode )
2078 {
2080  o->setBlendMode( blendMode );
2081 }
2082 
2084 {
2085  const QgsComposerMapOverview* o = constFirstMapOverview();
2086  return o->inverted();
2087 }
2088 
2090 {
2092  o->setInverted( inverted );
2093 }
2094 
2096 {
2097  const QgsComposerMapOverview* o = constFirstMapOverview();
2098  return o->centered();
2099 }
2100 
2102 {
2104  o->setCentered( centered );
2105  //overviewExtentChanged();
2106 }
2107 
2109 {
2110  QgsComposerMapGrid* g = grid();
2111  g->setLineSymbol( symbol );
2112 }
2113 
2115 {
2116  QgsComposerMapGrid* g = grid();
2117  return g->lineSymbol();
2118 }
2119 
2121 {
2122  QgsComposerMapGrid* g = grid();
2123  g->setEnabled( enabled );
2124 }
2125 
2127 {
2128  const QgsComposerMapGrid* g = constFirstMapGrid();
2129  return g->enabled();
2130 }
2131 
2132 void QgsComposerMap::transformShift( double& xShift, double& yShift ) const
2133 {
2134  double mmToMapUnits = 1.0 / mapUnitsToMM();
2135  double dxScaled = xShift * mmToMapUnits;
2136  double dyScaled = - yShift * mmToMapUnits;
2137 
2138  QgsComposerUtils::rotate( mEvaluatedMapRotation, dxScaled, dyScaled );
2139 
2140  xShift = dxScaled;
2141  yShift = dyScaled;
2142 }
2143 
2144 QPointF QgsComposerMap::mapToItemCoords( const QPointF& mapCoords ) const
2145 {
2146  QPolygonF mapPoly = transformedMapPolygon();
2147  if ( mapPoly.size() < 1 )
2148  {
2149  return QPointF( 0, 0 );
2150  }
2151 
2152  QgsRectangle tExtent = transformedExtent();
2153  QgsPoint rotationPoint(( tExtent.xMaximum() + tExtent.xMinimum() ) / 2.0, ( tExtent.yMaximum() + tExtent.yMinimum() ) / 2.0 );
2154  double dx = mapCoords.x() - rotationPoint.x();
2155  double dy = mapCoords.y() - rotationPoint.y();
2156  QgsComposerUtils::rotate( -mEvaluatedMapRotation, dx, dy );
2157  QgsPoint backRotatedCoords( rotationPoint.x() + dx, rotationPoint.y() + dy );
2158 
2159  QgsRectangle unrotatedExtent = transformedExtent();
2160  double xItem = rect().width() * ( backRotatedCoords.x() - unrotatedExtent.xMinimum() ) / unrotatedExtent.width();
2161  double yItem = rect().height() * ( 1 - ( backRotatedCoords.y() - unrotatedExtent.yMinimum() ) / unrotatedExtent.height() );
2162  return QPointF( xItem, yItem );
2163 }
2164 
2166 {
2167 
2168 }
2169 
2170 void QgsComposerMap::drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle )
2171 {
2172  if ( !mMapCanvas || !mDrawCanvasItems )
2173  {
2174  return;
2175  }
2176 
2177  QList<QGraphicsItem*> itemList = mMapCanvas->items();
2178  if ( itemList.size() < 1 )
2179  {
2180  return;
2181  }
2182  QGraphicsItem* currentItem = 0;
2183 
2184  for ( int i = itemList.size() - 1; i >= 0; --i )
2185  {
2186  currentItem = itemList.at( i );
2187  //don't draw mapcanvasmap (has z value -10)
2188  if ( !currentItem || currentItem->data( 0 ).toString() != "AnnotationItem" )
2189  {
2190  continue;
2191  }
2192  drawCanvasItem( currentItem, painter, itemStyle );
2193  }
2194 }
2195 
2196 void QgsComposerMap::drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle )
2197 {
2198  if ( !item || !mMapCanvas || !item->isVisible() )
2199  {
2200  return;
2201  }
2202 
2203  painter->save();
2204  painter->setRenderHint( QPainter::Antialiasing );
2205 
2206  //determine scale factor according to graphics view dpi
2207  double scaleFactor = 1.0 / mMapCanvas->logicalDpiX() * 25.4;
2208 
2209  double itemX, itemY;
2210  QGraphicsItem* parent = item->parentItem();
2211  if ( !parent )
2212  {
2213  QPointF mapPos = composerMapPosForItem( item );
2214  itemX = mapPos.x();
2215  itemY = mapPos.y();
2216  }
2217  else //place item relative to the parent item
2218  {
2219  QPointF itemScenePos = item->scenePos();
2220  QPointF parentScenePos = parent->scenePos();
2221 
2222  QPointF mapPos = composerMapPosForItem( parent );
2223 
2224  itemX = mapPos.x() + ( itemScenePos.x() - parentScenePos.x() ) * scaleFactor;
2225  itemY = mapPos.y() + ( itemScenePos.y() - parentScenePos.y() ) * scaleFactor;
2226  }
2227  painter->translate( itemX, itemY );
2228 
2229  painter->scale( scaleFactor, scaleFactor );
2230 
2231  //a little trick to let the item know that the paint request comes from the composer
2232  item->setData( 1, "composer" );
2233  item->paint( painter, itemStyle, 0 );
2234  item->setData( 1, "" );
2235  painter->restore();
2236 }
2237 
2238 QPointF QgsComposerMap::composerMapPosForItem( const QGraphicsItem* item ) const
2239 {
2240  if ( !item || !mMapCanvas )
2241  {
2242  return QPointF( 0, 0 );
2243  }
2244 
2245  if ( currentMapExtent()->height() <= 0 || currentMapExtent()->width() <= 0 || mMapCanvas->width() <= 0 || mMapCanvas->height() <= 0 )
2246  {
2247  return QPointF( 0, 0 );
2248  }
2249 
2250  QRectF graphicsSceneRect = mMapCanvas->sceneRect();
2251  QPointF itemScenePos = item->scenePos();
2252  QgsRectangle mapRendererExtent = mComposition->mapSettings().visibleExtent();
2253 
2254  double mapX = itemScenePos.x() / graphicsSceneRect.width() * mapRendererExtent.width() + mapRendererExtent.xMinimum();
2255  double mapY = mapRendererExtent.yMaximum() - itemScenePos.y() / graphicsSceneRect.height() * mapRendererExtent.height();
2256  return mapToItemCoords( QPointF( mapX, mapY ) );
2257 }
2258 
2260 {
2261  if ( !mComposition )
2262  {
2263  return;
2264  }
2265 
2266  const QgsComposerMap* existingMap = mComposition->getComposerMapById( mId );
2267  if ( !existingMap )
2268  {
2269  return; //keep mId as it is still available
2270  }
2271 
2272  int maxId = -1;
2273  QList<const QgsComposerMap*> mapList = mComposition->composerMapItems();
2274  QList<const QgsComposerMap*>::const_iterator mapIt = mapList.constBegin();
2275  for ( ; mapIt != mapList.constEnd(); ++mapIt )
2276  {
2277  if (( *mapIt )->id() > maxId )
2278  {
2279  maxId = ( *mapIt )->id();
2280  }
2281  }
2282  mId = maxId + 1;
2283  updateToolTip();
2284 }
2285 
2286 bool QgsComposerMap::imageSizeConsideringRotation( double& width, double& height ) const
2287 {
2288  //kept for api compatibility with QGIS 2.0 - use mMapRotation
2290  return QgsComposerItem::imageSizeConsideringRotation( width, height, mEvaluatedMapRotation );
2292 }
2293 
2294 bool QgsComposerMap::cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const
2295 {
2296  //kept for api compatibility with QGIS 2.0 - use mMapRotation
2298  return QgsComposerItem::cornerPointOnRotatedAndScaledRect( x, y, width, height, mEvaluatedMapRotation );
2300 }
2301 
2302 void QgsComposerMap::sizeChangedByRotation( double& width, double& height )
2303 {
2304  //kept for api compatibility with QGIS 2.0 - use mMapRotation
2306  return QgsComposerItem::sizeChangedByRotation( width, height, mEvaluatedMapRotation );
2308 }
2309 
2311 {
2312  mAtlasDriven = enabled;
2313 
2314  if ( !enabled )
2315  {
2316  //if not enabling the atlas, we still need to refresh the map extents
2317  //so that data defined extents and scale are recalculated
2318  refreshMapExtents();
2319  }
2320 }
2321 
2323 {
2324  return mAtlasScalingMode == Fixed;
2325 }
2326 
2328 {
2329  // implicit : if set to false => auto scaling
2330  mAtlasScalingMode = fixed ? Fixed : Auto;
2331 }
2332 
2334 {
2335  if ( valueType == QgsComposerObject::EvaluatedValue )
2336  {
2337  //evaluate data defined atlas margin
2338 
2339  //start with user specified margin
2340  double margin = mAtlasMargin;
2341  QVariant exprVal;
2343  {
2344  bool ok;
2345  double ddMargin = exprVal.toDouble( &ok );
2346  QgsDebugMsg( QString( "exprVal Map Atlas Margin:%1" ).arg( ddMargin ) );
2347  if ( ok && !exprVal.isNull() )
2348  {
2349  //divide by 100 to convert to 0 -> 1.0 range
2350  margin = ddMargin / 100;
2351  }
2352  }
2353  return margin;
2354  }
2355  else
2356  {
2357  return mAtlasMargin;
2358  }
2359 }
2360 
Q_DECL_DEPRECATED void setGridFrameWidth(double w)
Set grid frame width.
void setMapUnits(QGis::UnitType mapUnits)
Set the map units.
void setStyle(const GridStyle style)
Sets the grid style, which controls how the grid is drawn over the map's contents.
void preparedForAtlas()
Is emitted when the map has been prepared for atlas rendering, just before actual rendering...
void addGrid(QgsComposerMapGrid *grid)
Adds a new map grid to the stack and takes ownership of the grid.
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
QColor frameFillColor1() const
Retrieves the first fill color for the grid frame.
void draw(QPainter *painter, const QgsRectangle &extent, const QSizeF &size, double dpi, double *forceWidthScale=0)
Draw to paint device.
Q_DECL_DEPRECATED bool imageSizeConsideringRotation(double &width, double &height, double rotation) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
double outlineWidth
Definition: qgssvgcache.cpp:78
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
sets destination coordinate reference system
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:48
Job implementation that renders everything sequentially using a custom painter.
void setAnnotationDirection(const AnnotationDirection direction, const BorderSide border)
Sets the direction for drawing frame annotations.
double intervalX() const
Gets the interval between grid lines in the x-direction.
void setBlendMode(const QPainter::CompositionMode mode)
Sets the blending mode used for drawing the grid.
Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection(QgsComposerMap::Border border) const
Q_DECL_DEPRECATED QgsLineSymbolV2 * gridLineSymbol()
double atlasMargin(const QgsComposerObject::PropertyValueType valueType=QgsComposerObject::EvaluatedValue)
Returns the margin size (percentage) used when the map is in atlas mode.
Q_DECL_DEPRECATED void setGridIntervalY(double interval)
Sets coordinate interval in y-direction for composergrid.
Q_DECL_DEPRECATED void setOverviewCentered(bool centered)
Set the overview's centering mode.
void setLineSymbol(QgsLineSymbolV2 *symbol)
Sets the line symbol used for drawing grid lines.
GridStyle
Grid drawing style.
bool containsWMSLayer() const
True if composer map renders a WMS layer.
QgsComposerMapGrid * grid(const QString &gridId) const
Returns a reference to a grid within the stack.
Q_DECL_DEPRECATED void setGridAnnotationFormat(GridAnnotationFormat f)
Q_DECL_DEPRECATED void setGridFrameFillColor1(const QColor &c)
Sets first fill color for grid zebra frame.
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes...
Q_DECL_DEPRECATED double gridIntervalX() const
void setOffsetY(const double offset)
Sets the offset for grid lines in the y-direction.
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:163
double mapUnitsToMM() const
Returns the conversion factor map units -> mm.
bool hideCoverage() const
Returns true if the atlas is set to hide the coverage layer.
Q_DECL_DEPRECATED void setGridEnabled(bool enabled)
Enables a coordinate grid that is shown on top of this composermap.
const QgsLineSymbolV2 * lineSymbol() const
Gets the line symbol used for drawing grid lines.
Q_DECL_DEPRECATED double gridFramePenSize() const
void setFramePenSize(const double width)
Sets the width of the outline drawn in the grid frame.
void assignFreeId()
Sets mId to a number not yet used in the composition.
int size() const
Returns the number of items in the stack.
void setNewAtlasFeatureExtent(const QgsRectangle &extent)
Sets new Extent for the current atlas preview and changes width, height (and implicitely also scale)...
void setOffset(double xOffset, double yOffset)
Sets offset values to shift image (useful for live updates when moving item content) ...
Q_DECL_DEPRECATED QgsMapRenderer * mapRenderer()
Returns pointer to map renderer of qgis map canvas.
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:188
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void setOutputDpi(int dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
void itemChanged()
Emitted when the item changes.
void requestedExtent(QgsRectangle &extent) const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
Q_DECL_DEPRECATED QColor gridFrameFillColor1() const
Get first fill color for grid zebra frame.
void addOverview(QgsComposerMapOverview *overview)
Adds a new map overview to the stack and takes ownership of the overview.
Q_DECL_DEPRECATED void connectMapOverviewSignals()
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
QPainter::CompositionMode bufferBlendMode
A collection of grids which is drawn above the map content in a QgsComposerMap.
FrameStyle
Style for grid frame.
QStringList layerSet() const
Getter for stored layer set that is used if mKeepLayerSet is true.
Q_DECL_DEPRECATED void setAnnotationFontColor(const QColor &c)
Sets font color for grid annotations.
void cache()
Create cache image.
Q_DECL_DEPRECATED bool showGridAnnotation() const
bool centered() const
Returns whether the extent of the map is forced to center on the overview.
Q_DECL_DEPRECATED void setGridStyle(GridStyle style)
Sets coordinate grid style to solid or cross.
Q_DECL_DEPRECATED bool imageSizeConsideringRotation(double &width, double &height) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
QList< const QgsComposerMap * > composerMapItems() const
Returns pointers to all composer maps in the scene.
QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames
Map of data defined properties for the item to string name to use when exporting item to xml...
static QgsMapLayerRegistry * instance()
Definition: qgssingleton.h:23
A item that forms part of a map composition.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:439
Border
Enum for different frame borders.
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
void updateItem()
Updates item, with the possibility to do custom update for subclasses.
double annotationFrameDistance() const
Gets the distance between the map frame and annotations.
void setDpi(double dpi)
Set the dpi to be used in scale calculations.
bool readXML(const QDomElement &elem, const QDomDocument &doc)
Sets the overview stack's state from a DOM document.
double mLastValidViewScaleFactor
Backup to restore item appearance if no view scale factor is available.
QColor frameFillColor2() const
Retrieves the second fill color for the grid frame.
void drawItems(QPainter *painter)
Draws the items from the stack on a specified painter.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
void mapRotationChanged(double newRotation)
Is emitted on rotation change to notify north arrow pictures.
static QgsPalLayerSettings fromLayer(QgsVectorLayer *layer)
FrameStyle frameStyle() const
Gets the grid frame style.
virtual QString displayName() const
Get item display name.
static QColor decodeColor(QString str)
virtual void drawFrame(QPainter *p)
Draw black frame around item.
void updateCachedImage()
Called if map canvas has changed.
void setAnnotationFont(const QFont &font)
Sets the font used for drawing grid annotations.
Flags flags() const
Return combination of flags used for rendering.
DataDefinedProperty
Data defined properties for different item types.
Q_DECL_DEPRECATED double annotationFrameDistance() const
QColor framePenColor() const
Retrieves the color of the outline drawn in the grid frame.
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height) const
Calculates corner point after rotation and scaling.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
QPolygonF transformedMapPolygon() const
Returns extent that considers rotation and shift with mOffsetX / mOffsetY.
QColor backgroundColor() const
Gets the background color for this item.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
A non GUI class for rendering a map layer set onto a QPainter.
AnnotationDirection
Direction of grid annotations.
void setLayers(const QStringList &layers)
Set list of layer IDs for map rendering.
Enable layer transparency and blending effects.
void setGridLineColor(const QColor &color)
Sets color of grid lines.
Q_DECL_DEPRECATED double gridOffsetX() const
void setSceneRect(const QRectF &rectangle)
Sets new scene rectangle bounds and recalculates hight and extent.
bool containsAdvancedEffects() const
True if composer map contains layers with blend modes or flattened layers for vectors.
AnnotationFormat annotationFormat() const
Gets the format for drawing grid annotations.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties)
QMap< QString, QString > QgsStringMap
Definition: qgis.h:416
void setAnnotationDisplay(const DisplayMode display, const BorderSide border)
Sets what types of grid annotations should be drawn for a specified side of the map frame...
Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const
AnnotationFormat
Format for displaying grid annotations.
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=0) const
QRectF boundingRect() const
In case of annotations, the bounding rectangle can be larger than the map item rectangle.
double x() const
Definition: qgspoint.h:126
Q_DECL_DEPRECATED void zoomContent(int delta, double x, double y)
Zoom content of map.
AnnotationPosition annotationPosition(const BorderSide border) const
Gets the position for the grid annotations on a specified side of the map frame.
Q_DECL_DEPRECATED void setGridPenColor(const QColor &c)
Sets the color of the grid pen.
virtual QString name() const =0
return a provider name
void setWidth(double width)
double intervalY() const
Gets the interval between grid lines in the y-direction.
void setCrossLength(const double length)
Sets the length of the cross segments drawn for the grid.
QFont annotationFont() const
Gets the font used for drawing grid annotations.
bool dataDefinedEvaluate(const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue)
Evaluate a data defined property and return the calculated value.
Vector graphics should not be cached and drawn as raster images.
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
Q_DECL_DEPRECATED void setGridAnnotationPrecision(int p)
Sets coordinate precision for grid annotations.
Q_DECL_DEPRECATED QFont gridAnnotationFont() const
The QgsMapSettings class contains configuration for rendering of the map.
Q_DECL_DEPRECATED bool overviewInverted() const
Returns true if the overview frame is inverted.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
static bool staticWillUseLayer(QgsVectorLayer *layer)
called to find out whether the layer is used for labeling
void zoomToExtent(const QgsRectangle &extent)
Zooms the map so that the specified extent is fully visible within the map item.
Q_DECL_DEPRECATED const QgsMapRenderer * mapRenderer() const
Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const
Returns the grid's blending mode.
void storeCurrentLayerSet()
Stores the current layer set of the qgis mapcanvas in mLayerSet.
void setColor(const QColor &color)
Q_DECL_DEPRECATED QColor gridFrameFillColor2() const
Get second fill color for grid zebra frame.
Q_DECL_DEPRECATED int overviewFrameMapId() const
Returns id of overview frame (or -1 if no overfiew frame)
double scale() const
Scale.
Q_DECL_DEPRECATED double gridFrameWidth() const
QPainter::CompositionMode blendMode() const
Read blend mode for layer.
double horizontalViewScaleFactor() const
Returns the zoom factor of the graphics view.
An individual overview which is drawn above the map content in a QgsComposerMap, and shows the extent...
void setFrameSymbol(QgsFillSymbolV2 *symbol)
Sets the fill symbol used for drawing the overview extent.
QPainter::CompositionMode featureBlendMode() const
Read blend mode for layer.
double calculate(const QgsRectangle &mapExtent, int canvasWidth)
Calculate the scale denominator.
QgsFillSymbolV2 * frameSymbol()
Gets the fill symbol used for drawing the overview extent.
void updateBoundingRect()
Updates the bounding rect of this item.
Q_DECL_DEPRECATED void setGridAnnotationFont(const QFont &f)
Sets font for grid annotations.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:193
QPainter::CompositionMode blendMode() const
Retrieves the blending mode used for drawing the overview.
Q_DECL_DEPRECATED QColor annotationFontColor() const
Get font color for grid annotations.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:178
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
static QgsLineSymbolV2 * createSimple(const QgsStringMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties. ...
QPainter::CompositionMode blendMode
friend class QgsComposerMapOverview
void setNewScale(double scaleDenominator, bool forceUpdate=true)
Sets new scale and changes only mExtent.
bool mFrame
True if item fram needs to be painted.
Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const
Returns the overview's blending mode.
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom node
Whether vector selections should be shown in the rendered map.
void setMapUnits(QGis::UnitType u)
Set units of map's geographical coordinates - used for scale calculation.
const QgsComposition * composition() const
Returns the composition the item is attached to.
bool drawCanvasItems() const
void setCacheUpdated(bool u=false)
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:168
virtual void setEnabled(const bool enabled)
Controls whether the item will be drawn.
Q_DECL_DEPRECATED void setAnnotationFrameDistance(double d)
Sets distance between map frame and annotations.
void setAnnotationFormat(const AnnotationFormat format)
Sets the format for drawing grid annotations.
AnnotationPosition
Position for grid annotations.
double offsetX() const
Gets the offset for grid lines in the x-direction.
void setCentered(const bool centered)
Sets whether the extent of the map is forced to center on the overview.
void setAnnotationEnabled(const bool enabled)
Sets whether annotations should be shown for the grid.
void setNewExtent(const QgsRectangle &extent)
Sets new extent for the map.
Q_DECL_DEPRECATED void setGridPen(const QPen &p)
Sets the pen to draw composer grid.
Q_DECL_DEPRECATED void setGridFrameFillColor2(const QColor &c)
Sets second fill color for grid zebra frame.
Q_DECL_DEPRECATED void setGridIntervalX(double interval)
Sets coordinate interval in x-direction for composergrid.
void moveContent(double dx, double dy)
Move content of map.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
GridStyle style() const
Gets the grid's style, which controls how the grid is drawn over the map's contents.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
Q_DECL_DEPRECATED bool atlasFixedScale() const
Returns true if the map uses a fixed scale when in atlas mode.
QGis::UnitType mapUnits() const
Get units of map's geographical coordinates - used for scale calculation.
An individual grid which is drawn above the map content in a QgsComposerMap.
QPainter::CompositionMode shapeBlendMode
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setOutputImageFormat(QImage::Format format)
sets format of internal QImage
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle.
Q_DECL_DEPRECATED void setOverviewFrameMapSymbol(QgsFillSymbolV2 *symbol)
double offsetY() const
Gets the offset for grid lines in the y-direction.
Q_DECL_DEPRECATED void setGridFramePenSize(double w)
Set grid frame pen thickness.
void setGridLineWidth(const double width)
Sets width of grid lines.
double mapRotation(QgsComposerObject::PropertyValueType valueType=QgsComposerObject::EvaluatedValue) const
Returns the rotation used for drawing the map within the composer item.
double framePenSize() const
Retrieves the width of the outline drawn in the grid frame.
bool readXML(const QDomElement &elem, const QDomDocument &doc)
Sets the grid stack's state from a DOM document.
PreviewMode
Preview style.
Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition(QgsComposerMap::Border border) const
QPolygonF visibleExtentPolygon() const
Returns a polygon representing the current visible map extent, considering map extents and rotation...
A class to represent a point.
Definition: qgspoint.h:63
void setFrameWidth(const double width)
Sets the grid frame width.
void setAnnotationFontColor(const QColor &color)
Sets the font color used for drawing grid annotations.
Graphics scene for map printing.
Q_DECL_DEPRECATED QColor gridFramePenColor() const
Get pen color for grid frame.
This class tracks map layers that are currently loaded and provides a means to fetch a pointer to a m...
Object representing map window.
BlendMode
Blending modes enum defining the available composition modes that can be used when rendering a layer...
Enable drawing of vertex markers for layers in editing mode.
static void rotate(const double angle, double &x, double &y)
Rotates a point / vector around the origin.
Q_DECL_DEPRECATED QPen gridPen() const
QgsRectangle * currentMapExtent()
Returns a pointer to the current map extent, which is either the original user specified extent or th...
void renderModeUpdateCachedImage()
Call updateCachedImage if item is in render mode.
BorderSide
Border sides for annotations.
QString qgsDoubleToString(const double &a, const int &precision=17)
Definition: qgis.h:317
PreviewMode previewMode() const
void setFrameFillColor1(const QColor &color)
Sets the first fill color used for the grid frame.
void setFramePenColor(const QColor &color)
Sets the color of the outline drawn in the grid frame.
void setAnnotationPosition(const AnnotationPosition position, const BorderSide border)
Sets the position for the grid annotations on a specified side of the map frame.
virtual ~QgsComposerMap()
Q_DECL_DEPRECATED int gridAnnotationPrecision() const
Q_DECL_DEPRECATED void setGridAnnotationDirection(GridAnnotationDirection d, QgsComposerMap::Border border)
Calculates scale for a given combination of canvas size, map extent, and monitor dpi.
Q_DECL_DEPRECATED void setCrossLength(double l)
Sets length of the cross segments (if grid style is cross)
int layerTransparency() const
Read transparency for layer.
Q_DECL_DEPRECATED bool gridEnabled() const
virtual QgsSymbolV2 * clone() const
static QPainter::CompositionMode getCompositionMode(const QgsMapRenderer::BlendMode &blendMode)
Returns a QPainter::CompositionMode corresponding to a BlendMode.
int annotationPrecision() const
Returns the coordinate precision for grid annotations.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:440
bool useAdvancedEffects() const
Returns true if a composition should use advanced effects such as blend modes.
QPainter::CompositionMode blendMode() const
Retrieves the blending mode used for drawing the grid.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint - draw on canvas.
QgsComposition * mComposition
virtual bool enabled() const
Returns whether the item will be drawn.
void setBackgroundColor(const QColor &color)
Set the background color of the map.
QColor annotationFontColor() const
Gets the font color used for drawing grid annotations.
QgsComposerMapOverview * overview(const QString &overviewId) const
Returns a reference to an overview within the stack.
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
void setBackgroundColor(const QColor &backgroundColor)
Sets the background color for this item.
Q_DECL_DEPRECATED void setOverviewInverted(bool inverted)
Sets the overview's inversion mode.
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
int mCurrentExportLayer
The layer that needs to be exported.
QgsRectangle extent() const
void setOutputSize(const QSize &size)
Set the size of the resulting map image.
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:173
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores the state of the item stack in a DOM node.
virtual void drawBackground(QPainter *p)
Draw background.
bool hasFrame() const
Whether this item has a frame or not.
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
Q_DECL_DEPRECATED void setShowGridAnnotation(bool show)
Sets flag if grid annotation should be shown.
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:362
void setExtent(const QgsRectangle &rect)
Set coordinates of the rectangle which should be rendered.
void setMapRotation(double r)
Sets rotation for the map - this does not affect the composer item shape, only the way the map is dra...
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void setAnnotationPrecision(const int precision)
Sets the coordinate precision for grid annotations.
Q_DECL_DEPRECATED void setGridAnnotationPosition(GridAnnotationPosition p, QgsComposerMap::Border border)
Q_DECL_DEPRECATED double gridIntervalY() const
bool hasBackground() const
Whether this item has a Background or not.
void setIntervalY(const double interval)
Sets the interval between grid lines in the y-direction.
const QMap< QString, QgsMapLayer * > & mapLayers()
Retrieve the mapLayers collection (mainly intended for use by projection)
bool annotationEnabled() const
Gets whether annotations are shown for the grid.
QgsComposerMap(QgsComposition *composition, int x, int y, int width, int height)
Constructor.
void setInverted(const bool inverted)
Sets whether the overview frame is inverted, ie, whether the shaded area is drawn outside the extent ...
Q_DECL_DEPRECATED void setAtlasFixedScale(bool fixed)
Set to true if the map should use a fixed scale when in atlas mode.
void setFrameMap(const int mapId)
Sets overview frame map.
void resize(double dx, double dy)
resizes an item in x- and y direction (canvas coordinates)
double y() const
Definition: qgspoint.h:134
double crossLength() const
Retrieves the length of the cross segments drawn for the grid.
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
QgsAtlasComposition & atlasComposition()
Q_DECL_DEPRECATED double crossLength()
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
Q_DECL_DEPRECATED double gridOffsetY() const
Q_DECL_DEPRECATED void setGridOffsetX(double offset)
Sets x-coordinate offset for composer grid.
Enable vector simplification and other rendering optimizations.
QPainter::CompositionMode shadowBlendMode
static QgsSymbolV2 * loadSymbol(QDomElement &element)
QgsRasterDataProvider * dataProvider()
Returns the data provider.
Q_DECL_DEPRECATED QgsFillSymbolV2 * overviewFrameMapSymbol()
Q_DECL_DEPRECATED void setOverviewBlendMode(QPainter::CompositionMode blendMode)
Sets the overview's blending mode.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
Q_DECL_DEPRECATED void setGridLineSymbol(QgsLineSymbolV2 *symbol)
void extentChanged()
Q_DECL_DEPRECATED void setGridPenWidth(double w)
Sets width of grid pen.
static void setSpecialColumn(const QString &name, QVariant value)
Assign a special column.
void setAtlasDriven(bool enabled)
Sets whether the map extent will follow the current atlas feature.
double maxGridExtension() const
Calculates the maximum distance grids within the stack extend beyond the QgsComposerMap's item rect...
void setFrameFillColor2(const QColor &color)
Sets the second fill color used for the grid frame.
void setFrameStyle(const FrameStyle style)
Sets the grid frame style.
Q_DECL_DEPRECATED void setGridOffsetY(double offset)
Sets y-coordinate offset for composer grid.
QPointF mapToItemCoords(const QPointF &mapCoords) const
Transforms map coordinates to item coordinates (considering rotation and move offset) ...
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
void setPreviewMode(PreviewMode m)
QgsComposition::PlotStyle plotStyle() const
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:198
AnnotationDirection annotationDirection(const BorderSide border) const
Gets the direction for drawing frame annotations.
Represents a vector layer which manages a vector based data sets.
double size
Definition: qgssvgcache.cpp:77
Q_DECL_DEPRECATED void setGridFrameStyle(GridFrameStyle style)
Set grid frame style (NoGridFrame or Zebra)
void setFlags(Flags flags)
Set combination of flags that will be used for rendering.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:183
ZoomMode
Modes for zooming item content.
Q_DECL_DEPRECATED void setRotation(double r)
Sets rotation for the map - this does not affect the composer item shape, only the way the map is dra...
Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const
void renderSynchronously()
Render the map synchronously in this thread.
void setIntervalX(const double interval)
Sets the interval between grid lines in the x-direction.
int frameMapId() const
Returns id of source map.
void setOffsetX(const double offset)
Sets the offset for grid lines in the x-direction.
int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
Q_DECL_DEPRECATED void setGridBlendMode(QPainter::CompositionMode blendMode)
Sets the grid's blending mode.
Q_DECL_DEPRECATED void setOverviewFrameMap(int mapId)
Sets overview frame map.
void setBlendMode(const QPainter::CompositionMode blendMode)
Sets the blending mode used for drawing the overview.
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:158
QgsComposerMapGrid * grid()
Returns the map item's first grid.
Q_DECL_DEPRECATED void setGridFramePenColor(const QColor &c)
Sets pen color for grid frame.
void setAnnotationFrameDistance(const double distance)
Sets the distance between the map frame and annotations.
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.
double frameWidth() const
Gets the grid frame width.
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:203
void setCrsTransformEnabled(bool enabled)
sets whether to use projections for this layer set
Q_DECL_DEPRECATED GridStyle gridStyle() const
Base class for raster data providers.
QColor color() const
QgsMapSettings mapSettings(const QgsRectangle &extent, const QSizeF &size, int dpi) const
Return map settings that would be used for drawing of the map.
bool inverted() const
Returns whether the overview frame is inverted, ie, whether the shaded area is drawn outside the exte...
QgsComposerMapOverview * overview()
Returns the map item's first overview.
#define tr(sourceText)
void scale(double scaleFactor, const QgsPoint *c=0)
Scale the rectangle around its center point.
Q_DECL_DEPRECATED bool overviewCentered() const
Returns true if the extent is forced to center on the overview.
A collection of overviews which are drawn above the map content in a QgsComposerMap.
QString id() const
Get item's id (which is not necessarly unique)