Skip to content
Permalink
Browse files

[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 5262a4d6830103a1dae1fbe8f66c017e75319a7f
@@ -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
@@ -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();
@@ -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();
}
@@ -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 )
{
@@ -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;
@@ -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 );
@@ -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
@@ -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 );

@@ -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:
@@ -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 );
@@ -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 );
}
@@ -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 );

0 comments on commit 5262a4d

Please sign in to comment.
You can’t perform that action at this time.