Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] [composer] Replace custom item rotation with QGraphicsItem …
…rotation, support rotation for all types of composer items (fix #7933, fix #4884)
  • Loading branch information
nyalldawson committed Dec 30, 2013
1 parent 4169aa7 commit 5262a4d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 89 deletions.
9 changes: 8 additions & 1 deletion src/core/composer/qgscomposeritem.cpp
Expand Up @@ -226,7 +226,7 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
//rotation
if ( itemElem.attribute( "itemRotation", "0" ).toDouble() != 0 )
{
mItemRotation = itemElem.attribute( "itemRotation", "0" ).toDouble();
setItemRotation( itemElem.attribute( "itemRotation", "0" ).toDouble() );
}

//uuid
Expand Down Expand Up @@ -482,6 +482,8 @@ void QgsComposerItem::setSceneRect( const QRectF& rectangle )

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

emit sizeChanged();
Expand Down Expand Up @@ -716,6 +718,11 @@ void QgsComposerItem::setItemRotation( double r )
{
mItemRotation = r;
}

//rotate item around its centre point
setTransformOriginPoint( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) );
QGraphicsItem::setRotation( mItemRotation );

emit itemRotationChanged( r );
update();
}
Expand Down
36 changes: 1 addition & 35 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -65,9 +65,6 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*

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

if ( mHtmlState )
{
Expand Down Expand Up @@ -245,37 +242,6 @@ QFont QgsComposerLabel::font() const
return mFont;
}

void QgsComposerLabel::setRotation( double r )
{
//kept for api compatibility with QGIS 2.0
setItemRotation( r );
}

void QgsComposerLabel::setItemRotation( double r )
{
double width = mTextBoxWidth;
double height = mTextBoxHeight;
QgsComposerItem::setItemRotation( r );
sizeChangedByRotation( width, height );

double x = pos().x() + rect().width() / 2.0 - width / 2.0;
double y = pos().y() + rect().height() / 2.0 - height / 2.0;
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
}

void QgsComposerLabel::setSceneRect( const QRectF& rectangle )
{
if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
{
double textBoxWidth = rectangle.width();
double textBoxHeight = rectangle.height();
imageSizeConsideringRotation( textBoxWidth, textBoxHeight );
mTextBoxWidth = textBoxWidth;
mTextBoxHeight = textBoxHeight;
}
QgsComposerItem::setSceneRect( rectangle );
}

bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
{
QString alignment;
Expand Down Expand Up @@ -370,7 +336,7 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
if ( composerItemElem.attribute( "rotation", "0" ).toDouble() != 0 )
{
//check for old (pre 2.1) rotation attribute
mItemRotation = composerItemElem.attribute( "rotation", "0" ).toDouble();
setItemRotation( composerItemElem.attribute( "rotation", "0" ).toDouble() );
}

_readXML( composerItemElem, doc );
Expand Down
11 changes: 0 additions & 11 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -87,8 +87,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
@note: this function was added in version 1.4*/
QColor fontColor() const {return mFontColor;}

void setSceneRect( const QRectF& rectangle );

/** stores state in Dom element
* @param elem is Dom element corresponding to 'Composer' tag
* @param doc document
Expand All @@ -101,15 +99,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

public slots:
/* Sets rotation for the label
* @deprecated Use setItemRotation( double rotation ) instead
*/
virtual void setRotation( double r );

/* Sets rotation for the label */
virtual void setItemRotation( double r );

private slots:
void loadingHtmlFinished( bool );

Expand Down
35 changes: 1 addition & 34 deletions src/core/composer/qgscomposershape.cpp
Expand Up @@ -60,10 +60,6 @@ void QgsComposerShape::drawShape( QPainter* p )
p->save();
p->setRenderHint( QPainter::Antialiasing );

p->translate( rect().width() / 2.0, rect().height() / 2.0 );
p->rotate( mItemRotation );
p->translate( -rect().width() / 2.0, -rect().height() / 2.0 );

switch ( mShape )
{
case Ellipse:
Expand Down Expand Up @@ -140,7 +136,7 @@ bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument&
if ( composerItemElem.attribute( "rotation", "0" ).toDouble() != 0 )
{
//check for old (pre 2.1) rotation attribute
mItemRotation = composerItemElem.attribute( "rotation", "0" ).toDouble();
setItemRotation( composerItemElem.attribute( "rotation", "0" ).toDouble() );
}

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


void QgsComposerShape::setItemRotation( double r )
{
//adapt rectangle size
double width = rect().width();
double height = rect().height();
sizeChangedByRotation( width, height );

//adapt scene rect to have the same center and the new width / height
double x = pos().x() + rect().width() / 2.0 - width / 2.0;
double y = pos().y() + rect().height() / 2.0 - height / 2.0;
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );

QgsComposerItem::setItemRotation( r );
}

void QgsComposerShape::setCornerRadius( double radius )
{
mCornerRadius = radius;
}

void QgsComposerShape::setSceneRect( const QRectF& rectangle )
{
//consider to change size of the shape if the rectangle changes width and/or height
if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
{
double newShapeWidth = rectangle.width();
double newShapeHeight = rectangle.height();
imageSizeConsideringRotation( newShapeWidth, newShapeHeight );
}

QgsComposerItem::setSceneRect( rectangle );
}
8 changes: 0 additions & 8 deletions src/core/composer/qgscomposershape.h
Expand Up @@ -61,19 +61,11 @@ class CORE_EXPORT QgsComposerShape: public QgsComposerItem
QgsComposerShape::Shape shapeType() const {return mShape;}
void setShapeType( QgsComposerShape::Shape s ) {mShape = s;}

/**Sets this items bound in scene coordinates such that 1 item size units
corresponds to 1 scene size unit. Also, the shape is scaled*/
void setSceneRect( const QRectF& rectangle );

/**Sets radius for rounded rectangle corners. Added in v2.1 */
void setCornerRadius( double radius );
/**Returns the radius for rounded rectangle corners*/
double cornerRadius() const { return mCornerRadius; };

public slots:
/**Sets item rotation and resizes item bounds such that the shape always has the same size*/
virtual void setItemRotation( double r );

protected:
/* reimplement drawFrame, since it's not a rect, but a custom shape */
virtual void drawFrame( QPainter* p );
Expand Down

0 comments on commit 5262a4d

Please sign in to comment.