Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Parameterized svg support for composer svg images
This makes it possible to change SVG fill/outline color and outline
width when using parameterised SVG files such as those included
with QGIS (fix #10542)
  • Loading branch information
nyalldawson committed Mar 7, 2016
1 parent 94e7720 commit ed3cb12
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 56 deletions.
51 changes: 51 additions & 0 deletions python/core/composer/qgscomposerpicture.sip
Expand Up @@ -156,6 +156,57 @@ class QgsComposerPicture: QgsComposerItem
*/
ItemPositionMode pictureAnchor() const;

/** Returns the fill color used for parameterized SVG files.
* @see setSvgFillColor()
* @see svgBorderColor()
* @note added in QGIS 2.14.1
*/
QColor svgFillColor() const;

/** Sets the fill color used for parameterized SVG files.
* @param color fill color.
* @note this setting only has an effect on parameterized SVG files, and is ignored for
* non-parameterized SVG files.
* @see svgFillColor()
* @see setSvgBorderColor()
* @note added in QGIS 2.14.1
*/
void setSvgFillColor( const QColor& color );

/** Returns the border color used for parameterized SVG files.
* @see setSvgBorderColor()
* @see svgFillColor()
* @note added in QGIS 2.14.1
*/
QColor svgBorderColor() const;

/** Sets the border color used for parameterized SVG files.
* @param color border color.
* @note this setting only has an effect on parameterized SVG files, and is ignored for
* non-parameterized SVG files.
* @see svgBorderlColor()
* @see setSvgFillColor()
* @note added in QGIS 2.14.1
*/
void setSvgBorderColor( const QColor& color );

/** Returns the border width (in mm) used for parameterized SVG files.
* @see setSvgBorderWidth()
* @see svgBorderColor()
* @note added in QGIS 2.14.1
*/
double svgBorderWidth() const;

/** Sets the border width used for parameterized SVG files.
* @param width border width in mm
* @note this setting only has an effect on parameterized SVG files, and is ignored for
* non-parameterized SVG files.
* @see svgBorderWidth()
* @see setSvgBorderColor()
* @note added in QGIS 2.14.1
*/
void setSvgBorderWidth( double width );

/** Returns whether the picture item is using an expression for the image source.
* @returns true if the picture is using an expression for the source, false if
* it is using a single static file path for the source.
Expand Down
106 changes: 103 additions & 3 deletions src/app/composer/qgscomposerpicturewidget.cpp
Expand Up @@ -37,6 +37,13 @@ QgsComposerPictureWidget::QgsComposerPictureWidget( QgsComposerPicture* picture
{
setupUi( this );

mFillColorButton->setAllowAlpha( true );
mFillColorButton->setColorDialogTitle( tr( "Select fill color" ) );
mFillColorButton->setContext( "composer" );
mOutlineColorButton->setAllowAlpha( true );
mOutlineColorButton->setColorDialogTitle( tr( "Select outline color" ) );
mOutlineColorButton->setContext( "composer" );

//add widget for general composer item properties
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, picture );
mainLayout->addWidget( itemPropertiesWidget );
Expand Down Expand Up @@ -110,6 +117,7 @@ void QgsComposerPictureWidget::on_mPictureBrowseButton_clicked()
mPictureLineEdit->blockSignals( true );
mPictureLineEdit->setText( filePath );
mPictureLineEdit->blockSignals( false );
updateSvgParamGui();

//pass file path to QgsComposerPicture
if ( mPicture )
Expand All @@ -127,13 +135,11 @@ void QgsComposerPictureWidget::on_mPictureLineEdit_editingFinished()
{
QString filePath = mPictureLineEdit->text();

//check if file exists
QFileInfo fileInfo( filePath );

mPicture->beginCommand( tr( "Picture changed" ) );
mPicture->setPicturePath( filePath );
mPicture->update();
mPicture->endCommand();
updateSvgParamGui();
}
}

Expand Down Expand Up @@ -161,6 +167,7 @@ void QgsComposerPictureWidget::on_mPreviewListWidget_currentItemChanged( QListWi
mPictureLineEdit->setText( absoluteFilePath );
mPicture->update();
mPicture->endCommand();
updateSvgParamGui();
}

void QgsComposerPictureWidget::on_mAddDirectoryButton_clicked()
Expand Down Expand Up @@ -383,6 +390,9 @@ void QgsComposerPictureWidget::setGuiElementValues()
mRotationFromComposerMapCheckBox->blockSignals( true );
mResizeModeComboBox->blockSignals( true );
mAnchorPointComboBox->blockSignals( true );
mFillColorButton->blockSignals( true );
mOutlineColorButton->blockSignals( true );
mOutlineWidthSpinBox->blockSignals( true );

mPictureLineEdit->setText( mPicture->picturePath() );
mPictureRotationSpinBox->setValue( mPicture->pictureRotation() );
Expand Down Expand Up @@ -425,12 +435,20 @@ void QgsComposerPictureWidget::setGuiElementValues()
mAnchorPointComboBox->setEnabled( false );
}

updateSvgParamGui( false );
mFillColorButton->setColor( mPicture->svgFillColor() );
mOutlineColorButton->setColor( mPicture->svgBorderColor() );
mOutlineWidthSpinBox->setValue( mPicture->svgBorderWidth() );

mRotationFromComposerMapCheckBox->blockSignals( false );
mPictureRotationSpinBox->blockSignals( false );
mPictureLineEdit->blockSignals( false );
mComposerMapComboBox->blockSignals( false );
mResizeModeComboBox->blockSignals( false );
mAnchorPointComboBox->blockSignals( false );
mFillColorButton->blockSignals( false );
mOutlineColorButton->blockSignals( false );
mOutlineWidthSpinBox->blockSignals( false );

populateDataDefinedButtons();
}
Expand Down Expand Up @@ -465,6 +483,64 @@ QIcon QgsComposerPictureWidget::svgToIcon( const QString& filePath ) const
return QIcon( QPixmap::fromImage( img ) );
}

void QgsComposerPictureWidget::updateSvgParamGui( bool resetValues )
{
if ( !mPicture )
return;

QString picturePath = mPicture->picturePath();
if ( !picturePath.endsWith( ".svg", Qt::CaseInsensitive ) )
{
mFillColorButton->setEnabled( false );
mOutlineColorButton->setEnabled( false );
mOutlineWidthSpinBox->setEnabled( false );
return;
}

//activate gui for svg parameters only if supported by the svg file
bool hasFillParam, hasFillOpacityParam, hasOutlineParam, hasOutlineWidthParam, hasOutlineOpacityParam;
QColor defaultFill, defaultOutline;
double defaultOutlineWidth, defaultFillOpacity, defaultOutlineOpacity;
bool hasDefaultFillColor, hasDefaultFillOpacity, hasDefaultOutlineColor, hasDefaultOutlineWidth, hasDefaultOutlineOpacity;
QgsSvgCache::instance()->containsParams( picturePath, hasFillParam, hasDefaultFillColor, defaultFill,
hasFillOpacityParam, hasDefaultFillOpacity, defaultFillOpacity,
hasOutlineParam, hasDefaultOutlineColor, defaultOutline,
hasOutlineWidthParam, hasDefaultOutlineWidth, defaultOutlineWidth,
hasOutlineOpacityParam, hasDefaultOutlineOpacity, defaultOutlineOpacity );

if ( resetValues )
{
QColor fill = mFillColorButton->color();
double newOpacity = hasFillOpacityParam ? fill.alphaF() : 1.0;
if ( hasDefaultFillColor )
{
fill = defaultFill;
}
fill.setAlphaF( hasDefaultFillOpacity ? defaultFillOpacity : newOpacity );
mFillColorButton->setColor( fill );
}
mFillColorButton->setEnabled( hasFillParam );
mFillColorButton->setAllowAlpha( hasFillOpacityParam );
if ( resetValues )
{
QColor outline = mOutlineColorButton->color();
double newOpacity = hasOutlineOpacityParam ? outline.alphaF() : 1.0;
if ( hasDefaultOutlineColor )
{
outline = defaultOutline;
}
outline.setAlphaF( hasDefaultOutlineOpacity ? defaultOutlineOpacity : newOpacity );
mOutlineColorButton->setColor( outline );
}
mOutlineColorButton->setEnabled( hasOutlineParam );
mOutlineColorButton->setAllowAlpha( hasOutlineOpacityParam );
if ( hasDefaultOutlineWidth && resetValues )
{
mOutlineWidthSpinBox->setValue( defaultOutlineWidth );
}
mOutlineWidthSpinBox->setEnabled( hasOutlineWidthParam );
}

int QgsComposerPictureWidget::addDirectoryToPreview( const QString& path )
{
//go through all files of a directory
Expand Down Expand Up @@ -620,6 +696,30 @@ void QgsComposerPictureWidget::loadPicturePreviews( bool collapsed )
}
}

void QgsComposerPictureWidget::on_mFillColorButton_colorChanged( const QColor& color )
{
mPicture->beginCommand( tr( "Picture fill color changed" ) );
mPicture->setSvgFillColor( color );
mPicture->endCommand();
mPicture->update();
}

void QgsComposerPictureWidget::on_mOutlineColorButton_colorChanged( const QColor& color )
{
mPicture->beginCommand( tr( "Picture border color changed" ) );
mPicture->setSvgBorderColor( color );
mPicture->endCommand();
mPicture->update();
}

void QgsComposerPictureWidget::on_mOutlineWidthSpinBox_valueChanged( double d )
{
mPicture->beginCommand( tr( "Picture border width changed" ) );
mPicture->setSvgBorderWidth( d );
mPicture->endCommand();
mPicture->update();
}

void QgsComposerPictureWidget::showEvent( QShowEvent * event )
{
Q_UNUSED( event );
Expand Down
6 changes: 6 additions & 0 deletions src/app/composer/qgscomposerpicturewidget.h
Expand Up @@ -70,6 +70,10 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
* @param collapsed Whether the parent group box is collapsed */
void loadPicturePreviews( bool collapsed );

void on_mFillColorButton_colorChanged( const QColor& color );
void on_mOutlineColorButton_colorChanged( const QColor& color );
void on_mOutlineWidthSpinBox_valueChanged( double d );

private:
QgsComposerPicture* mPicture;
/** Whether the picture selection previews have been loaded */
Expand All @@ -87,6 +91,8 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg

//! Renders an svg file to a QIcon, correctly handling any SVG parameters present in the file
QIcon svgToIcon( const QString& filePath ) const;

void updateSvgParamGui( bool resetValues = true );
};

#endif

0 comments on commit ed3cb12

Please sign in to comment.