Skip to content

Commit bc23664

Browse files
committedNov 7, 2017
Serialization for legend items
1 parent d3430e4 commit bc23664

File tree

3 files changed

+22
-91
lines changed

3 files changed

+22
-91
lines changed
 

‎python/core/layout/qgslayoutitemlegend.sip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ class QgsLayoutItemLegend : QgsLayoutItem
442442
Updates the legend content when filtered by map.
443443
%End
444444

445-
446445
const QgsLegendSettings &legendSettings() const;
447446
%Docstring
448447
Returns the legend's renderer settings object.
@@ -461,6 +460,10 @@ class QgsLayoutItemLegend : QgsLayoutItem
461460
protected:
462461
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
463462

463+
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
464+
465+
virtual bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context );
466+
464467

465468
};
466469

‎src/core/layout/qgslayoutitemlegend.cpp

Lines changed: 16 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void QgsLayoutItemLegend::draw( QgsRenderContext &context, const QStyleOptionGra
144144
}
145145

146146
QgsLegendRenderer legendRenderer( mLegendModel.get(), mSettings );
147-
legendRenderer.setLegendSize( mForceResize && mSizeToContents ? QSize() : rect().size() );
147+
legendRenderer.setLegendSize( mSizeToContents ? QSize() : rect().size() );
148148

149149
legendRenderer.drawLegend( painter );
150150

@@ -448,17 +448,8 @@ void QgsLayoutItemLegend::updateLegend()
448448
updateFilterByMap( false );
449449
}
450450

451-
#if 0//TODO
452-
bool QgsLayoutItemLegend::writeXml( QDomElement &elem, QDomDocument &doc ) const
451+
bool QgsLayoutItemLegend::writePropertiesToElement( QDomElement &composerLegendElem, QDomDocument &doc, const QgsReadWriteContext & ) const
453452
{
454-
if ( elem.isNull() )
455-
{
456-
return false;
457-
}
458-
459-
QDomElement composerLegendElem = doc.createElement( QStringLiteral( "ComposerLegend" ) );
460-
elem.appendChild( composerLegendElem );
461-
462453
//write general properties
463454
composerLegendElem.setAttribute( QStringLiteral( "title" ), mTitle );
464455
composerLegendElem.setAttribute( QStringLiteral( "titleAlignment" ), QString::number( static_cast< int >( mSettings.titleAlignment() ) ) );
@@ -486,7 +477,7 @@ bool QgsLayoutItemLegend::writeXml( QDomElement &elem, QDomDocument &doc ) const
486477

487478
if ( mMap )
488479
{
489-
composerLegendElem.setAttribute( QStringLiteral( "map" ), mMap->id() );
480+
composerLegendElem.setAttribute( QStringLiteral( "map_uuid" ), mMap->uuid() );
490481
}
491482

492483
QDomElement composerLegendStyles = doc.createElement( QStringLiteral( "styles" ) );
@@ -510,16 +501,11 @@ bool QgsLayoutItemLegend::writeXml( QDomElement &elem, QDomDocument &doc ) const
510501
}
511502
composerLegendElem.setAttribute( QStringLiteral( "legendFilterByAtlas" ), mFilterOutAtlas ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
512503

513-
return _writeXml( composerLegendElem, doc );
504+
return true;
514505
}
515506

516-
bool QgsLayoutItemLegend::readXml( const QDomElement &itemElem, const QDomDocument &doc )
507+
bool QgsLayoutItemLegend::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext & )
517508
{
518-
if ( itemElem.isNull() )
519-
{
520-
return false;
521-
}
522-
523509
//read general properties
524510
mTitle = itemElem.attribute( QStringLiteral( "title" ) );
525511
mSettings.setTitle( mTitle );
@@ -578,10 +564,19 @@ bool QgsLayoutItemLegend::readXml( const QDomElement &itemElem, const QDomDocume
578564

579565
//composer map
580566
mLegendFilterByMap = itemElem.attribute( QStringLiteral( "legendFilterByMap" ), QStringLiteral( "0" ) ).toInt();
567+
568+
#if 0 //TODO
581569
if ( !itemElem.attribute( QStringLiteral( "map" ) ).isEmpty() )
582570
{
583571
setMap( mComposition->getComposerMapById( itemElem.attribute( QStringLiteral( "map" ) ).toInt() ) );
584572
}
573+
#endif
574+
if ( !itemElem.attribute( QStringLiteral( "map_uuid" ) ).isEmpty() )
575+
{
576+
setMap( qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( itemElem.attribute( QStringLiteral( "map_uuid" ) ) ) ) );
577+
}
578+
579+
585580
mFilterOutAtlas = itemElem.attribute( QStringLiteral( "legendFilterByAtlas" ), QStringLiteral( "0" ) ).toInt();
586581

587582
// QGIS >= 2.6
@@ -592,67 +587,15 @@ bool QgsLayoutItemLegend::readXml( const QDomElement &itemElem, const QDomDocume
592587
if ( !layerTreeElem.isNull() )
593588
{
594589
std::unique_ptr< QgsLayerTree > tree( QgsLayerTree::readXml( layerTreeElem ) );
595-
if ( mComposition )
596-
tree->resolveReferences( mComposition->project(), true );
590+
if ( mLayout )
591+
tree->resolveReferences( mLayout->project(), true );
597592
setCustomLayerTree( tree.release() );
598593
}
599594
else
600595
setCustomLayerTree( nullptr );
601596

602-
//restore general composer item properties
603-
QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
604-
if ( !composerItemList.isEmpty() )
605-
{
606-
QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
607-
_readXml( composerItemElem, doc );
608-
}
609-
610-
// < 2.0 projects backward compatibility >>>>>
611-
//title font
612-
QString titleFontString = itemElem.attribute( QStringLiteral( "titleFont" ) );
613-
if ( !titleFontString.isEmpty() )
614-
{
615-
rstyle( QgsLegendStyle::Title ).rfont().fromString( titleFontString );
616-
}
617-
//group font
618-
QString groupFontString = itemElem.attribute( QStringLiteral( "groupFont" ) );
619-
if ( !groupFontString.isEmpty() )
620-
{
621-
rstyle( QgsLegendStyle::Group ).rfont().fromString( groupFontString );
622-
}
623-
624-
//layer font
625-
QString layerFontString = itemElem.attribute( QStringLiteral( "layerFont" ) );
626-
if ( !layerFontString.isEmpty() )
627-
{
628-
rstyle( QgsLegendStyle::Subgroup ).rfont().fromString( layerFontString );
629-
}
630-
//item font
631-
QString itemFontString = itemElem.attribute( QStringLiteral( "itemFont" ) );
632-
if ( !itemFontString.isEmpty() )
633-
{
634-
rstyle( QgsLegendStyle::SymbolLabel ).rfont().fromString( itemFontString );
635-
}
636-
637-
if ( !itemElem.attribute( QStringLiteral( "groupSpace" ) ).isEmpty() )
638-
{
639-
rstyle( QgsLegendStyle::Group ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "groupSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
640-
}
641-
if ( !itemElem.attribute( QStringLiteral( "layerSpace" ) ).isEmpty() )
642-
{
643-
rstyle( QgsLegendStyle::Subgroup ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "layerSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
644-
}
645-
if ( !itemElem.attribute( QStringLiteral( "symbolSpace" ) ).isEmpty() )
646-
{
647-
rstyle( QgsLegendStyle::Symbol ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "symbolSpace" ), QStringLiteral( "2.0" ) ).toDouble() );
648-
rstyle( QgsLegendStyle::SymbolLabel ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "symbolSpace" ), QStringLiteral( "2.0" ) ).toDouble() );
649-
}
650-
// <<<<<<< < 2.0 projects backward compatibility
651-
652-
emit itemChanged();
653597
return true;
654598
}
655-
#endif
656599

657600
QString QgsLayoutItemLegend::displayName() const
658601
{

‎src/core/layout/qgslayoutitemlegend.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -434,23 +434,6 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
434434
*/
435435
void updateFilterByMap( bool redraw = true );
436436

437-
#if 0//TODO
438-
439-
/**
440-
* Stores state in Dom node
441-
* \param elem is Dom element corresponding to 'Composer' tag
442-
* \param doc Dom document
443-
*/
444-
bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
445-
446-
/**
447-
* Sets state from Dom document
448-
* \param itemElem is Dom node corresponding to item tag
449-
* \param doc is Dom document
450-
*/
451-
bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
452-
#endif
453-
454437
/**
455438
* Returns the legend's renderer settings object.
456439
*/
@@ -465,6 +448,8 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
465448

466449
protected:
467450
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
451+
bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
452+
bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
468453

469454
private slots:
470455

0 commit comments

Comments
 (0)
Please sign in to comment.