QGIS API Documentation  master-6227475
src/core/composer/qgscomposition.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                               qgscomposition.h
00003                              -------------------
00004     begin                : January 2005
00005     copyright            : (C) 2005 by Radim Blazek
00006     email                : blazek@itc.it
00007  ***************************************************************************/
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef QGSCOMPOSITION_H
00017 #define QGSCOMPOSITION_H
00018 
00019 #include "qgscomposeritem.h"
00020 #include <memory>
00021 
00022 #include <QDomDocument>
00023 #include <QGraphicsScene>
00024 #include <QLinkedList>
00025 #include <QList>
00026 #include <QPair>
00027 #include <QSet>
00028 #include <QUndoStack>
00029 #include <QPrinter>
00030 #include <QPainter>
00031 
00032 #include "qgsaddremoveitemcommand.h"
00033 #include "qgscomposeritemcommand.h"
00034 #include "qgsatlascomposition.h"
00035 
00036 class QgisApp;
00037 class QgsComposerFrame;
00038 class QgsComposerMap;
00039 class QgsPaperItem;
00040 class QGraphicsRectItem;
00041 class QgsMapRenderer;
00042 class QDomElement;
00043 class QgsComposerArrow;
00044 class QgsComposerHtml;
00045 class QgsComposerItem;
00046 class QgsComposerLabel;
00047 class QgsComposerLegend;
00048 class QgsComposerMap;
00049 class QgsComposerPicture;
00050 class QgsComposerScaleBar;
00051 class QgsComposerShape;
00052 class QgsComposerAttributeTable;
00053 class QgsComposerMultiFrame;
00054 class QgsComposerMultiFrameCommand;
00055 class QgsVectorLayer;
00056 class QgsComposer;
00057 
00064 class CORE_EXPORT QgsComposition: public QGraphicsScene
00065 {
00066     Q_OBJECT
00067   public:
00068 
00070     enum PlotStyle
00071     {
00072       Preview = 0, // Use cache etc
00073       Print,       // Render well
00074       Postscript   // Fonts need different scaling!
00075     };
00076 
00078     enum GridStyle
00079     {
00080       Solid,
00081       Dots,
00082       Crosses
00083     };
00084 
00085     QgsComposition( QgsMapRenderer* mapRenderer );
00086     ~QgsComposition();
00087 
00089     void setPaperSize( double width, double height );
00090 
00092     double paperHeight() const;
00093 
00095     double paperWidth() const;
00096 
00097     double spaceBetweenPages() const { return mSpaceBetweenPages; }
00098 
00100     void setNumPages( int pages );
00102     int numPages() const;
00103 
00104     void setSnapToGridEnabled( bool b );
00105     bool snapToGridEnabled() const {return mSnapToGrid;}
00106 
00107     void setSnapGridResolution( double r );
00108     double snapGridResolution() const {return mSnapGridResolution;}
00109 
00110     void setSnapGridOffsetX( double offset );
00111     double snapGridOffsetX() const {return mSnapGridOffsetX;}
00112 
00113     void setSnapGridOffsetY( double offset );
00114     double snapGridOffsetY() const {return mSnapGridOffsetY;}
00115 
00116     void setGridPen( const QPen& p );
00117     const QPen& gridPen() const {return mGridPen;}
00118 
00119     void setGridStyle( GridStyle s );
00120     GridStyle gridStyle() const {return mGridStyle;}
00121 
00122     void setAlignmentSnap( bool s ) { mAlignmentSnap = s; }
00123     bool alignmentSnap() const { return mAlignmentSnap; }
00124 
00125     void setAlignmentSnapTolerance( double t ) { mAlignmentSnapTolerance = t; }
00126     double alignmentSnapTolerance() const { return mAlignmentSnapTolerance; }
00127 
00129     QUndoStack* undoStack() { return &mUndoStack; }
00130 
00132     QgsComposerItem* composerItemAt( const QPointF & position );
00133 
00135     int pageNumberAt( const QPointF& position ) const;
00136 
00138     int itemPageNumber( const QgsComposerItem* ) const;
00139 
00140     QList<QgsComposerItem*> selectedComposerItems();
00141 
00145     QList<const QgsComposerMap*> composerMapItems() const;
00146 
00150     template<class T> void composerItems( QList<T*>& itemList );
00151 
00154     const QgsComposerMap* getComposerMapById( int id ) const;
00155 
00156     /*Returns the composer html with specified id (a string as named in the
00157       composer user interface item properties).
00158       @note Added in QGIS 2.0
00159       @param id - A QString representing the id of the item.
00160       @return QgsComposerHtml pointer or 0 pointer if no such item exists.
00161     */
00162     const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
00163 
00171     const QgsComposerItem* getComposerItemById( QString theId ) const;
00172 
00177     const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;
00178 
00179     int printResolution() const {return mPrintResolution;}
00180     void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
00181 
00182     bool printAsRaster() const {return mPrintAsRaster;}
00183     void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
00184 
00187     bool useAdvancedEffects() const {return mUseAdvancedEffects;}
00190     void setUseAdvancedEffects( bool effectsEnabled );
00191 
00192     double selectionTolerance() const { return mSelectionTolerance; }
00193     void setSelectionTolerance( double tol );
00194 
00196     QgsMapRenderer* mapRenderer() {return mMapRenderer;}
00197 
00198     QgsComposition::PlotStyle plotStyle() const {return mPlotStyle;}
00199     void setPlotStyle( QgsComposition::PlotStyle style ) {mPlotStyle = style;}
00200 
00204     int pixelFontSize( double pointSize ) const;
00205 
00207     double pointFontSize( int pixelSize ) const;
00208 
00210     bool writeXML( QDomElement& composerElem, QDomDocument& doc );
00211 
00213     bool readXML( const QDomElement& compositionElem, const QDomDocument& doc );
00214 
00220     bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0, bool addUndoCommands = false );
00221 
00231     void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
00232                           bool addUndoCommands = false, QPointF* pos = 0, bool pasteInPlace = false );
00233 
00235     void addItemToZList( QgsComposerItem* item );
00237     void removeItemFromZList( QgsComposerItem* item );
00238 
00239     //functions to move selected items in hierarchy
00240     void raiseSelectedItems();
00241     void raiseItem( QgsComposerItem* item );
00242     void lowerSelectedItems();
00243     void lowerItem( QgsComposerItem* item );
00244     void moveSelectedItemsToTop();
00245     void moveItemToTop( QgsComposerItem* item );
00246     void moveSelectedItemsToBottom();
00247     void moveItemToBottom( QgsComposerItem* item );
00248 
00249     //functions to align selected items
00250     void alignSelectedItemsLeft();
00251     void alignSelectedItemsHCenter();
00252     void alignSelectedItemsRight();
00253     void alignSelectedItemsTop();
00254     void alignSelectedItemsVCenter();
00255     void alignSelectedItemsBottom();
00256 
00259     void sortZList();
00260 
00262     QPointF snapPointToGrid( const QPointF& scenePoint ) const;
00263 
00271     QPointF alignItem( const QgsComposerItem* item, double& alignX, double& alignY, double dx = 0, double dy = 0 );
00272 
00279     QPointF alignPos( const QPointF& pos, const QgsComposerItem* excludeItem, double& alignX, double& alignY );
00280 
00282     QGraphicsLineItem* addSnapLine();
00284     void removeSnapLine( QGraphicsLineItem* line );
00286     QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
00288     void setSnapLinesVisible( bool visible );
00289 
00294     void beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
00295 
00297     void endCommand();
00299     void cancelCommand();
00300 
00301     void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
00302     void endMultiFrameCommand();
00303 
00305     void addMultiFrame( QgsComposerMultiFrame* multiFrame );
00307     void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
00310     void addComposerArrow( QgsComposerArrow* arrow );
00312     void addComposerLabel( QgsComposerLabel* label );
00314     void addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle = true );
00316     void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
00318     void addComposerLegend( QgsComposerLegend* legend );
00320     void addComposerPicture( QgsComposerPicture* picture );
00322     void addComposerShape( QgsComposerShape* shape );
00324     void addComposerTable( QgsComposerAttributeTable* table );
00326     void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );
00327 
00329     void removeComposerItem( QgsComposerItem* item, bool createCommand = true );
00330 
00332     void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
00333 
00334 
00335     //printing
00336 
00338     void beginPrint( QPrinter& printer );
00340     void beginPrintAsPDF( QPrinter& printer, const QString& file );
00342     void doPrint( QPrinter& printer, QPainter& painter );
00343 
00345     void print( QPrinter &printer );
00346 
00348     void exportAsPDF( const QString& file );
00349 
00352     QImage printPageAsRaster( int page );
00353 
00356     void renderPage( QPainter* p, int page );
00357 
00358     QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }
00359 
00360   public slots:
00362     void sendItemAddedSignal( QgsComposerItem* item );
00363 
00364   private:
00366     QgsMapRenderer* mMapRenderer;
00367     QgsComposition::PlotStyle mPlotStyle;
00368     double mPageWidth;
00369     double mPageHeight;
00370     QList< QgsPaperItem* > mPages;
00371     double mSpaceBetweenPages; //space in preview between pages
00372 
00374     QLinkedList<QgsComposerItem*> mItemZList;
00375 
00377     QSet<QgsComposerMultiFrame*> mMultiFrames;
00378 
00380     int mPrintResolution;
00381 
00383     bool mPrintAsRaster;
00384 
00386     bool mUseAdvancedEffects;
00387 
00389     double mSelectionTolerance;
00390 
00392     bool mSnapToGrid;
00393     double mSnapGridResolution;
00394     double mSnapGridOffsetX;
00395     double mSnapGridOffsetY;
00396     QPen mGridPen;
00397     GridStyle mGridStyle;
00398 
00400     bool mAlignmentSnap;
00401     double mAlignmentSnapTolerance;
00402 
00404     QList< QGraphicsLineItem* > mSnapLines;
00405 
00406     QUndoStack mUndoStack;
00407 
00408     QgsComposerItemCommand* mActiveItemCommand;
00409     QgsComposerMultiFrameCommand* mActiveMultiFrameCommand;
00410 
00412     QgsAtlasComposition mAtlasComposition;
00413 
00414     QgsComposition(); //default constructor is forbidden
00415 
00417     void updateZValues();
00418 
00421     int boundingRectOfSelectedItems( QRectF& bRect );
00422 
00423     void loadSettings();
00424     void saveSettings();
00425 
00426     void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
00427 
00428     void updatePaperItems();
00429     void addPaperItem();
00430     void removePaperItems();
00431     void deleteAndRemoveMultiFrames();
00432 
00433     static QString encodeStringForXML( const QString& str );
00434 
00435     //helper functions for item align
00436     void collectAlignCoordinates( QMap< double, const QgsComposerItem* >& alignCoordsX,
00437                                   QMap< double, const QgsComposerItem* >& alignCoordsY, const QgsComposerItem* excludeItem );
00438 
00439     void checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem* >& alignCoords, double& smallestDiff,
00440                            double itemCoordOffset, double& itemCoord, double& alignCoord ) const;
00441 
00444     static bool nearestItem( const QMap< double, const QgsComposerItem* >& coords, double value, double& nearestValue );
00445 
00446   signals:
00447     void paperSizeChanged();
00448     void nPagesChanged();
00449 
00451     void selectedItemChanged( QgsComposerItem* selected );
00453     void composerArrowAdded( QgsComposerArrow* arrow );
00455     void composerHtmlFrameAdded( QgsComposerHtml* html, QgsComposerFrame* frame );
00457     void composerLabelAdded( QgsComposerLabel* label );
00459     void composerMapAdded( QgsComposerMap* map );
00461     void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
00463     void composerLegendAdded( QgsComposerLegend* legend );
00465     void composerPictureAdded( QgsComposerPicture* picture );
00467     void composerShapeAdded( QgsComposerShape* shape );
00469     void composerTableAdded( QgsComposerAttributeTable* table );
00471     void itemRemoved( QgsComposerItem* );
00472 };
00473 
00474 template<class T> void QgsComposition::composerItems( QList<T*>& itemList )
00475 {
00476   itemList.clear();
00477   QList<QGraphicsItem *> graphicsItemList = items();
00478   QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
00479   for ( ; itemIt != graphicsItemList.end(); ++itemIt )
00480   {
00481     T* item = dynamic_cast<T*>( *itemIt );
00482     if ( item )
00483     {
00484       itemList.push_back( item );
00485     }
00486   }
00487 }
00488 
00489 #endif
00490 
00491 
00492 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines