QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscomposition.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposition.h
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSCOMPOSITION_H
17 #define QGSCOMPOSITION_H
18 
19 #include "qgscomposeritem.h"
20 #include <memory>
21 
22 #include <QDomDocument>
23 #include <QGraphicsScene>
24 #include <QLinkedList>
25 #include <QList>
26 #include <QPair>
27 #include <QSet>
28 #include <QUndoStack>
29 #include <QPrinter>
30 #include <QPainter>
31 
33 #include "qgscomposeritemcommand.h"
34 #include "qgsatlascomposition.h"
35 #include "qgspaperitem.h"
36 
37 class QgisApp;
38 class QgsComposerFrame;
39 class QgsComposerMap;
40 class QGraphicsRectItem;
41 class QgsMapRenderer;
42 class QDomElement;
43 class QgsComposerArrow;
45 class QgsComposerHtml;
46 class QgsComposerItem;
47 class QgsComposerLabel;
48 class QgsComposerLegend;
49 class QgsComposerMap;
50 class QgsComposerPicture;
52 class QgsComposerShape;
56 class QgsVectorLayer;
57 class QgsComposer;
58 class QgsFillSymbolV2;
59 
66 class CORE_EXPORT QgsComposition : public QGraphicsScene
67 {
68  Q_OBJECT
69  public:
70 
72  enum PlotStyle
73  {
74  Preview = 0, // Use cache etc
75  Print, // Render well
76  Postscript // Fonts need different scaling!
77  };
78 
80  enum GridStyle
81  {
84  Crosses
85  };
86 
88  {
90  ZValueAbove
91  };
92 
94  Q_DECL_DEPRECATED QgsComposition( QgsMapRenderer* mapRenderer );
95  explicit QgsComposition( const QgsMapSettings& mapSettings );
96 
98  enum AtlasMode
99  {
100  AtlasOff, // Composition is not being controlled by an atlas
101  PreviewAtlas, // An atlas composition is being previewed in the app
102  ExportAtlas // The composition is being exported as an atlas
103  };
104 
105  ~QgsComposition();
106 
110  void setPaperSize( double width, double height );
111 
113  double paperHeight() const;
114 
116  double paperWidth() const;
117 
118  double spaceBetweenPages() const { return mSpaceBetweenPages; }
119 
121  void setNumPages( int pages );
123  int numPages() const;
124 
126  void setPageStyleSymbol( QgsFillSymbolV2* symbol );
128  QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }
129 
133  QPointF positionOnPage( const QPointF & position ) const;
134 
138  int pageNumberForPoint( const QPointF & position ) const;
139 
143  void setStatusMessage( const QString & message );
144 
147  void updateSettings();
148 
149  void setSnapToGridEnabled( bool b );
150  bool snapToGridEnabled() const {return mSnapToGrid;}
151 
152  void setGridVisible( bool b );
153  bool gridVisible() const {return mGridVisible;}
154 
156  void setSnapLinesVisible( bool visible );
157  bool snapLinesVisible() const {return mGuidesVisible;}
158 
159  void setAlignmentSnap( bool s ) { mAlignmentSnap = s; }
160  bool alignmentSnap() const { return mAlignmentSnap; }
161 
162  void setSmartGuidesEnabled( bool b ) { mSmartGuides = b; }
163  bool smartGuidesEnabled() const {return mSmartGuides;}
164 
166  void clearSnapLines();
167 
168  void setSnapGridResolution( double r );
169  double snapGridResolution() const {return mSnapGridResolution;}
170 
171  void setSnapGridTolerance( double tolerance );
172  double snapGridTolerance() const {return mSnapGridTolerance;}
173 
174  void setSnapGridOffsetX( double offset );
175  double snapGridOffsetX() const {return mSnapGridOffsetX;}
176 
177  void setSnapGridOffsetY( double offset );
178  double snapGridOffsetY() const {return mSnapGridOffsetY;}
179 
180  void setGridPen( const QPen& p );
181  const QPen& gridPen() const {return mGridPen;}
182 
183  void setGridStyle( GridStyle s );
184  GridStyle gridStyle() const {return mGridStyle;}
185 
186  void setAlignmentSnapTolerance( double t ) { mAlignmentSnapTolerance = t; }
187  double alignmentSnapTolerance() const { return mAlignmentSnapTolerance; }
188 
190  QUndoStack* undoStack() { return &mUndoStack; }
191 
193  QgsComposerItem* composerItemAt( const QPointF & position );
194 
198  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem );
199 
201  int pageNumberAt( const QPointF& position ) const;
202 
204  int itemPageNumber( const QgsComposerItem* ) const;
205 
206  QList<QgsComposerItem*> selectedComposerItems();
207 
211  QList<const QgsComposerMap*> composerMapItems() const;
212 
216  template<class T> void composerItems( QList<T*>& itemList );
217 
220  const QgsComposerMap* getComposerMapById( int id ) const;
221 
228  const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
229 
237  const QgsComposerItem* getComposerItemById( QString theId ) const;
238 
243  const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;
244 
245  int printResolution() const {return mPrintResolution;}
246  void setPrintResolution( int dpi );
247 
248  bool printAsRaster() const {return mPrintAsRaster;}
249  void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
250 
251  bool generateWorldFile() const { return mGenerateWorldFile; }
252  void setGenerateWorldFile( bool enabled ) { mGenerateWorldFile = enabled; }
253 
254  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
255  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
256 
259  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
262  void setUseAdvancedEffects( bool effectsEnabled );
263 
265  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer() {return mMapRenderer;}
267 
270  const QgsMapSettings& mapSettings() const { return mMapSettings; }
271 
272  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
273  void setPlotStyle( QgsComposition::PlotStyle style ) {mPlotStyle = style;}
274 
278  int pixelFontSize( double pointSize ) const;
279 
281  double pointFontSize( int pixelSize ) const;
282 
284  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
285 
287  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
288 
294  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0, bool addUndoCommands = false );
295 
305  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
306  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
307 
309  void addItemToZList( QgsComposerItem* item );
311  void removeItemFromZList( QgsComposerItem* item );
312 
313  //functions to move selected items in hierarchy
314  void raiseSelectedItems();
315  void raiseItem( QgsComposerItem* item );
316  void lowerSelectedItems();
317  void lowerItem( QgsComposerItem* item );
318  void moveSelectedItemsToTop();
319  void moveItemToTop( QgsComposerItem* item );
320  void moveSelectedItemsToBottom();
321  void moveItemToBottom( QgsComposerItem* item );
322 
323  //functions to find items by their position in the z list
324  void selectNextByZOrder( ZValueDirection direction );
325  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item );
326  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item );
327 
328  //functions to align selected items
329  void alignSelectedItemsLeft();
330  void alignSelectedItemsHCenter();
331  void alignSelectedItemsRight();
332  void alignSelectedItemsTop();
333  void alignSelectedItemsVCenter();
334  void alignSelectedItemsBottom();
335 
336  //functions to lock and unlock items
338  void lockSelectedItems();
340  void unlockAllItems();
341 
344  void sortZList();
345 
347  void refreshZList();
348 
350  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
351 
353  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
354 
358  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
359 
361  QGraphicsLineItem* addSnapLine();
363  void removeSnapLine( QGraphicsLineItem* line );
367  QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
368 
373  void beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
374 
376  void endCommand();
378  void cancelCommand();
379 
380  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
381  void endMultiFrameCommand();
382 
384  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
386  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
389  void addComposerArrow( QgsComposerArrow* arrow );
391  void addComposerLabel( QgsComposerLabel* label );
393  void addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle = true );
395  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
397  void addComposerLegend( QgsComposerLegend* legend );
399  void addComposerPicture( QgsComposerPicture* picture );
401  void addComposerShape( QgsComposerShape* shape );
403  void addComposerTable( QgsComposerAttributeTable* table );
405  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
406 
408  void removeComposerItem( QgsComposerItem* item, bool createCommand = true );
409 
411  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
412 
415  void setPreventCursorChange( bool preventChange ) { mPreventCursorChange = preventChange; }
416  bool preventCursorChange() { return mPreventCursorChange; }
417 
418  //printing
419 
421  void beginPrint( QPrinter& printer );
423  void beginPrintAsPDF( QPrinter& printer, const QString& file );
425  void doPrint( QPrinter& printer, QPainter& painter );
426 
430  bool print( QPrinter &printer );
431 
435  bool exportAsPDF( const QString& file );
436 
439  QImage printPageAsRaster( int page );
440 
443  void renderPage( QPainter* p, int page );
444 
446  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
447 
448  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
449 
451  static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
453  static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
454 
456  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
458  bool setAtlasMode( QgsComposition::AtlasMode mode );
459 
463  QList< QgsPaperItem* > pages() { return mPages; }
464 
465  public slots:
467  void sendItemAddedSignal( QgsComposerItem* item );
468 
471  void updateBounds();
472 
477  void refreshItems();
478 
482  void setSelectedItem( QgsComposerItem* item );
483 
484  protected:
485  void init();
486 
487 
488  private:
492 
494  double mPageWidth;
495  double mPageHeight;
496  QList< QgsPaperItem* > mPages;
497  double mSpaceBetweenPages; //space in preview between pages
498 
501  void createDefaultPageStyleSymbol();
502 
504  QLinkedList<QgsComposerItem*> mItemZList;
505 
507  QSet<QgsComposerMultiFrame*> mMultiFrames;
508 
511 
514 
519 
522 
530  QPen mGridPen;
532 
538 
540  QList< QGraphicsLineItem* > mSnapLines;
541 
543 
544  QUndoStack mUndoStack;
545 
548 
551 
553 
554  QgsComposition(); //default constructor is forbidden
555 
557  QRectF compositionBounds() const;
558 
560  void updateZValues( bool addUndoCommands = true );
561 
564  int boundingRectOfSelectedItems( QRectF& bRect );
565 
567  void loadDefaults();
568 
570  void loadSettings();
571 
573  QPointF minPointFromXml( const QDomElement& elem ) const;
574 
575  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
576 
577  void updatePaperItems();
578  void addPaperItem();
579  void removePaperItems();
580  void deleteAndRemoveMultiFrames();
581 
582  static QString encodeStringForXML( const QString& str );
583 
585 
586  signals:
587  void paperSizeChanged();
588  void nPagesChanged();
589 
591  void printResolutionChanged();
592 
594  void selectedItemChanged( QgsComposerItem* selected );
596  void composerArrowAdded( QgsComposerArrow* arrow );
598  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
600  void composerLabelAdded( QgsComposerLabel* label );
602  void composerMapAdded( QgsComposerMap* map );
604  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
606  void composerLegendAdded( QgsComposerLegend* legend );
608  void composerPictureAdded( QgsComposerPicture* picture );
610  void composerShapeAdded( QgsComposerShape* shape );
612  void composerTableAdded( QgsComposerAttributeTable* table );
614  void itemRemoved( QgsComposerItem* );
615 
617  void refreshItemsTriggered();
618 
620  void statusMsgChanged( QString message );
621 };
622 
623 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
624 {
625  itemList.clear();
626  QList<QGraphicsItem *> graphicsItemList = items();
627  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
628  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
629  {
630  T* item = dynamic_cast<T*>( *itemIt );
631  if ( item )
632  {
633  itemList.push_back( item );
634  }
635  }
636 }
637 
638 #endif
639 
640 
641 
QgsComposition::PlotStyle mPlotStyle
A scale bar item that can be added to a map composition.
QUndoStack * undoStack()
Returns pointer to undo/redo command storage.
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
double alignmentSnapTolerance() const
bool snapLinesVisible() const
QgsFillSymbolV2 * pageStyleSymbol()
Note: added in version 2.1.
double mAlignmentSnapTolerance
An item that draws an arrow between to points.
double mSnapGridTolerance
void setGenerateWorldFile(bool enabled)
GridStyle
Style to draw the snapping grid.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
const QPen & gridPen() const
QgsComposerMouseHandles * selectionHandles()
Returns pointer to selection handles.
bool smartGuidesEnabled() const
A item that forms part of a map composition.
double snapGridTolerance() const
bool preventCursorChange()
bool mPrintAsRaster
Flag if map should be printed as a raster (via QImage).
GridStyle mGridStyle
QList< QgsPaperItem * > mPages
A non GUI class for rendering a map layer set onto a QPainter.
double spaceBetweenPages() const
bool alignmentSnap() const
A composer class that displays svg files or raster format (jpg, png, ...)
The QgsMapSettings class contains configuration for rendering of the map.
bool mGenerateWorldFile
Flag if a world file should be generated on raster export.
int printResolution() const
void setPrintAsRaster(bool enabled)
QSet< QgsComposerMultiFrame * > mMultiFrames
List multiframe objects.
QLinkedList< QgsComposerItem * > mItemZList
Maintains z-Order of items.
QgsMapRenderer * mMapRenderer
Pointer to map renderer of QGIS main map.
Abstract base class for composer entries with the ability to distribute the content to several frames...
double mSnapGridOffsetX
QgsComposerItemCommand * mActiveItemCommand
int mPrintResolution
Dpi for printout.
bool printAsRaster() const
void setWorldFileMap(QgsComposerMap *map)
QgsComposerMap * worldFileMap() const
Graphics scene for map printing.
bool snapToGridEnabled() const
Object representing map window.
Frame for html, table, text which can be divided onto several frames.
QgsComposerMouseHandles * mSelectionHandles
GridStyle gridStyle() const
void setAlignmentSnap(bool s)
double mSpaceBetweenPages
QString file
Definition: qgssvgcache.cpp:76
bool mSnapToGrid
Parameters for snap to grid function.
void setPreventCursorChange(bool preventChange)
If true, prevents any mouse cursor changes by the composition or by any composer items Used by QgsCom...
QList< QgsPaperItem * > pages()
Return pages in the correct order.
bool useAdvancedEffects() const
Returns true if a composition should use advanced effects such as blend modes.
QgsComposition::AtlasMode mAtlasMode
A composer command class for adding / removing composer items.
double snapGridOffsetY() const
double snapGridOffsetX() const
A table class that displays a vector attribute table.
bool mAlignmentSnap
Parameters for alignment snap.
const QgsMapSettings & mMapSettings
Undo command to undo/redo all composer item related changes.
A composer items that draws common shapes (ellipse, triangle, rectangle)
void setSmartGuidesEnabled(bool b)
double mSnapGridOffsetY
QgsComposerMap * mWorldFileMap
Composer map to use for the world file generation.
double snapGridResolution() const
AtlasMode
Composition atlas modes.
QList< QGraphicsLineItem * > mSnapLines
Arbitraty snap lines (horizontal and vertical)
void setAlignmentSnapTolerance(double t)
QgsComposerMultiFrameCommand * mActiveMultiFrameCommand
QgsAtlasComposition mAtlasComposition
The atlas composition object.
QUndoStack mUndoStack
A label that can be placed onto a map composition.
Class used to render an Atlas, iterating over geometry features.
bool gridVisible() const
QgsAtlasComposition & atlasComposition()
void setPlotStyle(QgsComposition::PlotStyle style)
Handles drawing of selection outlines and mouse handles.
void composerItems(QList< T * > &itemList)
Return composer items of a specific type.
PlotStyle
Plot type.
bool generateWorldFile() const
QgsComposition::PlotStyle plotStyle() const
bool mUseAdvancedEffects
Flag if advanced visual effects such as blend modes should be used.
Represents a vector layer which manages a vector based data sets.
double mSnapGridResolution
A legend that can be placed onto a map composition.
QgsFillSymbolV2 * mPageStyleSymbol
Drawing style for page.
QList< QGraphicsLineItem * > * snapLines()
Returns pointer to snap lines collection.