Skip to content

Commit

Permalink
[FEATURE]: Initial support for rotation in composer label
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 28, 2012
1 parent 4e36df0 commit 0cd7ceb
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerlabelwidget.cpp
Expand Up @@ -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 ) ) );
}
}

Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -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
Expand Down
29 changes: 27 additions & 2 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -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 );

Expand All @@ -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() )
{
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -24,6 +24,7 @@
*/
class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
{
Q_OBJECT
public:
QgsComposerLabel( QgsComposition *composition );
~QgsComposerLabel();
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
70 changes: 48 additions & 22 deletions src/ui/qgscomposerlabelwidgetbase.ui
Expand Up @@ -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">
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 0cd7ceb

Please sign in to comment.