Skip to content

Commit 7879d37

Browse files
committed
Also disable color buttons which don't apply for numeric scale bars
Make numeric scale bars respect font color and alignment settings (fix #7830)
1 parent e253cd3 commit 7879d37

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/app/composer/qgscomposerscalebarwidget.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
460460
mGroupBoxSegments->setCollapsed( true );
461461
mLabelBarSpaceSpinBox->setEnabled( false );
462462
mLineWidthSpinBox->setEnabled( false );
463+
mColorPushButton->setEnabled( false );
464+
mStrokeColorPushButton->setEnabled( false );
463465
}
464466
else
465467
{
@@ -468,6 +470,8 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
468470
mGroupBoxSegments->setEnabled( true );
469471
mLabelBarSpaceSpinBox->setEnabled( true );
470472
mLineWidthSpinBox->setEnabled( true );
473+
mColorPushButton->setEnabled( true );
474+
mStrokeColorPushButton->setEnabled( true );
471475
}
472476
}
473477

src/core/composer/qgscomposerscalebar.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ double QgsComposerScaleBar::mapWidth() const
202202
}
203203
}
204204

205+
void QgsComposerScaleBar::setAlignment( Alignment a )
206+
{
207+
mAlignment = a;
208+
update();
209+
emit itemChanged();
210+
}
211+
205212
void QgsComposerScaleBar::setUnits( ScaleBarUnits u )
206213
{
207214
mUnits = u;
@@ -505,6 +512,12 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
505512

506513
void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthAfter )
507514
{
515+
//Don't adjust position for numeric scale bars:
516+
if ( mStyle->name() == "Numeric" )
517+
{
518+
return;
519+
}
520+
508521
if ( mAlignment == Middle )
509522
{
510523
move( -( widthAfter - width ) / 2.0, 0 );

src/core/composer/qgscomposerscalebar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
106106
Alignment alignment() const { return mAlignment; }
107107

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

111111
/**@note: this method was added in version 1.9*/
112112
ScaleBarUnits units() const { return mUnits; }

src/core/composer/qgsnumericscalebarstyle.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,35 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
5050

5151
p->save();
5252
p->setFont( mScaleBar->font() );
53-
p->setPen( QColor( 0, 0, 0 ) );
54-
mScaleBar->drawText( p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ), scaleText(), mScaleBar->font() );
53+
p->setPen( mScaleBar->fontColor() );
54+
55+
//call QgsComposerItem's pen() function, since that refers to the frame pen
56+
//and QgsComposerScalebar's pen() function refers to the scale bar line width,
57+
//which is not used for numeric scale bars. Divide the pen width by 2 since
58+
//half the width of the frame is drawn outside the item.
59+
double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0;
60+
double margin = mScaleBar->boxContentSpace();
61+
//map scalebar alignment to Qt::AlignmentFlag type
62+
Qt::AlignmentFlag hAlign;
63+
switch ( mScaleBar->alignment() )
64+
{
65+
case QgsComposerScaleBar::Left:
66+
hAlign = Qt::AlignLeft;
67+
break;
68+
case QgsComposerScaleBar::Middle:
69+
hAlign = Qt::AlignHCenter;
70+
break;
71+
case QgsComposerScaleBar::Right:
72+
hAlign = Qt::AlignRight;
73+
break;
74+
default:
75+
hAlign = Qt::AlignLeft;
76+
break;
77+
}
78+
79+
//text destination is item's rect, excluding the margin and frame
80+
QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
81+
mScaleBar->drawText( p, painterRect, scaleText(), mScaleBar->font(), hAlign, Qt::AlignTop );
5582

5683
p->restore();
5784
}

0 commit comments

Comments
 (0)