QGIS API Documentation  2.0.1-Dufour
 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() {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 
141  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft );
142 
146  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
147 
150  virtual void setSceneRect( const QRectF& rectangle );
151 
156  virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
157 
159  bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
160 
165  virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
166 
168  bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
169 
175  bool hasFrame() const {return mFrame;}
176 
183  void setFrameEnabled( bool drawFrame ) {mFrame = drawFrame;}
184 
185 
191  bool hasBackground() const {return mBackground;}
192 
199  void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
200 
205  QColor backgroundColor() const { return mBackgroundColor; }
206 
212  void setBackgroundColor( const QColor& backgroundColor );
213 
215  QPainter::CompositionMode blendMode() const {return mBlendMode;}
216 
218  void setBlendMode( QPainter::CompositionMode blendMode );
219 
221  int transparency() const {return mTransparency;}
223  void setTransparency( int transparency );
224 
228  bool effectsEnabled() const {return mEffectsEnabled;}
232  void setEffectsEnabled( bool effectsEnabled );
233 
235  virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
236  virtual void removeItems() {}
237 
238  const QgsComposition* composition() const {return mComposition;}
239  QgsComposition* composition() {return mComposition;}
240 
241  virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
242 
246  void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
247 
248  virtual void endItemCommand() { endCommand(); }
250  void endCommand();
251  void cancelCommand();
252 
253  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
254  //painter down by the same factor for drawing
255 
258  void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
259 
261  void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop ) const;
262 
264  double textWidthMillimeters( const QFont& font, const QString& text ) const;
265 
268  double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
269 
271  double fontAscentMillimeters( const QFont& font ) const;
272 
274  double fontDescentMillimeters( const QFont& font ) const;
275 
277  double pixelFontSize( double pointSize ) const;
278 
280  QFont scaledFontPixelSize( const QFont& font ) const;
281 
284  void setPositionLock( bool lock ) {mItemPositionLocked = lock;}
285 
288  bool positionLock() const {return mItemPositionLocked;}
289 
292  void updateCursor( const QPointF& itemPos );
293 
294  double rotation() const {return mRotation;}
295 
297  virtual void updateItem() { QGraphicsRectItem::update(); }
298 
301  QString id() const { return mId; }
302 
305  virtual void setId( const QString& id );
306 
310  QString uuid() const { return mUuid; }
311 
312  public slots:
313  virtual void setRotation( double r );
314  void repaint();
315 
316  protected:
317 
319 
325 
327  QGraphicsRectItem* mBoundingResizeRectangle;
328  QGraphicsLineItem* mHAlignSnapItem;
329  QGraphicsLineItem* mVAlignSnapItem;
330 
332  bool mFrame;
337 
341 
344 
346  double mRotation;
347 
349  QPainter::CompositionMode mBlendMode;
352 
355 
359 
360  //event handlers
361  virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
362  virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
363  virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * event );
364 
365  virtual void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
366 
368  Qt::CursorShape cursorForPosition( const QPointF& itemCoordPos );
369 
371  QgsComposerItem::MouseMoveAction mouseMoveActionForPosition( const QPointF& itemCoordPos );
372 
381  void changeItemRectangle( const QPointF& currentPosition, const QPointF& mouseMoveStartPos, const QGraphicsRectItem* originalItem, double dx, double dy, QGraphicsRectItem* changeItem );
382 
384  virtual void drawSelectionBoxes( QPainter* p );
385 
387  virtual void drawFrame( QPainter* p );
388 
390  virtual void drawBackground( QPainter* p );
391 
393  void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
394 
396  double angle( const QPointF& p1, const QPointF& p2 ) const;
397 
400  double rectHandlerBorderTolerance() const;
401 
404  double lockSymbolSize() const;
405 
409  double horizontalViewScaleFactor() const;
410 
411  //some utility functions
412 
414  bool imageSizeConsideringRotation( double& width, double& height ) const;
416  bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
417 
419  void sizeChangedByRotation( double& width, double& height );
424  void rotate( double angle, double& x, double& y ) const;
425 
427  QGraphicsLineItem* hAlignSnapItem();
428  void deleteHAlignSnapItem();
430  QGraphicsLineItem* vAlignSnapItem();
431  void deleteVAlignSnapItem();
432  void deleteAlignItems();
433 
434  signals:
436  void rotationChanged( double newRotation );
438  void itemChanged();
440  void sizeChanged();
441  private:
442  // id (not unique)
443  QString mId;
444  // name (unique)
445  QString mUuid;
446  // name (temporary when loaded from template)
447  QString mTemplateUuid;
448 
449  void init( bool manageZValue );
450 
451  friend class QgsComposerItemGroup; // to access mTemplateUuid
452 };
453 
454 #endif