Skip to content

Commit dbd573b

Browse files
committedJun 29, 2012
Merge branch 'label_rotation'
2 parents 53dd796 + 663f5ad commit dbd573b

9 files changed

+133
-30
lines changed
 

‎src/app/composer/qgscomposerlabelwidget.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ void QgsComposerLabelWidget::on_mLabelIdLineEdit_textChanged( const QString& tex
172172
}
173173
}
174174

175+
void QgsComposerLabelWidget::on_mRotationSpinBox_valueChanged( double v )
176+
{
177+
if ( mComposerLabel )
178+
{
179+
mComposerLabel->beginCommand( tr( "Label rotation changed" ), QgsComposerMergeCommand::ComposerLabelRotation );
180+
mComposerLabel->setRotation( v );
181+
mComposerLabel->update();
182+
mComposerLabel->endCommand();
183+
}
184+
}
185+
175186
void QgsComposerLabelWidget::setGuiElementValues()
176187
{
177188
blockAllSignals( true );
@@ -184,6 +195,7 @@ void QgsComposerLabelWidget::setGuiElementValues()
184195
mLeftRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignLeft );
185196
mCenterRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignHCenter );
186197
mRightRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignRight );
198+
mRotationSpinBox->setValue( mComposerLabel->rotation() );
187199
blockAllSignals( false );
188200
}
189201

@@ -197,5 +209,5 @@ void QgsComposerLabelWidget::blockAllSignals( bool block )
197209
mLeftRadioButton->blockSignals( block );
198210
mCenterRadioButton->blockSignals( block );
199211
mRightRadioButton->blockSignals( block );
200-
212+
mRotationSpinBox->blockSignals( block );
201213
}

‎src/app/composer/qgscomposerlabelwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
4343
void on_mBottomRadioButton_clicked();
4444
void on_mMiddleRadioButton_clicked();
4545
void on_mLabelIdLineEdit_textChanged( const QString& text );
46+
void on_mRotationSpinBox_valueChanged( double v );
4647

4748
private slots:
4849
void setGuiElementValues();

‎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/qgscomposeritemcommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ QgsComposerMergeCommand::~QgsComposerMergeCommand()
8888
bool QgsComposerMergeCommand::mergeWith( const QUndoCommand * command )
8989
{
9090
const QgsComposerItemCommand* c = dynamic_cast<const QgsComposerItemCommand*>( command );
91-
if ( !c )
91+
if ( !c || mItem != c->item() )
9292
{
9393
return false;
9494
}

‎src/core/composer/qgscomposeritemcommand.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class CORE_EXPORT QgsComposerItemCommand: public QUndoCommand
4646
/**Returns true if previous state and after state are valid and different*/
4747
bool containsChange() const;
4848

49+
const QgsComposerItem* item() const { return mItem; }
50+
4951
protected:
5052
/**Target item of the command*/
5153
QgsComposerItem* mItem;
@@ -72,6 +74,7 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
7274
//composer label
7375
ComposerLabelSetText,
7476
ComposerLabelSetId,
77+
ComposerLabelRotation,
7578
//composer map
7679
ComposerMapRotation,
7780
ComposerMapAnnotationDistance,
@@ -107,7 +110,8 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
107110
ArrowOutlineWidth,
108111
ArrowHeadWidth,
109112
//item
110-
ItemOutlineWidth
113+
ItemOutlineWidth,
114+
ItemMove
111115
};
112116

113117
QgsComposerMergeCommand( Context c, QgsComposerItem* item, const QString& text );

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,26 @@ 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

4748
QFontMetricsF fontSize( mFont );
4849

4950
//support multiline labels
5051
double penWidth = pen().widthF();
51-
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
52-
rect().height() - 2 * penWidth - 2 * mMargin );
52+
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, mTextBoxWidth - 2 * penWidth - 2 * mMargin, mTextBoxHeight - 2 * penWidth - 2 * mMargin );
53+
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
54+
painter->rotate( mRotation );
55+
painter->translate( -mTextBoxWidth / 2.0, -mTextBoxHeight / 2.0 );
56+
57+
//debug
58+
painter->setPen( QColor( Qt::red ) );
59+
painter->drawRect( painterRect );
60+
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
5361

5462

55-
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
63+
painter->restore();
5664

5765
drawFrame( painter );
5866
if ( isSelected() )
@@ -105,15 +113,47 @@ void QgsComposerLabel::adjustSizeToText()
105113
double textWidth = textWidthMillimeters( mFont, displayText() );
106114
double fontAscent = fontAscentMillimeters( mFont );
107115

108-
setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1,
109-
fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
116+
mTextBoxWidth = textWidth + 2 * mMargin + 2 * pen().widthF() + 1;
117+
mTextBoxHeight = fontAscent + 2 * mMargin + 2 * pen().widthF() + 1;
118+
119+
double width = mTextBoxWidth;
120+
double height = mTextBoxHeight;
121+
122+
sizeChangedByRotation( width, height );
123+
124+
QgsComposerItem::setSceneRect( QRectF( transform().dx(), transform().dy(), width, height ) );
110125
}
111126

112127
QFont QgsComposerLabel::font() const
113128
{
114129
return mFont;
115130
}
116131

132+
void QgsComposerLabel::setRotation( double r )
133+
{
134+
double width = mTextBoxWidth;
135+
double height = mTextBoxHeight;
136+
QgsComposerItem::setRotation( r );
137+
sizeChangedByRotation( width, height );
138+
139+
double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
140+
double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
141+
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
142+
}
143+
144+
void QgsComposerLabel::setSceneRect( const QRectF& rectangle )
145+
{
146+
if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
147+
{
148+
double textBoxWidth = rectangle.width();
149+
double textBoxHeight = rectangle.height();
150+
imageSizeConsideringRotation( textBoxWidth, textBoxHeight );
151+
mTextBoxWidth = textBoxWidth;
152+
mTextBoxHeight = textBoxHeight;
153+
}
154+
QgsComposerItem::setSceneRect( rectangle );
155+
}
156+
117157
bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
118158
{
119159
QString alignment;

‎src/core/composer/qgscomposerlabel.h

Lines changed: 11 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();
@@ -76,6 +77,8 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
7677
@note: this function was added in version 1.4*/
7778
QColor fontColor() const {return mFontColor;}
7879

80+
void setSceneRect( const QRectF& rectangle );
81+
7982
/** stores state in Dom element
8083
* @param elem is Dom element corresponding to 'Composer' tag
8184
* @param doc document
@@ -88,6 +91,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
8891
*/
8992
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
9093

94+
public slots:
95+
virtual void setRotation( double r );
96+
9197
private:
9298
// Text
9399
QString mText;
@@ -109,6 +115,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
109115

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

114125
#endif

‎src/gui/qgscomposerview.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,28 +502,36 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
502502
{
503503
for ( ; itemIt != composerItemList.end(); ++itemIt )
504504
{
505+
( *itemIt )->beginCommand( tr( "Item moved" ), QgsComposerMergeCommand::ItemMove );
505506
( *itemIt )->move( -1.0, 0.0 );
507+
( *itemIt )->endCommand();
506508
}
507509
}
508510
else if ( e->key() == Qt::Key_Right )
509511
{
510512
for ( ; itemIt != composerItemList.end(); ++itemIt )
511513
{
514+
( *itemIt )->beginCommand( tr( "Item moved" ), QgsComposerMergeCommand::ItemMove );
512515
( *itemIt )->move( 1.0, 0.0 );
516+
( *itemIt )->endCommand();
513517
}
514518
}
515519
else if ( e->key() == Qt::Key_Down )
516520
{
517521
for ( ; itemIt != composerItemList.end(); ++itemIt )
518522
{
523+
( *itemIt )->beginCommand( tr( "Item moved" ), QgsComposerMergeCommand::ItemMove );
519524
( *itemIt )->move( 0.0, 1.0 );
525+
( *itemIt )->endCommand();
520526
}
521527
}
522528
else if ( e->key() == Qt::Key_Up )
523529
{
524530
for ( ; itemIt != composerItemList.end(); ++itemIt )
525531
{
532+
( *itemIt )->beginCommand( tr( "Item moved" ), QgsComposerMergeCommand::ItemMove );
526533
( *itemIt )->move( 0.0, -1.0 );
534+
( *itemIt )->endCommand();
527535
}
528536
}
529537
}

‎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.