QGIS API Documentation  2.3.0-Master
 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 : blazek@itc.it
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 
36 class QgisApp;
37 class QgsComposerFrame;
38 class QgsComposerMap;
39 class QgsPaperItem;
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 
108  void setPaperSize( double width, double height );
109 
111  double paperHeight() const;
112 
114  double paperWidth() const;
115 
116  double spaceBetweenPages() const { return mSpaceBetweenPages; }
117 
119  void setNumPages( int pages );
121  int numPages() const;
122 
124  void setPageStyleSymbol( QgsFillSymbolV2* symbol );
126  QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }
127 
131  QPointF positionOnPage( const QPointF & position ) const;
132 
136  int pageNumberForPoint( const QPointF & position ) const;
137 
141  void setStatusMessage( const QString & message );
142 
145  void updateSettings();
146 
147  void setSnapToGridEnabled( bool b );
148  bool snapToGridEnabled() const {return mSnapToGrid;}
149 
150  void setGridVisible( bool b );
151  bool gridVisible() const {return mGridVisible;}
152 
154  void setSnapLinesVisible( bool visible );
155  bool snapLinesVisible() const {return mGuidesVisible;}
156 
157  void setAlignmentSnap( bool s ) { mAlignmentSnap = s; }
158  bool alignmentSnap() const { return mAlignmentSnap; }
159 
160  void setSmartGuidesEnabled( bool b ) { mSmartGuides = b; }
161  bool smartGuidesEnabled() const {return mSmartGuides;}
162 
164  void clearSnapLines();
165 
166  void setSnapGridResolution( double r );
167  double snapGridResolution() const {return mSnapGridResolution;}
168 
169  void setSnapGridTolerance( double tolerance );
170  double snapGridTolerance() const {return mSnapGridTolerance;}
171 
172  void setSnapGridOffsetX( double offset );
173  double snapGridOffsetX() const {return mSnapGridOffsetX;}
174 
175  void setSnapGridOffsetY( double offset );
176  double snapGridOffsetY() const {return mSnapGridOffsetY;}
177 
178  void setGridPen( const QPen& p );
179  const QPen& gridPen() const {return mGridPen;}
180 
181  void setGridStyle( GridStyle s );
182  GridStyle gridStyle() const {return mGridStyle;}
183 
184  void setAlignmentSnapTolerance( double t ) { mAlignmentSnapTolerance = t; }
185  double alignmentSnapTolerance() const { return mAlignmentSnapTolerance; }
186 
188  QUndoStack* undoStack() { return &mUndoStack; }
189 
191  QgsComposerItem* composerItemAt( const QPointF & position );
192 
196  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem );
197 
199  int pageNumberAt( const QPointF& position ) const;
200 
202  int itemPageNumber( const QgsComposerItem* ) const;
203 
204  QList<QgsComposerItem*> selectedComposerItems();
205 
209  QList<const QgsComposerMap*> composerMapItems() const;
210 
214  template<class T> void composerItems( QList<T*>& itemList );
215 
218  const QgsComposerMap* getComposerMapById( int id ) const;
219 
220  /*Returns the composer html with specified id (a string as named in the
221  composer user interface item properties).
222  @note Added in QGIS 2.0
223  @param id - A QString representing the id of the item.
224  @return QgsComposerHtml pointer or 0 pointer if no such item exists.
225  */
226  const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
227 
235  const QgsComposerItem* getComposerItemById( QString theId ) const;
236 
241  const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;
242 
243  int printResolution() const {return mPrintResolution;}
244  void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
245 
246  bool printAsRaster() const {return mPrintAsRaster;}
247  void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
248 
249  bool generateWorldFile() const { return mGenerateWorldFile; }
250  void setGenerateWorldFile( bool enabled ) { mGenerateWorldFile = enabled; }
251 
252  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
253  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
254 
257  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
260  void setUseAdvancedEffects( bool effectsEnabled );
261 
263  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer() {return mMapRenderer;}
265 
268  const QgsMapSettings& mapSettings() const { return mMapSettings; }
269 
270  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
271  void setPlotStyle( QgsComposition::PlotStyle style ) {mPlotStyle = style;}
272 
276  int pixelFontSize( double pointSize ) const;
277 
279  double pointFontSize( int pixelSize ) const;
280 
282  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
283 
285  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
286 
292  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0, bool addUndoCommands = false );
293 
303  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
304  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
305 
307  void addItemToZList( QgsComposerItem* item );
309  void removeItemFromZList( QgsComposerItem* item );
310 
311  //functions to move selected items in hierarchy
312  void raiseSelectedItems();
313  void raiseItem( QgsComposerItem* item );
314  void lowerSelectedItems();
315  void lowerItem( QgsComposerItem* item );
316  void moveSelectedItemsToTop();
317  void moveItemToTop( QgsComposerItem* item );
318  void moveSelectedItemsToBottom();
319  void moveItemToBottom( QgsComposerItem* item );
320 
321  //functions to find items by their position in the z list
322  void selectNextByZOrder( ZValueDirection direction );
323  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item );
324  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item );
325 
326  //functions to align selected items
327  void alignSelectedItemsLeft();
328  void alignSelectedItemsHCenter();
329  void alignSelectedItemsRight();
330  void alignSelectedItemsTop();
331  void alignSelectedItemsVCenter();
332  void alignSelectedItemsBottom();
333 
334  //functions to lock and unlock items
336  void lockSelectedItems();
338  void unlockAllItems();
339 
342  void sortZList();
343 
345  void refreshZList();
346 
348  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
349 
351  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
352 
356  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
357 
359  QGraphicsLineItem* addSnapLine();
361  void removeSnapLine( QGraphicsLineItem* line );
365  QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
366 
371  void beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
372 
374  void endCommand();
376  void cancelCommand();
377 
378  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
379  void endMultiFrameCommand();
380 
382  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
384  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
387  void addComposerArrow( QgsComposerArrow* arrow );
389  void addComposerLabel( QgsComposerLabel* label );
391  void addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle = true );
393  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
395  void addComposerLegend( QgsComposerLegend* legend );
397  void addComposerPicture( QgsComposerPicture* picture );
399  void addComposerShape( QgsComposerShape* shape );
401  void addComposerTable( QgsComposerAttributeTable* table );
403  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
404 
406  void removeComposerItem( QgsComposerItem* item, bool createCommand = true );
407 
409  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
410 
413  void setPreventCursorChange( bool preventChange ) { mPreventCursorChange = preventChange; }
414  bool preventCursorChange() { return mPreventCursorChange; }
415 
416  //printing
417 
419  void beginPrint( QPrinter& printer );
421  void beginPrintAsPDF( QPrinter& printer, const QString& file );
423  void doPrint( QPrinter& printer, QPainter& painter );
424 
426  void print( QPrinter &printer );
427 
429  void exportAsPDF( const QString& file );
430 
433  QImage printPageAsRaster( int page );
434 
437  void renderPage( QPainter* p, int page );
438 
440  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
441 
442  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
443 
445  static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
447  static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
448 
450  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
452  bool setAtlasMode( QgsComposition::AtlasMode mode );
453 
454  public slots:
456  void sendItemAddedSignal( QgsComposerItem* item );
457 
460  void updateBounds();
461 
462  protected:
463  void init();
464 
465 
466  private:
470 
472  double mPageWidth;
473  double mPageHeight;
474  QList< QgsPaperItem* > mPages;
475  double mSpaceBetweenPages; //space in preview between pages
476 
479  void createDefaultPageStyleSymbol();
480 
482  QLinkedList<QgsComposerItem*> mItemZList;
483 
485  QSet<QgsComposerMultiFrame*> mMultiFrames;
486 
489 
492 
497 
500 
508  QPen mGridPen;
510 
516 
518  QList< QGraphicsLineItem* > mSnapLines;
519 
521 
522  QUndoStack mUndoStack;
523 
526 
529 
531 
532  QgsComposition(); //default constructor is forbidden
533 
535  QRectF compositionBounds() const;
536 
538  void updateZValues( bool addUndoCommands = true );
539 
542  int boundingRectOfSelectedItems( QRectF& bRect );
543 
545  void loadDefaults();
546 
548  void loadSettings();
549 
551  QPointF minPointFromXml( const QDomElement& elem ) const;
552 
553  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
554 
555  void updatePaperItems();
556  void addPaperItem();
557  void removePaperItems();
558  void deleteAndRemoveMultiFrames();
559 
560  static QString encodeStringForXML( const QString& str );
561 
563 
564  signals:
565  void paperSizeChanged();
566  void nPagesChanged();
567 
569  void selectedItemChanged( QgsComposerItem* selected );
571  void composerArrowAdded( QgsComposerArrow* arrow );
573  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
575  void composerLabelAdded( QgsComposerLabel* label );
577  void composerMapAdded( QgsComposerMap* map );
579  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
581  void composerLegendAdded( QgsComposerLegend* legend );
583  void composerPictureAdded( QgsComposerPicture* picture );
585  void composerShapeAdded( QgsComposerShape* shape );
587  void composerTableAdded( QgsComposerAttributeTable* table );
589  void itemRemoved( QgsComposerItem* );
590 
592  void statusMsgChanged( QString message );
593 };
594 
595 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
596 {
597  itemList.clear();
598  QList<QGraphicsItem *> graphicsItemList = items();
599  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
600  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
601  {
602  T* item = dynamic_cast<T*>( *itemIt );
603  if ( item )
604  {
605  itemList.push_back( item );
606  }
607  }
608 }
609 
610 #endif
611 
612 
613 
QgsComposition::PlotStyle mPlotStyle
Item representing the paper.
Definition: qgspaperitem.h:40
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, ...)
void setPrintResolution(int dpi)
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...
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.