1
/***************************************************************************
5
copyright : (C) 2005 by Radim Blazek
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
17
#ifndef QGSCOMPOSERITEM_H
18
#define QGSCOMPOSERITEM_H
20
#include "qgscomposition.h"
21
#include <QGraphicsRectItem>
30
/** \ingroup MapComposer
31
* A item that forms part of a map composition.
33
class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
38
/**Describes the action (move or resize in different directon) to be done during mouse move*/
67
@param manageZValue true if the z-Value of this object should be managed by mComposition*/
68
QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
69
/**Constructor with box position and composer object
70
@param manageZValue true if the z-Value of this object should be managed by mComposition*/
71
QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
72
virtual ~QgsComposerItem();
74
/** \brief Set selected, selected item should be highlighted */
75
virtual void setSelected( bool s );
77
/** \brief Is selected */
78
virtual bool selected( void ) {return QGraphicsRectItem::isSelected();}
80
/** stores state in project */
81
virtual bool writeSettings( void );
83
/** read state from project */
84
virtual bool readSettings( void );
86
/** delete settings from project file */
87
virtual bool removeSettings( void );
89
/**Moves item in canvas coordinates*/
90
void move( double dx, double dy );
92
/**Move Content of item. Does nothing per default (but implemented in composer map)
93
@param dx move in x-direction (canvas coordinates)
94
@param dy move in y-direction(canvas coordinates)*/
95
virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
97
/**Zoom content of item. Does nothing per default (but implemented in composer map)
98
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
99
@param x x-position of mouse cursor (in item coordinates)
100
@param y y-position of mouse cursor (in item coordinates)*/
101
virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
103
/**Moves the item to a new position (in canvas coordinates)*/
104
void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );
106
/**Sets this items bound in scene coordinates such that 1 item size units
107
corresponds to 1 scene size unit*/
108
virtual void setSceneRect( const QRectF& rectangle );
110
/** stores state in Dom node
111
* @param node is Dom node corresponding to 'Composer' tag
112
* @param temp write template file
114
virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
116
/**Writes parameter that are not subclass specific in document. Usually called from writeXML methods of subclasses*/
117
bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
119
/** sets state from Dom document
120
* @param itemElem is Dom node corresponding to item tag
122
virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
124
/**Reads parameter that are not subclass specific in document. Usually called from readXML methods of subclasses*/
125
bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
129
bool frame() const {return mFrame;}
130
void setFrame( bool drawFrame ) {mFrame = drawFrame;}
132
/**Composite operations for item groups do nothing per default*/
133
virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
134
virtual void removeItems() {}
136
const QgsComposition* composition() const {return mComposition;}
138
//functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
139
//painter down by the same factor for drawing
141
/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
142
to work arount the Qt font bug)*/
143
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
145
/**Like the above, but with a rectangle for multiline text*/
146
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font ) const;
148
/**Returns the font width in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
149
double textWidthMillimeters( const QFont& font, const QString& text ) const;
151
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
152
double fontAscentMillimeters( const QFont& font ) const;
154
/**Calculates font to from point size to pixel size*/
155
double pixelFontSize( double pointSize ) const;
157
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
158
QFont scaledFontPixelSize( const QFont& font ) const;
160
/**Locks / unlocks the item position for mouse drags
161
@note this method was added in version 1.2*/
162
void setPositionLock( bool lock ) {mItemPositionLocked = lock;}
164
/**Returns position lock for mouse drags (true means locked)
165
@note this method was added in version 1.2*/
166
bool positionLock() const {return mItemPositionLocked;}
168
/**Update mouse cursor at (item) position
169
@note this method was added in version 1.2*/
170
void updateCursor( const QPointF& itemPos );
172
double rotation() const {return mRotation;}
175
virtual void setRotation( double r );
179
QgsComposition* mComposition;
181
QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction;
182
/**Start point of the last mouse move action (in scene coordinates)*/
183
QPointF mMouseMoveStartPos;
184
/**Position of the last mouse move event (in scene coordinates)*/
185
QPointF mLastMouseEventPos;
187
/**Rectangle used during move and resize actions*/
188
QGraphicsRectItem* mBoundingResizeRectangle;
190
/**True if item fram needs to be painted*/
193
/**True if item position and size cannot be changed with mouse move
194
@note: this member was added in version 1.2*/
195
bool mItemPositionLocked;
197
/**Backup to restore item appearance if no view scale factor is available*/
198
mutable double mLastValidViewScaleFactor;
200
/**Item rotation in degrees, clockwise*/
204
virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
205
virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
206
virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * event );
208
virtual void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
210
/**Finds out the appropriate cursor for the current mouse position in the widget (e.g. move in the middle, resize at border)*/
211
Qt::CursorShape cursorForPosition( const QPointF& itemCoordPos );
213
/**Finds out which mouse move action to choose depending on the cursor position inside the widget*/
214
QgsComposerItem::MouseMoveAction mouseMoveActionForPosition( const QPointF& itemCoordPos );
216
/**Changes the rectangle of an item depending on current mouse action (resize or move)
217
@param currentPosition current position of mouse cursor
218
@param mouseMoveStartPos cursor position at the start of the current mouse action
219
@param originalItem Item position at the start of the mouse action
220
@param dx x-Change of mouse cursor
221
@param dy y-Change of mouse cursor
222
@param changeItem Item to change size (can be the same as originalItem or a differen one)
224
void changeItemRectangle( const QPointF& currentPosition, const QPointF& mouseMoveStartPos, const QGraphicsRectItem* originalItem, double dx, double dy, QGraphicsRectItem* changeItem );
226
/**Draw selection boxes around item*/
227
virtual void drawSelectionBoxes( QPainter* p );
229
/**Draw black frame around item*/
230
virtual void drawFrame( QPainter* p );
233
virtual void drawBackground( QPainter* p );
235
/**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the \
236
item border for resizing*/
237
double rectHandlerBorderTolerance() const;
239
/**Returns the size of the lock symbol depending on the composer zoom level and the item size
240
@note: this function was introduced in version 1.2*/
241
double lockSymbolSize() const;
243
/**Returns the zoom factor of the graphics view.
244
@return the factor or -1 in case of error (e.g. graphic view does not exist)
245
@note: this function was introduced in version 1.2*/
246
double horizontalViewScaleFactor() const;
248
//some utility functions
250
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation*/
251
bool imageSizeConsideringRotation( double& width, double& height ) const;
252
/**Calculates corner point after rotation and scaling*/
253
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
254
/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
255
QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;
256
/**Calculates width / height of the bounding box of a rotated rectangle (mRotation)*/
257
void sizeChangedByRotation( double& width, double& height );
258
/**Rotates a point / vector
259
@param angle rotation angle in degrees, counterclockwise
260
@param x in/out: x coordinate before / after the rotation
261
@param y in/out: y cooreinate before / after the rotation*/
262
void rotate( double angle, double& x, double& y ) const;
265
/**Is emitted on rotation change to notify north arrow pictures*/
266
void rotationChanged( double newRotation );