QGIS API Documentation  2.3.0-Master
 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 : blazek@itc.it
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  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
214  void setFrameJoinStyle( Qt::PenJoinStyle style );
215 
222  virtual double estimatedFrameBleed() const;
223 
232  virtual QRectF rectWithFrame() const;
233 
239  bool hasBackground() const {return mBackground;}
240 
247  void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
248 
253  QColor backgroundColor() const { return mBackgroundColor; }
254 
260  void setBackgroundColor( const QColor& backgroundColor );
261 
263  QPainter::CompositionMode blendMode() const {return mBlendMode;}
264 
266  void setBlendMode( QPainter::CompositionMode blendMode );
267 
269  int transparency() const {return mTransparency;}
271  void setTransparency( int transparency );
272 
276  bool effectsEnabled() const {return mEffectsEnabled;}
280  void setEffectsEnabled( bool effectsEnabled );
281 
283  virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
284  virtual void removeItems() {}
285 
286  const QgsComposition* composition() const {return mComposition;}
287  QgsComposition* composition() {return mComposition;}
288 
289  virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
290 
294  void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
295 
296  virtual void endItemCommand() { endCommand(); }
298  void endCommand();
299  void cancelCommand();
300 
301  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
302  //painter down by the same factor for drawing
303 
306  void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
307 
309  void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop ) const;
310 
312  double textWidthMillimeters( const QFont& font, const QString& text ) const;
313 
316  double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
317 
319  double fontAscentMillimeters( const QFont& font ) const;
320 
322  double fontDescentMillimeters( const QFont& font ) const;
323 
325  double pixelFontSize( double pointSize ) const;
326 
328  QFont scaledFontPixelSize( const QFont& font ) const;
329 
332  void setPositionLock( bool lock );
333 
336  bool positionLock() const {return mItemPositionLocked;}
337 
340  double itemRotation() const {return mItemRotation;}
341 
346  double rotation() const {return mItemRotation;}
347 
349  virtual void updateItem() { QGraphicsRectItem::update(); }
350 
353  QString id() const { return mId; }
354 
357  virtual void setId( const QString& id );
358 
362  QString uuid() const { return mUuid; }
363 
369  virtual int numberExportLayers() const { return 0; }
370 
375  virtual void setCurrentExportLayer( int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
376 
377  public slots:
381  virtual void setRotation( double r );
382 
389  virtual void setItemRotation( double r, bool adjustPosition = false );
390 
391  void repaint();
392 
393  protected:
394 
396 
402 
404  QGraphicsRectItem* mBoundingResizeRectangle;
405  QGraphicsLineItem* mHAlignSnapItem;
406  QGraphicsLineItem* mVAlignSnapItem;
407 
409  bool mFrame;
415  Qt::PenJoinStyle mFrameJoinStyle;
416 
420 
423 
426 
428  QPainter::CompositionMode mBlendMode;
431 
434 
438 
443 
445  virtual void drawSelectionBoxes( QPainter* p );
446 
448  virtual void drawFrame( QPainter* p );
449 
451  virtual void drawBackground( QPainter* p );
452 
454  void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
455 
457  double angle( const QPointF& p1, const QPointF& p2 ) const;
458 
461  double rectHandlerBorderTolerance() const;
462 
465  double lockSymbolSize() const;
466 
470  double horizontalViewScaleFactor() const;
471 
472  //some utility functions
473 
475  bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const;
480  bool imageSizeConsideringRotation( double& width, double& height ) const;
481 
488  QRectF largestRotatedRectWithinBounds( QRectF originalRect, QRectF boundsRect, double rotation ) const;
489 
491  bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const;
496  bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
497 
499  void sizeChangedByRotation( double& width, double& height, double rotation );
504  void sizeChangedByRotation( double& width, double& height );
505 
510  void rotate( double angle, double& x, double& y ) const;
511 
513  QGraphicsLineItem* hAlignSnapItem();
514  void deleteHAlignSnapItem();
516  QGraphicsLineItem* vAlignSnapItem();
517  void deleteVAlignSnapItem();
518  void deleteAlignItems();
519 
520  signals:
522  void itemRotationChanged( double newRotation );
524  void itemChanged();
526  void sizeChanged();
530  void frameChanged();
531  private:
532  // id (not unique)
533  QString mId;
534  // name (unique)
535  QString mUuid;
536  // name (temporary when loaded from template)
537  QString mTemplateUuid;
538 
539  void init( bool manageZValue );
540 
541  friend class QgsComposerItemGroup; // to access mTemplateUuid
542 };
543 
544 #endif
bool positionLock() const
Returns position lock for mouse drags (true means locked)
bool effectsEnabled() const
Returns true if effects (eg blend modes) are enabled for the item.
double outlineWidth
Definition: qgssvgcache.cpp:78
int mTransparency
Item transparency.
QPointF mLastMouseEventPos
Position of the last mouse move event (in scene coordinates)
virtual void beginItemCommand(const QString &text)
ItemPositionMode lastUsedPositionMode()
Returns item's last used position mode.
virtual bool selected() const
Is selected.
virtual void removeItems()
Qt::PenJoinStyle frameJoinStyle() const
Returns the join style used for drawing the item's frame.
A item that forms part of a map composition.
QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction
double mLastValidViewScaleFactor
Backup to restore item appearance if no view scale factor is available.
ItemPositionMode mLastUsedPositionMode
The item's position mode.
A container for grouping several QgsComposerItems.
QColor backgroundColor() const
Gets the background color for this item.
QPainter::CompositionMode mBlendMode
Composition blend mode for item.
virtual void setCurrentExportLayer(int layerIdx=-1)
Sets the current layer to draw for exporting.
QgsComposition * composition()
virtual int type() const
return correct graphics item type.
virtual void moveContent(double dx, double dy)
Move Content of item.
QString uuid() const
Get item identification name.
void setBackgroundEnabled(bool drawBackground)
Set whether this item has a Background drawn around it or not.
int transparency() const
Returns the item's transparency.
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
QGraphicsRectItem * mBoundingResizeRectangle
Rectangle used during move and resize actions.
bool mFrame
True if item fram needs to be painted.
double itemRotation() const
Returns the rotation for the composer item.
MouseMoveAction
Describes the action (move or resize in different directon) to be done during mouse move...
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
QPointF mMouseMoveStartPos
Start point of the last mouse move action (in scene coordinates)
QgsComposition * mComposition
Graphics scene for map printing.
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
virtual void zoomContent(int delta, double x, double y)
Zoom content of item.
double rotation() const
Returns the rotation for the composer item.
Qt::PenJoinStyle mFrameJoinStyle
Frame join style.
QColor mBackgroundColor
Background color.
QGraphicsLineItem * mVAlignSnapItem
QGraphicsLineItem * mHAlignSnapItem
int mCurrentExportLayer
The layer that needs to be exported.
virtual void endItemCommand()
virtual void addItem(QgsComposerItem *item)
Composite operations for item groups do nothing per default.
bool mItemPositionLocked
True if item position and size cannot be changed with mouse move.
QPainter::CompositionMode blendMode() const
Returns the item's composition blending mode.
bool hasFrame() const
Whether this item has a frame or not.
const QgsComposition * composition() const
bool hasBackground() const
Whether this item has a Background or not.
QgsComposerEffect * mEffect
bool mBackground
True if item background needs to be painted.
double mItemRotation
Item rotation in degrees, clockwise.
QString id() const
Get item's id (which is not necessarly unique)