Quantum GIS API Documentation  1.7.4
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                : [email protected]
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 
00161 
00162 
00163     bool frame() const {return mFrame;}
00164     void setFrame( bool drawFrame ) {mFrame = drawFrame;}
00165 
00167     virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
00168     virtual void removeItems() {}
00169 
00170     const QgsComposition* composition() const {return mComposition;}
00171 
00175     void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
00177     void endCommand();
00178     void cancelCommand();
00179 
00180     //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
00181     //painter down by the same factor for drawing
00182 
00185     void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
00186 
00188     void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement = Qt::AlignLeft, Qt::AlignmentFlag valignement = Qt::AlignTop ) const;
00189 
00191     double textWidthMillimeters( const QFont& font, const QString& text ) const;
00192 
00195     double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
00196 
00198     double fontAscentMillimeters( const QFont& font ) const;
00199 
00201     double pixelFontSize( double pointSize ) const;
00202 
00204     QFont scaledFontPixelSize( const QFont& font ) const;
00205 
00208     void setPositionLock( bool lock ) {mItemPositionLocked = lock;}
00209 
00212     bool positionLock() const {return mItemPositionLocked;}
00213 
00216     void updateCursor( const QPointF& itemPos );
00217 
00218     double rotation() const {return mRotation;}
00219 
00220   public slots:
00221     virtual void setRotation( double r );
00222     void repaint();
00223 
00224   protected:
00225 
00226     QgsComposition* mComposition;
00227 
00228     QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction;
00230     QPointF mMouseMoveStartPos;
00232     QPointF mLastMouseEventPos;
00233 
00235     QGraphicsRectItem* mBoundingResizeRectangle;
00236 
00238     bool mFrame;
00239 
00242     bool mItemPositionLocked;
00243 
00245     mutable double mLastValidViewScaleFactor;
00246 
00248     double mRotation;
00249 
00250     //event handlers
00251     virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
00252     virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
00253     virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * event );
00254 
00255     virtual void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
00256 
00258     Qt::CursorShape cursorForPosition( const QPointF& itemCoordPos );
00259 
00261     QgsComposerItem::MouseMoveAction mouseMoveActionForPosition( const QPointF& itemCoordPos );
00262 
00271     void changeItemRectangle( const QPointF& currentPosition, const QPointF& mouseMoveStartPos, const QGraphicsRectItem* originalItem, double dx, double dy, QGraphicsRectItem* changeItem );
00272 
00274     virtual void drawSelectionBoxes( QPainter* p );
00275 
00277     virtual void drawFrame( QPainter* p );
00278 
00280     virtual void drawBackground( QPainter* p );
00281 
00283     void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
00284 
00286     double angle( const QPointF& p1, const QPointF& p2 ) const;
00287 
00290     double rectHandlerBorderTolerance() const;
00291 
00294     double lockSymbolSize() const;
00295 
00299     double horizontalViewScaleFactor() const;
00300 
00301     //some utility functions
00302 
00304     bool imageSizeConsideringRotation( double& width, double& height ) const;
00306     bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
00308     QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;
00310     void sizeChangedByRotation( double& width, double& height );
00315     void rotate( double angle, double& x, double& y ) const;
00316 
00317   signals:
00319     void rotationChanged( double newRotation );
00321     void itemChanged();
00322 };
00323 
00324 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines