Skip to content

Commit 0cd7ceb

Browse files
committedJun 28, 2012
[FEATURE]: Initial support for rotation in composer label
1 parent 4e36df0 commit 0cd7ceb

File tree

5 files changed

+86
-24
lines changed

5 files changed

+86
-24
lines changed
 

‎src/app/composer/qgscomposerlabelwidget.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ QgsComposerLabelWidget::QgsComposerLabelWidget( QgsComposerLabel* label ): QWidg
3434
{
3535
setGuiElementValues();
3636
connect( mComposerLabel, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
37+
connect( mRotationSpinBox, SIGNAL( valueChanged( double ) ), mComposerLabel, SLOT( setRotation( double ) ) );
3738
}
3839
}
3940

‎src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ SET(QGIS_CORE_MOC_HDRS
276276
composer/qgscomposerscalebar.h
277277
composer/qgscomposeritem.h
278278
composer/qgscomposeritemgroup.h
279+
composer/qgscomposerlabel.h
279280
composer/qgscomposershape.h
280281
composer/qgscomposerattributetable.h
281282
composer/qgscomposition.h

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
4141
}
4242

4343
drawBackground( painter );
44+
painter->save();
4445
painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
4546
painter->setFont( mFont );
4647

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

55+
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
56+
painter->rotate( mRotation );
57+
painter->translate( -mTextBoxWidth / 2.0, -mTextBoxHeight / 2.0 );
5458

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

61+
painter->restore();
62+
5763
drawFrame( painter );
5864
if ( isSelected() )
5965
{
@@ -105,15 +111,34 @@ void QgsComposerLabel::adjustSizeToText()
105111
double textWidth = textWidthMillimeters( mFont, displayText() );
106112
double fontAscent = fontAscentMillimeters( mFont );
107113

108-
setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1,
109-
fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
114+
mTextBoxWidth = textWidth + 2 * mMargin + 2 * pen().widthF() + 1;
115+
mTextBoxHeight = fontAscent + 2 * mMargin + 2 * pen().widthF() + 1;
116+
117+
double width = mTextBoxWidth;
118+
double height = mTextBoxHeight;
119+
120+
sizeChangedByRotation( width, height );
121+
122+
setSceneRect( QRectF( transform().dx(), transform().dy(), width, height ) );
110123
}
111124

112125
QFont QgsComposerLabel::font() const
113126
{
114127
return mFont;
115128
}
116129

130+
void QgsComposerLabel::setRotation( double r )
131+
{
132+
double width = mTextBoxWidth;
133+
double height = mTextBoxHeight;
134+
sizeChangedByRotation( width, height );
135+
136+
double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
137+
double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
138+
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
139+
QgsComposerItem::setRotation( r );
140+
}
141+
117142
bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
118143
{
119144
QString alignment;

‎src/core/composer/qgscomposerlabel.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
2626
{
27+
Q_OBJECT
2728
public:
2829
QgsComposerLabel( QgsComposition *composition );
2930
~QgsComposerLabel();
@@ -88,6 +89,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
8889
*/
8990
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
9091

92+
public slots:
93+
virtual void setRotation( double r );
94+
9195
private:
9296
// Text
9397
QString mText;
@@ -109,6 +113,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
109113

110114
/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
111115
void replaceDateText( QString& text ) const;
116+
117+
/**Width of the text box. This is different to rectangle().width() in case there is rotation*/
118+
double mTextBoxWidth;
119+
/**Height of the text box. This is different to rectangle().height() in case there is rotation*/
120+
double mTextBoxHeight;
112121
};
113122

114123
#endif

‎src/ui/qgscomposerlabelwidgetbase.ui

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>433</width>
10-
<height>425</height>
9+
<width>274</width>
10+
<height>488</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -30,42 +30,29 @@
3030
<rect>
3131
<x>0</x>
3232
<y>0</y>
33-
<width>415</width>
34-
<height>378</height>
33+
<width>256</width>
34+
<height>444</height>
3535
</rect>
3636
</property>
3737
<attribute name="label">
3838
<string>Label</string>
3939
</attribute>
4040
<layout class="QGridLayout" name="gridLayout">
41-
<item row="0" column="0">
41+
<item row="0" column="0" colspan="2">
4242
<widget class="QTextEdit" name="mTextEdit">
4343
<property name="lineWrapMode">
4444
<enum>QTextEdit::NoWrap</enum>
4545
</property>
4646
</widget>
4747
</item>
48-
<item row="1" column="0">
49-
<widget class="QPushButton" name="mFontButton">
50-
<property name="sizePolicy">
51-
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
52-
<horstretch>0</horstretch>
53-
<verstretch>0</verstretch>
54-
</sizepolicy>
55-
</property>
56-
<property name="text">
57-
<string>Font</string>
58-
</property>
59-
</widget>
60-
</item>
61-
<item row="2" column="0">
48+
<item row="2" column="0" colspan="2">
6249
<widget class="QPushButton" name="mFontColorButton">
6350
<property name="text">
6451
<string>Font color...</string>
6552
</property>
6653
</widget>
6754
</item>
68-
<item row="3" column="0">
55+
<item row="3" column="0" colspan="2">
6956
<widget class="QGroupBox" name="buttonGroup1">
7057
<property name="title">
7158
<string>Horizontal Alignment:</string>
@@ -111,7 +98,7 @@
11198
</layout>
11299
</widget>
113100
</item>
114-
<item row="4" column="0">
101+
<item row="4" column="0" colspan="2">
115102
<widget class="QGroupBox" name="buttonGroup2">
116103
<property name="title">
117104
<string>Vertical Alignment:</string>
@@ -154,7 +141,7 @@
154141
</layout>
155142
</widget>
156143
</item>
157-
<item row="6" column="0">
144+
<item row="5" column="0" colspan="2">
158145
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox">
159146
<property name="prefix">
160147
<string>Margin </string>
@@ -164,6 +151,45 @@
164151
</property>
165152
</widget>
166153
</item>
154+
<item row="6" column="0">
155+
<widget class="QLabel" name="mRotationLabel">
156+
<property name="sizePolicy">
157+
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
158+
<horstretch>0</horstretch>
159+
<verstretch>0</verstretch>
160+
</sizepolicy>
161+
</property>
162+
<property name="text">
163+
<string>Rotation</string>
164+
</property>
165+
<property name="wordWrap">
166+
<bool>true</bool>
167+
</property>
168+
<property name="buddy">
169+
<cstring>mRotationSpinBox</cstring>
170+
</property>
171+
</widget>
172+
</item>
173+
<item row="6" column="1">
174+
<widget class="QDoubleSpinBox" name="mRotationSpinBox">
175+
<property name="maximum">
176+
<double>360.000000000000000</double>
177+
</property>
178+
</widget>
179+
</item>
180+
<item row="1" column="0" colspan="2">
181+
<widget class="QPushButton" name="mFontButton">
182+
<property name="sizePolicy">
183+
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
184+
<horstretch>0</horstretch>
185+
<verstretch>0</verstretch>
186+
</sizepolicy>
187+
</property>
188+
<property name="text">
189+
<string>Font</string>
190+
</property>
191+
</widget>
192+
</item>
167193
</layout>
168194
</widget>
169195
</widget>

0 commit comments

Comments
 (0)
Please sign in to comment.