Skip to content

Commit

Permalink
[FEATURE][composer] Data defined legend titles and column count
Browse files Browse the repository at this point in the history
(fix #11913)
  • Loading branch information
nyalldawson committed Jan 23, 2017
1 parent a67c84b commit ec9ba9c
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 56 deletions.
4 changes: 4 additions & 0 deletions python/core/composer/qgscomposerlegend.sip
Expand Up @@ -242,9 +242,13 @@ class QgsComposerLegend : QgsComposerItem
//Overridden to show legend title
virtual QString displayName() const;

const QgsLegendSettings& legendSettings() const;

public slots:
/** Data changed*/
void synchronizeWithModel();
/** Sets mCompositionMap to 0 if the map is deleted*/
void invalidateCurrentMap();
virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr );

};
3 changes: 3 additions & 0 deletions python/core/composer/qgscomposerobject.sip
Expand Up @@ -51,6 +51,9 @@ class QgsComposerObject : QObject, QgsExpressionContextGenerator
PictureSvgOutlineWidth, //!< SVG outline width
//html item
SourceUrl /*!< html source url */
//legend item
LegendTitle, //!< Legend title
LegendColumnCount, //!< Legend column count
};

/** Specifies whether the value returned by a function should be the original, user
Expand Down
7 changes: 7 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.cpp
Expand Up @@ -100,6 +100,11 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend )
connect( &legend->composition()->atlasComposition(), SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( updateFilterLegendByAtlasButton() ) );
}

registerDataDefinedButton( mLegendTitleDDBtn, QgsComposerObject::LegendTitle,
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::anyStringDesc() );
registerDataDefinedButton( mColumnsDDBtn, QgsComposerObject::LegendColumnCount,
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::intPosOneDesc() );

setGuiElements();

connect( mItemTreeView->selectionModel(), SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
Expand Down Expand Up @@ -160,6 +165,8 @@ void QgsComposerLegendWidget::setGuiElements()
blockAllSignals( false );

on_mCheckBoxAutoUpdate_stateChanged( mLegend->autoUpdateModel() ? Qt::Checked : Qt::Unchecked );
updateDataDefinedButton( mLegendTitleDDBtn );
updateDataDefinedButton( mColumnsDDBtn );
}

void QgsComposerLegendWidget::on_mWrapCharLineEdit_textChanged( const QString &text )
Expand Down
53 changes: 46 additions & 7 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -265,6 +265,7 @@ void QgsComposerLegend::setLegendFilterByMapEnabled( bool enabled )

void QgsComposerLegend::setTitle( const QString& t )
{
mTitle = t;
mSettings.setTitle( t );

if ( mComposition && id().isEmpty() )
Expand All @@ -273,7 +274,7 @@ void QgsComposerLegend::setTitle( const QString& t )
mComposition->itemsModel()->updateItemDisplayName( this );
}
}
QString QgsComposerLegend::title() const { return mSettings.title(); }
QString QgsComposerLegend::title() const { return mTitle; }

Qt::AlignmentFlag QgsComposerLegend::titleAlignment() const { return mSettings.titleAlignment(); }
void QgsComposerLegend::setTitleAlignment( Qt::AlignmentFlag alignment ) { mSettings.setTitleAlignment( alignment ); }
Expand Down Expand Up @@ -315,8 +316,8 @@ void QgsComposerLegend::setWmsLegendHeight( double h ) { mSettings.setWmsLegendS
void QgsComposerLegend::setWrapChar( const QString& t ) { mSettings.setWrapChar( t ); }
QString QgsComposerLegend::wrapChar() const {return mSettings.wrapChar(); }

int QgsComposerLegend::columnCount() const { return mSettings.columnCount(); }
void QgsComposerLegend::setColumnCount( int c ) { mSettings.setColumnCount( c ); }
int QgsComposerLegend::columnCount() const { return mColumnCount; }
void QgsComposerLegend::setColumnCount( int c ) { mColumnCount = c; mSettings.setColumnCount( c ); }

bool QgsComposerLegend::splitLayer() const { return mSettings.splitLayer(); }
void QgsComposerLegend::setSplitLayer( bool s ) { mSettings.setSplitLayer( s ); }
Expand Down Expand Up @@ -362,9 +363,9 @@ bool QgsComposerLegend::writeXml( QDomElement& elem, QDomDocument & doc ) const
elem.appendChild( composerLegendElem );

//write general properties
composerLegendElem.setAttribute( QStringLiteral( "title" ), mSettings.title() );
composerLegendElem.setAttribute( QStringLiteral( "title" ), mTitle );
composerLegendElem.setAttribute( QStringLiteral( "titleAlignment" ), QString::number( static_cast< int >( mSettings.titleAlignment() ) ) );
composerLegendElem.setAttribute( QStringLiteral( "columnCount" ), QString::number( mSettings.columnCount() ) );
composerLegendElem.setAttribute( QStringLiteral( "columnCount" ), QString::number( mColumnCount ) );
composerLegendElem.setAttribute( QStringLiteral( "splitLayer" ), QString::number( mSettings.splitLayer() ) );
composerLegendElem.setAttribute( QStringLiteral( "equalColumnWidth" ), QString::number( mSettings.equalColumnWidth() ) );

Expand Down Expand Up @@ -462,14 +463,16 @@ bool QgsComposerLegend::readXml( const QDomElement& itemElem, const QDomDocument
}

//read general properties
mSettings.setTitle( itemElem.attribute( QStringLiteral( "title" ) ) );
mTitle = itemElem.attribute( QStringLiteral( "title" ) );
mSettings.setTitle( mTitle );
if ( !itemElem.attribute( QStringLiteral( "titleAlignment" ) ).isEmpty() )
{
mSettings.setTitleAlignment( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "titleAlignment" ) ).toInt() ) );
}
int colCount = itemElem.attribute( QStringLiteral( "columnCount" ), QStringLiteral( "1" ) ).toInt();
if ( colCount < 1 ) colCount = 1;
mSettings.setColumnCount( colCount );
mColumnCount = colCount;
mSettings.setColumnCount( mColumnCount );
mSettings.setSplitLayer( itemElem.attribute( QStringLiteral( "splitLayer" ), QStringLiteral( "0" ) ).toInt() == 1 );
mSettings.setEqualColumnWidth( itemElem.attribute( QStringLiteral( "equalColumnWidth" ), QStringLiteral( "0" ) ).toInt() == 1 );

Expand Down Expand Up @@ -642,6 +645,42 @@ void QgsComposerLegend::invalidateCurrentMap()
setComposerMap( nullptr );
}

void QgsComposerLegend::refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, const QgsExpressionContext* context )
{
QgsExpressionContext scopedContext = createExpressionContext();
const QgsExpressionContext* evalContext = context ? context : &scopedContext;

bool forceUpdate = false;
//updates data defined properties and redraws item to match
if ( property == QgsComposerObject::LegendTitle || property == QgsComposerObject::AllProperties )
{
bool ok = false;
QString t = mProperties.valueAsString( QgsComposerObject::LegendTitle, *evalContext, mTitle, &ok );
if ( ok )
{
mSettings.setTitle( t );
forceUpdate = true;
}
}
if ( property == QgsComposerObject::LegendColumnCount || property == QgsComposerObject::AllProperties )
{
bool ok = false;
int cols = mProperties.valueAsInt( QgsComposerObject::LegendColumnCount, *evalContext, mColumnCount, &ok );
if ( ok && cols >= 0 )
{
mSettings.setColumnCount( cols );
forceUpdate = true;
}
}
if ( forceUpdate )
{
adjustBoxSize();
update();
}

QgsComposerObject::refreshDataDefinedProperty( property, context );
}

void QgsComposerLegend::mapLayerStyleOverridesChanged()
{
if ( !mComposerMap )
Expand Down
12 changes: 12 additions & 0 deletions src/core/composer/qgscomposerlegend.h
Expand Up @@ -272,12 +272,21 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
//Overridden to show legend title
virtual QString displayName() const override;

/**
* Returns the legend's renderer settings object.
* @note added in QGIS 3.0
*/
const QgsLegendSettings& legendSettings() const { return mSettings; }

public slots:
//! Data changed
void synchronizeWithModel();
//! Sets mCompositionMap to 0 if the map is deleted
void invalidateCurrentMap();

virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;


private slots:
void updateFilterByMap( bool redraw = true );

Expand All @@ -301,6 +310,9 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem

QgsLegendSettings mSettings;

QString mTitle;
int mColumnCount = 1;

const QgsComposerMap* mComposerMap;

bool mLegendFilterByMap;
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposerobject.cpp
Expand Up @@ -61,6 +61,8 @@ const QgsPropertyDefinition QgsComposerObject::sPropertyNameMap
{ QgsComposerObject::PictureSvgBackgroundColor, "dataDefinedSvgBackgroundColor" },
{ QgsComposerObject::PictureSvgOutlineColor, "dataDefinedSvgOutlineColor" },
{ QgsComposerObject::PictureSvgOutlineWidth, "dataDefinedSvgOutlineWidth" },
{ QgsComposerObject::LegendTitle, "dataDefinedLegendTitle" },
{ QgsComposerObject::LegendColumnCount, "dataDefinedLegendColumns" },
};


Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposerobject.h
Expand Up @@ -78,6 +78,9 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
PictureSvgOutlineWidth, //!< SVG outline width
//html item
SourceUrl, //!< Html source url
//legend item
LegendTitle, //!< Legend title
LegendColumnCount, //!< Legend column count
};

static const QgsPropertyDefinition sPropertyNameMap;
Expand Down

0 comments on commit ec9ba9c

Please sign in to comment.