Skip to content

Commit a2a094b

Browse files
committedOct 31, 2014
[FEATURE][composer] Allow control of hoz and vert margins for labels
Previously only a single margin setting would apply to both horizontal and vertical margins. This change allows users to specify different horizontal and vertical margins.
1 parent c180fec commit a2a094b

File tree

7 files changed

+275
-94
lines changed

7 files changed

+275
-94
lines changed
 

‎python/core/composer/qgscomposerlabel.sip

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,54 @@ class QgsComposerLabel : QgsComposerItem
5252
* @returns void
5353
*/
5454
void setVAlign( Qt::AlignmentFlag a );
55-
//!brief Accessor for the margin of the label
56-
double margin();
57-
//!brief Mutator for the margin of the label
58-
void setMargin( double m );
55+
56+
/**Returns the margin between the edge of the frame and the label contents
57+
* @returns margin in mm
58+
* @deprecated use marginX and marginY instead
59+
*/
60+
double margin() /Deprecated/;
61+
62+
/**Returns the horizontal margin between the edge of the frame and the label
63+
* contents.
64+
* @returns horizontal margin in mm
65+
* @note added in QGIS 2.7
66+
*/
67+
double marginX() const;
68+
69+
/**Returns the vertical margin between the edge of the frame and the label
70+
* contents.
71+
* @returns vertical margin in mm
72+
* @note added in QGIS 2.7
73+
*/
74+
double marginY() const;
75+
76+
/**Sets the margin between the edge of the frame and the label contents.
77+
* This method sets both the horizontal and vertical margins to the same
78+
* value. The margins can be individually controlled using the setMarginX
79+
* and setMarginY methods.
80+
* @param m margin in mm
81+
* @see setMarginX
82+
* @see setMarginY
83+
*/
84+
void setMargin( const double m );
85+
86+
/**Sets the horizontal margin between the edge of the frame and the label
87+
* contents.
88+
* @param margin horizontal margin in mm
89+
* @see setMargin
90+
* @see setMarginY
91+
* @note added in QGIS 2.7
92+
*/
93+
void setMarginX( const double margin );
94+
95+
/**Sets the vertical margin between the edge of the frame and the label
96+
* contents.
97+
* @param margin vertical margin in mm
98+
* @see setMargin
99+
* @see setMarginX
100+
* @note added in QGIS 2.7
101+
*/
102+
void setMarginY( const double margin );
59103

60104
/**Sets text color
61105
@note: this function was added in version 1.4*/

‎src/app/composer/qgscomposerlabelwidget.cpp

100755100644
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,23 @@ void QgsComposerLabelWidget::on_mFontButton_clicked()
104104
}
105105
}
106106

107-
void QgsComposerLabelWidget::on_mMarginDoubleSpinBox_valueChanged( double d )
107+
void QgsComposerLabelWidget::on_mMarginXDoubleSpinBox_valueChanged( double d )
108108
{
109109
if ( mComposerLabel )
110110
{
111111
mComposerLabel->beginCommand( tr( "Label margin changed" ) );
112-
mComposerLabel->setMargin( d );
112+
mComposerLabel->setMarginX( d );
113+
mComposerLabel->update();
114+
mComposerLabel->endCommand();
115+
}
116+
}
117+
118+
void QgsComposerLabelWidget::on_mMarginYDoubleSpinBox_valueChanged( double d )
119+
{
120+
if ( mComposerLabel )
121+
{
122+
mComposerLabel->beginCommand( tr( "Label margin changed" ) );
123+
mComposerLabel->setMarginY( d );
113124
mComposerLabel->update();
114125
mComposerLabel->endCommand();
115126
}
@@ -228,7 +239,8 @@ void QgsComposerLabelWidget::setGuiElementValues()
228239
blockAllSignals( true );
229240
mTextEdit->setPlainText( mComposerLabel->text() );
230241
mTextEdit->moveCursor( QTextCursor::End, QTextCursor::MoveAnchor );
231-
mMarginDoubleSpinBox->setValue( mComposerLabel->margin() );
242+
mMarginXDoubleSpinBox->setValue( mComposerLabel->marginX() );
243+
mMarginYDoubleSpinBox->setValue( mComposerLabel->marginY() );
232244
mHtmlCheckBox->setChecked( mComposerLabel->htmlState() );
233245
mTopRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignTop );
234246
mMiddleRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignVCenter );
@@ -244,7 +256,8 @@ void QgsComposerLabelWidget::blockAllSignals( bool block )
244256
{
245257
mTextEdit->blockSignals( block );
246258
mHtmlCheckBox->blockSignals( block );
247-
mMarginDoubleSpinBox->blockSignals( block );
259+
mMarginXDoubleSpinBox->blockSignals( block );
260+
mMarginYDoubleSpinBox->blockSignals( block );
248261
mTopRadioButton->blockSignals( block );
249262
mMiddleRadioButton->blockSignals( block );
250263
mBottomRadioButton->blockSignals( block );

‎src/app/composer/qgscomposerlabelwidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class QgsComposerLabelWidget: public QgsComposerItemBaseWidget, private Ui::QgsC
3737
void on_mTextEdit_textChanged();
3838
void on_mFontButton_clicked();
3939
void on_mInsertExpressionButton_clicked();
40-
void on_mMarginDoubleSpinBox_valueChanged( double d );
40+
void on_mMarginXDoubleSpinBox_valueChanged( double d );
41+
void on_mMarginYDoubleSpinBox_valueChanged( double d );
4142
void on_mFontColorButton_colorChanged( const QColor& newLabelColor );
4243
void on_mCenterRadioButton_clicked();
4344
void on_mLeftRadioButton_clicked();

‎src/core/composer/qgscomposerlabel.cpp

100755100644
Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
3939
, mHtmlState( 0 )
4040
, mHtmlUnitsToMM( 1.0 )
4141
, mHtmlLoaded( false )
42-
, mMargin( 1.0 )
42+
, mMarginX( 1.0 )
43+
, mMarginY( 1.0 )
4344
, mFontColor( QColor( 0, 0, 0 ) )
4445
, mHAlignment( Qt::AlignLeft )
4546
, mVAlignment( Qt::AlignTop )
@@ -104,7 +105,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
104105
painter->setRenderHint( QPainter::Antialiasing, true );
105106

106107
double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
107-
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin, rect().height() - 2 * penWidth - 2 * mMargin );
108+
QRectF painterRect( penWidth + mMarginX, penWidth + mMarginY, rect().width() - 2 * penWidth - 2 * mMarginX, rect().height() - 2 * penWidth - 2 * mMarginY );
108109

109110
QString textToDraw = displayText();
110111

@@ -309,15 +310,31 @@ void QgsComposerLabel::setFont( const QFont& f )
309310
mFont = f;
310311
}
311312

313+
void QgsComposerLabel::setMargin( const double m )
314+
{
315+
mMarginX = m;
316+
mMarginY = m;
317+
}
318+
319+
void QgsComposerLabel::setMarginX( const double margin )
320+
{
321+
mMarginX = margin;
322+
}
323+
324+
void QgsComposerLabel::setMarginY( const double margin )
325+
{
326+
mMarginY = margin;
327+
}
328+
312329
void QgsComposerLabel::adjustSizeToText()
313330
{
314331
double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
315332
double fontHeight = QgsComposerUtils::fontHeightMM( mFont );
316333

317334
double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
318335

319-
double width = textWidth + 2 * mMargin + 2 * penWidth + 1;
320-
double height = fontHeight + 2 * mMargin + 2 * penWidth;
336+
double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
337+
double height = fontHeight + 2 * mMarginY + 2 * penWidth;
321338

322339
//keep alignment point constant
323340
double xShift = 0;
@@ -348,8 +365,8 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
348365
composerLabelElem.setAttribute( "htmlState", mHtmlState );
349366

350367
composerLabelElem.setAttribute( "labelText", mText );
351-
composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );
352-
368+
composerLabelElem.setAttribute( "marginX", QString::number( mMarginX ) );
369+
composerLabelElem.setAttribute( "marginY", QString::number( mMarginY ) );
353370
composerLabelElem.setAttribute( "halign", mHAlignment );
354371
composerLabelElem.setAttribute( "valign", mVAlignment );
355372

@@ -387,7 +404,17 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
387404
mHtmlState = itemElem.attribute( "htmlState" ).toInt();
388405

389406
//margin
390-
mMargin = itemElem.attribute( "margin" ).toDouble();
407+
bool marginXOk = false;
408+
bool marginYOk = false;
409+
mMarginX = itemElem.attribute( "marginX" ).toDouble( &marginXOk );
410+
mMarginY = itemElem.attribute( "marginY" ).toDouble( &marginYOk );
411+
if ( !marginXOk || !marginYOk )
412+
{
413+
//upgrade old projects where margins where stored in a single attribute
414+
double margin = itemElem.attribute( "margin", "1.0" ).toDouble();
415+
mMarginX = margin;
416+
mMarginY = margin;
417+
}
391418

392419
//Horizontal alignment
393420
mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );

‎src/core/composer/qgscomposerlabel.h

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,54 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
7575
* @returns void
7676
*/
7777
void setVAlign( Qt::AlignmentFlag a ) { mVAlignment = a; }
78-
//!brief Accessor for the margin of the label
79-
double margin() { return mMargin; }
80-
//!brief Mutator for the margin of the label
81-
void setMargin( double m ) { mMargin = m; }
78+
79+
/**Returns the margin between the edge of the frame and the label contents
80+
* @returns margin in mm
81+
* @deprecated use marginX and marginY instead
82+
*/
83+
Q_DECL_DEPRECATED double margin() { return mMarginX; }
84+
85+
/**Returns the horizontal margin between the edge of the frame and the label
86+
* contents.
87+
* @returns horizontal margin in mm
88+
* @note added in QGIS 2.7
89+
*/
90+
double marginX() const { return mMarginX; }
91+
92+
/**Returns the vertical margin between the edge of the frame and the label
93+
* contents.
94+
* @returns vertical margin in mm
95+
* @note added in QGIS 2.7
96+
*/
97+
double marginY() const { return mMarginY; }
98+
99+
/**Sets the margin between the edge of the frame and the label contents.
100+
* This method sets both the horizontal and vertical margins to the same
101+
* value. The margins can be individually controlled using the setMarginX
102+
* and setMarginY methods.
103+
* @param m margin in mm
104+
* @see setMarginX
105+
* @see setMarginY
106+
*/
107+
void setMargin( const double m );
108+
109+
/**Sets the horizontal margin between the edge of the frame and the label
110+
* contents.
111+
* @param margin horizontal margin in mm
112+
* @see setMargin
113+
* @see setMarginY
114+
* @note added in QGIS 2.7
115+
*/
116+
void setMarginX( const double margin );
117+
118+
/**Sets the vertical margin between the edge of the frame and the label
119+
* contents.
120+
* @param margin vertical margin in mm
121+
* @see setMargin
122+
* @see setMarginX
123+
* @note added in QGIS 2.7
124+
*/
125+
void setMarginY( const double margin );
82126

83127
/**Sets text color
84128
*/
@@ -125,8 +169,10 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
125169
// Font
126170
QFont mFont;
127171

128-
// Border between text and fram (in mm)
129-
double mMargin;
172+
/**Horizontal margin between contents and frame (in mm)*/
173+
double mMarginX;
174+
/**Vertical margin between contents and frame (in mm)*/
175+
double mMarginY;
130176

131177
// Font color
132178
QColor mFontColor;

‎src/ui/qgscomposerlabelwidgetbase.ui

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -117,71 +117,62 @@
117117
<bool>false</bool>
118118
</property>
119119
<layout class="QGridLayout" name="gridLayout">
120-
<item row="0" column="0" colspan="2">
121-
<widget class="QPushButton" name="mFontButton">
122-
<property name="sizePolicy">
123-
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
124-
<horstretch>0</horstretch>
125-
<verstretch>0</verstretch>
126-
</sizepolicy>
127-
</property>
128-
<property name="text">
129-
<string>Font...</string>
130-
</property>
131-
</widget>
132-
</item>
133120
<item row="1" column="0">
134121
<widget class="QLabel" name="label_2">
135122
<property name="text">
136123
<string>Font color</string>
137124
</property>
138125
</widget>
139126
</item>
140-
<item row="1" column="1">
141-
<layout class="QHBoxLayout" name="horizontalLayout">
127+
<item row="7" column="0" colspan="2">
128+
<layout class="QHBoxLayout" name="horizontalLayout_6">
142129
<item>
143-
<widget class="QgsColorButtonV2" name="mFontColorButton">
144-
<property name="minimumSize">
145-
<size>
146-
<width>120</width>
147-
<height>0</height>
148-
</size>
130+
<widget class="QRadioButton" name="mTopRadioButton">
131+
<property name="text">
132+
<string>Top</string>
149133
</property>
150-
<property name="maximumSize">
151-
<size>
152-
<width>120</width>
153-
<height>16777215</height>
154-
</size>
134+
<attribute name="buttonGroup">
135+
<string notr="true">buttonGroup</string>
136+
</attribute>
137+
</widget>
138+
</item>
139+
<item>
140+
<widget class="QRadioButton" name="mMiddleRadioButton">
141+
<property name="text">
142+
<string>Middle</string>
155143
</property>
144+
<attribute name="buttonGroup">
145+
<string notr="true">buttonGroup</string>
146+
</attribute>
147+
</widget>
148+
</item>
149+
<item>
150+
<widget class="QRadioButton" name="mBottomRadioButton">
156151
<property name="text">
157-
<string/>
152+
<string>Bottom</string>
158153
</property>
154+
<attribute name="buttonGroup">
155+
<string notr="true">buttonGroup</string>
156+
</attribute>
159157
</widget>
160158
</item>
161159
<item>
162-
<spacer name="horizontalSpacer_2">
160+
<spacer name="horizontalSpacer_3">
163161
<property name="orientation">
164162
<enum>Qt::Horizontal</enum>
165163
</property>
166164
<property name="sizeHint" stdset="0">
167165
<size>
168-
<width>40</width>
166+
<width>0</width>
169167
<height>20</height>
170168
</size>
171169
</property>
172170
</spacer>
173171
</item>
174172
</layout>
175173
</item>
176-
<item row="2" column="0">
177-
<widget class="QLabel" name="mMarginLabel">
178-
<property name="text">
179-
<string>Margin</string>
180-
</property>
181-
</widget>
182-
</item>
183174
<item row="2" column="1">
184-
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox">
175+
<widget class="QDoubleSpinBox" name="mMarginXDoubleSpinBox">
185176
<property name="prefix">
186177
<string/>
187178
</property>
@@ -190,7 +181,7 @@
190181
</property>
191182
</widget>
192183
</item>
193-
<item row="4" column="0" colspan="2">
184+
<item row="5" column="0" colspan="2">
194185
<layout class="QHBoxLayout" name="horizontalLayout_5">
195186
<item>
196187
<widget class="QRadioButton" name="mLeftRadioButton">
@@ -237,64 +228,87 @@
237228
</item>
238229
</layout>
239230
</item>
231+
<item row="4" column="0" colspan="2">
232+
<widget class="QLabel" name="mHorizontalAlignementLabel">
233+
<property name="text">
234+
<string>Horizontal alignment</string>
235+
</property>
236+
</widget>
237+
</item>
240238
<item row="6" column="0" colspan="2">
241-
<layout class="QHBoxLayout" name="horizontalLayout_6">
239+
<widget class="QLabel" name="mVerticalAlignementLabel">
240+
<property name="text">
241+
<string>Vertical alignment</string>
242+
</property>
243+
</widget>
244+
</item>
245+
<item row="0" column="0" colspan="2">
246+
<widget class="QPushButton" name="mFontButton">
247+
<property name="sizePolicy">
248+
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
249+
<horstretch>0</horstretch>
250+
<verstretch>0</verstretch>
251+
</sizepolicy>
252+
</property>
253+
<property name="text">
254+
<string>Font...</string>
255+
</property>
256+
</widget>
257+
</item>
258+
<item row="2" column="0">
259+
<widget class="QLabel" name="mMarginXLabel">
260+
<property name="text">
261+
<string>Horizontal margin</string>
262+
</property>
263+
</widget>
264+
</item>
265+
<item row="1" column="1">
266+
<layout class="QHBoxLayout" name="horizontalLayout">
242267
<item>
243-
<widget class="QRadioButton" name="mTopRadioButton">
244-
<property name="text">
245-
<string>Top</string>
268+
<widget class="QgsColorButtonV2" name="mFontColorButton">
269+
<property name="minimumSize">
270+
<size>
271+
<width>120</width>
272+
<height>0</height>
273+
</size>
246274
</property>
247-
<attribute name="buttonGroup">
248-
<string notr="true">buttonGroup</string>
249-
</attribute>
250-
</widget>
251-
</item>
252-
<item>
253-
<widget class="QRadioButton" name="mMiddleRadioButton">
254-
<property name="text">
255-
<string>Middle</string>
275+
<property name="maximumSize">
276+
<size>
277+
<width>120</width>
278+
<height>16777215</height>
279+
</size>
256280
</property>
257-
<attribute name="buttonGroup">
258-
<string notr="true">buttonGroup</string>
259-
</attribute>
260-
</widget>
261-
</item>
262-
<item>
263-
<widget class="QRadioButton" name="mBottomRadioButton">
264281
<property name="text">
265-
<string>Bottom</string>
282+
<string/>
266283
</property>
267-
<attribute name="buttonGroup">
268-
<string notr="true">buttonGroup</string>
269-
</attribute>
270284
</widget>
271285
</item>
272286
<item>
273-
<spacer name="horizontalSpacer_3">
287+
<spacer name="horizontalSpacer_2">
274288
<property name="orientation">
275289
<enum>Qt::Horizontal</enum>
276290
</property>
277291
<property name="sizeHint" stdset="0">
278292
<size>
279-
<width>0</width>
293+
<width>40</width>
280294
<height>20</height>
281295
</size>
282296
</property>
283297
</spacer>
284298
</item>
285299
</layout>
286300
</item>
287-
<item row="3" column="0" colspan="2">
288-
<widget class="QLabel" name="mHorizontalAlignementLabel">
289-
<property name="text">
290-
<string>Horizontal alignment</string>
301+
<item row="3" column="1">
302+
<widget class="QDoubleSpinBox" name="mMarginYDoubleSpinBox">
303+
<property name="suffix">
304+
<string> mm</string>
291305
</property>
292306
</widget>
293307
</item>
294-
<item row="5" column="0" colspan="2">
295-
<widget class="QLabel" name="mVerticalAlignementLabel">
308+
<item row="3" column="0">
309+
<widget class="QLabel" name="mMarginYLabel">
296310
<property name="text">
297-
<string>Vertical alignment</string>
311+
<string>Vertical margin</string>
298312
</property>
299313
</widget>
300314
</item>
@@ -331,7 +345,8 @@
331345
<tabstop>mAppearanceGroup</tabstop>
332346
<tabstop>mFontButton</tabstop>
333347
<tabstop>mFontColorButton</tabstop>
334-
<tabstop>mMarginDoubleSpinBox</tabstop>
348+
<tabstop>mMarginXDoubleSpinBox</tabstop>
349+
<tabstop>mMarginYDoubleSpinBox</tabstop>
335350
<tabstop>mLeftRadioButton</tabstop>
336351
<tabstop>mCenterRadioButton</tabstop>
337352
<tabstop>mRightRadioButton</tabstop>

‎tests/src/core/testqgscomposerlabel.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class TestQgsComposerLabel: public QObject
4141
void feature_evaluation();
4242
// test "$page" expressions
4343
void page_evaluation();
44+
45+
void marginMethods(); //tests getting/setting margins
46+
4447
private:
4548
QgsComposition* mComposition;
4649
QgsComposerLabel* mComposerLabel;
@@ -173,5 +176,37 @@ void TestQgsComposerLabel::page_evaluation()
173176
}
174177
}
175178

179+
void TestQgsComposerLabel::marginMethods()
180+
{
181+
QgsComposerLabel label( mComposition );
182+
//test setting margins seperately
183+
label.setMarginX( 3.0 );
184+
label.setMarginY( 4.0 );
185+
QCOMPARE( label.marginX(), 3.0 );
186+
QCOMPARE( label.marginY(), 4.0 );
187+
//test setting margins together
188+
label.setMargin( 5.0 );
189+
QCOMPARE( label.marginX(), 5.0 );
190+
QCOMPARE( label.marginY(), 5.0 );
191+
192+
//test reading label margins from pre 2.7 projects
193+
QDomDocument labelDoc;
194+
QString labelXml;
195+
labelXml = "<ComposerLabel margin=\"9\"><ComposerItem></ComposerItem></ComposerLabel";
196+
labelDoc.setContent( labelXml );
197+
QgsComposerLabel label2( mComposition );
198+
label2.readXML( labelDoc.firstChildElement(), labelDoc );
199+
QCOMPARE( label2.marginX(), 9.0 );
200+
QCOMPARE( label2.marginY(), 9.0 );
201+
202+
//test reading label margins from >=2.7 projects
203+
labelXml = "<ComposerLabel marginX=\"11\" marginY=\"12\"><ComposerItem></ComposerItem></ComposerLabel";
204+
labelDoc.setContent( labelXml );
205+
QgsComposerLabel label3( mComposition );
206+
label3.readXML( labelDoc.firstChildElement(), labelDoc );
207+
QCOMPARE( label3.marginX(), 11.0 );
208+
QCOMPARE( label3.marginY(), 12.0 );
209+
}
210+
176211
QTEST_MAIN( TestQgsComposerLabel )
177212
#include "moc_testqgscomposerlabel.cxx"

0 commit comments

Comments
 (0)
Please sign in to comment.