Skip to content

Commit 6bf395d

Browse files
author
mhugent
committed
Apply patch #2868 (adds label alignment) with modifications. Thanks medspx
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13938 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 014f568 commit 6bf395d

7 files changed

+162
-11
lines changed

src/app/composer/qgscomposerlabelwidget.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ QgsComposerLabelWidget::QgsComposerLabelWidget( QgsComposerLabel* label ): QWidg
3434
{
3535
mTextEdit->setText( mComposerLabel->text() );
3636
mMarginDoubleSpinBox->setValue( mComposerLabel->margin() );
37+
mTopRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignTop );
38+
mMiddleRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignVCenter );
39+
mBottomRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignBottom );
40+
mLeftRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignLeft );
41+
mCenterRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignHCenter );
42+
mRightRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignRight );
3743
}
3844
}
3945

@@ -88,3 +94,56 @@ void QgsComposerLabelWidget::on_mFontColorButton_clicked()
8894
mComposerLabel->setFontColor( newColor );
8995
}
9096

97+
void QgsComposerLabelWidget::on_mCenterRadioButton_clicked()
98+
{
99+
if ( mComposerLabel )
100+
{
101+
mComposerLabel->setHAlign( Qt::AlignHCenter );
102+
mComposerLabel->update();
103+
}
104+
}
105+
106+
void QgsComposerLabelWidget::on_mRightRadioButton_clicked()
107+
{
108+
if ( mComposerLabel )
109+
{
110+
mComposerLabel->setHAlign( Qt::AlignRight );
111+
mComposerLabel->update();
112+
}
113+
}
114+
115+
void QgsComposerLabelWidget::on_mLeftRadioButton_clicked()
116+
{
117+
if ( mComposerLabel )
118+
{
119+
mComposerLabel->setHAlign( Qt::AlignLeft );
120+
mComposerLabel->update();
121+
}
122+
}
123+
124+
void QgsComposerLabelWidget::on_mTopRadioButton_clicked()
125+
{
126+
if ( mComposerLabel )
127+
{
128+
mComposerLabel->setVAlign( Qt::AlignTop );
129+
mComposerLabel->update();
130+
}
131+
}
132+
133+
void QgsComposerLabelWidget::on_mBottomRadioButton_clicked()
134+
{
135+
if ( mComposerLabel )
136+
{
137+
mComposerLabel->setVAlign( Qt::AlignBottom );
138+
mComposerLabel->update();
139+
}
140+
}
141+
142+
void QgsComposerLabelWidget::on_mMiddleRadioButton_clicked()
143+
{
144+
if ( mComposerLabel )
145+
{
146+
mComposerLabel->setVAlign( Qt::AlignVCenter );
147+
mComposerLabel->update();
148+
}
149+
}

src/app/composer/qgscomposerlabelwidget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
3636
void on_mFontButton_clicked();
3737
void on_mMarginDoubleSpinBox_valueChanged( double d );
3838
void on_mFontColorButton_clicked();
39+
void on_mCenterRadioButton_clicked();
40+
void on_mLeftRadioButton_clicked();
41+
void on_mRightRadioButton_clicked();
42+
void on_mTopRadioButton_clicked();
43+
void on_mBottomRadioButton_clicked();
44+
void on_mMiddleRadioButton_clicked();
3945

4046
private:
4147
QgsComposerLabel* mComposerLabel;

src/core/composer/qgscomposeritem.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ void QgsComposerItem::drawText( QPainter* p, double x, double y, const QString&
706706
p->restore();
707707
}
708708

709-
void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font ) const
709+
void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement, Qt::AlignmentFlag valignment ) const
710710
{
711711
QFont textFont = scaledFontPixelSize( font );
712712

@@ -717,10 +717,9 @@ void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString&
717717
p->setFont( textFont );
718718
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
719719
p->scale( scaleFactor, scaleFactor );
720-
p->drawText( scaledRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );
720+
p->drawText( scaledRect, halignement | valignment | Qt::TextWordWrap, text );
721721
p->restore();
722722
}
723-
724723
void QgsComposerItem::drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const
725724
{
726725
if ( !p )

src/core/composer/qgscomposeritem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
143143
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
144144

145145
/**Like the above, but with a rectangle for multiline text*/
146-
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font ) const;
146+
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement = Qt::AlignLeft, Qt::AlignmentFlag valignement = Qt::AlignTop ) const;
147147

148148
/**Returns the font width in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
149149
double textWidthMillimeters( const QFont& font, const QString& text ) const;

src/core/composer/qgscomposerlabel.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#include <QDomElement>
2121
#include <QPainter>
2222

23-
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) )
23+
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ), \
24+
mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop )
2425
{
2526
//default font size is 10 point
2627
mFont.setPointSizeF( 10 );
@@ -49,7 +50,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
4950
rect().height() - 2 * penWidth - 2 * mMargin );
5051

5152

52-
drawText( painter, painterRect, displayText(), mFont );
53+
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
5354

5455
drawFrame( painter );
5556
if ( isSelected() )
@@ -112,6 +113,8 @@ QFont QgsComposerLabel::font() const
112113

113114
bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
114115
{
116+
QString alignment;
117+
115118
if ( elem.isNull() )
116119
{
117120
return false;
@@ -122,6 +125,9 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
122125
composerLabelElem.setAttribute( "labelText", mText );
123126
composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );
124127

128+
composerLabelElem.setAttribute( "halign", mHAlignment );
129+
composerLabelElem.setAttribute( "valign", mVAlignment );
130+
125131

126132
//font
127133
QDomElement labelFontElem = doc.createElement( "LabelFont" );
@@ -141,6 +147,8 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
141147

142148
bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument& doc )
143149
{
150+
QString alignment;
151+
144152
if ( itemElem.isNull() )
145153
{
146154
return false;
@@ -154,6 +162,12 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
154162
//margin
155163
mMargin = itemElem.attribute( "margin" ).toDouble();
156164

165+
//Horizontal alignment
166+
mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );
167+
168+
//Vertical alignment
169+
mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "valign" ).toInt() );
170+
157171
//font
158172
QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
159173
if ( labelFontList.size() > 0 )

src/core/composer/qgscomposerlabel.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
4343

4444
QFont font() const;
4545
void setFont( const QFont& f );
46+
Qt::AlignmentFlag vAlign() const { return mVAlignment; }
47+
Qt::AlignmentFlag hAlign() const { return mHAlignment; }
48+
void setHAlign( Qt::AlignmentFlag a ) {mHAlignment = a;}
49+
void setVAlign( Qt::AlignmentFlag a ) {mVAlignment = a;}
4650
double margin() {return mMargin;}
4751
void setMargin( double m ) {mMargin = m;}
4852

@@ -77,6 +81,12 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
7781
// Font color
7882
QColor mFontColor;
7983

84+
// Horizontal Alignment
85+
Qt::AlignmentFlag mHAlignment;
86+
87+
// Vertical Alignment
88+
Qt::AlignmentFlag mVAlignment;
89+
8090
/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
8191
void replaceDateText( QString& text ) const;
8292
};

src/ui/qgscomposerlabelwidgetbase.ui

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>203</width>
9+
<width>519</width>
1010
<height>362</height>
1111
</rect>
1212
</property>
@@ -30,8 +30,8 @@
3030
<rect>
3131
<x>0</x>
3232
<y>0</y>
33-
<width>185</width>
34-
<height>317</height>
33+
<width>486</width>
34+
<height>320</height>
3535
</rect>
3636
</property>
3737
<attribute name="label">
@@ -65,7 +65,7 @@
6565
</property>
6666
</widget>
6767
</item>
68-
<item row="3" column="0">
68+
<item row="5" column="0">
6969
<widget class="QLabel" name="mMarginTextLabel">
7070
<property name="text">
7171
<string>Margin (mm)</string>
@@ -75,9 +75,72 @@
7575
</property>
7676
</widget>
7777
</item>
78-
<item row="4" column="0">
78+
<item row="6" column="0">
7979
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox"/>
8080
</item>
81+
<item row="3" column="0">
82+
<widget class="QGroupBox" name="buttonGroup1">
83+
<property name="title">
84+
<string>Horizontal Alignment:</string>
85+
</property>
86+
<layout class="QHBoxLayout" name="horizontalLayout">
87+
<property name="sizeConstraint">
88+
<enum>QLayout::SetMinimumSize</enum>
89+
</property>
90+
<item>
91+
<widget class="QRadioButton" name="mLeftRadioButton">
92+
<property name="text">
93+
<string>Left</string>
94+
</property>
95+
</widget>
96+
</item>
97+
<item>
98+
<widget class="QRadioButton" name="mCenterRadioButton">
99+
<property name="text">
100+
<string>Center</string>
101+
</property>
102+
</widget>
103+
</item>
104+
<item>
105+
<widget class="QRadioButton" name="mRightRadioButton">
106+
<property name="text">
107+
<string>Right</string>
108+
</property>
109+
</widget>
110+
</item>
111+
</layout>
112+
</widget>
113+
</item>
114+
<item row="4" column="0">
115+
<widget class="QGroupBox" name="buttonGroup2">
116+
<property name="title">
117+
<string>Vertical Alignment:</string>
118+
</property>
119+
<layout class="QHBoxLayout" name="horizontalLayout_2">
120+
<item>
121+
<widget class="QRadioButton" name="mTopRadioButton">
122+
<property name="text">
123+
<string>Top</string>
124+
</property>
125+
</widget>
126+
</item>
127+
<item>
128+
<widget class="QRadioButton" name="mMiddleRadioButton">
129+
<property name="text">
130+
<string>Middle</string>
131+
</property>
132+
</widget>
133+
</item>
134+
<item>
135+
<widget class="QRadioButton" name="mBottomRadioButton">
136+
<property name="text">
137+
<string>Bottom</string>
138+
</property>
139+
</widget>
140+
</item>
141+
</layout>
142+
</widget>
143+
</item>
81144
</layout>
82145
</widget>
83146
</widget>

0 commit comments

Comments
 (0)