QGIS API Documentation  2.5.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 <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 QgsComposerItem;
49 class QgsComposerLabel;
50 class QgsComposerLegend;
51 class QgsComposerMap;
52 class QgsComposerPicture;
54 class QgsComposerShape;
58 class QgsVectorLayer;
59 class QgsComposer;
60 class QgsFillSymbolV2;
61 class QgsDataDefined;
62 class QgsComposerModel;
63 
70 class CORE_EXPORT QgsComposition : public QGraphicsScene
71 {
72  Q_OBJECT
73  public:
74 
76  enum PlotStyle
77  {
78  Preview = 0, // Use cache etc
79  Print, // Render well
80  Postscript // Fonts need different scaling!
81  };
82 
84  enum GridStyle
85  {
88  Crosses
89  };
90 
92  {
94  ZValueAbove
95  };
96 
98  {
100  Landscape
101  };
102 
104  Q_DECL_DEPRECATED QgsComposition( QgsMapRenderer* mapRenderer );
105  explicit QgsComposition( const QgsMapSettings& mapSettings );
106 
109  {
110  AtlasOff, // Composition is not being controlled by an atlas
111  PreviewAtlas, // An atlas composition is being previewed in the app
112  ExportAtlas // The composition is being exported as an atlas
113  };
114 
115  ~QgsComposition();
116 
120  void setPaperSize( const double width, const double height );
121 
123  double paperHeight() const;
124 
126  double paperWidth() const;
127 
128  double spaceBetweenPages() const { return mSpaceBetweenPages; }
129 
131  void setNumPages( const int pages );
133  int numPages() const;
134 
136  void setPageStyleSymbol( QgsFillSymbolV2* symbol );
138  QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }
139 
143  QPointF positionOnPage( const QPointF & position ) const;
144 
148  int pageNumberForPoint( const QPointF & position ) const;
149 
153  void setStatusMessage( const QString & message );
154 
158  void updateSettings();
159 
160  void setSnapToGridEnabled( const bool b );
161  bool snapToGridEnabled() const {return mSnapToGrid;}
162 
163  void setGridVisible( const bool b );
164  bool gridVisible() const {return mGridVisible;}
165 
167  void setSnapLinesVisible( const bool visible );
168  bool snapLinesVisible() const {return mGuidesVisible;}
169 
170  void setAlignmentSnap( const bool s ) { mAlignmentSnap = s; }
171  bool alignmentSnap() const { return mAlignmentSnap; }
172 
173  void setSmartGuidesEnabled( const bool b ) { mSmartGuides = b; }
174  bool smartGuidesEnabled() const {return mSmartGuides;}
175 
177  void clearSnapLines();
178 
179  void setSnapGridResolution( const double r );
180  double snapGridResolution() const {return mSnapGridResolution;}
181 
182  void setSnapGridOffsetX( const double offset );
183  double snapGridOffsetX() const {return mSnapGridOffsetX;}
184 
185  void setSnapGridOffsetY( const double offset );
186  double snapGridOffsetY() const {return mSnapGridOffsetY;}
187 
188  void setGridPen( const QPen& p );
189  const QPen& gridPen() const {return mGridPen;}
190 
191  void setGridStyle( const GridStyle s );
192  GridStyle gridStyle() const {return mGridStyle;}
193 
200  Q_DECL_DEPRECATED void setSnapGridTolerance( double tolerance ) { mSnapTolerance = tolerance; }
201 
208  Q_DECL_DEPRECATED double snapGridTolerance() const {return mSnapTolerance;}
209 
216  Q_DECL_DEPRECATED void setAlignmentSnapTolerance( double t ) { mSnapTolerance = t; }
217 
224  Q_DECL_DEPRECATED double alignmentSnapTolerance() const { return mSnapTolerance; }
225 
232  void setSnapTolerance( const int snapTolerance ) { mSnapTolerance = snapTolerance; }
233 
240  int snapTolerance() const { return mSnapTolerance; }
241 
243  QUndoStack* undoStack() { return mUndoStack; }
244 
250  QgsComposerItem* composerItemAt( const QPointF & position, const bool ignoreLocked = false ) const;
251 
258  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem, const bool ignoreLocked = false ) const;
259 
261  int pageNumberAt( const QPointF& position ) const;
262 
264  int itemPageNumber( const QgsComposerItem* ) const;
265 
270  QList<QgsComposerItem*> selectedComposerItems( const bool includeLockedItems = true );
271 
275  QList<const QgsComposerMap*> composerMapItems() const;
276 
280  template<class T> void composerItems( QList<T*>& itemList );
281 
284  const QgsComposerMap* getComposerMapById( const int id ) const;
285 
292  Q_DECL_DEPRECATED const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
293 
301  const QgsComposerItem* getComposerItemById( const QString theId ) const;
302 
307  const QgsComposerItem* getComposerItemByUuid( const QString theUuid ) const;
308 
309  int printResolution() const {return mPrintResolution;}
310  void setPrintResolution( const int dpi );
311 
312  bool printAsRaster() const {return mPrintAsRaster;}
313  void setPrintAsRaster( const bool enabled ) { mPrintAsRaster = enabled; }
314 
315  bool generateWorldFile() const { return mGenerateWorldFile; }
316  void setGenerateWorldFile( const bool enabled ) { mGenerateWorldFile = enabled; }
317 
318  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
319  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
320 
323  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
326  void setUseAdvancedEffects( const bool effectsEnabled );
327 
329  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer() {return mMapRenderer;}
331 
334  const QgsMapSettings& mapSettings() const { return mMapSettings; }
335 
336  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
337  void setPlotStyle( const QgsComposition::PlotStyle style ) {mPlotStyle = style;}
338 
343  Q_DECL_DEPRECATED int pixelFontSize( double pointSize ) const;
344 
348  Q_DECL_DEPRECATED double pointFontSize( int pixelSize ) const;
349 
351  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
352 
354  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
355 
364  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0,
365  bool addUndoCommands = false, const bool clearComposition = true );
366 
376  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
377  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
378 
380  void addItemToZList( QgsComposerItem* item );
382  void removeItemFromZList( QgsComposerItem* item );
383 
384  //functions to move selected items in hierarchy
385  void raiseSelectedItems();
386 
387  //returns true if successful
388  bool raiseItem( QgsComposerItem* item );
389  void lowerSelectedItems();
390  //returns true if successful
391  bool lowerItem( QgsComposerItem* item );
392  void moveSelectedItemsToTop();
393  //returns true if successful
394  bool moveItemToTop( QgsComposerItem* item );
395  void moveSelectedItemsToBottom();
396  //returns true if successful
397  bool moveItemToBottom( QgsComposerItem* item );
398 
399  //functions to find items by their position in the z list
400  void selectNextByZOrder( const ZValueDirection direction );
401  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item ) const;
402  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item ) const;
403 
404  //functions to align selected items
405  void alignSelectedItemsLeft();
406  void alignSelectedItemsHCenter();
407  void alignSelectedItemsRight();
408  void alignSelectedItemsTop();
409  void alignSelectedItemsVCenter();
410  void alignSelectedItemsBottom();
411 
412  //functions to lock and unlock items
414  void lockSelectedItems();
416  void unlockAllItems();
417 
422  Q_DECL_DEPRECATED void sortZList() {};
423 
427  void refreshZList();
428 
430  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
431 
433  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
434 
438  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
439 
441  QGraphicsLineItem* addSnapLine();
443  void removeSnapLine( QGraphicsLineItem* line );
447  QGraphicsLineItem* nearestSnapLine( const bool horizontal, const double x, const double y, const double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems ) const;
448 
453  void beginCommand( QgsComposerItem* item, const QString& commandText, const QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
454 
456  void endCommand();
458  void cancelCommand();
459 
460  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c = QgsComposerMultiFrameMergeCommand::Unknown );
461  void endMultiFrameCommand();
462 
464  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
466  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
469  void addComposerArrow( QgsComposerArrow* arrow );
471  void addComposerLabel( QgsComposerLabel* label );
473  void addComposerMap( QgsComposerMap* map, const bool setDefaultPreviewStyle = true );
475  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
477  void addComposerLegend( QgsComposerLegend* legend );
479  void addComposerPicture( QgsComposerPicture* picture );
481  void addComposerShape( QgsComposerShape* shape );
483  void addComposerTable( QgsComposerAttributeTable* table );
485  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
486 
488  void removeComposerItem( QgsComposerItem* item, const bool createCommand = true, const bool removeGroupItems = true );
489 
491  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, const QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
492 
495  void setPreventCursorChange( const bool preventChange ) { mPreventCursorChange = preventChange; }
496  bool preventCursorChange() const { return mPreventCursorChange; }
497 
498  //printing
499 
501  void beginPrint( QPrinter& printer, const bool evaluateDDPageSize = false );
503  void beginPrintAsPDF( QPrinter& printer, const QString& file );
504 
510  void doPrint( QPrinter& printer, QPainter& painter, bool startNewPage = false );
511 
515  bool print( QPrinter &printer, const bool evaluateDDPageSize = false );
516 
520  bool exportAsPDF( const QString& file );
521 
524  QImage printPageAsRaster( int page );
525 
528  void renderPage( QPainter* p, int page );
529 
531  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
532 
533  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
534 
538  Q_DECL_DEPRECATED static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
539 
543  Q_DECL_DEPRECATED static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
544 
549  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
550 
556  bool setAtlasMode( const QgsComposition::AtlasMode mode );
557 
561  QList< QgsPaperItem* > pages() { return mPages; }
562 
567  QgsDataDefined* dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property );
568 
577  void setDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, bool active, bool useExpression, const QString &expression, const QString &field );
578 
583  QgsComposerModel * itemsModel() { return mItemsModel; }
584 
585  public slots:
587  void sendItemAddedSignal( QgsComposerItem* item );
588 
591  void updateBounds();
592 
598  void refreshItems();
599 
603  void setSelectedItem( QgsComposerItem* item );
604 
609  void setAllUnselected();
610 
618  void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties );
619 
620  protected:
621  void init();
622 
623 
624  private:
628 
630  double mPageWidth;
631  double mPageHeight;
632  QList< QgsPaperItem* > mPages;
633  double mSpaceBetweenPages; //space in preview between pages
634 
637  void createDefaultPageStyleSymbol();
638 
640  QSet<QgsComposerMultiFrame*> mMultiFrames;
641 
644 
647 
652 
655 
662  QPen mGridPen;
664 
670 
672  QList< QGraphicsLineItem* > mSnapLines;
673 
675 
676  QUndoStack* mUndoStack;
677 
680 
683 
685 
687 
689 
691  QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames;
693  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;
694 
695  QgsComposition(); //default constructor is forbidden
696 
698  QRectF compositionBounds() const;
699 
701  void updateZValues( const bool addUndoCommands = true );
702 
705  int boundingRectOfSelectedItems( QRectF& bRect );
706 
708  void loadDefaults();
709 
711  void loadSettings();
712 
714  QPointF minPointFromXml( const QDomElement& elem ) const;
715 
716  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
717 
718  void updatePaperItems();
719  void addPaperItem();
720  void removePaperItems();
721  void deleteAndRemoveMultiFrames();
722 
723  static QString encodeStringForXML( const QString& str );
724 
725  //tries to return the current QGraphicsView attached to the composition
726  QGraphicsView* graphicsView() const;
727 
728  /*Recalculates the page size using data defined page settings*/
729  void refreshPageSize();
730 
738  bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue,
739  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
740 
748  QVariant dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields,
749  QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties );
750 
751 
757  void prepareDataDefinedExpression( QgsDataDefined *dd, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties ) const;
758 
759  private slots:
760  /*Prepares all data defined expressions*/
761  void prepareAllDataDefinedExpressions();
762 
763  signals:
764  void paperSizeChanged();
765  void nPagesChanged();
766 
768  void printResolutionChanged();
769 
771  void selectedItemChanged( QgsComposerItem* selected );
773  void composerArrowAdded( QgsComposerArrow* arrow );
775  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
777  void composerLabelAdded( QgsComposerLabel* label );
779  void composerMapAdded( QgsComposerMap* map );
781  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
783  void composerLegendAdded( QgsComposerLegend* legend );
785  void composerPictureAdded( QgsComposerPicture* picture );
787  void composerShapeAdded( QgsComposerShape* shape );
789  void composerTableAdded( QgsComposerAttributeTable* table );
791  void itemRemoved( QgsComposerItem* );
792 
794  void refreshItemsTriggered();
795 
797  void statusMsgChanged( QString message );
798 
799  friend class QgsComposerObject; //for accessing dataDefinedEvaluate, readDataDefinedPropertyMap and writeDataDefinedPropertyMap
800 };
801 
802 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
803 {
804  itemList.clear();
805  QList<QGraphicsItem *> graphicsItemList = items();
806  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
807  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
808  {
809  T* item = dynamic_cast<T*>( *itemIt );
810  if ( item )
811  {
812  itemList.push_back( item );
813  }
814  }
815 }
816 
817 #endif
818 
819 
820 
void setSnapTolerance(const int snapTolerance)
Sets the snap tolerance to use when automatically snapping items during movement and resizing to guid...
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.
QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined * > mDataDefinedProperties
Map of current data defined properties to QgsDataDefined for the composition.
bool snapLinesVisible() const
A base class for objects which belong to a map composition.
QgsFillSymbolV2 * pageStyleSymbol()
Note: added in version 2.1.
void setPrintAsRaster(const bool enabled)
A container class for data source field mapping or expression.
An item that draws an arrow between to points.
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
GridStyle
Style to draw the snapping grid.
void setPlotStyle(const QgsComposition::PlotStyle style)
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.
Q_DECL_DEPRECATED double snapGridTolerance() const
Returns the snap tolerance to use when automatically snapping items during movement and resizing to t...
QUndoStack * mUndoStack
bool preventCursorChange() const
Container of fields for a vector layer.
Definition: qgsfield.h:163
bool mPrintAsRaster
Flag if map should be printed as a raster (via QImage).
GridStyle mGridStyle
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
QList< QgsPaperItem * > mPages
A non GUI class for rendering a map layer set onto a QPainter.
double spaceBetweenPages() const
bool alignmentSnap() const
DataDefinedProperty
Data defined properties for different item types.
A composer class that displays svg files or raster format (jpg, png, ...)
The QgsMapSettings class contains configuration for rendering of the map.
Q_DECL_DEPRECATED void sortZList()
Sorts the zList.
void setPreventCursorChange(const bool preventChange)
If true, prevents any mouse cursor changes by the composition or by any composer items Used by QgsCom...
bool mGenerateWorldFile
Flag if a world file should be generated on raster export.
int printResolution() const
QSet< QgsComposerMultiFrame * > mMultiFrames
List multiframe objects.
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
void setAlignmentSnap(const bool s)
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
void setGenerateWorldFile(const bool enabled)
GridStyle gridStyle() const
double mSpaceBetweenPages
A model for items attached to a composition.
QString file
Definition: qgssvgcache.cpp:76
bool mSnapToGrid
Parameters for snap to grid function.
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
Q_DECL_DEPRECATED void setSnapGridTolerance(double tolerance)
Sets the snap tolerance to use when automatically snapping items during movement and resizing to the ...
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.
int snapTolerance() const
Returns the snap tolerance to use when automatically snapping items during movement and resizing to g...
A composer items that draws common shapes (ellipse, triangle, rectangle)
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)
Q_DECL_DEPRECATED double alignmentSnapTolerance() const
Returns the snap tolerance to use when automatically snapping items during movement and resizing to g...
QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames
Map of data defined properties for the composition to string name to use when exporting composition t...
QgsComposerMultiFrameCommand * mActiveMultiFrameCommand
QgsAtlasComposition mAtlasComposition
The atlas composition object.
void setSmartGuidesEnabled(const bool b)
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()
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.
Q_DECL_DEPRECATED void setAlignmentSnapTolerance(double t)
Sets the snap tolerance to use when automatically snapping items during movement and resizing to guid...
QgsComposerModel * mItemsModel
QgsFillSymbolV2 * mPageStyleSymbol
Drawing style for page.
QList< QGraphicsLineItem * > * snapLines()
Returns pointer to snap lines collection.