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"
33 #include "qgsatlascomposition.h"
34 #include "qgspaperitem.h"
35 #include "qgscomposerobject.h"
36 #include "qgscomposeritem.h"
37 
38 class QgisApp;
39 class QgsComposerFrame;
40 class QgsComposerMap;
41 class QGraphicsRectItem;
42 class QgsMapRenderer;
43 class QDomElement;
44 class QgsComposerArrow;
46 class QgsComposerHtml;
47 class QgsComposerItem;
48 class QgsComposerLabel;
49 class QgsComposerLegend;
50 class QgsComposerMap;
51 class QgsComposerPicture;
53 class QgsComposerShape;
57 class QgsVectorLayer;
58 class QgsComposer;
59 class QgsFillSymbolV2;
60 class QgsDataDefined;
61 
68 class CORE_EXPORT QgsComposition : public QGraphicsScene
69 {
70  Q_OBJECT
71  public:
72 
74  enum PlotStyle
75  {
76  Preview = 0, // Use cache etc
77  Print, // Render well
78  Postscript // Fonts need different scaling!
79  };
80 
82  enum GridStyle
83  {
86  Crosses
87  };
88 
90  {
92  ZValueAbove
93  };
94 
96  {
98  Landscape
99  };
100 
102  Q_DECL_DEPRECATED QgsComposition( QgsMapRenderer* mapRenderer );
103  explicit QgsComposition( const QgsMapSettings& mapSettings );
104 
107  {
108  AtlasOff, // Composition is not being controlled by an atlas
109  PreviewAtlas, // An atlas composition is being previewed in the app
110  ExportAtlas // The composition is being exported as an atlas
111  };
112 
113  ~QgsComposition();
114 
118  void setPaperSize( const double width, const double height );
119 
121  double paperHeight() const;
122 
124  double paperWidth() const;
125 
126  double spaceBetweenPages() const { return mSpaceBetweenPages; }
127 
129  void setNumPages( const int pages );
131  int numPages() const;
132 
134  void setPageStyleSymbol( QgsFillSymbolV2* symbol );
136  QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }
137 
141  QPointF positionOnPage( const QPointF & position ) const;
142 
146  int pageNumberForPoint( const QPointF & position ) const;
147 
151  void setStatusMessage( const QString & message );
152 
155  void updateSettings();
156 
157  void setSnapToGridEnabled( const bool b );
158  bool snapToGridEnabled() const {return mSnapToGrid;}
159 
160  void setGridVisible( const bool b );
161  bool gridVisible() const {return mGridVisible;}
162 
164  void setSnapLinesVisible( const bool visible );
165  bool snapLinesVisible() const {return mGuidesVisible;}
166 
167  void setAlignmentSnap( const bool s ) { mAlignmentSnap = s; }
168  bool alignmentSnap() const { return mAlignmentSnap; }
169 
170  void setSmartGuidesEnabled( const bool b ) { mSmartGuides = b; }
171  bool smartGuidesEnabled() const {return mSmartGuides;}
172 
174  void clearSnapLines();
175 
176  void setSnapGridResolution( const double r );
177  double snapGridResolution() const {return mSnapGridResolution;}
178 
179  void setSnapGridOffsetX( const double offset );
180  double snapGridOffsetX() const {return mSnapGridOffsetX;}
181 
182  void setSnapGridOffsetY( const double offset );
183  double snapGridOffsetY() const {return mSnapGridOffsetY;}
184 
185  void setGridPen( const QPen& p );
186  const QPen& gridPen() const {return mGridPen;}
187 
188  void setGridStyle( const GridStyle s );
189  GridStyle gridStyle() const {return mGridStyle;}
190 
197  Q_DECL_DEPRECATED void setSnapGridTolerance( double tolerance ) { mSnapTolerance = tolerance; }
198 
205  Q_DECL_DEPRECATED double snapGridTolerance() const {return mSnapTolerance;}
206 
213  Q_DECL_DEPRECATED void setAlignmentSnapTolerance( double t ) { mSnapTolerance = t; }
214 
221  Q_DECL_DEPRECATED double alignmentSnapTolerance() const { return mSnapTolerance; }
222 
229  void setSnapTolerance( const int snapTolerance ) { mSnapTolerance = snapTolerance; }
230 
237  int snapTolerance() const { return mSnapTolerance; }
238 
240  QUndoStack* undoStack() { return &mUndoStack; }
241 
243  QgsComposerItem* composerItemAt( const QPointF & position ) const;
244 
248  QgsComposerItem* composerItemAt( const QPointF & position, const QgsComposerItem* belowItem ) const;
249 
251  int pageNumberAt( const QPointF& position ) const;
252 
254  int itemPageNumber( const QgsComposerItem* ) const;
255 
256  QList<QgsComposerItem*> selectedComposerItems();
257 
261  QList<const QgsComposerMap*> composerMapItems() const;
262 
266  template<class T> void composerItems( QList<T*>& itemList );
267 
270  const QgsComposerMap* getComposerMapById( const int id ) const;
271 
278  Q_DECL_DEPRECATED const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
279 
287  const QgsComposerItem* getComposerItemById( const QString theId ) const;
288 
293  const QgsComposerItem* getComposerItemByUuid( const QString theUuid ) const;
294 
295  int printResolution() const {return mPrintResolution;}
296  void setPrintResolution( const int dpi );
297 
298  bool printAsRaster() const {return mPrintAsRaster;}
299  void setPrintAsRaster( const bool enabled ) { mPrintAsRaster = enabled; }
300 
301  bool generateWorldFile() const { return mGenerateWorldFile; }
302  void setGenerateWorldFile( const bool enabled ) { mGenerateWorldFile = enabled; }
303 
304  QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
305  void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
306 
309  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
312  void setUseAdvancedEffects( const bool effectsEnabled );
313 
315  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer() {return mMapRenderer;}
317 
320  const QgsMapSettings& mapSettings() const { return mMapSettings; }
321 
322  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
323  void setPlotStyle( const QgsComposition::PlotStyle style ) {mPlotStyle = style;}
324 
329  Q_DECL_DEPRECATED int pixelFontSize( double pointSize ) const;
330 
334  Q_DECL_DEPRECATED double pointFontSize( int pixelSize ) const;
335 
337  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
338 
340  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
341 
347  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0, bool addUndoCommands = false );
348 
358  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
359  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
360 
362  void addItemToZList( QgsComposerItem* item );
364  void removeItemFromZList( QgsComposerItem* item );
365 
366  //functions to move selected items in hierarchy
367  void raiseSelectedItems();
368  void raiseItem( QgsComposerItem* item );
369  void lowerSelectedItems();
370  void lowerItem( QgsComposerItem* item );
371  void moveSelectedItemsToTop();
372  void moveItemToTop( QgsComposerItem* item );
373  void moveSelectedItemsToBottom();
374  void moveItemToBottom( QgsComposerItem* item );
375 
376  //functions to find items by their position in the z list
377  void selectNextByZOrder( const ZValueDirection direction );
378  QgsComposerItem* getComposerItemBelow( QgsComposerItem* item ) const;
379  QgsComposerItem* getComposerItemAbove( QgsComposerItem* item ) const;
380 
381  //functions to align selected items
382  void alignSelectedItemsLeft();
383  void alignSelectedItemsHCenter();
384  void alignSelectedItemsRight();
385  void alignSelectedItemsTop();
386  void alignSelectedItemsVCenter();
387  void alignSelectedItemsBottom();
388 
389  //functions to lock and unlock items
391  void lockSelectedItems();
393  void unlockAllItems();
394 
397  void sortZList();
398 
400  void refreshZList();
401 
403  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
404 
406  QList< QGraphicsLineItem* >* snapLines() {return &mSnapLines;}
407 
411  QgsComposerMouseHandles* selectionHandles() {return mSelectionHandles;}
412 
414  QGraphicsLineItem* addSnapLine();
416  void removeSnapLine( QGraphicsLineItem* line );
420  QGraphicsLineItem* nearestSnapLine( const bool horizontal, const double x, const double y, const double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems ) const;
421 
426  void beginCommand( QgsComposerItem* item, const QString& commandText, const QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
427 
429  void endCommand();
431  void cancelCommand();
432 
433  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
434  void endMultiFrameCommand();
435 
437  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
439  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
442  void addComposerArrow( QgsComposerArrow* arrow );
444  void addComposerLabel( QgsComposerLabel* label );
446  void addComposerMap( QgsComposerMap* map, const bool setDefaultPreviewStyle = true );
448  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
450  void addComposerLegend( QgsComposerLegend* legend );
452  void addComposerPicture( QgsComposerPicture* picture );
454  void addComposerShape( QgsComposerShape* shape );
456  void addComposerTable( QgsComposerAttributeTable* table );
458  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
459 
461  void removeComposerItem( QgsComposerItem* item, const bool createCommand = true );
462 
464  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, const QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
465 
468  void setPreventCursorChange( const bool preventChange ) { mPreventCursorChange = preventChange; }
469  bool preventCursorChange() const { return mPreventCursorChange; }
470 
471  //printing
472 
474  void beginPrint( QPrinter& printer );
476  void beginPrintAsPDF( QPrinter& printer, const QString& file );
477 
483  void doPrint( QPrinter& printer, QPainter& painter, bool startNewPage = false );
484 
488  bool print( QPrinter &printer );
489 
493  bool exportAsPDF( const QString& file );
494 
497  QImage printPageAsRaster( int page );
498 
501  void renderPage( QPainter* p, int page );
502 
504  void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;
505 
506  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
507 
511  Q_DECL_DEPRECATED static void relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter );
512 
516  Q_DECL_DEPRECATED static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
517 
522  QgsComposition::AtlasMode atlasMode() const { return mAtlasMode; }
523 
529  bool setAtlasMode( const QgsComposition::AtlasMode mode );
530 
534  QList< QgsPaperItem* > pages() { return mPages; }
535 
540  QgsDataDefined* dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property );
541 
550  void setDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, bool active, bool useExpression, const QString &expression, const QString &field );
551 
552  public slots:
554  void sendItemAddedSignal( QgsComposerItem* item );
555 
558  void updateBounds();
559 
565  void refreshItems();
566 
570  void setSelectedItem( QgsComposerItem* item );
571 
579  void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties );
580 
581  protected:
582  void init();
583 
584 
585  private:
589 
591  double mPageWidth;
592  double mPageHeight;
593  QList< QgsPaperItem* > mPages;
594  double mSpaceBetweenPages; //space in preview between pages
595 
598  void createDefaultPageStyleSymbol();
599 
601  QLinkedList<QgsComposerItem*> mItemZList;
602 
604  QSet<QgsComposerMultiFrame*> mMultiFrames;
605 
608 
611 
616 
619 
626  QPen mGridPen;
628 
634 
636  QList< QGraphicsLineItem* > mSnapLines;
637 
639 
640  QUndoStack mUndoStack;
641 
644 
647 
649 
651 
653  QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames;
655  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;
656 
657  QgsComposition(); //default constructor is forbidden
658 
660  QRectF compositionBounds() const;
661 
663  void updateZValues( const bool addUndoCommands = true );
664 
667  int boundingRectOfSelectedItems( QRectF& bRect );
668 
670  void loadDefaults();
671 
673  void loadSettings();
674 
676  QPointF minPointFromXml( const QDomElement& elem ) const;
677 
678  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
679 
680  void updatePaperItems();
681  void addPaperItem();
682  void removePaperItems();
683  void deleteAndRemoveMultiFrames();
684 
685  static QString encodeStringForXML( const QString& str );
686 
687  //tries to return the current QGraphicsView attached to the composition
688  QGraphicsView* graphicsView() const;
689 
690  /*Recalculates the page size using data defined page settings*/
691  void refreshPageSize();
692 
700  bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue,
701  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
702 
710  QVariant dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields *fields,
711  QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties );
712 
713 
719  void prepareDataDefinedExpression( QgsDataDefined *dd, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties ) const;
720 
721  private slots:
722  /*Prepares all data defined expressions*/
723  void prepareAllDataDefinedExpressions();
724 
725  signals:
726  void paperSizeChanged();
727  void nPagesChanged();
728 
730  void printResolutionChanged();
731 
733  void selectedItemChanged( QgsComposerItem* selected );
735  void composerArrowAdded( QgsComposerArrow* arrow );
737  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
739  void composerLabelAdded( QgsComposerLabel* label );
741  void composerMapAdded( QgsComposerMap* map );
743  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
745  void composerLegendAdded( QgsComposerLegend* legend );
747  void composerPictureAdded( QgsComposerPicture* picture );
749  void composerShapeAdded( QgsComposerShape* shape );
751  void composerTableAdded( QgsComposerAttributeTable* table );
753  void itemRemoved( QgsComposerItem* );
754 
756  void refreshItemsTriggered();
757 
759  void statusMsgChanged( QString message );
760 
761  friend class QgsComposerObject; //for accessing dataDefinedEvaluate, readDataDefinedPropertyMap and writeDataDefinedPropertyMap
762 };
763 
764 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
765 {
766  itemList.clear();
767  QList<QGraphicsItem *> graphicsItemList = items();
768  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
769  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
770  {
771  T* item = dynamic_cast<T*>( *itemIt );
772  if ( item )
773  {
774  itemList.push_back( item );
775  }
776  }
777 }
778 
779 #endif
780 
781 
782 
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.
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...
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.
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.
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
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
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)
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()
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...
QgsFillSymbolV2 * mPageStyleSymbol
Drawing style for page.
QList< QGraphicsLineItem * > * snapLines()
Returns pointer to snap lines collection.