Skip to content
Permalink
Browse files
[FEATURE][composer] Data defined legend titles and column count
(fix #11913)
  • Loading branch information
nyalldawson committed Jan 23, 2017
1 parent a67c84b commit ec9ba9c2a2fab4969d0927b4c509ce7573ea35a3
@@ -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 );

};
@@ -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
@@ -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 & ) ),
@@ -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 )
@@ -265,6 +265,7 @@ void QgsComposerLegend::setLegendFilterByMapEnabled( bool enabled )

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

if ( mComposition && id().isEmpty() )
@@ -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 ); }
@@ -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 ); }
@@ -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() ) );

@@ -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 );

@@ -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 )
@@ -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 );

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

QgsLegendSettings mSettings;

QString mTitle;
int mColumnCount = 1;

const QgsComposerMap* mComposerMap;

bool mLegendFilterByMap;
@@ -61,6 +61,8 @@ const QgsPropertyDefinition QgsComposerObject::sPropertyNameMap
{ QgsComposerObject::PictureSvgBackgroundColor, "dataDefinedSvgBackgroundColor" },
{ QgsComposerObject::PictureSvgOutlineColor, "dataDefinedSvgOutlineColor" },
{ QgsComposerObject::PictureSvgOutlineWidth, "dataDefinedSvgOutlineWidth" },
{ QgsComposerObject::LegendTitle, "dataDefinedLegendTitle" },
{ QgsComposerObject::LegendColumnCount, "dataDefinedLegendColumns" },
};


@@ -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;

0 comments on commit ec9ba9c

Please sign in to comment.