Skip to content

Commit

Permalink
[FEATURE][composer] Allow setting secondary color for scalebars, allo…
Browse files Browse the repository at this point in the history
…w alpha channels in scalebar colors
  • Loading branch information
nyalldawson committed Jun 27, 2014
1 parent 9eb0e6c commit 09645e4
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 21 deletions.
54 changes: 52 additions & 2 deletions python/core/composer/qgscomposerscalebar.sip
Expand Up @@ -53,18 +53,68 @@ class QgsComposerScaleBar: QgsComposerItem
void setUnitLabeling( const QString& label );

QFont font() const;
void setFont( const QFont& font );

/**Returns the color used for drawing text in the scalebar.
* @returns font color for scalebar.
* @see setFontColor
* @see font
*/
QColor fontColor() const;
void setFontColor( const QColor& c );

void setFont( const QFont& font );
/**Sets the color used for drawing text in the scalebar.
* @param c font color for scalebar.
* @see fontColor
* @see setFont
*/
void setFontColor( const QColor& c );

/**Returns the pen used for drawing the scalebar.
* @returns QPen used for drawing the scalebar outlines.
* @see setPen
* @see brush
*/
QPen pen() const;

/**Sets the pen used for drawing the scalebar.
* @param pen QPen to use for drawing the scalebar outlines.
* @see pen
* @see setBrush
*/
void setPen( const QPen& pen );

/**Returns the primary brush for the scalebar.
* @returns QBrush used for filling the scalebar
* @see setBrush
* @see brush2
* @see pen
*/
QBrush brush() const;

/**Sets primary brush for the scalebar.
* @param brush QBrush to use for filling the scalebar
* @see brush
* @see setBrush2
* @see setPen
*/
void setBrush( const QBrush& brush );

/**Returns the secondary brush for the scalebar. This is used for alternating color style scalebars, such
* as single and double box styles.
* @returns QBrush used for secondary color areas
* @see setBrush2
* @see brush
*/
QBrush brush2() const;

/**Sets secondary brush for the scalebar. This is used for alternating color style scalebars, such
* as single and double box styles.
* @param brush QBrush to use for secondary color areas
* @see brush2
* @see setBrush
*/
void setBrush2( const QBrush& brush );

double height() const;
void setHeight( double h );

Expand Down
34 changes: 33 additions & 1 deletion src/app/composer/qgscomposerscalebarwidget.cpp
Expand Up @@ -52,6 +52,16 @@ QgsComposerScaleBarWidget::QgsComposerScaleBarWidget( QgsComposerScaleBar* scale
mUnitsComboBox->insertItem( 1, tr( "Meters" ), 1 );
mUnitsComboBox->insertItem( 2, tr( "Feet" ), 2 );
mUnitsComboBox->insertItem( 3, tr( "Nautical Miles" ), 3 );

mFillColorButton->setColorDialogTitle( tr( "Select fill color" ) );
mFillColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
mFillColor2Button->setColorDialogTitle( tr( "Select alternate fill color" ) );
mFillColor2Button->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
mFontColorButton->setColorDialogTitle( tr( "Select font color" ) );
mFontColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
mStrokeColorButton->setColorDialogTitle( tr( "Select stroke color" ) );
mStrokeColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );

blockMemberSignals( false );
setGuiElements(); //set the GUI elements to the state of scaleBar
}
Expand Down Expand Up @@ -173,6 +183,7 @@ void QgsComposerScaleBarWidget::setGuiElements()
mLineCapStyleCombo->setPenCapStyle( mComposerScaleBar->lineCapStyle() );
mFontColorButton->setColor( mComposerScaleBar->fontColor() );
mFillColorButton->setColor( mComposerScaleBar->brush().color() );
mFillColor2Button->setColor( mComposerScaleBar->brush2().color() );
mStrokeColorButton->setColor( mComposerScaleBar->pen().color() );

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

mComposerScaleBar->beginCommand( tr( "Scalebar color changed" ) );
disconnectUpdateSignal();
QBrush newBrush( newColor );
QBrush newBrush = mComposerScaleBar->brush();
newBrush.setColor( newColor );
mComposerScaleBar->setBrush( newBrush );
mComposerScaleBar->update();
connectUpdateSignal();
mComposerScaleBar->endCommand();
}

void QgsComposerScaleBarWidget::on_mFillColor2Button_colorChanged( const QColor &newColor )
{
if ( !mComposerScaleBar )
{
return;
}

mComposerScaleBar->beginCommand( tr( "Scalebar secondary color changed" ) );
disconnectUpdateSignal();
QBrush newBrush = mComposerScaleBar->brush2();
newBrush.setColor( newColor );
mComposerScaleBar->setBrush2( newBrush );
mComposerScaleBar->update();
connectUpdateSignal();
mComposerScaleBar->endCommand();
}

void QgsComposerScaleBarWidget::on_mStrokeColorButton_colorChanged( const QColor& newColor )
{
if ( !mComposerScaleBar )
Expand Down Expand Up @@ -442,6 +471,7 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
mLabelBarSpaceSpinBox->setEnabled( false );
mLineWidthSpinBox->setEnabled( false );
mFillColorButton->setEnabled( false );
mFillColor2Button->setEnabled( false );
mStrokeColorButton->setEnabled( false );
mLineJoinStyleCombo->setEnabled( false );
mLineCapStyleCombo->setEnabled( false );
Expand All @@ -454,6 +484,7 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
mLabelBarSpaceSpinBox->setEnabled( true );
mLineWidthSpinBox->setEnabled( true );
mFillColorButton->setEnabled( true );
mFillColor2Button->setEnabled( true );
mStrokeColorButton->setEnabled( true );
if ( style == "Single Box" || style == "Double Box" )
{
Expand Down Expand Up @@ -581,6 +612,7 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
mLineCapStyleCombo->blockSignals( block );
mFontColorButton->blockSignals( block );
mFillColorButton->blockSignals( block );
mFillColor2Button->blockSignals( block );
mStrokeColorButton->blockSignals( block );
}

Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerscalebarwidget.h
Expand Up @@ -44,6 +44,7 @@ class QgsComposerScaleBarWidget: public QWidget, private Ui::QgsComposerScaleBar
void on_mFontButton_clicked();
void on_mFontColorButton_colorChanged( const QColor& newColor );
void on_mFillColorButton_colorChanged( const QColor& newColor );
void on_mFillColor2Button_colorChanged( const QColor& newColor );
void on_mStrokeColorButton_colorChanged( const QColor& newColor );
void on_mStyleComboBox_currentIndexChanged( const QString& text );
void on_mLabelBarSpaceSpinBox_valueChanged( double d );
Expand Down
138 changes: 130 additions & 8 deletions src/core/composer/qgscomposerscalebar.cpp
Expand Up @@ -270,14 +270,17 @@ void QgsComposerScaleBar::applyDefaultSettings()

mHeight = 3;

mPen = QPen( QColor( 0, 0, 0 ) );
mPen = QPen( Qt::black );
mPen.setJoinStyle( mLineJoinStyle );
mPen.setCapStyle( mLineCapStyle );
mPen.setWidthF( 1.0 );

mBrush.setColor( QColor( 0, 0, 0 ) );
mBrush.setColor( Qt::black );
mBrush.setStyle( Qt::SolidPattern );

mBrush2.setColor( Qt::white );
mBrush2.setStyle( Qt::SolidPattern );

//get default composer font from settings
QSettings settings;
QString defaultFontString = settings.value( "/Composer/defaultFont" ).toString();
Expand Down Expand Up @@ -533,9 +536,41 @@ bool QgsComposerScaleBar::writeXML( QDomElement& elem, QDomDocument & doc ) cons
}

//colors
composerScaleBarElem.setAttribute( "brushColor", mBrush.color().name() );
composerScaleBarElem.setAttribute( "penColor", mPen.color().name() );
composerScaleBarElem.setAttribute( "fontColor", mFontColor.name() );

//fill color
QDomElement fillColorElem = doc.createElement( "fillColor" );
QColor fillColor = mBrush.color();
fillColorElem.setAttribute( "red", QString::number( fillColor.red() ) );
fillColorElem.setAttribute( "green", QString::number( fillColor.green() ) );
fillColorElem.setAttribute( "blue", QString::number( fillColor.blue() ) );
fillColorElem.setAttribute( "alpha", QString::number( fillColor.alpha() ) );
composerScaleBarElem.appendChild( fillColorElem );

//fill color 2
QDomElement fillColor2Elem = doc.createElement( "fillColor2" );
QColor fillColor2 = mBrush2.color();
fillColor2Elem.setAttribute( "red", QString::number( fillColor2.red() ) );
fillColor2Elem.setAttribute( "green", QString::number( fillColor2.green() ) );
fillColor2Elem.setAttribute( "blue", QString::number( fillColor2.blue() ) );
fillColor2Elem.setAttribute( "alpha", QString::number( fillColor2.alpha() ) );
composerScaleBarElem.appendChild( fillColor2Elem );

//pen color
QDomElement strokeColorElem = doc.createElement( "strokeColor" );
QColor strokeColor = mPen.color();
strokeColorElem.setAttribute( "red", QString::number( strokeColor.red() ) );
strokeColorElem.setAttribute( "green", QString::number( strokeColor.green() ) );
strokeColorElem.setAttribute( "blue", QString::number( strokeColor.blue() ) );
strokeColorElem.setAttribute( "alpha", QString::number( strokeColor.alpha() ) );
composerScaleBarElem.appendChild( strokeColorElem );

//font color
QDomElement fontColorElem = doc.createElement( "textColor" );
fontColorElem.setAttribute( "red", QString::number( mFontColor.red() ) );
fontColorElem.setAttribute( "green", QString::number( mFontColor.green() ) );
fontColorElem.setAttribute( "blue", QString::number( mFontColor.blue() ) );
fontColorElem.setAttribute( "alpha", QString::number( mFontColor.alpha() ) );
composerScaleBarElem.appendChild( fontColorElem );

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

//colors
//fill color
mBrush.setColor( QColor( itemElem.attribute( "brushColor", "#000000" ) ) );
mPen.setColor( QColor( itemElem.attribute( "penColor", "#000000" ) ) );
mFontColor.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
QDomNodeList fillColorList = itemElem.elementsByTagName( "fillColor" );
if ( fillColorList.size() > 0 )
{
QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
bool redOk, greenOk, blueOk, alphaOk;
int fillRed, fillGreen, fillBlue, fillAlpha;

fillRed = fillColorElem.attribute( "red" ).toDouble( &redOk );
fillGreen = fillColorElem.attribute( "green" ).toDouble( &greenOk );
fillBlue = fillColorElem.attribute( "blue" ).toDouble( &blueOk );
fillAlpha = fillColorElem.attribute( "alpha" ).toDouble( &alphaOk );

if ( redOk && greenOk && blueOk && alphaOk )
{
mBrush.setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
}
}
else
{
mBrush.setColor( QColor( itemElem.attribute( "brushColor", "#000000" ) ) );
}

//fill color 2
QDomNodeList fillColor2List = itemElem.elementsByTagName( "fillColor2" );
if ( fillColor2List.size() > 0 )
{
QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
bool redOk, greenOk, blueOk, alphaOk;
int fillRed, fillGreen, fillBlue, fillAlpha;

fillRed = fillColor2Elem.attribute( "red" ).toDouble( &redOk );
fillGreen = fillColor2Elem.attribute( "green" ).toDouble( &greenOk );
fillBlue = fillColor2Elem.attribute( "blue" ).toDouble( &blueOk );
fillAlpha = fillColor2Elem.attribute( "alpha" ).toDouble( &alphaOk );

if ( redOk && greenOk && blueOk && alphaOk )
{
mBrush2.setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
}
}
else
{
mBrush2.setColor( QColor( itemElem.attribute( "brush2Color", "#ffffff" ) ) );
}

//stroke color
QDomNodeList strokeColorList = itemElem.elementsByTagName( "strokeColor" );
if ( strokeColorList.size() > 0 )
{
QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
bool redOk, greenOk, blueOk, alphaOk;
int strokeRed, strokeGreen, strokeBlue, strokeAlpha;

strokeRed = strokeColorElem.attribute( "red" ).toDouble( &redOk );
strokeGreen = strokeColorElem.attribute( "green" ).toDouble( &greenOk );
strokeBlue = strokeColorElem.attribute( "blue" ).toDouble( &blueOk );
strokeAlpha = strokeColorElem.attribute( "alpha" ).toDouble( &alphaOk );

if ( redOk && greenOk && blueOk && alphaOk )
{
mPen.setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
}
}
else
{
mPen.setColor( QColor( itemElem.attribute( "penColor", "#000000" ) ) );
}

//font color
QDomNodeList textColorList = itemElem.elementsByTagName( "textColor" );
if ( textColorList.size() > 0 )
{
QDomElement textColorElem = textColorList.at( 0 ).toElement();
bool redOk, greenOk, blueOk, alphaOk;
int textRed, textGreen, textBlue, textAlpha;

textRed = textColorElem.attribute( "red" ).toDouble( &redOk );
textGreen = textColorElem.attribute( "green" ).toDouble( &greenOk );
textBlue = textColorElem.attribute( "blue" ).toDouble( &blueOk );
textAlpha = textColorElem.attribute( "alpha" ).toDouble( &alphaOk );

if ( redOk && greenOk && blueOk && alphaOk )
{
mFontColor = QColor( textRed, textGreen, textBlue, textAlpha );
}
}
else
{
mFontColor.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
}

//style
delete mStyle;
Expand Down

0 comments on commit 09645e4

Please sign in to comment.