Skip to content

Commit

Permalink
[FEATURE][composer] Allow control of hoz and vert margins for labels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyalldawson committed Oct 31, 2014
1 parent c180fec commit a2a094b
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 94 deletions.
52 changes: 48 additions & 4 deletions python/core/composer/qgscomposerlabel.sip
Expand Up @@ -52,10 +52,54 @@ class QgsComposerLabel : QgsComposerItem
* @returns void
*/
void setVAlign( Qt::AlignmentFlag a );
//!brief Accessor for the margin of the label
double margin();
//!brief Mutator for the margin of the label
void setMargin( double m );

/**Returns the margin between the edge of the frame and the label contents
* @returns margin in mm
* @deprecated use marginX and marginY instead
*/
double margin() /Deprecated/;

/**Returns the horizontal margin between the edge of the frame and the label
* contents.
* @returns horizontal margin in mm
* @note added in QGIS 2.7
*/
double marginX() const;

/**Returns the vertical margin between the edge of the frame and the label
* contents.
* @returns vertical margin in mm
* @note added in QGIS 2.7
*/
double marginY() const;

/**Sets the margin between the edge of the frame and the label contents.
* This method sets both the horizontal and vertical margins to the same
* value. The margins can be individually controlled using the setMarginX
* and setMarginY methods.
* @param m margin in mm
* @see setMarginX
* @see setMarginY
*/
void setMargin( const double m );

/**Sets the horizontal margin between the edge of the frame and the label
* contents.
* @param margin horizontal margin in mm
* @see setMargin
* @see setMarginY
* @note added in QGIS 2.7
*/
void setMarginX( const double margin );

/**Sets the vertical margin between the edge of the frame and the label
* contents.
* @param margin vertical margin in mm
* @see setMargin
* @see setMarginX
* @note added in QGIS 2.7
*/
void setMarginY( const double margin );

/**Sets text color
@note: this function was added in version 1.4*/
Expand Down
21 changes: 17 additions & 4 deletions src/app/composer/qgscomposerlabelwidget.cpp 100755 → 100644
Expand Up @@ -104,12 +104,23 @@ void QgsComposerLabelWidget::on_mFontButton_clicked()
}
}

void QgsComposerLabelWidget::on_mMarginDoubleSpinBox_valueChanged( double d )
void QgsComposerLabelWidget::on_mMarginXDoubleSpinBox_valueChanged( double d )
{
if ( mComposerLabel )
{
mComposerLabel->beginCommand( tr( "Label margin changed" ) );
mComposerLabel->setMargin( d );
mComposerLabel->setMarginX( d );
mComposerLabel->update();
mComposerLabel->endCommand();
}
}

void QgsComposerLabelWidget::on_mMarginYDoubleSpinBox_valueChanged( double d )
{
if ( mComposerLabel )
{
mComposerLabel->beginCommand( tr( "Label margin changed" ) );
mComposerLabel->setMarginY( d );
mComposerLabel->update();
mComposerLabel->endCommand();
}
Expand Down Expand Up @@ -228,7 +239,8 @@ void QgsComposerLabelWidget::setGuiElementValues()
blockAllSignals( true );
mTextEdit->setPlainText( mComposerLabel->text() );
mTextEdit->moveCursor( QTextCursor::End, QTextCursor::MoveAnchor );
mMarginDoubleSpinBox->setValue( mComposerLabel->margin() );
mMarginXDoubleSpinBox->setValue( mComposerLabel->marginX() );
mMarginYDoubleSpinBox->setValue( mComposerLabel->marginY() );
mHtmlCheckBox->setChecked( mComposerLabel->htmlState() );
mTopRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignTop );
mMiddleRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignVCenter );
Expand All @@ -244,7 +256,8 @@ void QgsComposerLabelWidget::blockAllSignals( bool block )
{
mTextEdit->blockSignals( block );
mHtmlCheckBox->blockSignals( block );
mMarginDoubleSpinBox->blockSignals( block );
mMarginXDoubleSpinBox->blockSignals( block );
mMarginYDoubleSpinBox->blockSignals( block );
mTopRadioButton->blockSignals( block );
mMiddleRadioButton->blockSignals( block );
mBottomRadioButton->blockSignals( block );
Expand Down
3 changes: 2 additions & 1 deletion src/app/composer/qgscomposerlabelwidget.h
Expand Up @@ -37,7 +37,8 @@ class QgsComposerLabelWidget: public QgsComposerItemBaseWidget, private Ui::QgsC
void on_mTextEdit_textChanged();
void on_mFontButton_clicked();
void on_mInsertExpressionButton_clicked();
void on_mMarginDoubleSpinBox_valueChanged( double d );
void on_mMarginXDoubleSpinBox_valueChanged( double d );
void on_mMarginYDoubleSpinBox_valueChanged( double d );
void on_mFontColorButton_colorChanged( const QColor& newLabelColor );
void on_mCenterRadioButton_clicked();
void on_mLeftRadioButton_clicked();
Expand Down
41 changes: 34 additions & 7 deletions src/core/composer/qgscomposerlabel.cpp 100755 → 100644
Expand Up @@ -39,7 +39,8 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
, mHtmlState( 0 )
, mHtmlUnitsToMM( 1.0 )
, mHtmlLoaded( false )
, mMargin( 1.0 )
, mMarginX( 1.0 )
, mMarginY( 1.0 )
, mFontColor( QColor( 0, 0, 0 ) )
, mHAlignment( Qt::AlignLeft )
, mVAlignment( Qt::AlignTop )
Expand Down Expand Up @@ -104,7 +105,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
painter->setRenderHint( QPainter::Antialiasing, true );

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

QString textToDraw = displayText();

Expand Down Expand Up @@ -309,15 +310,31 @@ void QgsComposerLabel::setFont( const QFont& f )
mFont = f;
}

void QgsComposerLabel::setMargin( const double m )
{
mMarginX = m;
mMarginY = m;
}

void QgsComposerLabel::setMarginX( const double margin )
{
mMarginX = margin;
}

void QgsComposerLabel::setMarginY( const double margin )
{
mMarginY = margin;
}

void QgsComposerLabel::adjustSizeToText()
{
double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
double fontHeight = QgsComposerUtils::fontHeightMM( mFont );

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

double width = textWidth + 2 * mMargin + 2 * penWidth + 1;
double height = fontHeight + 2 * mMargin + 2 * penWidth;
double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
double height = fontHeight + 2 * mMarginY + 2 * penWidth;

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

composerLabelElem.setAttribute( "labelText", mText );
composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );

composerLabelElem.setAttribute( "marginX", QString::number( mMarginX ) );
composerLabelElem.setAttribute( "marginY", QString::number( mMarginY ) );
composerLabelElem.setAttribute( "halign", mHAlignment );
composerLabelElem.setAttribute( "valign", mVAlignment );

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

//margin
mMargin = itemElem.attribute( "margin" ).toDouble();
bool marginXOk = false;
bool marginYOk = false;
mMarginX = itemElem.attribute( "marginX" ).toDouble( &marginXOk );
mMarginY = itemElem.attribute( "marginY" ).toDouble( &marginYOk );
if ( !marginXOk || !marginYOk )
{
//upgrade old projects where margins where stored in a single attribute
double margin = itemElem.attribute( "margin", "1.0" ).toDouble();
mMarginX = margin;
mMarginY = margin;
}

//Horizontal alignment
mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );
Expand Down
58 changes: 52 additions & 6 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -75,10 +75,54 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
* @returns void
*/
void setVAlign( Qt::AlignmentFlag a ) { mVAlignment = a; }
//!brief Accessor for the margin of the label
double margin() { return mMargin; }
//!brief Mutator for the margin of the label
void setMargin( double m ) { mMargin = m; }

/**Returns the margin between the edge of the frame and the label contents
* @returns margin in mm
* @deprecated use marginX and marginY instead
*/
Q_DECL_DEPRECATED double margin() { return mMarginX; }

/**Returns the horizontal margin between the edge of the frame and the label
* contents.
* @returns horizontal margin in mm
* @note added in QGIS 2.7
*/
double marginX() const { return mMarginX; }

/**Returns the vertical margin between the edge of the frame and the label
* contents.
* @returns vertical margin in mm
* @note added in QGIS 2.7
*/
double marginY() const { return mMarginY; }

/**Sets the margin between the edge of the frame and the label contents.
* This method sets both the horizontal and vertical margins to the same
* value. The margins can be individually controlled using the setMarginX
* and setMarginY methods.
* @param m margin in mm
* @see setMarginX
* @see setMarginY
*/
void setMargin( const double m );

/**Sets the horizontal margin between the edge of the frame and the label
* contents.
* @param margin horizontal margin in mm
* @see setMargin
* @see setMarginY
* @note added in QGIS 2.7
*/
void setMarginX( const double margin );

/**Sets the vertical margin between the edge of the frame and the label
* contents.
* @param margin vertical margin in mm
* @see setMargin
* @see setMarginX
* @note added in QGIS 2.7
*/
void setMarginY( const double margin );

/**Sets text color
*/
Expand Down Expand Up @@ -125,8 +169,10 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
// Font
QFont mFont;

// Border between text and fram (in mm)
double mMargin;
/**Horizontal margin between contents and frame (in mm)*/
double mMarginX;
/**Vertical margin between contents and frame (in mm)*/
double mMarginY;

// Font color
QColor mFontColor;
Expand Down

0 comments on commit a2a094b

Please sign in to comment.