Skip to content

Commit 09645e4

Browse files
committed
[FEATURE][composer] Allow setting secondary color for scalebars, allow alpha channels in scalebar colors
1 parent 9eb0e6c commit 09645e4

10 files changed

+305
-21
lines changed

python/core/composer/qgscomposerscalebar.sip

+52-2
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,68 @@ class QgsComposerScaleBar: QgsComposerItem
5353
void setUnitLabeling( const QString& label );
5454

5555
QFont font() const;
56+
void setFont( const QFont& font );
5657

58+
/**Returns the color used for drawing text in the scalebar.
59+
* @returns font color for scalebar.
60+
* @see setFontColor
61+
* @see font
62+
*/
5763
QColor fontColor() const;
58-
void setFontColor( const QColor& c );
5964

60-
void setFont( const QFont& font );
65+
/**Sets the color used for drawing text in the scalebar.
66+
* @param c font color for scalebar.
67+
* @see fontColor
68+
* @see setFont
69+
*/
70+
void setFontColor( const QColor& c );
6171

72+
/**Returns the pen used for drawing the scalebar.
73+
* @returns QPen used for drawing the scalebar outlines.
74+
* @see setPen
75+
* @see brush
76+
*/
6277
QPen pen() const;
78+
79+
/**Sets the pen used for drawing the scalebar.
80+
* @param pen QPen to use for drawing the scalebar outlines.
81+
* @see pen
82+
* @see setBrush
83+
*/
6384
void setPen( const QPen& pen );
6485

86+
/**Returns the primary brush for the scalebar.
87+
* @returns QBrush used for filling the scalebar
88+
* @see setBrush
89+
* @see brush2
90+
* @see pen
91+
*/
6592
QBrush brush() const;
93+
94+
/**Sets primary brush for the scalebar.
95+
* @param brush QBrush to use for filling the scalebar
96+
* @see brush
97+
* @see setBrush2
98+
* @see setPen
99+
*/
66100
void setBrush( const QBrush& brush );
67101

102+
/**Returns the secondary brush for the scalebar. This is used for alternating color style scalebars, such
103+
* as single and double box styles.
104+
* @returns QBrush used for secondary color areas
105+
* @see setBrush2
106+
* @see brush
107+
*/
108+
QBrush brush2() const;
109+
110+
/**Sets secondary brush for the scalebar. This is used for alternating color style scalebars, such
111+
* as single and double box styles.
112+
* @param brush QBrush to use for secondary color areas
113+
* @see brush2
114+
* @see setBrush
115+
*/
116+
void setBrush2( const QBrush& brush );
117+
68118
double height() const;
69119
void setHeight( double h );
70120

src/app/composer/qgscomposerscalebarwidget.cpp

+33-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ QgsComposerScaleBarWidget::QgsComposerScaleBarWidget( QgsComposerScaleBar* scale
5252
mUnitsComboBox->insertItem( 1, tr( "Meters" ), 1 );
5353
mUnitsComboBox->insertItem( 2, tr( "Feet" ), 2 );
5454
mUnitsComboBox->insertItem( 3, tr( "Nautical Miles" ), 3 );
55+
56+
mFillColorButton->setColorDialogTitle( tr( "Select fill color" ) );
57+
mFillColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
58+
mFillColor2Button->setColorDialogTitle( tr( "Select alternate fill color" ) );
59+
mFillColor2Button->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
60+
mFontColorButton->setColorDialogTitle( tr( "Select font color" ) );
61+
mFontColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
62+
mStrokeColorButton->setColorDialogTitle( tr( "Select stroke color" ) );
63+
mStrokeColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
64+
5565
blockMemberSignals( false );
5666
setGuiElements(); //set the GUI elements to the state of scaleBar
5767
}
@@ -173,6 +183,7 @@ void QgsComposerScaleBarWidget::setGuiElements()
173183
mLineCapStyleCombo->setPenCapStyle( mComposerScaleBar->lineCapStyle() );
174184
mFontColorButton->setColor( mComposerScaleBar->fontColor() );
175185
mFillColorButton->setColor( mComposerScaleBar->brush().color() );
186+
mFillColor2Button->setColor( mComposerScaleBar->brush2().color() );
176187
mStrokeColorButton->setColor( mComposerScaleBar->pen().color() );
177188

178189
//map combo box
@@ -328,13 +339,31 @@ void QgsComposerScaleBarWidget::on_mFillColorButton_colorChanged( const QColor&
328339

329340
mComposerScaleBar->beginCommand( tr( "Scalebar color changed" ) );
330341
disconnectUpdateSignal();
331-
QBrush newBrush( newColor );
342+
QBrush newBrush = mComposerScaleBar->brush();
343+
newBrush.setColor( newColor );
332344
mComposerScaleBar->setBrush( newBrush );
333345
mComposerScaleBar->update();
334346
connectUpdateSignal();
335347
mComposerScaleBar->endCommand();
336348
}
337349

350+
void QgsComposerScaleBarWidget::on_mFillColor2Button_colorChanged( const QColor &newColor )
351+
{
352+
if ( !mComposerScaleBar )
353+
{
354+
return;
355+
}
356+
357+
mComposerScaleBar->beginCommand( tr( "Scalebar secondary color changed" ) );
358+
disconnectUpdateSignal();
359+
QBrush newBrush = mComposerScaleBar->brush2();
360+
newBrush.setColor( newColor );
361+
mComposerScaleBar->setBrush2( newBrush );
362+
mComposerScaleBar->update();
363+
connectUpdateSignal();
364+
mComposerScaleBar->endCommand();
365+
}
366+
338367
void QgsComposerScaleBarWidget::on_mStrokeColorButton_colorChanged( const QColor& newColor )
339368
{
340369
if ( !mComposerScaleBar )
@@ -442,6 +471,7 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
442471
mLabelBarSpaceSpinBox->setEnabled( false );
443472
mLineWidthSpinBox->setEnabled( false );
444473
mFillColorButton->setEnabled( false );
474+
mFillColor2Button->setEnabled( false );
445475
mStrokeColorButton->setEnabled( false );
446476
mLineJoinStyleCombo->setEnabled( false );
447477
mLineCapStyleCombo->setEnabled( false );
@@ -454,6 +484,7 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
454484
mLabelBarSpaceSpinBox->setEnabled( true );
455485
mLineWidthSpinBox->setEnabled( true );
456486
mFillColorButton->setEnabled( true );
487+
mFillColor2Button->setEnabled( true );
457488
mStrokeColorButton->setEnabled( true );
458489
if ( style == "Single Box" || style == "Double Box" )
459490
{
@@ -581,6 +612,7 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
581612
mLineCapStyleCombo->blockSignals( block );
582613
mFontColorButton->blockSignals( block );
583614
mFillColorButton->blockSignals( block );
615+
mFillColor2Button->blockSignals( block );
584616
mStrokeColorButton->blockSignals( block );
585617
}
586618

src/app/composer/qgscomposerscalebarwidget.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class QgsComposerScaleBarWidget: public QWidget, private Ui::QgsComposerScaleBar
4444
void on_mFontButton_clicked();
4545
void on_mFontColorButton_colorChanged( const QColor& newColor );
4646
void on_mFillColorButton_colorChanged( const QColor& newColor );
47+
void on_mFillColor2Button_colorChanged( const QColor& newColor );
4748
void on_mStrokeColorButton_colorChanged( const QColor& newColor );
4849
void on_mStyleComboBox_currentIndexChanged( const QString& text );
4950
void on_mLabelBarSpaceSpinBox_valueChanged( double d );

src/core/composer/qgscomposerscalebar.cpp

+130-8
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,17 @@ void QgsComposerScaleBar::applyDefaultSettings()
270270

271271
mHeight = 3;
272272

273-
mPen = QPen( QColor( 0, 0, 0 ) );
273+
mPen = QPen( Qt::black );
274274
mPen.setJoinStyle( mLineJoinStyle );
275275
mPen.setCapStyle( mLineCapStyle );
276276
mPen.setWidthF( 1.0 );
277277

278-
mBrush.setColor( QColor( 0, 0, 0 ) );
278+
mBrush.setColor( Qt::black );
279279
mBrush.setStyle( Qt::SolidPattern );
280280

281+
mBrush2.setColor( Qt::white );
282+
mBrush2.setStyle( Qt::SolidPattern );
283+
281284
//get default composer font from settings
282285
QSettings settings;
283286
QString defaultFontString = settings.value( "/Composer/defaultFont" ).toString();
@@ -533,9 +536,41 @@ bool QgsComposerScaleBar::writeXML( QDomElement& elem, QDomDocument & doc ) cons
533536
}
534537

535538
//colors
536-
composerScaleBarElem.setAttribute( "brushColor", mBrush.color().name() );
537-
composerScaleBarElem.setAttribute( "penColor", mPen.color().name() );
538-
composerScaleBarElem.setAttribute( "fontColor", mFontColor.name() );
539+
540+
//fill color
541+
QDomElement fillColorElem = doc.createElement( "fillColor" );
542+
QColor fillColor = mBrush.color();
543+
fillColorElem.setAttribute( "red", QString::number( fillColor.red() ) );
544+
fillColorElem.setAttribute( "green", QString::number( fillColor.green() ) );
545+
fillColorElem.setAttribute( "blue", QString::number( fillColor.blue() ) );
546+
fillColorElem.setAttribute( "alpha", QString::number( fillColor.alpha() ) );
547+
composerScaleBarElem.appendChild( fillColorElem );
548+
549+
//fill color 2
550+
QDomElement fillColor2Elem = doc.createElement( "fillColor2" );
551+
QColor fillColor2 = mBrush2.color();
552+
fillColor2Elem.setAttribute( "red", QString::number( fillColor2.red() ) );
553+
fillColor2Elem.setAttribute( "green", QString::number( fillColor2.green() ) );
554+
fillColor2Elem.setAttribute( "blue", QString::number( fillColor2.blue() ) );
555+
fillColor2Elem.setAttribute( "alpha", QString::number( fillColor2.alpha() ) );
556+
composerScaleBarElem.appendChild( fillColor2Elem );
557+
558+
//pen color
559+
QDomElement strokeColorElem = doc.createElement( "strokeColor" );
560+
QColor strokeColor = mPen.color();
561+
strokeColorElem.setAttribute( "red", QString::number( strokeColor.red() ) );
562+
strokeColorElem.setAttribute( "green", QString::number( strokeColor.green() ) );
563+
strokeColorElem.setAttribute( "blue", QString::number( strokeColor.blue() ) );
564+
strokeColorElem.setAttribute( "alpha", QString::number( strokeColor.alpha() ) );
565+
composerScaleBarElem.appendChild( strokeColorElem );
566+
567+
//font color
568+
QDomElement fontColorElem = doc.createElement( "textColor" );
569+
fontColorElem.setAttribute( "red", QString::number( mFontColor.red() ) );
570+
fontColorElem.setAttribute( "green", QString::number( mFontColor.green() ) );
571+
fontColorElem.setAttribute( "blue", QString::number( mFontColor.blue() ) );
572+
fontColorElem.setAttribute( "alpha", QString::number( mFontColor.alpha() ) );
573+
composerScaleBarElem.appendChild( fontColorElem );
539574

540575
//alignment
541576
composerScaleBarElem.setAttribute( "alignment", QString::number(( int ) mAlignment ) );
@@ -573,9 +608,96 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
573608

574609
//colors
575610
//fill color
576-
mBrush.setColor( QColor( itemElem.attribute( "brushColor", "#000000" ) ) );
577-
mPen.setColor( QColor( itemElem.attribute( "penColor", "#000000" ) ) );
578-
mFontColor.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
611+
QDomNodeList fillColorList = itemElem.elementsByTagName( "fillColor" );
612+
if ( fillColorList.size() > 0 )
613+
{
614+
QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
615+
bool redOk, greenOk, blueOk, alphaOk;
616+
int fillRed, fillGreen, fillBlue, fillAlpha;
617+
618+
fillRed = fillColorElem.attribute( "red" ).toDouble( &redOk );
619+
fillGreen = fillColorElem.attribute( "green" ).toDouble( &greenOk );
620+
fillBlue = fillColorElem.attribute( "blue" ).toDouble( &blueOk );
621+
fillAlpha = fillColorElem.attribute( "alpha" ).toDouble( &alphaOk );
622+
623+
if ( redOk && greenOk && blueOk && alphaOk )
624+
{
625+
mBrush.setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
626+
}
627+
}
628+
else
629+
{
630+
mBrush.setColor( QColor( itemElem.attribute( "brushColor", "#000000" ) ) );
631+
}
632+
633+
//fill color 2
634+
QDomNodeList fillColor2List = itemElem.elementsByTagName( "fillColor2" );
635+
if ( fillColor2List.size() > 0 )
636+
{
637+
QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
638+
bool redOk, greenOk, blueOk, alphaOk;
639+
int fillRed, fillGreen, fillBlue, fillAlpha;
640+
641+
fillRed = fillColor2Elem.attribute( "red" ).toDouble( &redOk );
642+
fillGreen = fillColor2Elem.attribute( "green" ).toDouble( &greenOk );
643+
fillBlue = fillColor2Elem.attribute( "blue" ).toDouble( &blueOk );
644+
fillAlpha = fillColor2Elem.attribute( "alpha" ).toDouble( &alphaOk );
645+
646+
if ( redOk && greenOk && blueOk && alphaOk )
647+
{
648+
mBrush2.setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
649+
}
650+
}
651+
else
652+
{
653+
mBrush2.setColor( QColor( itemElem.attribute( "brush2Color", "#ffffff" ) ) );
654+
}
655+
656+
//stroke color
657+
QDomNodeList strokeColorList = itemElem.elementsByTagName( "strokeColor" );
658+
if ( strokeColorList.size() > 0 )
659+
{
660+
QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
661+
bool redOk, greenOk, blueOk, alphaOk;
662+
int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
663+
664+
strokeRed = strokeColorElem.attribute( "red" ).toDouble( &redOk );
665+
strokeGreen = strokeColorElem.attribute( "green" ).toDouble( &greenOk );
666+
strokeBlue = strokeColorElem.attribute( "blue" ).toDouble( &blueOk );
667+
strokeAlpha = strokeColorElem.attribute( "alpha" ).toDouble( &alphaOk );
668+
669+
if ( redOk && greenOk && blueOk && alphaOk )
670+
{
671+
mPen.setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
672+
}
673+
}
674+
else
675+
{
676+
mPen.setColor( QColor( itemElem.attribute( "penColor", "#000000" ) ) );
677+
}
678+
679+
//font color
680+
QDomNodeList textColorList = itemElem.elementsByTagName( "textColor" );
681+
if ( textColorList.size() > 0 )
682+
{
683+
QDomElement textColorElem = textColorList.at( 0 ).toElement();
684+
bool redOk, greenOk, blueOk, alphaOk;
685+
int textRed, textGreen, textBlue, textAlpha;
686+
687+
textRed = textColorElem.attribute( "red" ).toDouble( &redOk );
688+
textGreen = textColorElem.attribute( "green" ).toDouble( &greenOk );
689+
textBlue = textColorElem.attribute( "blue" ).toDouble( &blueOk );
690+
textAlpha = textColorElem.attribute( "alpha" ).toDouble( &alphaOk );
691+
692+
if ( redOk && greenOk && blueOk && alphaOk )
693+
{
694+
mFontColor = QColor( textRed, textGreen, textBlue, textAlpha );
695+
}
696+
}
697+
else
698+
{
699+
mFontColor.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
700+
}
579701

580702
//style
581703
delete mStyle;

0 commit comments

Comments
 (0)