Skip to content

Commit 934c7c9

Browse files
committed
[FEATURE][composer] Data defined svg colors and outline width for
composer picture items Especially useful when the picture is showing a north arrow!
1 parent d6c7569 commit 934c7c9

File tree

7 files changed

+104
-57
lines changed

7 files changed

+104
-57
lines changed

python/core/composer/qgscomposerobject.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class QgsComposerObject : QObject, QgsExpressionContextGenerator
4646
MapStylePreset, /*!< layer and style visibility preset */
4747
//composer picture
4848
PictureSource, /*!< picture source url */
49+
PictureSvgBackgroundColor, //!< SVG background color
50+
PictureSvgOutlineColor, //!< SVG outline color
51+
PictureSvgOutlineWidth, //!< SVG outline width
4952
//html item
5053
SourceUrl /*!< html source url */
5154
};

src/app/composer/qgscomposerpicturewidget.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ QgsComposerPictureWidget::QgsComposerPictureWidget( QgsComposerPicture* picture
7777
connect( mPicture, SIGNAL( pictureRotationChanged( double ) ), this, SLOT( setPicRotationSpinValue( double ) ) );
7878

7979
//connections for data defined buttons
80-
connect( mSourceDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mPictureLineEdit, SLOT( setDisabled( bool ) ) );
80+
connect( mSourceDDBtn, &QgsDataDefinedButtonV2::activated, mPictureLineEdit, &QLineEdit::setDisabled );
8181
}
8282

8383
QgsComposerPictureWidget::~QgsComposerPictureWidget()
@@ -702,6 +702,18 @@ void QgsComposerPictureWidget::populateDataDefinedButtons()
702702
{
703703
registerDataDefinedButton( mSourceDDBtn, QgsComposerObject::PictureSource,
704704
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::anyStringDesc() );
705+
mFillColorDDBtn->blockSignals( true );
706+
registerDataDefinedButton( mFillColorDDBtn, QgsComposerObject::PictureSvgBackgroundColor,
707+
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::colorAlphaDesc() );
708+
mFillColorDDBtn->blockSignals( false );
709+
mOutlineColorDDBtn->blockSignals( true );
710+
registerDataDefinedButton( mOutlineColorDDBtn, QgsComposerObject::PictureSvgOutlineColor,
711+
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::colorAlphaDesc() );
712+
mOutlineColorDDBtn->blockSignals( false );
713+
mOutlineWidthDDBtn->blockSignals( true );
714+
registerDataDefinedButton( mOutlineWidthDDBtn, QgsComposerObject::PictureSvgOutlineWidth,
715+
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::doublePosDesc() );
716+
mOutlineWidthDDBtn->blockSignals( false );
705717

706718
//initial state of controls - disable related controls when dd buttons are active
707719
mPictureLineEdit->setEnabled( !mSourceDDBtn->isActive() );

src/core/composer/qgscomposerobject.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ const QgsPropertyDefinition QgsComposerObject::sPropertyNameMap
5858
{ QgsComposerObject::PaperHeight, "dataDefinedPaperHeight" },
5959
{ QgsComposerObject::NumPages, "dataDefinedNumPages" },
6060
{ QgsComposerObject::PaperOrientation, "dataDefinedPaperOrientation" },
61+
{ QgsComposerObject::PictureSvgBackgroundColor, "dataDefinedSvgBackgroundColor" },
62+
{ QgsComposerObject::PictureSvgOutlineColor, "dataDefinedSvgOutlineColor" },
63+
{ QgsComposerObject::PictureSvgOutlineWidth, "dataDefinedSvgOutlineWidth" },
6164
};
6265

6366

src/core/composer/qgscomposerobject.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
7373
MapStylePreset, //!< Layer and style map theme
7474
//composer picture
7575
PictureSource, //!< Picture source url
76+
PictureSvgBackgroundColor, //!< SVG background color
77+
PictureSvgOutlineColor, //!< SVG outline color
78+
PictureSvgOutlineWidth, //!< SVG outline width
7679
//html item
77-
SourceUrl //!< Html source url
80+
SourceUrl, //!< Html source url
7881
};
7982

8083
static const QgsPropertyDefinition sPropertyNameMap;

src/core/composer/qgscomposerpicture.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ QgsComposerPicture::QgsComposerPicture( QgsComposition *composition )
5252
, mNorthOffset( 0.0 )
5353
, mResizeMode( QgsComposerPicture::Zoom )
5454
, mPictureAnchor( UpperLeft )
55-
, mSvgFillColor( QColor( 255, 255, 255 ) )
56-
, mSvgBorderColor( QColor( 0, 0, 0 ) )
57-
, mSvgBorderWidth( 0.2 )
5855
, mHasExpressionError( false )
5956
, mLoadingSvg( false )
6057
{
@@ -71,9 +68,6 @@ QgsComposerPicture::QgsComposerPicture()
7168
, mNorthOffset( 0.0 )
7269
, mResizeMode( QgsComposerPicture::Zoom )
7370
, mPictureAnchor( UpperLeft )
74-
, mSvgFillColor( QColor( 255, 255, 255 ) )
75-
, mSvgBorderColor( QColor( 0, 0, 0 ) )
76-
, mSvgBorderWidth( 0.2 )
7771
, mHasExpressionError( false )
7872
, mLoadingSvg( false )
7973
{
@@ -369,7 +363,11 @@ void QgsComposerPicture::loadLocalPicture( const QString &path )
369363
if ( sourceFileSuffix.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
370364
{
371365
//try to open svg
372-
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( pic.fileName(), rect().width(), mSvgFillColor, mSvgBorderColor, mSvgBorderWidth,
366+
QgsExpressionContext context = createExpressionContext();
367+
QColor fillColor = mProperties.valueAsColor( QgsComposerObject::PictureSvgBackgroundColor, context, mSvgFillColor );
368+
QColor outlineColor = mProperties.valueAsColor( QgsComposerObject::PictureSvgOutlineColor, context, mSvgBorderColor );
369+
double outlineWidth = mProperties.valueAsDouble( QgsComposerObject::PictureSvgOutlineWidth, context, mSvgBorderWidth );
370+
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( pic.fileName(), rect().width(), fillColor, outlineColor, outlineWidth,
373371
1.0 );
374372
mSVG.load( svgContent );
375373
if ( mSVG.isValid() )
@@ -715,7 +713,9 @@ void QgsComposerPicture::refreshDataDefinedProperty( const QgsComposerObject::Da
715713
QgsExpressionContext scopedContext = createExpressionContext();
716714
const QgsExpressionContext* evalContext = context ? context : &scopedContext;
717715

718-
if ( property == QgsComposerObject::PictureSource || property == QgsComposerObject::AllProperties )
716+
if ( property == QgsComposerObject::PictureSource || property == QgsComposerObject::PictureSvgBackgroundColor
717+
|| property == QgsComposerObject::PictureSvgOutlineColor || property == QgsComposerObject::PictureSvgOutlineWidth
718+
|| property == QgsComposerObject::AllProperties )
719719
{
720720
refreshPicture( evalContext );
721721
}

src/core/composer/qgscomposerpicture.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ class CORE_EXPORT QgsComposerPicture: public QgsComposerItem
324324
ResizeMode mResizeMode;
325325
QgsComposerItem::ItemPositionMode mPictureAnchor;
326326

327-
QColor mSvgFillColor;
328-
QColor mSvgBorderColor;
329-
double mSvgBorderWidth;
327+
QColor mSvgFillColor = QColor( 255, 255, 255 );
328+
QColor mSvgBorderColor = QColor( 0, 0, 0 );
329+
double mSvgBorderWidth = 0.2;
330330

331331
bool mHasExpressionError;
332332
bool mLoaded;

src/ui/composer/qgscomposerpicturewidgetbase.ui

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
<property name="geometry">
6161
<rect>
6262
<x>0</x>
63-
<y>-312</y>
64-
<width>314</width>
65-
<height>871</height>
63+
<y>-275</y>
64+
<width>316</width>
65+
<height>827</height>
6666
</rect>
6767
</property>
6868
<layout class="QVBoxLayout" name="mainLayout">
@@ -333,6 +333,19 @@
333333
<string>SVG Parameters</string>
334334
</property>
335335
<layout class="QGridLayout" name="gridLayout_3">
336+
<item row="0" column="2">
337+
<spacer name="horizontalSpacer">
338+
<property name="orientation">
339+
<enum>Qt::Horizontal</enum>
340+
</property>
341+
<property name="sizeHint" stdset="0">
342+
<size>
343+
<width>40</width>
344+
<height>20</height>
345+
</size>
346+
</property>
347+
</spacer>
348+
</item>
336349
<item row="2" column="1">
337350
<widget class="QgsDoubleSpinBox" name="mOutlineWidthSpinBox">
338351
<property name="sizePolicy">
@@ -361,8 +374,28 @@
361374
</property>
362375
</widget>
363376
</item>
364-
<item row="1" column="1">
365-
<widget class="QgsColorButton" name="mOutlineColorButton">
377+
<item row="0" column="3">
378+
<widget class="QgsDataDefinedButtonV2" name="mFillColorDDBtn">
379+
<property name="text">
380+
<string>...</string>
381+
</property>
382+
</widget>
383+
</item>
384+
<item row="1" column="0">
385+
<widget class="QLabel" name="mBorderColorLabel">
386+
<property name="sizePolicy">
387+
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
388+
<horstretch>0</horstretch>
389+
<verstretch>0</verstretch>
390+
</sizepolicy>
391+
</property>
392+
<property name="text">
393+
<string>Outline color</string>
394+
</property>
395+
</widget>
396+
</item>
397+
<item row="0" column="1">
398+
<widget class="QgsColorButton" name="mFillColorButton">
366399
<property name="minimumSize">
367400
<size>
368401
<width>120</width>
@@ -380,8 +413,15 @@
380413
</property>
381414
</widget>
382415
</item>
383-
<item row="0" column="1">
384-
<widget class="QgsColorButton" name="mFillColorButton">
416+
<item row="2" column="0">
417+
<widget class="QLabel" name="mBorderWidthLabel">
418+
<property name="text">
419+
<string>Outline width</string>
420+
</property>
421+
</widget>
422+
</item>
423+
<item row="1" column="1">
424+
<widget class="QgsColorButton" name="mOutlineColorButton">
385425
<property name="minimumSize">
386426
<size>
387427
<width>120</width>
@@ -399,52 +439,33 @@
399439
</property>
400440
</widget>
401441
</item>
402-
<item row="1" column="0">
403-
<widget class="QLabel" name="mBorderColorLabel">
442+
<item row="0" column="0">
443+
<widget class="QLabel" name="label_6">
404444
<property name="sizePolicy">
405445
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
406446
<horstretch>0</horstretch>
407447
<verstretch>0</verstretch>
408448
</sizepolicy>
409449
</property>
410450
<property name="text">
411-
<string>Outline color</string>
451+
<string>Fill color</string>
412452
</property>
413453
</widget>
414454
</item>
415-
<item row="2" column="0">
416-
<widget class="QLabel" name="mBorderWidthLabel">
455+
<item row="1" column="3">
456+
<widget class="QgsDataDefinedButtonV2" name="mOutlineColorDDBtn">
417457
<property name="text">
418-
<string>Outline width</string>
458+
<string>...</string>
419459
</property>
420460
</widget>
421461
</item>
422-
<item row="0" column="0">
423-
<widget class="QLabel" name="label_6">
424-
<property name="sizePolicy">
425-
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
426-
<horstretch>0</horstretch>
427-
<verstretch>0</verstretch>
428-
</sizepolicy>
429-
</property>
462+
<item row="2" column="3">
463+
<widget class="QgsDataDefinedButtonV2" name="mOutlineWidthDDBtn">
430464
<property name="text">
431-
<string>Fill color</string>
465+
<string>...</string>
432466
</property>
433467
</widget>
434468
</item>
435-
<item row="0" column="2">
436-
<spacer name="horizontalSpacer">
437-
<property name="orientation">
438-
<enum>Qt::Horizontal</enum>
439-
</property>
440-
<property name="sizeHint" stdset="0">
441-
<size>
442-
<width>40</width>
443-
<height>20</height>
444-
</size>
445-
</property>
446-
</spacer>
447-
</item>
448469
</layout>
449470
</widget>
450471
</item>
@@ -527,27 +548,27 @@
527548
</widget>
528549
<layoutdefault spacing="6" margin="11"/>
529550
<customwidgets>
530-
<customwidget>
531-
<class>QgsCollapsibleGroupBoxBasic</class>
532-
<extends>QGroupBox</extends>
533-
<header location="global">qgscollapsiblegroupbox.h</header>
534-
<container>1</container>
535-
</customwidget>
536551
<customwidget>
537552
<class>QgsColorButton</class>
538553
<extends>QToolButton</extends>
539554
<header>qgscolorbutton.h</header>
540555
<container>1</container>
541556
</customwidget>
557+
<customwidget>
558+
<class>QgsDataDefinedButtonV2</class>
559+
<extends>QToolButton</extends>
560+
<header>qgsdatadefinedbuttonv2.h</header>
561+
</customwidget>
542562
<customwidget>
543563
<class>QgsDoubleSpinBox</class>
544564
<extends>QDoubleSpinBox</extends>
545565
<header>qgsdoublespinbox.h</header>
546566
</customwidget>
547567
<customwidget>
548-
<class>QgsDataDefinedButtonV2</class>
549-
<extends>QToolButton</extends>
550-
<header>qgsdatadefinedbuttonv2.h</header>
568+
<class>QgsCollapsibleGroupBoxBasic</class>
569+
<extends>QGroupBox</extends>
570+
<header location="global">qgscollapsiblegroupbox.h</header>
571+
<container>1</container>
551572
</customwidget>
552573
<customwidget>
553574
<class>QgsComposerItemComboBox</class>
@@ -569,12 +590,17 @@
569590
<tabstop>mRemoveDirectoryButton</tabstop>
570591
<tabstop>mAddDirectoryButton</tabstop>
571592
<tabstop>mFillColorButton</tabstop>
593+
<tabstop>mFillColorDDBtn</tabstop>
572594
<tabstop>mOutlineColorButton</tabstop>
595+
<tabstop>mOutlineColorDDBtn</tabstop>
573596
<tabstop>mOutlineWidthSpinBox</tabstop>
597+
<tabstop>mOutlineWidthDDBtn</tabstop>
574598
<tabstop>mRotationGroupBox</tabstop>
575599
<tabstop>mPictureRotationSpinBox</tabstop>
576600
<tabstop>mRotationFromComposerMapCheckBox</tabstop>
577601
<tabstop>mComposerMapComboBox</tabstop>
602+
<tabstop>mNorthTypeComboBox</tabstop>
603+
<tabstop>mPictureRotationOffsetSpinBox</tabstop>
578604
</tabstops>
579605
<resources/>
580606
<connections/>

0 commit comments

Comments
 (0)