Skip to content

Commit 5262a4d

Browse files
committed
[FEATURE] [composer] Replace custom item rotation with QGraphicsItem rotation, support rotation for all types of composer items (fix #7933, fix #4884)
1 parent 4169aa7 commit 5262a4d

5 files changed

+10
-89
lines changed

src/core/composer/qgscomposeritem.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
226226
//rotation
227227
if ( itemElem.attribute( "itemRotation", "0" ).toDouble() != 0 )
228228
{
229-
mItemRotation = itemElem.attribute( "itemRotation", "0" ).toDouble();
229+
setItemRotation( itemElem.attribute( "itemRotation", "0" ).toDouble() );
230230
}
231231

232232
//uuid
@@ -482,6 +482,8 @@ void QgsComposerItem::setSceneRect( const QRectF& rectangle )
482482

483483
QRectF newRect( 0, 0, newWidth, newHeight );
484484
QGraphicsRectItem::setRect( newRect );
485+
//rotate item around its centre point
486+
setTransformOriginPoint( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) );
485487
setPos( xTranslation, yTranslation );
486488

487489
emit sizeChanged();
@@ -716,6 +718,11 @@ void QgsComposerItem::setItemRotation( double r )
716718
{
717719
mItemRotation = r;
718720
}
721+
722+
//rotate item around its centre point
723+
setTransformOriginPoint( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) );
724+
QGraphicsItem::setRotation( mItemRotation );
725+
719726
emit itemRotationChanged( r );
720727
update();
721728
}

src/core/composer/qgscomposerlabel.cpp

+1-35
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
6565

6666
double penWidth = pen().widthF();
6767
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, mTextBoxWidth - 2 * penWidth - 2 * mMargin, mTextBoxHeight - 2 * penWidth - 2 * mMargin );
68-
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
69-
painter->rotate( mItemRotation );
70-
painter->translate( -mTextBoxWidth / 2.0, -mTextBoxHeight / 2.0 );
7168

7269
if ( mHtmlState )
7370
{
@@ -245,37 +242,6 @@ QFont QgsComposerLabel::font() const
245242
return mFont;
246243
}
247244

248-
void QgsComposerLabel::setRotation( double r )
249-
{
250-
//kept for api compatibility with QGIS 2.0
251-
setItemRotation( r );
252-
}
253-
254-
void QgsComposerLabel::setItemRotation( double r )
255-
{
256-
double width = mTextBoxWidth;
257-
double height = mTextBoxHeight;
258-
QgsComposerItem::setItemRotation( r );
259-
sizeChangedByRotation( width, height );
260-
261-
double x = pos().x() + rect().width() / 2.0 - width / 2.0;
262-
double y = pos().y() + rect().height() / 2.0 - height / 2.0;
263-
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
264-
}
265-
266-
void QgsComposerLabel::setSceneRect( const QRectF& rectangle )
267-
{
268-
if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
269-
{
270-
double textBoxWidth = rectangle.width();
271-
double textBoxHeight = rectangle.height();
272-
imageSizeConsideringRotation( textBoxWidth, textBoxHeight );
273-
mTextBoxWidth = textBoxWidth;
274-
mTextBoxHeight = textBoxHeight;
275-
}
276-
QgsComposerItem::setSceneRect( rectangle );
277-
}
278-
279245
bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
280246
{
281247
QString alignment;
@@ -370,7 +336,7 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
370336
if ( composerItemElem.attribute( "rotation", "0" ).toDouble() != 0 )
371337
{
372338
//check for old (pre 2.1) rotation attribute
373-
mItemRotation = composerItemElem.attribute( "rotation", "0" ).toDouble();
339+
setItemRotation( composerItemElem.attribute( "rotation", "0" ).toDouble() );
374340
}
375341

376342
_readXML( composerItemElem, doc );

src/core/composer/qgscomposerlabel.h

-11
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
8787
@note: this function was added in version 1.4*/
8888
QColor fontColor() const {return mFontColor;}
8989

90-
void setSceneRect( const QRectF& rectangle );
91-
9290
/** stores state in Dom element
9391
* @param elem is Dom element corresponding to 'Composer' tag
9492
* @param doc document
@@ -101,15 +99,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
10199
*/
102100
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
103101

104-
public slots:
105-
/* Sets rotation for the label
106-
* @deprecated Use setItemRotation( double rotation ) instead
107-
*/
108-
virtual void setRotation( double r );
109-
110-
/* Sets rotation for the label */
111-
virtual void setItemRotation( double r );
112-
113102
private slots:
114103
void loadingHtmlFinished( bool );
115104

src/core/composer/qgscomposershape.cpp

+1-34
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ void QgsComposerShape::drawShape( QPainter* p )
6060
p->save();
6161
p->setRenderHint( QPainter::Antialiasing );
6262

63-
p->translate( rect().width() / 2.0, rect().height() / 2.0 );
64-
p->rotate( mItemRotation );
65-
p->translate( -rect().width() / 2.0, -rect().height() / 2.0 );
66-
6763
switch ( mShape )
6864
{
6965
case Ellipse:
@@ -140,7 +136,7 @@ bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument&
140136
if ( composerItemElem.attribute( "rotation", "0" ).toDouble() != 0 )
141137
{
142138
//check for old (pre 2.1) rotation attribute
143-
mItemRotation = composerItemElem.attribute( "rotation", "0" ).toDouble();
139+
setItemRotation( composerItemElem.attribute( "rotation", "0" ).toDouble() );
144140
}
145141

146142
_readXML( composerItemElem, doc );
@@ -149,36 +145,7 @@ bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument&
149145
return true;
150146
}
151147

152-
153-
void QgsComposerShape::setItemRotation( double r )
154-
{
155-
//adapt rectangle size
156-
double width = rect().width();
157-
double height = rect().height();
158-
sizeChangedByRotation( width, height );
159-
160-
//adapt scene rect to have the same center and the new width / height
161-
double x = pos().x() + rect().width() / 2.0 - width / 2.0;
162-
double y = pos().y() + rect().height() / 2.0 - height / 2.0;
163-
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
164-
165-
QgsComposerItem::setItemRotation( r );
166-
}
167-
168148
void QgsComposerShape::setCornerRadius( double radius )
169149
{
170150
mCornerRadius = radius;
171151
}
172-
173-
void QgsComposerShape::setSceneRect( const QRectF& rectangle )
174-
{
175-
//consider to change size of the shape if the rectangle changes width and/or height
176-
if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
177-
{
178-
double newShapeWidth = rectangle.width();
179-
double newShapeHeight = rectangle.height();
180-
imageSizeConsideringRotation( newShapeWidth, newShapeHeight );
181-
}
182-
183-
QgsComposerItem::setSceneRect( rectangle );
184-
}

src/core/composer/qgscomposershape.h

-8
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,11 @@ class CORE_EXPORT QgsComposerShape: public QgsComposerItem
6161
QgsComposerShape::Shape shapeType() const {return mShape;}
6262
void setShapeType( QgsComposerShape::Shape s ) {mShape = s;}
6363

64-
/**Sets this items bound in scene coordinates such that 1 item size units
65-
corresponds to 1 scene size unit. Also, the shape is scaled*/
66-
void setSceneRect( const QRectF& rectangle );
67-
6864
/**Sets radius for rounded rectangle corners. Added in v2.1 */
6965
void setCornerRadius( double radius );
7066
/**Returns the radius for rounded rectangle corners*/
7167
double cornerRadius() const { return mCornerRadius; };
7268

73-
public slots:
74-
/**Sets item rotation and resizes item bounds such that the shape always has the same size*/
75-
virtual void setItemRotation( double r );
76-
7769
protected:
7870
/* reimplement drawFrame, since it's not a rect, but a custom shape */
7971
virtual void drawFrame( QPainter* p );

0 commit comments

Comments
 (0)