Skip to content

Commit

Permalink
Also disable color buttons which don't apply for numeric scale bars
Browse files Browse the repository at this point in the history
Make numeric scale bars respect font color and alignment settings (fix #7830)
  • Loading branch information
nyalldawson committed Jun 12, 2013
1 parent e253cd3 commit 7879d37
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/app/composer/qgscomposerscalebarwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
mGroupBoxSegments->setCollapsed( true );
mLabelBarSpaceSpinBox->setEnabled( false );
mLineWidthSpinBox->setEnabled( false );
mColorPushButton->setEnabled( false );
mStrokeColorPushButton->setEnabled( false );
}
else
{
Expand All @@ -468,6 +470,8 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
mGroupBoxSegments->setEnabled( true );
mLabelBarSpaceSpinBox->setEnabled( true );
mLineWidthSpinBox->setEnabled( true );
mColorPushButton->setEnabled( true );
mStrokeColorPushButton->setEnabled( true );
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/core/composer/qgscomposerscalebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ double QgsComposerScaleBar::mapWidth() const
}
}

void QgsComposerScaleBar::setAlignment( Alignment a )
{
mAlignment = a;
update();
emit itemChanged();
}

void QgsComposerScaleBar::setUnits( ScaleBarUnits u )
{
mUnits = u;
Expand Down Expand Up @@ -505,6 +512,12 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume

void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthAfter )
{
//Don't adjust position for numeric scale bars:
if ( mStyle->name() == "Numeric" )
{
return;
}

if ( mAlignment == Middle )
{
move( -( widthAfter - width ) / 2.0, 0 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerscalebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
Alignment alignment() const { return mAlignment; }

/**@note: this method was added in version 1.8*/
void setAlignment( Alignment a ) { mAlignment = a; }
void setAlignment( Alignment a );

/**@note: this method was added in version 1.9*/
ScaleBarUnits units() const { return mUnits; }
Expand Down
31 changes: 29 additions & 2 deletions src/core/composer/qgsnumericscalebarstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,35 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const

p->save();
p->setFont( mScaleBar->font() );
p->setPen( QColor( 0, 0, 0 ) );
mScaleBar->drawText( p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ), scaleText(), mScaleBar->font() );
p->setPen( mScaleBar->fontColor() );

//call QgsComposerItem's pen() function, since that refers to the frame pen
//and QgsComposerScalebar's pen() function refers to the scale bar line width,
//which is not used for numeric scale bars. Divide the pen width by 2 since
//half the width of the frame is drawn outside the item.
double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0;
double margin = mScaleBar->boxContentSpace();
//map scalebar alignment to Qt::AlignmentFlag type
Qt::AlignmentFlag hAlign;
switch ( mScaleBar->alignment() )
{
case QgsComposerScaleBar::Left:
hAlign = Qt::AlignLeft;
break;
case QgsComposerScaleBar::Middle:
hAlign = Qt::AlignHCenter;
break;
case QgsComposerScaleBar::Right:
hAlign = Qt::AlignRight;
break;
default:
hAlign = Qt::AlignLeft;
break;
}

//text destination is item's rect, excluding the margin and frame
QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
mScaleBar->drawText( p, painterRect, scaleText(), mScaleBar->font(), hAlign, Qt::AlignTop );

p->restore();
}
Expand Down

0 comments on commit 7879d37

Please sign in to comment.