QGIS API Documentation  2.8.2-Wien
 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 <memory>
20 
21 #include <QDomDocument>
22 #include <QGraphicsScene>
23 #include <QLinkedList>
24 #include <QList>
25 #include <QPair>
26 #include <QSet>
27 #include <QUndoStack>
28 #include <QPrinter>
29 #include <QPainter>
30 
32 #include "qgscomposeritemcommand.h"
34 #include "qgsatlascomposition.h"
35 #include "qgspaperitem.h"
36 #include "qgscomposerobject.h"
37 #include "qgscomposeritem.h"
38 
39 class QgisApp;
40 class QgsComposerFrame;
41 class QgsComposerMap;
42 class QGraphicsRectItem;
43 class QgsMapRenderer;
44 class QDomElement;
45 class QgsComposerArrow;
47 class QgsComposerHtml;
48 class QgsComposerTableV2;
49 class QgsComposerItem;
51 class QgsComposerLabel;
52 class QgsComposerLegend;
53 class QgsComposerMap;
54 class QgsComposerPicture;
56 class QgsComposerShape;
61 class QgsVectorLayer;
62 class QgsComposer;
63 class QgsFillSymbolV2;
64 class QgsDataDefined;
65 class QgsComposerModel;
66 
73 class CORE_EXPORT QgsComposition : public QGraphicsScene
74 {
75  Q_OBJECT
76  public:
77 
79  enum PlotStyle
80  {
81  Preview = 0, // Use cache etc
82  Print, // Render well
83  Postscript // Fonts need different scaling!
84  };
85 
87  enum GridStyle
88  {
91  Crosses
92  };
93 
95  {
97  ZValueAbove
98  };
99 
101  {
103  Landscape
104  };
105 
107  Q_DECL_DEPRECATED QgsComposition( QgsMapRenderer* mapRenderer );
108  explicit QgsComposition( const QgsMapSettings& mapSettings );
109 
112  {
113  AtlasOff, // Composition is not being controlled by an atlas
114  PreviewAtlas, // An atlas composition is being previewed in the app
115  ExportAtlas // The composition is being exported as an atlas
116  };
117 
118  ~QgsComposition();
119 
127  void setPaperSize( const double width, const double height );
128 
134  double paperHeight() const;
135 
141  double paperWidth() const;
142 
146  double spaceBetweenPages() const { return mSpaceBetweenPages; }
147 
152  void setNumPages( const int pages );
153 
158  int numPages() const;
159 
169  bool pageIsEmpty( const int page ) const;
170 
178  bool shouldExportPage( const int page ) const;
179 
181  void setPageStyleSymbol( QgsFillSymbolV2* symbol );
183  QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }
184 
188  QPointF positionOnPage( const QPointF & position ) const;
189 
193  int pageNumberForPoint( const QPointF & position ) const;
194 
198  void setStatusMessage( const QString & message );
199 
203  void updateSettings();
204 
205  void setSnapToGridEnabled( const bool b );
206  bool snapToGridEnabled() const {return mSnapToGrid;}
207 
208  void setGridVisible( const bool b );
209  bool gridVisible() const {return mGridVisible;}
210 
212  void setSnapLinesVisible( const bool visible );
213  bool snapLinesVisible() const {return mGuidesVisible;}
214 
215  void setAlignmentSnap( const bool s ) { mAlignmentSnap = s; }
216  bool alignmentSnap() const { return mAlignmentSnap; }
217 
218  void setSmartGuidesEnabled( const bool b ) { mSmartGuides = b; }
219  bool smartGuidesEnabled() const {return mSmartGuides;}
220 
222  void clearSnapLines();
223 
224  void setSnapGridResolution( const double r );
225  double snapGridResolution() const {return mSnapGridResolution;}
226 
227  void setSnapGridOffsetX( const double offset );
228  double snapGridOffsetX() const {return mSnapGridOffsetX;}
229 
230  void setSnapGridOffsetY( const double offset );
231  double snapGridOffsetY() const {return mSnapGridOffsetY;}
232 
233  void setGridPen( const QPen& p );
234  const QPen& gridPen() const {return mGridPen;}
235 
236  void setGridStyle( const GridStyle s );
237  GridStyle gridStyle() const {return mGridStyle;}
238 
245  Q_DECL_DEPRECATED void setSnapGridTolerance( double tolerance ) { mSnapTolerance = tolerance; }
246 
253  Q_DECL_DEPRECATED double snapGridTolerance() const {return mSnapTolerance;}
254 
261  Q_DECL_DEPRECATED void setAlignmentSnapTolerance( double t ) { mSnapTolerance = t; }
262 
269  Q_DECL_DEPRECATED double alignmentSnapTolerance() const { return mSnapTolerance; }
270 
277  void setSnapTolerance( const int snapTolerance ) { mSnapTolerance = snapTolerance; }
278 
285  int snapTolerance() const { return mSnapTolerance; }
286 
292  void setBoundingBoxesVisible( const bool boundsVisible );
293 
299  bool boundingBoxesVisible() const { return mBoundingBoxesVisible; }
300 
302  QUndoStack* undoStack() { return mUndoStack; }
303 
309  QgsComposerItem* composerItemAt( const QPointF & position, const bool ignoreLocked = false ) const;
310 
317  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem, const bool ignoreLocked = false ) const;
318 
320  int pageNumberAt( const QPointF& position ) const;
321 
323  int itemPageNumber( const QgsComposerItem* ) const;
324 
329  QList<QgsComposerItem*> selectedComposerItems( const bool includeLockedItems = true );
330 
334  QList<const QgsComposerMap*> composerMapItems() const;
335 
340  template<class T> void composerItems( QList<T*>& itemList );
341 
348  template<class T> void composerItemsOnPage( QList<T*>& itemList, const int pageNumber ) const;
349 
352  const QgsComposerMap* getComposerMapById( const int id ) const;
353 
359  Q_DECL_DEPRECATED const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
360 
367  const QgsComposerItem* getComposerItemById( const QString theId ) const;
368 
372  const QgsComposerItem* getComposerItemByUuid( const QString theUuid ) const;
373 
374  int printResolution() const {return mPrintResolution;}
375  void setPrintResolution( const int dpi );
376 
377  bool printAsRaster() const {return mPrintAsRaster;}
378  void setPrintAsRaster( const bool enabled ) { mPrintAsRaster = enabled; }
379 
380  bool generateWorldFile() const { return mGenerateWorldFile; }
381  void setGenerateWorldFile( const bool enabled ) { mGenerateWorldFile = enabled; }
382 
383  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
384  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
385 
387  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
389  void setUseAdvancedEffects( const bool effectsEnabled );
390 
392 
393  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer() { return mMapRenderer; }
394 
397  const QgsMapSettings& mapSettings() const { return mMapSettings; }
398 
399  QgsComposition::PlotStyle plotStyle() const { return mPlotStyle; }
400  void setPlotStyle( const QgsComposition::PlotStyle style ) { mPlotStyle = style; }
401 
406  Q_DECL_DEPRECATED int pixelFontSize( double pointSize ) const;
407 
411  Q_DECL_DEPRECATED double pointFontSize( int pixelSize ) const;
412 
414  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
415 
417  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
418 
427  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0,
428  bool addUndoCommands = false, const bool clearComposition = true );
429 
439  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
440  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
441 
443  void addItemToZList( QgsComposerItem* item );
445  void removeItemFromZList( QgsComposerItem* item );
446 
447  //functions to move selected items in hierarchy
448  void raiseSelectedItems();
449 
450  //returns true if successful
451  bool raiseItem( QgsComposerItem* item );
452  void lowerSelectedItems();
453  //returns true if successful
454  bool lowerItem( QgsComposerItem* item );
455  void moveSelectedItemsToTop();
456  //returns true if successful
457  bool moveItemToTop( QgsComposerItem* item );
458  void moveSelectedItemsToBottom();
459  //returns true if successful
460  bool moveItemToBottom( QgsComposerItem* item );
461 
462  //functions to find items by their position in the z list
463  void selectNextByZOrder( const ZValueDirection direction );
464  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item ) const;
465  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item ) const;
466 
467  //functions to align selected items
468  void alignSelectedItemsLeft();
469  void alignSelectedItemsHCenter();
470  void alignSelectedItemsRight();
471  void alignSelectedItemsTop();
472  void alignSelectedItemsVCenter();
473  void alignSelectedItemsBottom();
474 
475  //functions to lock and unlock items
477  void lockSelectedItems();
479  void unlockAllItems();
480 
486  QgsComposerItemGroup* groupItems( QList<QgsComposerItem*> items );
487 
495  QList<QgsComposerItem*> ungroupItems( QgsComposerItemGroup* group );
496 
501  Q_DECL_DEPRECATED void sortZList() {};
502 
506  void refreshZList();
507 
509  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
510 
512  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
513 
517  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
518 
520  QGraphicsLineItem* addSnapLine();
522  void removeSnapLine( QGraphicsLineItem* line );
526  QGraphicsLineItem* nearestSnapLine( const bool horizontal, const double x, const double y, const double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems ) const;
527 
532  void beginCommand( QgsComposerItem* item, const QString& commandText, const QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
533 
535  void endCommand();
537  void cancelCommand();
538 
539  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c = QgsComposerMultiFrameMergeCommand::Unknown );
540  void endMultiFrameCommand();
542  void cancelMultiFrameCommand();
543 
545  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
547  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
550  void addComposerArrow( QgsComposerArrow* arrow );
552  void addComposerLabel( QgsComposerLabel* label );
554  void addComposerMap( QgsComposerMap* map, const bool setDefaultPreviewStyle = true );
556  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
558  void addComposerLegend( QgsComposerLegend* legend );
560  void addComposerPicture( QgsComposerPicture* picture );
562  void addComposerShape( QgsComposerShape* shape );
564  void addComposerTable( QgsComposerAttributeTable* table );
566  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
568  void addComposerTableFrame( QgsComposerAttributeTableV2* table, QgsComposerFrame* frame );
569 
571  void removeComposerItem( QgsComposerItem* item, const bool createCommand = true, const bool removeGroupItems = true );
572 
574  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, const QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
575 
578  void setPreventCursorChange( const bool preventChange ) { mPreventCursorChange = preventChange; }
579  bool preventCursorChange() const { return mPreventCursorChange; }
580 
581  //printing
582 
584  void beginPrint( QPrinter& printer, const bool evaluateDDPageSize = false );
586  void beginPrintAsPDF( QPrinter& printer, const QString& file );
587 
593  void doPrint( QPrinter& printer, QPainter& painter, bool startNewPage = false );
594 
598  bool print( QPrinter &printer, const bool evaluateDDPageSize = false );
599 
603  bool exportAsPDF( const QString& file );
604 
607  QImage printPageAsRaster( int page );
608 
612  void renderPage( QPainter* p, int page );
613 
615  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
616 
617  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
618 
622  Q_DECL_DEPRECATED static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
623 
627  Q_DECL_DEPRECATED static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
628 
633  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
634 
640  bool setAtlasMode( const QgsComposition::AtlasMode mode );
641 
645  QList< QgsPaperItem* > pages() { return mPages; }
646 
651  QgsDataDefined* dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property );
652 
661  void setDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, bool active, bool useExpression, const QString &expression, const QString &field );
662 
667  QgsComposerModel * itemsModel() { return mItemsModel; }
668 
669  public slots:
671  void sendItemAddedSignal( QgsComposerItem* item );
672 
675  void updateBounds();
676 
682  void refreshItems();
683 
687  void setSelectedItem( QgsComposerItem* item );
688 
693  void setAllUnselected();
694 
702  void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties );
703 
704  protected:
705  void init();
706 
707 
708  private:
710  QgsMapRenderer* mMapRenderer;
711  const QgsMapSettings& mMapSettings;
712 
713  QgsComposition::PlotStyle mPlotStyle;
714  double mPageWidth;
715  double mPageHeight;
716  QList< QgsPaperItem* > mPages;
717  double mSpaceBetweenPages; //space in preview between pages
718 
720  QgsFillSymbolV2* mPageStyleSymbol;
721  void createDefaultPageStyleSymbol();
722 
724  QSet<QgsComposerMultiFrame*> mMultiFrames;
725 
727  int mPrintResolution;
728 
730  bool mPrintAsRaster;
731 
733  bool mGenerateWorldFile;
735  QgsComposerMap* mWorldFileMap;
736 
738  bool mUseAdvancedEffects;
739 
741  bool mSnapToGrid;
742  bool mGridVisible;
743  double mSnapGridResolution;
744  double mSnapGridOffsetX;
745  double mSnapGridOffsetY;
746  QPen mGridPen;
747  GridStyle mGridStyle;
748 
750  bool mAlignmentSnap;
751  bool mGuidesVisible;
752  bool mSmartGuides;
753  int mSnapTolerance;
754 
756  QList< QGraphicsLineItem* > mSnapLines;
757 
758  bool mBoundingBoxesVisible;
759  QgsComposerMouseHandles* mSelectionHandles;
760 
761  QUndoStack* mUndoStack;
762 
763  QgsComposerItemCommand* mActiveItemCommand;
764  QgsComposerMultiFrameCommand* mActiveMultiFrameCommand;
765 
767  QgsAtlasComposition mAtlasComposition;
768 
769  QgsComposition::AtlasMode mAtlasMode;
770 
771  bool mPreventCursorChange;
772 
773  QgsComposerModel * mItemsModel;
774 
776  QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames;
778  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;
779 
780  QgsComposition(); //default constructor is forbidden
781 
783  QRectF compositionBounds() const;
784 
786  void updateZValues( const bool addUndoCommands = true );
787 
790  int boundingRectOfSelectedItems( QRectF& bRect );
791 
793  void loadDefaults();
794 
796  void loadSettings();
797 
799  QPointF minPointFromXml( const QDomElement& elem ) const;
800 
801  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
802 
803  void updatePaperItems();
804  void addPaperItem();
805  void removePaperItems();
806  void deleteAndRemoveMultiFrames();
807 
808  static QString encodeStringForXML( const QString& str );
809 
810  //tries to return the current QGraphicsView attached to the composition
811  QGraphicsView* graphicsView() const;
812 
813  /*Recalculates the page size using data defined page settings*/
814  void refreshPageSize();
815 
823  bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue,
824  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
825 
831  bool dataDefinedActive( const QgsComposerObject::DataDefinedProperty property,
832  const QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const;
833 
841  QVariant dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields,
842  QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const;
843 
844 
850  void prepareDataDefinedExpression( QgsDataDefined *dd, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties ) const;
851 
856  bool ddPageSizeActive() const;
857 
858  private slots:
859  /*Prepares all data defined expressions*/
860  void prepareAllDataDefinedExpressions();
861 
862  signals:
863  void paperSizeChanged();
864  void nPagesChanged();
865 
867  void printResolutionChanged();
868 
870  void selectedItemChanged( QgsComposerItem* selected );
872  void composerArrowAdded( QgsComposerArrow* arrow );
874  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
876  void composerLabelAdded( QgsComposerLabel* label );
878  void composerMapAdded( QgsComposerMap* map );
880  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
882  void composerLegendAdded( QgsComposerLegend* legend );
884  void composerPictureAdded( QgsComposerPicture* picture );
886  void composerShapeAdded( QgsComposerShape* shape );
888  void composerTableAdded( QgsComposerAttributeTable* table );
890  void composerTableFrameAdded( QgsComposerAttributeTableV2* table, QgsComposerFrame* frame );
892  void itemRemoved( QgsComposerItem* );
893 
895  void refreshItemsTriggered();
896 
898  void statusMsgChanged( QString message );
899 
900  friend class QgsComposerObject; //for accessing dataDefinedEvaluate, readDataDefinedPropertyMap and writeDataDefinedPropertyMap
901  friend class QgsComposerModel; //for accessing updateZValues (should not be public)
902 };
903 
904 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
905 {
906  itemList.clear();
907  QList<QGraphicsItem *> graphicsItemList = items();
908  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
909  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
910  {
911  T* item = dynamic_cast<T*>( *itemIt );
912  if ( item )
913  {
914  itemList.push_back( item );
915  }
916  }
917 }
918 
919 template<class T> void QgsComposition::composerItemsOnPage( QList<T*>& itemList, const int pageNumber ) const
920 {
921  itemList.clear();
922  QList<QGraphicsItem *> graphicsItemList = items();
923  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
924  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
925  {
926  T* item = dynamic_cast<T*>( *itemIt );
927  if ( item && itemPageNumber( item ) == pageNumber )
928  {
929  itemList.push_back( item );
930  }
931  }
932 }
933 
934 
935 #endif
936 
937 
938