Skip to content
Permalink
Browse files
[FEATURE][composer] Data defined transparency and blend modes for com…
…poser items. Funded by Canton of Neuchâtel, Switzerland
  • Loading branch information
nyalldawson committed Jul 5, 2014
1 parent be5aeac commit 47e8bde7d4006c7e972345a3dec052b639fb34d4
@@ -149,6 +149,15 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
connect( atlas, SIGNAL( toggled( bool ) ), this, SLOT( populateDataDefinedButtons() ) );
}

//connect data defined buttons
connect( mTransparencyDDBtn, SIGNAL( dataDefinedChanged( const QString& ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mTransparencyDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mTransparencyDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mTransparencySlider, SLOT( setDisabled( bool ) ) );
connect( mTransparencyDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mTransparencySpnBx, SLOT( setDisabled( bool ) ) );

connect( mBlendModeDDBtn, SIGNAL( dataDefinedChanged( const QString& ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mBlendModeDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mBlendModeDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mBlendModeCombo, SLOT( setDisabled( bool ) ) );
}

QgsComposerItemWidget::QgsComposerItemWidget(): QgsComposerItemBaseWidget( 0, 0 )
@@ -502,16 +511,40 @@ void QgsComposerItemWidget::setValuesForGuiNonPositionElements()

void QgsComposerItemWidget::populateDataDefinedButtons()
{
//QgsVectorLayer* vl = atlasCoverageLayer();
QgsVectorLayer* vl = atlasCoverageLayer();

//block signals from data defined buttons
mTransparencyDDBtn->blockSignals( true );
mBlendModeDDBtn->blockSignals( true );

//initialise buttons to use atlas coverage layer
mTransparencyDDBtn->init( vl, mItem->dataDefinedProperty( QgsComposerItem::Transparency ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::intTranspDesc() );
mBlendModeDDBtn->init( vl, mItem->dataDefinedProperty( QgsComposerItem::BlendMode ),
QgsDataDefinedButton::String, QgsDataDefinedButton::blendModesDesc() );

//initial state of controls - disable related controls when dd buttons are active
mTransparencySlider->setEnabled( !mTransparencyDDBtn->isActive() );
mTransparencySpnBx->setEnabled( !mTransparencyDDBtn->isActive() );
mBlendModeCombo->setEnabled( !mBlendModeDDBtn->isActive() );

//unblock signals from data defined buttons
mTransparencyDDBtn->blockSignals( false );
mBlendModeDDBtn->blockSignals( false );
}

QgsComposerItem::DataDefinedProperty QgsComposerItemWidget::ddPropertyForWidget( QgsDataDefinedButton* widget )
{
if ( widget == mTransparencyDDBtn )
{
return QgsComposerItem::Transparency;
}
else if ( widget == mBlendModeDDBtn )
{
return QgsComposerItem::BlendMode;
}

return QgsComposerItem::NoProperty;
}

void QgsComposerItemWidget::setValuesForGuiElements()
@@ -119,6 +119,9 @@ class QgsComposerItemWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
//sets the values for all non-position related elements
void setValuesForGuiNonPositionElements();

protected:
QgsComposerItem::DataDefinedProperty ddPropertyForWidget( QgsDataDefinedButton *widget );

protected slots:
/**Initializes data defined buttons to current atlas coverage layer*/
void populateDataDefinedButtons();
@@ -120,6 +120,8 @@ void QgsComposerItem::init( bool manageZValue )
setGraphicsEffect( mEffect );

// data defined strings
mDataDefinedNames.insert( Transparency, QString( "dataDefinedTransparency" ) );
mDataDefinedNames.insert( BlendMode, QString( "dataDefinedBlendMode" ) );

if ( mComposition )
{
@@ -682,14 +684,58 @@ void QgsComposerItem::setBlendMode( QPainter::CompositionMode blendMode )
{
mBlendMode = blendMode;
// Update the composer effect to use the new blend mode
mEffect->setCompositionMode( mBlendMode );
refreshBlendMode();
}

void QgsComposerItem::refreshBlendMode()
{
QPainter::CompositionMode blendMode = mBlendMode;

//data defined blend mode set?
QVariant exprVal;
if ( dataDefinedEvaluate( QgsComposerItem::BlendMode, exprVal ) )
{
QString blendstr = exprVal.toString().trimmed();
QPainter::CompositionMode blendModeD = QgsSymbolLayerV2Utils::decodeBlendMode( blendstr );

QgsDebugMsg( QString( "exprVal BlendMode:%1" ).arg( blendModeD ) );
blendMode = blendModeD;
}

// Update the composer effect to use the new blend mode
mEffect->setCompositionMode( blendMode );
}

void QgsComposerItem::setTransparency( int transparency )
{
mTransparency = transparency;
refreshTransparency( true );
}

void QgsComposerItem::refreshTransparency( bool updateItem )
{
int transparency = mTransparency;

//data defined transparency set?
QVariant exprVal;
if ( dataDefinedEvaluate( QgsComposerItem::Transparency, exprVal ) )
{
bool ok;
int transparencyD = exprVal.toInt( &ok );
QgsDebugMsg( QString( "exprVal Transparency:%1" ).arg( transparencyD ) );
if ( ok )
{
transparency = transparencyD;
}
}

// Set the QGraphicItem's opacity
setOpacity( 1. - ( transparency / 100. ) );

if ( updateItem )
{
update();
}
}

void QgsComposerItem::setEffectsEnabled( bool effectsEnabled )
@@ -1243,7 +1289,14 @@ void QgsComposerItem::repaint()
void QgsComposerItem::refreshDataDefinedProperty( QgsComposerItem::DataDefinedProperty property )
{
//update data defined properties and redraw item to match

if ( property == QgsComposerItem::Transparency || property == QgsComposerItem::AllProperties )
{
refreshTransparency( false );
}
if ( property == QgsComposerItem::BlendMode || property == QgsComposerItem::AllProperties )
{
refreshBlendMode();
}

update();
}
@@ -678,6 +678,15 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
/**Map of current data defined properties*/
QMap< QgsComposerItem::DataDefinedProperty, QgsDataDefined* > mDataDefinedProperties;

/**Refresh item's transparency, considering data defined transparency
*@param updateItem set to false to prevent the item being automatically updated
* after the transparency is set
*/
void refreshTransparency( bool updateItem = true );

/**Refresh item's blend mode, considering data defined blend mode*/
void refreshBlendMode();

void init( bool manageZValue );

friend class QgsComposerItemGroup; // to access mTemplateUuid
@@ -7,23 +7,14 @@
<x>0</x>
<y>0</y>
<width>376</width>
<height>689</height>
<height>693</height>
</rect>
</property>
<property name="windowTitle">
<string>Global Options</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>0</number>
</property>
<item>
@@ -428,13 +419,7 @@
<property name="syncGroup" stdset="0">
<string notr="true">composeritem</string>
</property>
<layout class="QFormLayout" name="formLayout_3">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="labelBlendMode">
<property name="text">
@@ -443,7 +428,18 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QgsBlendModeComboBox" name="mBlendModeCombo"/>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QgsBlendModeComboBox" name="mBlendModeCombo"/>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mBlendModeDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelTransparency">
@@ -471,6 +467,13 @@
</property>
</widget>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mTransparencyDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
@@ -498,14 +501,19 @@
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<header location="global">qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsColorButton</class>
<extends>QPushButton</extends>
<header>qgscolorbutton.h</header>
</customwidget>
<customwidget>
<class>QgsDataDefinedButton</class>
<extends>QToolButton</extends>
<header>qgsdatadefinedbutton.h</header>
</customwidget>
<customwidget>
<class>QgsBlendModeComboBox</class>
<extends>QComboBox</extends>

0 comments on commit 47e8bde

Please sign in to comment.