QGIS API Documentation  2.6.0-Brighton
 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 <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 
288  QUndoStack* undoStack() { return mUndoStack; }
289 
295  QgsComposerItem* composerItemAt( const QPointF & position, const bool ignoreLocked = false ) const;
296 
303  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem, const bool ignoreLocked = false ) const;
304 
306  int pageNumberAt( const QPointF& position ) const;
307 
309  int itemPageNumber( const QgsComposerItem* ) const;
310 
315  QList<QgsComposerItem*> selectedComposerItems( const bool includeLockedItems = true );
316 
320  QList<const QgsComposerMap*> composerMapItems() const;
321 
326  template<class T> void composerItems( QList<T*>& itemList );
327 
334  template<class T> void composerItemsOnPage( QList<T*>& itemList, const int pageNumber ) const;
335 
338  const QgsComposerMap* getComposerMapById( const int id ) const;
339 
345  Q_DECL_DEPRECATED const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
346 
353  const QgsComposerItem* getComposerItemById( const QString theId ) const;
354 
358  const QgsComposerItem* getComposerItemByUuid( const QString theUuid ) const;
359 
360  int printResolution() const {return mPrintResolution;}
361  void setPrintResolution( const int dpi );
362 
363  bool printAsRaster() const {return mPrintAsRaster;}
364  void setPrintAsRaster( const bool enabled ) { mPrintAsRaster = enabled; }
365 
366  bool generateWorldFile() const { return mGenerateWorldFile; }
367  void setGenerateWorldFile( const bool enabled ) { mGenerateWorldFile = enabled; }
368 
369  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
370  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
371 
373  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
376  void setUseAdvancedEffects( const bool effectsEnabled );
377 
379 
380  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer() {return mMapRenderer;}
381 
384  const QgsMapSettings& mapSettings() const { return mMapSettings; }
385 
386  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
387  void setPlotStyle( const QgsComposition::PlotStyle style ) {mPlotStyle = style;}
388 
393  Q_DECL_DEPRECATED int pixelFontSize( double pointSize ) const;
394 
398  Q_DECL_DEPRECATED double pointFontSize( int pixelSize ) const;
399 
401  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
402 
404  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
405 
414  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0,
415  bool addUndoCommands = false, const bool clearComposition = true );
416 
426  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
427  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
428 
430  void addItemToZList( QgsComposerItem* item );
432  void removeItemFromZList( QgsComposerItem* item );
433 
434  //functions to move selected items in hierarchy
435  void raiseSelectedItems();
436 
437  //returns true if successful
438  bool raiseItem( QgsComposerItem* item );
439  void lowerSelectedItems();
440  //returns true if successful
441  bool lowerItem( QgsComposerItem* item );
442  void moveSelectedItemsToTop();
443  //returns true if successful
444  bool moveItemToTop( QgsComposerItem* item );
445  void moveSelectedItemsToBottom();
446  //returns true if successful
447  bool moveItemToBottom( QgsComposerItem* item );
448 
449  //functions to find items by their position in the z list
450  void selectNextByZOrder( const ZValueDirection direction );
451  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item ) const;
452  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item ) const;
453 
454  //functions to align selected items
455  void alignSelectedItemsLeft();
456  void alignSelectedItemsHCenter();
457  void alignSelectedItemsRight();
458  void alignSelectedItemsTop();
459  void alignSelectedItemsVCenter();
460  void alignSelectedItemsBottom();
461 
462  //functions to lock and unlock items
464  void lockSelectedItems();
466  void unlockAllItems();
467 
473  QgsComposerItemGroup* groupItems( QList<QgsComposerItem*> items );
474 
482  QList<QgsComposerItem*> ungroupItems( QgsComposerItemGroup* group );
483 
488  Q_DECL_DEPRECATED void sortZList() {};
489 
493  void refreshZList();
494 
496  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
497 
499  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
500 
504  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
505 
507  QGraphicsLineItem* addSnapLine();
509  void removeSnapLine( QGraphicsLineItem* line );
513  QGraphicsLineItem* nearestSnapLine( const bool horizontal, const double x, const double y, const double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems ) const;
514 
519  void beginCommand( QgsComposerItem* item, const QString& commandText, const QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
520 
522  void endCommand();
524  void cancelCommand();
525 
526  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c = QgsComposerMultiFrameMergeCommand::Unknown );
527  void endMultiFrameCommand();
529  void cancelMultiFrameCommand();
530 
532  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
534  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
537  void addComposerArrow( QgsComposerArrow* arrow );
539  void addComposerLabel( QgsComposerLabel* label );
541  void addComposerMap( QgsComposerMap* map, const bool setDefaultPreviewStyle = true );
543  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
545  void addComposerLegend( QgsComposerLegend* legend );
547  void addComposerPicture( QgsComposerPicture* picture );
549  void addComposerShape( QgsComposerShape* shape );
551  void addComposerTable( QgsComposerAttributeTable* table );
553  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
555  void addComposerTableFrame( QgsComposerAttributeTableV2* table, QgsComposerFrame* frame );
556 
558  void removeComposerItem( QgsComposerItem* item, const bool createCommand = true, const bool removeGroupItems = true );
559 
561  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, const QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
562 
565  void setPreventCursorChange( const bool preventChange ) { mPreventCursorChange = preventChange; }
566  bool preventCursorChange() const { return mPreventCursorChange; }
567 
568  //printing
569 
571  void beginPrint( QPrinter& printer, const bool evaluateDDPageSize = false );
573  void beginPrintAsPDF( QPrinter& printer, const QString& file );
574 
580  void doPrint( QPrinter& printer, QPainter& painter, bool startNewPage = false );
581 
585  bool print( QPrinter &printer, const bool evaluateDDPageSize = false );
586 
590  bool exportAsPDF( const QString& file );
591 
594  QImage printPageAsRaster( int page );
595 
599  void renderPage( QPainter* p, int page );
600 
602  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
603 
604  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
605 
609  Q_DECL_DEPRECATED static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
610 
614  Q_DECL_DEPRECATED static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
615 
620  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
621 
627  bool setAtlasMode( const QgsComposition::AtlasMode mode );
628 
632  QList< QgsPaperItem* > pages() { return mPages; }
633 
638  QgsDataDefined* dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property );
639 
648  void setDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, bool active, bool useExpression, const QString &expression, const QString &field );
649 
654  QgsComposerModel * itemsModel() { return mItemsModel; }
655 
656  public slots:
658  void sendItemAddedSignal( QgsComposerItem* item );
659 
662  void updateBounds();
663 
669  void refreshItems();
670 
674  void setSelectedItem( QgsComposerItem* item );
675 
680  void setAllUnselected();
681 
689  void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties );
690 
691  protected:
692  void init();
693 
694 
695  private:
697  QgsMapRenderer* mMapRenderer;
698  const QgsMapSettings& mMapSettings;
699 
700  QgsComposition::PlotStyle mPlotStyle;
701  double mPageWidth;
702  double mPageHeight;
703  QList< QgsPaperItem* > mPages;
704  double mSpaceBetweenPages; //space in preview between pages
705 
707  QgsFillSymbolV2* mPageStyleSymbol;
708  void createDefaultPageStyleSymbol();
709 
711  QSet<QgsComposerMultiFrame*> mMultiFrames;
712 
714  int mPrintResolution;
715 
717  bool mPrintAsRaster;
718 
720  bool mGenerateWorldFile;
722  QgsComposerMap* mWorldFileMap;
723 
725  bool mUseAdvancedEffects;
726 
728  bool mSnapToGrid;
729  bool mGridVisible;
730  double mSnapGridResolution;
731  double mSnapGridOffsetX;
732  double mSnapGridOffsetY;
733  QPen mGridPen;
734  GridStyle mGridStyle;
735 
737  bool mAlignmentSnap;
738  bool mGuidesVisible;
739  bool mSmartGuides;
740  int mSnapTolerance;
741 
743  QList< QGraphicsLineItem* > mSnapLines;
744 
745  QgsComposerMouseHandles* mSelectionHandles;
746 
747  QUndoStack* mUndoStack;
748 
749  QgsComposerItemCommand* mActiveItemCommand;
750  QgsComposerMultiFrameCommand* mActiveMultiFrameCommand;
751 
753  QgsAtlasComposition mAtlasComposition;
754 
755  QgsComposition::AtlasMode mAtlasMode;
756 
757  bool mPreventCursorChange;
758 
759  QgsComposerModel * mItemsModel;
760 
762  QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames;
764  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;
765 
766  QgsComposition(); //default constructor is forbidden
767 
769  QRectF compositionBounds() const;
770 
772  void updateZValues( const bool addUndoCommands = true );
773 
776  int boundingRectOfSelectedItems( QRectF& bRect );
777 
779  void loadDefaults();
780 
782  void loadSettings();
783 
785  QPointF minPointFromXml( const QDomElement& elem ) const;
786 
787  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
788 
789  void updatePaperItems();
790  void addPaperItem();
791  void removePaperItems();
792  void deleteAndRemoveMultiFrames();
793 
794  static QString encodeStringForXML( const QString& str );
795 
796  //tries to return the current QGraphicsView attached to the composition
797  QGraphicsView* graphicsView() const;
798 
799  /*Recalculates the page size using data defined page settings*/
800  void refreshPageSize();
801 
809  bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue,
810  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
811 
817  bool dataDefinedActive( const QgsComposerObject::DataDefinedProperty property,
818  const QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const;
819 
827  QVariant dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields,
828  QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const;
829 
830 
836  void prepareDataDefinedExpression( QgsDataDefined *dd, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties ) const;
837 
842  bool ddPageSizeActive() const;
843 
844  private slots:
845  /*Prepares all data defined expressions*/
846  void prepareAllDataDefinedExpressions();
847 
848  signals:
849  void paperSizeChanged();
850  void nPagesChanged();
851 
853  void printResolutionChanged();
854 
856  void selectedItemChanged( QgsComposerItem* selected );
858  void composerArrowAdded( QgsComposerArrow* arrow );
860  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
862  void composerLabelAdded( QgsComposerLabel* label );
864  void composerMapAdded( QgsComposerMap* map );
866  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
868  void composerLegendAdded( QgsComposerLegend* legend );
870  void composerPictureAdded( QgsComposerPicture* picture );
872  void composerShapeAdded( QgsComposerShape* shape );
874  void composerTableAdded( QgsComposerAttributeTable* table );
876  void composerTableFrameAdded( QgsComposerAttributeTableV2* table, QgsComposerFrame* frame );
878  void itemRemoved( QgsComposerItem* );
879 
881  void refreshItemsTriggered();
882 
884  void statusMsgChanged( QString message );
885 
886  friend class QgsComposerObject; //for accessing dataDefinedEvaluate, readDataDefinedPropertyMap and writeDataDefinedPropertyMap
887  friend class QgsComposerModel; //for accessing updateZValues (should not be public)
888 };
889 
890 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
891 {
892  itemList.clear();
893  QList<QGraphicsItem *> graphicsItemList = items();
894  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
895  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
896  {
897  T* item = dynamic_cast<T*>( *itemIt );
898  if ( item )
899  {
900  itemList.push_back( item );
901  }
902  }
903 }
904 
905 template<class T> void QgsComposition::composerItemsOnPage( QList<T*>& itemList, const int pageNumber ) const
906 {
907  itemList.clear();
908  QList<QGraphicsItem *> graphicsItemList = items();
909  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
910  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
911  {
912  T* item = dynamic_cast<T*>( *itemIt );
913  if ( item && itemPageNumber( item ) == pageNumber )
914  {
915  itemList.push_back( item );
916  }
917  }
918 }
919 
920 
921 #endif
922 
923 
924