QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscomposeritem.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposeritem.h
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSCOMPOSERITEM_H
18 #define QGSCOMPOSERITEM_H
19 
20 #include "qgscomposeritemcommand.h"
21 #include "qgscomposereffect.h"
22 #include "qgsmaprenderer.h" // for blend mode functions & enums
23 #include <QGraphicsRectItem>
24 #include <QObject>
25 
26 class QgsComposition;
27 class QWidget;
28 class QDomDocument;
29 class QDomElement;
30 class QGraphicsLineItem;
32 
36 class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
37 {
38  Q_OBJECT
39  public:
40 
41  enum ItemType
42  {
43  // base class for the items
44  ComposerItem = UserType + 100,
45 
46  // derived classes
52  ComposerPaper, // QgsPaperItem
59  ComposerFrame
60  };
61 
64  {
74  NoAction
75  };
76 
78  {
87  LowerRight
88  };
89 
93  QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
101  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
102  virtual ~QgsComposerItem();
103 
105  virtual int type() const { return ComposerItem; }
106 
108  virtual void setSelected( bool s );
109 
111  virtual bool selected() const {return QGraphicsRectItem::isSelected();};
112 
114  virtual bool writeSettings();
115 
117  virtual bool readSettings();
118 
120  virtual bool removeSettings();
121 
123  void move( double dx, double dy );
124 
128  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
129 
134  virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
135 
137  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );
138 
148  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false );
149 
153  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
154 
157  virtual void setSceneRect( const QRectF& rectangle );
158 
163  virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
164 
166  bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
167 
172  virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
173 
175  bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
176 
182  bool hasFrame() const {return mFrame;}
183 
190  void setFrameEnabled( bool drawFrame );
191 
198  virtual void setFrameOutlineWidth( double outlineWidth );
199 
206  virtual double estimatedFrameBleed() const;
207 
216  virtual QRectF rectWithFrame() const;
217 
223  bool hasBackground() const {return mBackground;}
224 
231  void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
232 
237  QColor backgroundColor() const { return mBackgroundColor; }
238 
244  void setBackgroundColor( const QColor& backgroundColor );
245 
247  QPainter::CompositionMode blendMode() const {return mBlendMode;}
248 
250  void setBlendMode( QPainter::CompositionMode blendMode );
251 
253  int transparency() const {return mTransparency;}
255  void setTransparency( int transparency );
256 
260  bool effectsEnabled() const {return mEffectsEnabled;}
264  void setEffectsEnabled( bool effectsEnabled );
265 
267  virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
268  virtual void removeItems() {}
269 
270  const QgsComposition* composition() const {return mComposition;}
271  QgsComposition* composition() {return mComposition;}
272 
273  virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
274 
278  void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
279 
280  virtual void endItemCommand() { endCommand(); }
282  void endCommand();
283  void cancelCommand();
284 
285  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
286  //painter down by the same factor for drawing
287 
290  void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
291 
293  void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop ) const;
294 
296  double textWidthMillimeters( const QFont& font, const QString& text ) const;
297 
300  double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
301 
303  double fontAscentMillimeters( const QFont& font ) const;
304 
306  double fontDescentMillimeters( const QFont& font ) const;
307 
309  double pixelFontSize( double pointSize ) const;
310 
312  QFont scaledFontPixelSize( const QFont& font ) const;
313 
316  void setPositionLock( bool lock );
317 
320  bool positionLock() const {return mItemPositionLocked;}
321 
324  double itemRotation() const {return mItemRotation;}
325 
330  double rotation() const {return mItemRotation;}
331 
333  virtual void updateItem() { QGraphicsRectItem::update(); }
334 
337  QString id() const { return mId; }
338 
341  virtual void setId( const QString& id );
342 
346  QString uuid() const { return mUuid; }
347 
348  public slots:
352  virtual void setRotation( double r );
353 
360  virtual void setItemRotation( double r, bool adjustPosition = false );
361 
362  void repaint();
363 
364  protected:
365 
367 
373 
375  QGraphicsRectItem* mBoundingResizeRectangle;
376  QGraphicsLineItem* mHAlignSnapItem;
377  QGraphicsLineItem* mVAlignSnapItem;
378 
380  bool mFrame;
385 
389 
392 
395 
397  QPainter::CompositionMode mBlendMode;
400 
403 
407 
409  virtual void drawSelectionBoxes( QPainter* p );
410 
412  virtual void drawFrame( QPainter* p );
413 
415  virtual void drawBackground( QPainter* p );
416 
418  void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
419 
421  double angle( const QPointF& p1, const QPointF& p2 ) const;
422 
425  double rectHandlerBorderTolerance() const;
426 
429  double lockSymbolSize() const;
430 
434  double horizontalViewScaleFactor() const;
435 
436  //some utility functions
437 
439  bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const;
444  bool imageSizeConsideringRotation( double& width, double& height ) const;
445 
452  QRectF largestRotatedRectWithinBounds( QRectF originalRect, QRectF boundsRect, double rotation ) const;
453 
455  bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const;
460  bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
461 
463  void sizeChangedByRotation( double& width, double& height, double rotation );
468  void sizeChangedByRotation( double& width, double& height );
469 
474  void rotate( double angle, double& x, double& y ) const;
475 
477  QGraphicsLineItem* hAlignSnapItem();
478  void deleteHAlignSnapItem();
480  QGraphicsLineItem* vAlignSnapItem();
481  void deleteVAlignSnapItem();
482  void deleteAlignItems();
483 
484  signals:
486  void itemRotationChanged( double newRotation );
488  void itemChanged();
490  void sizeChanged();
494  void frameChanged();
495  private:
496  // id (not unique)
497  QString mId;
498  // name (unique)
499  QString mUuid;
500  // name (temporary when loaded from template)
501  QString mTemplateUuid;
502 
503  void init( bool manageZValue );
504 
505  friend class QgsComposerItemGroup; // to access mTemplateUuid
506 };
507 
508 #endif