Quantum GIS API Documentation  1.8
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 "qgscomposition.h"
00021 #include <QGraphicsRectItem>
00022 #include <QObject>
00023 
00024 class QWidget;
00025 class QDomDocument;
00026 class QDomElement;
00027 
00028 class QqsComposition;
00029 
00033 class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
00034 {
00035     Q_OBJECT
00036   public:
00037 
00038     enum ItemType
00039     {
00040       // base class for the items
00041       ComposerItem = UserType + 100,
00042 
00043       // derived classes
00044       ComposerArrow,
00045       ComposerItemGroup,
00046       ComposerLabel,
00047       ComposerLegend,
00048       ComposerMap,
00049       ComposerPaper,  // QgsPaperItem
00050       ComposerPicture,
00051       ComposerScaleBar,
00052       ComposerShape,
00053       ComposerTable,
00054       ComposerAttributeTable,
00055       ComposerTextTable
00056     };
00057 
00059     enum MouseMoveAction
00060     {
00061       MoveItem,
00062       ResizeUp,
00063       ResizeDown,
00064       ResizeLeft,
00065       ResizeRight,
00066       ResizeLeftUp,
00067       ResizeRightUp,
00068       ResizeLeftDown,
00069       ResizeRightDown,
00070       NoAction
00071     };
00072 
00073     enum ItemPositionMode
00074     {
00075       UpperLeft,
00076       UpperMiddle,
00077       UpperRight,
00078       MiddleLeft,
00079       Middle,
00080       MiddleRight,
00081       LowerLeft,
00082       LowerMiddle,
00083       LowerRight
00084     };
00085 
00089     QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
00097     QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
00098     virtual ~QgsComposerItem();
00099 
00101     virtual int type() const { return ComposerItem; }
00102 
00104     virtual void setSelected( bool s );
00105 
00107     virtual bool selected( void ) {return QGraphicsRectItem::isSelected();}
00108 
00110     virtual bool writeSettings( void );
00111 
00113     virtual bool readSettings( void );
00114 
00116     virtual bool removeSettings( void );
00117 
00119     void move( double dx, double dy );
00120 
00124     virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
00125 
00130     virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
00131 
00133     void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );
00134 
00137     void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft );
00138 
00141     virtual void setSceneRect( const QRectF& rectangle );
00142 
00147     virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
00148 
00150     bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
00151 
00156     virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
00157 
00159     bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
00160 
00166     Q_DECL_DEPRECATED bool frame() const {return hasFrame();}
00171     bool hasFrame() const {return mFrame;}
00177     Q_DECL_DEPRECATED void setFrame( bool drawFrame ) { setFrameEnabled( drawFrame );}
00184     void setFrameEnabled( bool drawFrame ) {mFrame = drawFrame;}
00185 
00187     virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
00188     virtual void removeItems() {}
00189 
00190     const QgsComposition* composition() const {return mComposition;}
00191 
00195     void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
00197     void endCommand();
00198     void cancelCommand();
00199 
00200     //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
00201     //painter down by the same factor for drawing
00202 
00205     void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
00206 
00208     void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement = Qt::AlignLeft, Qt::AlignmentFlag valignement = Qt::AlignTop ) const;
00209 
00211     double textWidthMillimeters( const QFont& font, const QString& text ) const;
00212 
00215     double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
00216 
00218     double fontAscentMillimeters( const QFont& font ) const;
00219 
00221     double pixelFontSize( double pointSize ) const;
00222 
00224     QFont scaledFontPixelSize( const QFont& font ) const;
00225 
00228     void setPositionLock( bool lock ) {mItemPositionLocked = lock;}
00229 
00232     bool positionLock() const {return mItemPositionLocked;}
00233 
00236     void updateCursor( const QPointF& itemPos );
00237 
00238     double rotation() const {return mRotation;}
00239 
00241     virtual void updateItem() { QGraphicsRectItem::update(); }
00242 
00245     QString id() const { return mId; }
00246 
00251     void setId( const QString& id ) { mId = id; }
00252 
00253   public slots:
00254     virtual void setRotation( double r );
00255     void repaint();
00256 
00257   protected:
00258 
00259     QgsComposition* mComposition;
00260 
00261     QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction;
00263     QPointF mMouseMoveStartPos;
00265     QPointF mLastMouseEventPos;
00266 
00268     QGraphicsRectItem* mBoundingResizeRectangle;
00269 
00271     bool mFrame;
00272 
00275     bool mItemPositionLocked;
00276 
00278     mutable double mLastValidViewScaleFactor;
00279 
00281     double mRotation;
00282 
00283     //event handlers
00284     virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
00285     virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
00286     virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * event );
00287 
00288     virtual void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
00289 
00291     Qt::CursorShape cursorForPosition( const QPointF& itemCoordPos );
00292 
00294     QgsComposerItem::MouseMoveAction mouseMoveActionForPosition( const QPointF& itemCoordPos );
00295 
00304     void changeItemRectangle( const QPointF& currentPosition, const QPointF& mouseMoveStartPos, const QGraphicsRectItem* originalItem, double dx, double dy, QGraphicsRectItem* changeItem );
00305 
00307     virtual void drawSelectionBoxes( QPainter* p );
00308 
00310     virtual void drawFrame( QPainter* p );
00311 
00313     virtual void drawBackground( QPainter* p );
00314 
00316     void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
00317 
00319     double angle( const QPointF& p1, const QPointF& p2 ) const;
00320 
00323     double rectHandlerBorderTolerance() const;
00324 
00327     double lockSymbolSize() const;
00328 
00332     double horizontalViewScaleFactor() const;
00333 
00334     //some utility functions
00335 
00337     bool imageSizeConsideringRotation( double& width, double& height ) const;
00339     bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
00340 
00342     void sizeChangedByRotation( double& width, double& height );
00347     void rotate( double angle, double& x, double& y ) const;
00348 
00349   signals:
00351     void rotationChanged( double newRotation );
00353     void itemChanged();
00354   private:
00355     // Label id (unique within the same composition)
00356     QString mId;
00357 
00358     void init( bool manageZValue );
00359 };
00360 
00361 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines