Skip to content
Permalink
Browse files

[FEATURE]: Initial support for rotation in composer label

  • Loading branch information
mhugent committed Jun 28, 2012
1 parent 4e36df0 commit 0cd7cebd70a8f3ec6e730d8cc6f2843b5be0c195
@@ -34,6 +34,7 @@ QgsComposerLabelWidget::QgsComposerLabelWidget( QgsComposerLabel* label ): QWidg
{
setGuiElementValues();
connect( mComposerLabel, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
connect( mRotationSpinBox, SIGNAL( valueChanged( double ) ), mComposerLabel, SLOT( setRotation( double ) ) );
}
}

@@ -276,6 +276,7 @@ SET(QGIS_CORE_MOC_HDRS
composer/qgscomposerscalebar.h
composer/qgscomposeritem.h
composer/qgscomposeritemgroup.h
composer/qgscomposerlabel.h
composer/qgscomposershape.h
composer/qgscomposerattributetable.h
composer/qgscomposition.h
@@ -41,6 +41,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
}

drawBackground( painter );
painter->save();
painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
painter->setFont( mFont );

@@ -51,9 +52,14 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
rect().height() - 2 * penWidth - 2 * mMargin );

painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
painter->rotate( mRotation );
painter->translate( -mTextBoxWidth / 2.0, -mTextBoxHeight / 2.0 );

drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );

painter->restore();

drawFrame( painter );
if ( isSelected() )
{
@@ -105,15 +111,34 @@ void QgsComposerLabel::adjustSizeToText()
double textWidth = textWidthMillimeters( mFont, displayText() );
double fontAscent = fontAscentMillimeters( mFont );

setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1,
fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
mTextBoxWidth = textWidth + 2 * mMargin + 2 * pen().widthF() + 1;
mTextBoxHeight = fontAscent + 2 * mMargin + 2 * pen().widthF() + 1;

double width = mTextBoxWidth;
double height = mTextBoxHeight;

sizeChangedByRotation( width, height );

setSceneRect( QRectF( transform().dx(), transform().dy(), width, height ) );
}

QFont QgsComposerLabel::font() const
{
return mFont;
}

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

double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
QgsComposerItem::setRotation( r );
}

bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
{
QString alignment;
@@ -24,6 +24,7 @@
*/
class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
{
Q_OBJECT
public:
QgsComposerLabel( QgsComposition *composition );
~QgsComposerLabel();
@@ -88,6 +89,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

public slots:
virtual void setRotation( double r );

private:
// Text
QString mText;
@@ -109,6 +113,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem

/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
void replaceDateText( QString& text ) const;

/**Width of the text box. This is different to rectangle().width() in case there is rotation*/
double mTextBoxWidth;
/**Height of the text box. This is different to rectangle().height() in case there is rotation*/
double mTextBoxHeight;
};

#endif
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>433</width>
<height>425</height>
<width>274</width>
<height>488</height>
</rect>
</property>
<property name="sizePolicy">
@@ -30,42 +30,29 @@
<rect>
<x>0</x>
<y>0</y>
<width>415</width>
<height>378</height>
<width>256</width>
<height>444</height>
</rect>
</property>
<attribute name="label">
<string>Label</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<item row="0" column="0" colspan="2">
<widget class="QTextEdit" name="mTextEdit">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="mFontButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Font</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="2" column="0" colspan="2">
<widget class="QPushButton" name="mFontColorButton">
<property name="text">
<string>Font color...</string>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="3" column="0" colspan="2">
<widget class="QGroupBox" name="buttonGroup1">
<property name="title">
<string>Horizontal Alignment:</string>
@@ -111,7 +98,7 @@
</layout>
</widget>
</item>
<item row="4" column="0">
<item row="4" column="0" colspan="2">
<widget class="QGroupBox" name="buttonGroup2">
<property name="title">
<string>Vertical Alignment:</string>
@@ -154,7 +141,7 @@
</layout>
</widget>
</item>
<item row="6" column="0">
<item row="5" column="0" colspan="2">
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox">
<property name="prefix">
<string>Margin </string>
@@ -164,6 +151,45 @@
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="mRotationLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Rotation</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="buddy">
<cstring>mRotationSpinBox</cstring>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QDoubleSpinBox" name="mRotationSpinBox">
<property name="maximum">
<double>360.000000000000000</double>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="mFontButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Font</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>

0 comments on commit 0cd7ceb

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