~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/core/composer/qgscomposeritem.h

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "qgscomposition.h"
 
21
#include <QGraphicsRectItem>
 
22
#include <QObject>
 
23
 
 
24
class QWidget;
 
25
class QDomDocument;
 
26
class QDomElement;
 
27
 
 
28
class QqsComposition;
 
29
 
 
30
/** \ingroup MapComposer
 
31
 * A item that forms part of a map composition.
 
32
 */
 
33
class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
 
34
{
 
35
    Q_OBJECT
 
36
  public:
 
37
 
 
38
    /**Describes the action (move or resize in different directon) to be done during mouse move*/
 
39
    enum MouseMoveAction
 
40
    {
 
41
      MoveItem,
 
42
      ResizeUp,
 
43
      ResizeDown,
 
44
      ResizeLeft,
 
45
      ResizeRight,
 
46
      ResizeLeftUp,
 
47
      ResizeRightUp,
 
48
      ResizeLeftDown,
 
49
      ResizeRightDown,
 
50
      NoAction
 
51
    };
 
52
 
 
53
    enum ItemPositionMode
 
54
    {
 
55
      UpperLeft,
 
56
      UpperMiddle,
 
57
      UpperRight,
 
58
      MiddleLeft,
 
59
      Middle,
 
60
      MiddleRight,
 
61
      LowerLeft,
 
62
      LowerMiddle,
 
63
      LowerRight
 
64
    };
 
65
 
 
66
    /**Constructor
 
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();
 
73
 
 
74
    /** \brief Set selected, selected item should be highlighted */
 
75
    virtual void setSelected( bool s );
 
76
 
 
77
    /** \brief Is selected */
 
78
    virtual bool selected( void ) {return QGraphicsRectItem::isSelected();}
 
79
 
 
80
    /** stores state in project */
 
81
    virtual bool writeSettings( void );
 
82
 
 
83
    /** read state from project */
 
84
    virtual bool readSettings( void );
 
85
 
 
86
    /** delete settings from project file  */
 
87
    virtual bool removeSettings( void );
 
88
 
 
89
    /**Moves item in canvas coordinates*/
 
90
    void move( double dx, double dy );
 
91
 
 
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 ); }
 
96
 
 
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 ); }
 
102
 
 
103
    /**Moves the item to a new position (in canvas coordinates)*/
 
104
    void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );
 
105
 
 
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 );
 
109
 
 
110
    /** stores state in Dom node
 
111
     * @param node is Dom node corresponding to 'Composer' tag
 
112
     * @param temp write template file
 
113
     */
 
114
    virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
 
115
 
 
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;
 
118
 
 
119
    /** sets state from Dom document
 
120
     * @param itemElem is Dom node corresponding to item tag
 
121
     */
 
122
    virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
 
123
 
 
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 );
 
126
 
 
127
 
 
128
 
 
129
    bool frame() const {return mFrame;}
 
130
    void setFrame( bool drawFrame ) {mFrame = drawFrame;}
 
131
 
 
132
    /**Composite operations for item groups do nothing per default*/
 
133
    virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
 
134
    virtual void removeItems() {}
 
135
 
 
136
    const QgsComposition* composition() const {return mComposition;}
 
137
 
 
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
 
140
 
 
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;
 
144
 
 
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;
 
147
 
 
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;
 
150
 
 
151
    /**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
 
152
    double fontAscentMillimeters( const QFont& font ) const;
 
153
 
 
154
    /**Calculates font to from point size to pixel size*/
 
155
    double pixelFontSize( double pointSize ) const;
 
156
 
 
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;
 
159
 
 
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;}
 
163
 
 
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;}
 
167
 
 
168
    /**Update mouse cursor at (item) position
 
169
    @note this method was added in version 1.2*/
 
170
    void updateCursor( const QPointF& itemPos );
 
171
 
 
172
    double rotation() const {return mRotation;}
 
173
 
 
174
  public slots:
 
175
    virtual void setRotation( double r );
 
176
 
 
177
  protected:
 
178
 
 
179
    QgsComposition* mComposition;
 
180
 
 
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;
 
186
 
 
187
    /**Rectangle used during move and resize actions*/
 
188
    QGraphicsRectItem* mBoundingResizeRectangle;
 
189
 
 
190
    /**True if item fram needs to be painted*/
 
191
    bool mFrame;
 
192
 
 
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;
 
196
 
 
197
    /**Backup to restore item appearance if no view scale factor is available*/
 
198
    mutable double mLastValidViewScaleFactor;
 
199
 
 
200
    /**Item rotation in degrees, clockwise*/
 
201
    double mRotation;
 
202
 
 
203
    //event handlers
 
204
    virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
 
205
    virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
 
206
    virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * event );
 
207
 
 
208
    virtual void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
 
209
 
 
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 );
 
212
 
 
213
    /**Finds out which mouse move action to choose depending on the cursor position inside the widget*/
 
214
    QgsComposerItem::MouseMoveAction mouseMoveActionForPosition( const QPointF& itemCoordPos );
 
215
 
 
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)
 
223
    */
 
224
    void changeItemRectangle( const QPointF& currentPosition, const QPointF& mouseMoveStartPos, const QGraphicsRectItem* originalItem, double dx, double dy, QGraphicsRectItem* changeItem );
 
225
 
 
226
    /**Draw selection boxes around item*/
 
227
    virtual void drawSelectionBoxes( QPainter* p );
 
228
 
 
229
    /**Draw black frame around item*/
 
230
    virtual void drawFrame( QPainter* p );
 
231
 
 
232
    /**Draw background*/
 
233
    virtual void drawBackground( QPainter* p );
 
234
 
 
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;
 
238
 
 
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;
 
242
 
 
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;
 
247
 
 
248
    //some utility functions
 
249
 
 
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;
 
263
 
 
264
  signals:
 
265
    /**Is emitted on rotation change to notify north arrow pictures*/
 
266
    void rotationChanged( double newRotation );
 
267
};
 
268
 
 
269
#endif