QGIS API Documentation  2.2.0-Valmiera
 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 
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  enum AtlasMode
95  {
96  AtlasOff, // Composition is not being controlled by an atlas
97  PreviewAtlas, // An atlas composition is being previewed in the app
98  ExportAtlas // The composition is being exported as an atlas
99  };
100 
101  QgsComposition( QgsMapRenderer* mapRenderer );
102  ~QgsComposition();
103 
105  void setPaperSize( double width, double height );
106 
108  double paperHeight() const;
109 
111  double paperWidth() const;
112 
113  double spaceBetweenPages() const { return mSpaceBetweenPages; }
114 
116  void setNumPages( int pages );
118  int numPages() const;
119 
121  void setPageStyleSymbol( QgsFillSymbolV2* symbol );
123  QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }
124 
128  QPointF positionOnPage( const QPointF & position ) const;
129 
133  int pageNumberForPoint( const QPointF & position ) const;
134 
138  void setStatusMessage( const QString & message );
139 
142  void updateSettings();
143 
144  void setSnapToGridEnabled( bool b );
145  bool snapToGridEnabled() const {return mSnapToGrid;}
146 
147  void setGridVisible( bool b );
148  bool gridVisible() const {return mGridVisible;}
149 
151  void setSnapLinesVisible( bool visible );
152  bool snapLinesVisible() const {return mGuidesVisible;}
153 
154  void setAlignmentSnap( bool s ) { mAlignmentSnap = s; }
155  bool alignmentSnap() const { return mAlignmentSnap; }
156 
157  void setSmartGuidesEnabled( bool b ) { mSmartGuides = b; }
158  bool smartGuidesEnabled() const {return mSmartGuides;}
159 
161  void clearSnapLines();
162 
163  void setSnapGridResolution( double r );
164  double snapGridResolution() const {return mSnapGridResolution;}
165 
166  void setSnapGridTolerance( double tolerance );
167  double snapGridTolerance() const {return mSnapGridTolerance;}
168 
169  void setSnapGridOffsetX( double offset );
170  double snapGridOffsetX() const {return mSnapGridOffsetX;}
171 
172  void setSnapGridOffsetY( double offset );
173  double snapGridOffsetY() const {return mSnapGridOffsetY;}
174 
175  void setGridPen( const QPen& p );
176  const QPen& gridPen() const {return mGridPen;}
177 
178  void setGridStyle( GridStyle s );
179  GridStyle gridStyle() const {return mGridStyle;}
180 
181  void setAlignmentSnapTolerance( double t ) { mAlignmentSnapTolerance = t; }
182  double alignmentSnapTolerance() const { return mAlignmentSnapTolerance; }
183 
185  QUndoStack* undoStack() { return &mUndoStack; }
186 
188  QgsComposerItem* composerItemAt( const QPointF & position );
189 
193  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem );
194 
196  int pageNumberAt( const QPointF& position ) const;
197 
199  int itemPageNumber( const QgsComposerItem* ) const;
200 
201  QList<QgsComposerItem*> selectedComposerItems();
202 
206  QList<const QgsComposerMap*> composerMapItems() const;
207 
211  template<class T> void composerItems( QList<T*>& itemList );
212 
215  const QgsComposerMap* getComposerMapById( int id ) const;
216 
217  /*Returns the composer html with specified id (a string as named in the
218  composer user interface item properties).
219  @note Added in QGIS 2.0
220  @param id - A QString representing the id of the item.
221  @return QgsComposerHtml pointer or 0 pointer if no such item exists.
222  */
223  const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
224 
232  const QgsComposerItem* getComposerItemById( QString theId ) const;
233 
238  const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;
239 
240  int printResolution() const {return mPrintResolution;}
241  void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
242 
243  bool printAsRaster() const {return mPrintAsRaster;}
244  void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
245 
246  bool generateWorldFile() const { return mGenerateWorldFile; }
247  void setGenerateWorldFile( bool enabled ) { mGenerateWorldFile = enabled; }
248 
249  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
250  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
251 
254  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
257  void setUseAdvancedEffects( bool effectsEnabled );
258 
260  QgsMapRenderer* mapRenderer() {return mMapRenderer;}
261 
262  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
263  void setPlotStyle( QgsComposition::PlotStyle style ) {mPlotStyle = style;}
264 
268  int pixelFontSize( double pointSize ) const;
269 
271  double pointFontSize( int pixelSize ) const;
272 
274  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
275 
277  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
278 
284  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0, bool addUndoCommands = false );
285 
295  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
296  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
297 
299  void addItemToZList( QgsComposerItem* item );
301  void removeItemFromZList( QgsComposerItem* item );
302 
303  //functions to move selected items in hierarchy
304  void raiseSelectedItems();
305  void raiseItem( QgsComposerItem* item );
306  void lowerSelectedItems();
307  void lowerItem( QgsComposerItem* item );
308  void moveSelectedItemsToTop();
309  void moveItemToTop( QgsComposerItem* item );
310  void moveSelectedItemsToBottom();
311  void moveItemToBottom( QgsComposerItem* item );
312 
313  //functions to find items by their position in the z list
314  void selectNextByZOrder( ZValueDirection direction );
315  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item );
316  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item );
317 
318  //functions to align selected items
319  void alignSelectedItemsLeft();
320  void alignSelectedItemsHCenter();
321  void alignSelectedItemsRight();
322  void alignSelectedItemsTop();
323  void alignSelectedItemsVCenter();
324  void alignSelectedItemsBottom();
325 
326  //functions to lock and unlock items
328  void lockSelectedItems();
330  void unlockAllItems();
331 
334  void sortZList();
335 
337  void refreshZList();
338 
340  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
341 
343  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
344 
348  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
349 
351  QGraphicsLineItem* addSnapLine();
353  void removeSnapLine( QGraphicsLineItem* line );
357  QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
358 
363  void beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
364 
366  void endCommand();
368  void cancelCommand();
369 
370  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
371  void endMultiFrameCommand();
372 
374  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
376  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
379  void addComposerArrow( QgsComposerArrow* arrow );
381  void addComposerLabel( QgsComposerLabel* label );
383  void addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle = true );
385  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
387  void addComposerLegend( QgsComposerLegend* legend );
389  void addComposerPicture( QgsComposerPicture* picture );
391  void addComposerShape( QgsComposerShape* shape );
393  void addComposerTable( QgsComposerAttributeTable* table );
395  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
396 
398  void removeComposerItem( QgsComposerItem* item, bool createCommand = true );
399 
401  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
402 
405  void setPreventCursorChange( bool preventChange ) { mPreventCursorChange = preventChange; }
406  bool preventCursorChange() { return mPreventCursorChange; }
407 
408  //printing
409 
411  void beginPrint( QPrinter& printer );
413  void beginPrintAsPDF( QPrinter& printer, const QString& file );
415  void doPrint( QPrinter& printer, QPainter& painter );
416 
418  void print( QPrinter &printer );
419 
421  void exportAsPDF( const QString& file );
422 
425  QImage printPageAsRaster( int page );
426 
429  void renderPage( QPainter* p, int page );
430 
432  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
433 
434  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
435 
437  static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
439  static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
440 
442  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
444  bool setAtlasMode( QgsComposition::AtlasMode mode );
445 
446  public slots:
448  void sendItemAddedSignal( QgsComposerItem* item );
449 
452  void updateBounds();
453 
454  private:
458  double mPageWidth;
459  double mPageHeight;
460  QList< QgsPaperItem* > mPages;
461  double mSpaceBetweenPages; //space in preview between pages
462 
465  void createDefaultPageStyleSymbol();
466 
468  QLinkedList<QgsComposerItem*> mItemZList;
469 
471  QSet<QgsComposerMultiFrame*> mMultiFrames;
472 
475 
478 
483 
486 
494  QPen mGridPen;
496 
502 
504  QList< QGraphicsLineItem* > mSnapLines;
505 
507 
508  QUndoStack mUndoStack;
509 
512 
515 
517 
518  QgsComposition(); //default constructor is forbidden
519 
521  QRectF compositionBounds() const;
522 
524  void updateZValues( bool addUndoCommands = true );
525 
528  int boundingRectOfSelectedItems( QRectF& bRect );
529 
531  void loadDefaults();
532 
534  void loadSettings();
535 
537  QPointF minPointFromXml( const QDomElement& elem ) const;
538 
539  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
540 
541  void updatePaperItems();
542  void addPaperItem();
543  void removePaperItems();
544  void deleteAndRemoveMultiFrames();
545 
546  static QString encodeStringForXML( const QString& str );
547 
549 
550  signals:
551  void paperSizeChanged();
552  void nPagesChanged();
553 
555  void selectedItemChanged( QgsComposerItem* selected );
557  void composerArrowAdded( QgsComposerArrow* arrow );
559  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
561  void composerLabelAdded( QgsComposerLabel* label );
563  void composerMapAdded( QgsComposerMap* map );
565  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
567  void composerLegendAdded( QgsComposerLegend* legend );
569  void composerPictureAdded( QgsComposerPicture* picture );
571  void composerShapeAdded( QgsComposerShape* shape );
573  void composerTableAdded( QgsComposerAttributeTable* table );
575  void itemRemoved( QgsComposerItem* );
576 
578  void statusMsgChanged( QString message );
579 };
580 
581 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
582 {
583  itemList.clear();
584  QList<QGraphicsItem *> graphicsItemList = items();
585  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
586  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
587  {
588  T* item = dynamic_cast<T*>( *itemIt );
589  if ( item )
590  {
591  itemList.push_back( item );
592  }
593  }
594 }
595 
596 #endif
597 
598 
599