QGIS API Documentation  2.0.1-Dufour
 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;
44 class QgsComposerHtml;
45 class QgsComposerItem;
46 class QgsComposerLabel;
47 class QgsComposerLegend;
48 class QgsComposerMap;
49 class QgsComposerPicture;
51 class QgsComposerShape;
55 class QgsVectorLayer;
56 class QgsComposer;
57 
64 class CORE_EXPORT QgsComposition : public QGraphicsScene
65 {
66  Q_OBJECT
67  public:
68 
70  enum PlotStyle
71  {
72  Preview = 0, // Use cache etc
73  Print, // Render well
74  Postscript // Fonts need different scaling!
75  };
76 
78  enum GridStyle
79  {
82  Crosses
83  };
84 
85  QgsComposition( QgsMapRenderer* mapRenderer );
86  ~QgsComposition();
87 
89  void setPaperSize( double width, double height );
90 
92  double paperHeight() const;
93 
95  double paperWidth() const;
96 
97  double spaceBetweenPages() const { return mSpaceBetweenPages; }
98 
100  void setNumPages( int pages );
102  int numPages() const;
103 
104  void setSnapToGridEnabled( bool b );
105  bool snapToGridEnabled() const {return mSnapToGrid;}
106 
107  void setSnapGridResolution( double r );
108  double snapGridResolution() const {return mSnapGridResolution;}
109 
110  void setSnapGridOffsetX( double offset );
111  double snapGridOffsetX() const {return mSnapGridOffsetX;}
112 
113  void setSnapGridOffsetY( double offset );
114  double snapGridOffsetY() const {return mSnapGridOffsetY;}
115 
116  void setGridPen( const QPen& p );
117  const QPen& gridPen() const {return mGridPen;}
118 
119  void setGridStyle( GridStyle s );
120  GridStyle gridStyle() const {return mGridStyle;}
121 
122  void setAlignmentSnap( bool s ) { mAlignmentSnap = s; }
123  bool alignmentSnap() const { return mAlignmentSnap; }
124 
125  void setAlignmentSnapTolerance( double t ) { mAlignmentSnapTolerance = t; }
126  double alignmentSnapTolerance() const { return mAlignmentSnapTolerance; }
127 
129  QUndoStack* undoStack() { return &mUndoStack; }
130 
132  QgsComposerItem* composerItemAt( const QPointF & position );
133 
135  int pageNumberAt( const QPointF& position ) const;
136 
138  int itemPageNumber( const QgsComposerItem* ) const;
139 
140  QList<QgsComposerItem*> selectedComposerItems();
141 
145  QList<const QgsComposerMap*> composerMapItems() const;
146 
150  template<class T> void composerItems( QList<T*>& itemList );
151 
154  const QgsComposerMap* getComposerMapById( int id ) const;
155 
156  /*Returns the composer html with specified id (a string as named in the
157  composer user interface item properties).
158  @note Added in QGIS 2.0
159  @param id - A QString representing the id of the item.
160  @return QgsComposerHtml pointer or 0 pointer if no such item exists.
161  */
162  const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
163 
171  const QgsComposerItem* getComposerItemById( QString theId ) const;
172 
177  const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;
178 
179  int printResolution() const {return mPrintResolution;}
180  void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
181 
182  bool printAsRaster() const {return mPrintAsRaster;}
183  void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
184 
187  bool useAdvancedEffects() const {return mUseAdvancedEffects;}
190  void setUseAdvancedEffects( bool effectsEnabled );
191 
192  double selectionTolerance() const { return mSelectionTolerance; }
193  void setSelectionTolerance( double tol );
194 
196  QgsMapRenderer* mapRenderer() {return mMapRenderer;}
197 
198  QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
199  void setPlotStyle( QgsComposition::PlotStyle style ) {mPlotStyle = style;}
200 
204  int pixelFontSize( double pointSize ) const;
205 
207  double pointFontSize( int pixelSize ) const;
208 
210  bool writeXML( QDomElement& composerElem, QDomDocument& doc );
211 
213  bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
214 
220  bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0, bool addUndoCommands = false );
221 
231  void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
232  bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
233 
235  void addItemToZList( QgsComposerItem* item );
237  void removeItemFromZList( QgsComposerItem* item );
238 
239  //functions to move selected items in hierarchy
240  void raiseSelectedItems();
241  void raiseItem( QgsComposerItem* item );
242  void lowerSelectedItems();
243  void lowerItem( QgsComposerItem* item );
244  void moveSelectedItemsToTop();
245  void moveItemToTop( QgsComposerItem* item );
246  void moveSelectedItemsToBottom();
247  void moveItemToBottom( QgsComposerItem* item );
248 
249  //functions to align selected items
250  void alignSelectedItemsLeft();
251  void alignSelectedItemsHCenter();
252  void alignSelectedItemsRight();
253  void alignSelectedItemsTop();
254  void alignSelectedItemsVCenter();
255  void alignSelectedItemsBottom();
256 
259  void sortZList();
260 
262  QPointF snapPointToGrid( const QPointF& scenePoint ) const;
263 
271  QPointF alignItem( const QgsComposerItem* item, double& alignX, double& alignY, double dx = 0, double dy = 0 );
272 
279  QPointF alignPos( const QPointF& pos, const QgsComposerItem* excludeItem, double& alignX, double& alignY );
280 
282  QGraphicsLineItem* addSnapLine();
284  void removeSnapLine( QGraphicsLineItem* line );
288  QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
290  void setSnapLinesVisible( bool visible );
291 
296  void beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
297 
299  void endCommand();
301  void cancelCommand();
302 
303  void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
304  void endMultiFrameCommand();
305 
307  void addMultiFrame( QgsComposerMultiFrame* multiFrame );
309  void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
312  void addComposerArrow( QgsComposerArrow* arrow );
314  void addComposerLabel( QgsComposerLabel* label );
316  void addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle = true );
318  void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
320  void addComposerLegend( QgsComposerLegend* legend );
322  void addComposerPicture( QgsComposerPicture* picture );
324  void addComposerShape( QgsComposerShape* shape );
326  void addComposerTable( QgsComposerAttributeTable* table );
328  void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
329 
331  void removeComposerItem( QgsComposerItem* item, bool createCommand = true );
332 
334  void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
335 
336 
337  //printing
338 
340  void beginPrint( QPrinter& printer );
342  void beginPrintAsPDF( QPrinter& printer, const QString& file );
344  void doPrint( QPrinter& printer, QPainter& painter );
345 
347  void print( QPrinter &printer );
348 
350  void exportAsPDF( const QString& file );
351 
354  QImage printPageAsRaster( int page );
355 
358  void renderPage( QPainter* p, int page );
359 
360  QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
361 
362  public slots:
364  void sendItemAddedSignal( QgsComposerItem* item );
365 
366  private:
370  double mPageWidth;
371  double mPageHeight;
372  QList< QgsPaperItem* > mPages;
373  double mSpaceBetweenPages; //space in preview between pages
374 
376  QLinkedList<QgsComposerItem*> mItemZList;
377 
379  QSet<QgsComposerMultiFrame*> mMultiFrames;
380 
383 
386 
389 
392 
398  QPen mGridPen;
400 
404 
406  QList< QGraphicsLineItem* > mSnapLines;
407 
408  QUndoStack mUndoStack;
409 
412 
415 
416  QgsComposition(); //default constructor is forbidden
417 
419  void updateZValues();
420 
423  int boundingRectOfSelectedItems( QRectF& bRect );
424 
425  void loadSettings();
426  void saveSettings();
427 
428  void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
429 
430  void updatePaperItems();
431  void addPaperItem();
432  void removePaperItems();
433  void deleteAndRemoveMultiFrames();
434 
435  static QString encodeStringForXML( const QString& str );
436 
437  //helper functions for item align
438  void collectAlignCoordinates( QMap< double, const QgsComposerItem* >& alignCoordsX,
439  QMap< double, const QgsComposerItem* >& alignCoordsY, const QgsComposerItem* excludeItem );
440 
441  void checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem* >& alignCoords, double& smallestDiff,
442  double itemCoordOffset, double& itemCoord, double& alignCoord ) const;
443 
446  static bool nearestItem( const QMap< double, const QgsComposerItem* >& coords, double value, double& nearestValue );
447 
448  signals:
449  void paperSizeChanged();
450  void nPagesChanged();
451 
453  void selectedItemChanged( QgsComposerItem* selected );
455  void composerArrowAdded( QgsComposerArrow* arrow );
457  void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
459  void composerLabelAdded( QgsComposerLabel* label );
461  void composerMapAdded( QgsComposerMap* map );
463  void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
465  void composerLegendAdded( QgsComposerLegend* legend );
467  void composerPictureAdded( QgsComposerPicture* picture );
469  void composerShapeAdded( QgsComposerShape* shape );
471  void composerTableAdded( QgsComposerAttributeTable* table );
473  void itemRemoved( QgsComposerItem* );
474 };
475 
476 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
477 {
478  itemList.clear();
479  QList<QGraphicsItem *> graphicsItemList = items();
480  QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
481  for ( ; itemIt != graphicsItemList.end(); ++itemIt )
482  {
483  T* item = dynamic_cast<T*>( *itemIt );
484  if ( item )
485  {
486  itemList.push_back( item );
487  }
488  }
489 }
490 
491 #endif
492 
493 
494