Skip to content

Commit ec9ba9c

Browse files
committed
[FEATURE][composer] Data defined legend titles and column count
(fix #11913)
1 parent a67c84b commit ec9ba9c

File tree

9 files changed

+187
-56
lines changed

9 files changed

+187
-56
lines changed

python/core/composer/qgscomposerlegend.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,13 @@ class QgsComposerLegend : QgsComposerItem
242242
//Overridden to show legend title
243243
virtual QString displayName() const;
244244

245+
const QgsLegendSettings& legendSettings() const;
246+
245247
public slots:
246248
/** Data changed*/
247249
void synchronizeWithModel();
248250
/** Sets mCompositionMap to 0 if the map is deleted*/
249251
void invalidateCurrentMap();
252+
virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr );
253+
250254
};

python/core/composer/qgscomposerobject.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class QgsComposerObject : QObject, QgsExpressionContextGenerator
5151
PictureSvgOutlineWidth, //!< SVG outline width
5252
//html item
5353
SourceUrl /*!< html source url */
54+
//legend item
55+
LegendTitle, //!< Legend title
56+
LegendColumnCount, //!< Legend column count
5457
};
5558

5659
/** Specifies whether the value returned by a function should be the original, user

src/app/composer/qgscomposerlegendwidget.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend )
100100
connect( &legend->composition()->atlasComposition(), SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( updateFilterLegendByAtlasButton() ) );
101101
}
102102

103+
registerDataDefinedButton( mLegendTitleDDBtn, QgsComposerObject::LegendTitle,
104+
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::anyStringDesc() );
105+
registerDataDefinedButton( mColumnsDDBtn, QgsComposerObject::LegendColumnCount,
106+
QgsDataDefinedButtonV2::AnyType, QgsDataDefinedButtonV2::intPosOneDesc() );
107+
103108
setGuiElements();
104109

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

162167
on_mCheckBoxAutoUpdate_stateChanged( mLegend->autoUpdateModel() ? Qt::Checked : Qt::Unchecked );
168+
updateDataDefinedButton( mLegendTitleDDBtn );
169+
updateDataDefinedButton( mColumnsDDBtn );
163170
}
164171

165172
void QgsComposerLegendWidget::on_mWrapCharLineEdit_textChanged( const QString &text )

src/core/composer/qgscomposerlegend.cpp

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ void QgsComposerLegend::setLegendFilterByMapEnabled( bool enabled )
265265

266266
void QgsComposerLegend::setTitle( const QString& t )
267267
{
268+
mTitle = t;
268269
mSettings.setTitle( t );
269270

270271
if ( mComposition && id().isEmpty() )
@@ -273,7 +274,7 @@ void QgsComposerLegend::setTitle( const QString& t )
273274
mComposition->itemsModel()->updateItemDisplayName( this );
274275
}
275276
}
276-
QString QgsComposerLegend::title() const { return mSettings.title(); }
277+
QString QgsComposerLegend::title() const { return mTitle; }
277278

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

318-
int QgsComposerLegend::columnCount() const { return mSettings.columnCount(); }
319-
void QgsComposerLegend::setColumnCount( int c ) { mSettings.setColumnCount( c ); }
319+
int QgsComposerLegend::columnCount() const { return mColumnCount; }
320+
void QgsComposerLegend::setColumnCount( int c ) { mColumnCount = c; mSettings.setColumnCount( c ); }
320321

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

364365
//write general properties
365-
composerLegendElem.setAttribute( QStringLiteral( "title" ), mSettings.title() );
366+
composerLegendElem.setAttribute( QStringLiteral( "title" ), mTitle );
366367
composerLegendElem.setAttribute( QStringLiteral( "titleAlignment" ), QString::number( static_cast< int >( mSettings.titleAlignment() ) ) );
367-
composerLegendElem.setAttribute( QStringLiteral( "columnCount" ), QString::number( mSettings.columnCount() ) );
368+
composerLegendElem.setAttribute( QStringLiteral( "columnCount" ), QString::number( mColumnCount ) );
368369
composerLegendElem.setAttribute( QStringLiteral( "splitLayer" ), QString::number( mSettings.splitLayer() ) );
369370
composerLegendElem.setAttribute( QStringLiteral( "equalColumnWidth" ), QString::number( mSettings.equalColumnWidth() ) );
370371

@@ -462,14 +463,16 @@ bool QgsComposerLegend::readXml( const QDomElement& itemElem, const QDomDocument
462463
}
463464

464465
//read general properties
465-
mSettings.setTitle( itemElem.attribute( QStringLiteral( "title" ) ) );
466+
mTitle = itemElem.attribute( QStringLiteral( "title" ) );
467+
mSettings.setTitle( mTitle );
466468
if ( !itemElem.attribute( QStringLiteral( "titleAlignment" ) ).isEmpty() )
467469
{
468470
mSettings.setTitleAlignment( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "titleAlignment" ) ).toInt() ) );
469471
}
470472
int colCount = itemElem.attribute( QStringLiteral( "columnCount" ), QStringLiteral( "1" ) ).toInt();
471473
if ( colCount < 1 ) colCount = 1;
472-
mSettings.setColumnCount( colCount );
474+
mColumnCount = colCount;
475+
mSettings.setColumnCount( mColumnCount );
473476
mSettings.setSplitLayer( itemElem.attribute( QStringLiteral( "splitLayer" ), QStringLiteral( "0" ) ).toInt() == 1 );
474477
mSettings.setEqualColumnWidth( itemElem.attribute( QStringLiteral( "equalColumnWidth" ), QStringLiteral( "0" ) ).toInt() == 1 );
475478

@@ -642,6 +645,42 @@ void QgsComposerLegend::invalidateCurrentMap()
642645
setComposerMap( nullptr );
643646
}
644647

648+
void QgsComposerLegend::refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, const QgsExpressionContext* context )
649+
{
650+
QgsExpressionContext scopedContext = createExpressionContext();
651+
const QgsExpressionContext* evalContext = context ? context : &scopedContext;
652+
653+
bool forceUpdate = false;
654+
//updates data defined properties and redraws item to match
655+
if ( property == QgsComposerObject::LegendTitle || property == QgsComposerObject::AllProperties )
656+
{
657+
bool ok = false;
658+
QString t = mProperties.valueAsString( QgsComposerObject::LegendTitle, *evalContext, mTitle, &ok );
659+
if ( ok )
660+
{
661+
mSettings.setTitle( t );
662+
forceUpdate = true;
663+
}
664+
}
665+
if ( property == QgsComposerObject::LegendColumnCount || property == QgsComposerObject::AllProperties )
666+
{
667+
bool ok = false;
668+
int cols = mProperties.valueAsInt( QgsComposerObject::LegendColumnCount, *evalContext, mColumnCount, &ok );
669+
if ( ok && cols >= 0 )
670+
{
671+
mSettings.setColumnCount( cols );
672+
forceUpdate = true;
673+
}
674+
}
675+
if ( forceUpdate )
676+
{
677+
adjustBoxSize();
678+
update();
679+
}
680+
681+
QgsComposerObject::refreshDataDefinedProperty( property, context );
682+
}
683+
645684
void QgsComposerLegend::mapLayerStyleOverridesChanged()
646685
{
647686
if ( !mComposerMap )

src/core/composer/qgscomposerlegend.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,21 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
272272
//Overridden to show legend title
273273
virtual QString displayName() const override;
274274

275+
/**
276+
* Returns the legend's renderer settings object.
277+
* @note added in QGIS 3.0
278+
*/
279+
const QgsLegendSettings& legendSettings() const { return mSettings; }
280+
275281
public slots:
276282
//! Data changed
277283
void synchronizeWithModel();
278284
//! Sets mCompositionMap to 0 if the map is deleted
279285
void invalidateCurrentMap();
280286

287+
virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
288+
289+
281290
private slots:
282291
void updateFilterByMap( bool redraw = true );
283292

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

302311
QgsLegendSettings mSettings;
303312

313+
QString mTitle;
314+
int mColumnCount = 1;
315+
304316
const QgsComposerMap* mComposerMap;
305317

306318
bool mLegendFilterByMap;

src/core/composer/qgscomposerobject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const QgsPropertyDefinition QgsComposerObject::sPropertyNameMap
6161
{ QgsComposerObject::PictureSvgBackgroundColor, "dataDefinedSvgBackgroundColor" },
6262
{ QgsComposerObject::PictureSvgOutlineColor, "dataDefinedSvgOutlineColor" },
6363
{ QgsComposerObject::PictureSvgOutlineWidth, "dataDefinedSvgOutlineWidth" },
64+
{ QgsComposerObject::LegendTitle, "dataDefinedLegendTitle" },
65+
{ QgsComposerObject::LegendColumnCount, "dataDefinedLegendColumns" },
6466
};
6567

6668

src/core/composer/qgscomposerobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
7878
PictureSvgOutlineWidth, //!< SVG outline width
7979
//html item
8080
SourceUrl, //!< Html source url
81+
//legend item
82+
LegendTitle, //!< Legend title
83+
LegendColumnCount, //!< Legend column count
8184
};
8285

8386
static const QgsPropertyDefinition sPropertyNameMap;

0 commit comments

Comments
 (0)