QGIS API Documentation  master-6227475
src/core/composer/qgscomposeritem.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgscomposeritem.h
00003                              -------------------
00004     begin                : January 2005
00005     copyright            : (C) 2005 by Radim Blazek
00006     email                : blazek@itc.it
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #ifndef QGSCOMPOSERITEM_H
00018 #define QGSCOMPOSERITEM_H
00019 
00020 #include "qgscomposeritemcommand.h"
00021 #include "qgscomposereffect.h"
00022 #include "qgsmaprenderer.h" // for blend mode functions & enums
00023 #include <QGraphicsRectItem>
00024 #include <QObject>
00025 
00026 class QgsComposition;
00027 class QWidget;
00028 class QDomDocument;
00029 class QDomElement;
00030 class QGraphicsLineItem;
00031 
00035 class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
00036 {
00037     Q_OBJECT
00038   public:
00039 
00040     enum ItemType
00041     {
00042       // base class for the items
00043       ComposerItem = UserType + 100,
00044 
00045       // derived classes
00046       ComposerArrow,
00047       ComposerItemGroup,
00048       ComposerLabel,
00049       ComposerLegend,
00050       ComposerMap,
00051       ComposerPaper,  // QgsPaperItem
00052       ComposerPicture,
00053       ComposerScaleBar,
00054       ComposerShape,
00055       ComposerTable,
00056       ComposerAttributeTable,
00057       ComposerTextTable,
00058       ComposerFrame
00059     };
00060 
00062     enum MouseMoveAction
00063     {
00064       MoveItem,
00065       ResizeUp,
00066       ResizeDown,
00067       ResizeLeft,
00068       ResizeRight,
00069       ResizeLeftUp,
00070       ResizeRightUp,
00071       ResizeLeftDown,
00072       ResizeRightDown,
00073       NoAction
00074     };
00075 
00076     enum ItemPositionMode
00077     {
00078       UpperLeft,
00079       UpperMiddle,
00080       UpperRight,
00081       MiddleLeft,
00082       Middle,
00083       MiddleRight,
00084       LowerLeft,
00085       LowerMiddle,
00086       LowerRight
00087     };
00088 
00092     QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
00100     QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
00101     virtual ~QgsComposerItem();
00102 
00104     virtual int type() const { return ComposerItem; }
00105 
00107     virtual void setSelected( bool s );
00108 
00110     virtual bool selected() {return QGraphicsRectItem::isSelected();}
00111 
00113     virtual bool writeSettings();
00114 
00116     virtual bool readSettings();
00117 
00119     virtual bool removeSettings();
00120 
00122     void move( double dx, double dy );
00123 
00127     virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
00128 
00133     virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
00134 
00136     void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );
00137 
00140     void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft );
00141 
00145     ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
00146 
00149     virtual void setSceneRect( const QRectF& rectangle );
00150 
00155     virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
00156 
00158     bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
00159 
00164     virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
00165 
00167     bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
00168 
00174     bool hasFrame() const {return mFrame;}
00175 
00182     void setFrameEnabled( bool drawFrame ) {mFrame = drawFrame;}
00183 
00184 
00190     bool hasBackground() const {return mBackground;}
00191 
00198     void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
00199 
00204     QColor backgroundColor() const { return mBackgroundColor; }
00205 
00211     void setBackgroundColor( const QColor& backgroundColor );
00212 
00214     QPainter::CompositionMode blendMode() const {return mBlendMode;}
00215 
00217     void setBlendMode( QPainter::CompositionMode blendMode );
00218 
00220     int transparency() const {return mTransparency;}
00222     void setTransparency( int transparency );
00223 
00227     bool effectsEnabled() const {return mEffectsEnabled;}
00231     void setEffectsEnabled( bool effectsEnabled );
00232 
00234     virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
00235     virtual void removeItems() {}
00236 
00237     const QgsComposition* composition() const {return mComposition;}
00238     QgsComposition* composition() {return mComposition;}
00239 
00240     virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
00241 
00245     void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
00246 
00247     virtual void endItemCommand() { endCommand(); }
00249     void endCommand();
00250     void cancelCommand();
00251 
00252     //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
00253     //painter down by the same factor for drawing
00254 
00257     void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
00258 
00260     void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop ) const;
00261 
00263     double textWidthMillimeters( const QFont& font, const QString& text ) const;
00264 
00267     double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
00268 
00270     double fontAscentMillimeters( const QFont& font ) const;
00271 
00273     double fontDescentMillimeters( const QFont& font ) const;
00274 
00276     double pixelFontSize( double pointSize ) const;
00277 
00279     QFont scaledFontPixelSize( const QFont& font ) const;
00280 
00283     void setPositionLock( bool lock ) {mItemPositionLocked = lock;}
00284 
00287     bool positionLock() const {return mItemPositionLocked;}
00288 
00291     void updateCursor( const QPointF& itemPos );
00292 
00293     double rotation() const {return mRotation;}
00294 
00296     virtual void updateItem() { QGraphicsRectItem::update(); }
00297 
00300     QString id() const { return mId; }
00301 
00304     virtual void setId( const QString& id );
00305 
00309     QString uuid() const { return mUuid; }
00310 
00311   public slots:
00312     virtual void setRotation( double r );
00313     void repaint();
00314 
00315   protected:
00316 
00317     QgsComposition* mComposition;
00318 
00319     QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction;
00321     QPointF mMouseMoveStartPos;
00323     QPointF mLastMouseEventPos;
00324 
00326     QGraphicsRectItem* mBoundingResizeRectangle;
00327     QGraphicsLineItem* mHAlignSnapItem;
00328     QGraphicsLineItem* mVAlignSnapItem;
00329 
00331     bool mFrame;
00333     bool mBackground;
00335     QColor mBackgroundColor;
00336 
00339     bool mItemPositionLocked;
00340 
00342     mutable double mLastValidViewScaleFactor;
00343 
00345     double mRotation;
00346 
00348     QPainter::CompositionMode mBlendMode;
00349     bool mEffectsEnabled;
00350     QgsComposerEffect *mEffect;
00351 
00353     int mTransparency;
00354 
00357     ItemPositionMode mLastUsedPositionMode;
00358 
00359     //event handlers
00360     virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
00361     virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
00362     virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * event );
00363 
00364     virtual void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
00365 
00367     Qt::CursorShape cursorForPosition( const QPointF& itemCoordPos );
00368 
00370     QgsComposerItem::MouseMoveAction mouseMoveActionForPosition( const QPointF& itemCoordPos );
00371 
00380     void changeItemRectangle( const QPointF& currentPosition, const QPointF& mouseMoveStartPos, const QGraphicsRectItem* originalItem, double dx, double dy, QGraphicsRectItem* changeItem );
00381 
00383     virtual void drawSelectionBoxes( QPainter* p );
00384 
00386     virtual void drawFrame( QPainter* p );
00387 
00389     virtual void drawBackground( QPainter* p );
00390 
00392     void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
00393 
00395     double angle( const QPointF& p1, const QPointF& p2 ) const;
00396 
00399     double rectHandlerBorderTolerance() const;
00400 
00403     double lockSymbolSize() const;
00404 
00408     double horizontalViewScaleFactor() const;
00409 
00410     //some utility functions
00411 
00413     bool imageSizeConsideringRotation( double& width, double& height ) const;
00415     bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
00416 
00418     void sizeChangedByRotation( double& width, double& height );
00423     void rotate( double angle, double& x, double& y ) const;
00424 
00426     QGraphicsLineItem* hAlignSnapItem();
00427     void deleteHAlignSnapItem();
00429     QGraphicsLineItem* vAlignSnapItem();
00430     void deleteVAlignSnapItem();
00431     void deleteAlignItems();
00432 
00433   signals:
00435     void rotationChanged( double newRotation );
00437     void itemChanged();
00439     void sizeChanged();
00440   private:
00441     // id (not unique)
00442     QString mId;
00443     // name (unique)
00444     QString mUuid;
00445 
00446     void init( bool manageZValue );
00447 };
00448 
00449 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines