From c236af48f7e52e795b68f797f3773a33f4fc6c9a Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 10 Jul 2024 10:58:32 +1000 Subject: [PATCH] Drop generic option, go with custom property specific for GeoPDF exports It's too messy to try to handle this in a format-agnostic way, as the requirement for GeoPDF is very unique to GeoPDF (ie SVG and other formats don't export as mini-read-only-projects with custom layer trees like GeoPDF does) --- .../layout/qgslayoutitem.sip.in | 34 -------- .../layout/qgslayoutitem.sip.in | 34 -------- src/core/layout/qgslayoutexporter.cpp | 17 +++- src/core/layout/qgslayoutexporter.h | 5 +- src/core/layout/qgslayoutitem.cpp | 12 --- src/core/layout/qgslayoutitem.h | 32 -------- src/gui/layout/qgslayoutitemwidget.cpp | 9 ++- tests/src/core/testqgslayoutexporter.cpp | 81 ++++++++++--------- tests/src/core/testqgslayoutitem.cpp | 17 +--- 9 files changed, 69 insertions(+), 172 deletions(-) diff --git a/python/PyQt6/core/auto_generated/layout/qgslayoutitem.sip.in b/python/PyQt6/core/auto_generated/layout/qgslayoutitem.sip.in index be0f9d5dfc35..e4302514cf24 100644 --- a/python/PyQt6/core/auto_generated/layout/qgslayoutitem.sip.in +++ b/python/PyQt6/core/auto_generated/layout/qgslayoutitem.sip.in @@ -442,40 +442,6 @@ Returns the behavior of this item during exporting to layered exports (e.g. SVG .. seealso:: :py:func:`exportLayerName` .. versionadded:: 3.10 -%End - - QString exportLayerName() const; -%Docstring -Returns the name for this item during exporting to layered exports (e.g. SVG or GeoPDF). - -By default this is an empty string, which indicates that the item does not need to be placed in any specific -layer and will automatically be grouped with other items where possible. - -If the layer name is non-empty, then the item will be placed in a group with the corresponding name -during layered exports. - -.. seealso:: :py:func:`setExportLayerName` - -.. seealso:: :py:func:`exportLayerBehavior` - -.. versionadded:: 3.40 -%End - - void setExportLayerName( const QString &name ); -%Docstring -Sets the ``name`` for this item during exporting to layered exports (e.g. SVG or GeoPDF). - -If ``name`` is an empty string then the item does not need to be placed in any specific -layer and will automatically be grouped with other items where possible. - -If the layer ``name`` is non-empty, then the item will be placed in a group with the corresponding name -during layered exports. - -.. seealso:: :py:func:`exportLayerName` - -.. seealso:: :py:func:`exportLayerBehavior` - -.. versionadded:: 3.40 %End virtual int numberExportLayers() const /Deprecated/; diff --git a/python/core/auto_generated/layout/qgslayoutitem.sip.in b/python/core/auto_generated/layout/qgslayoutitem.sip.in index 72f52e6ac7f3..ee7011244b89 100644 --- a/python/core/auto_generated/layout/qgslayoutitem.sip.in +++ b/python/core/auto_generated/layout/qgslayoutitem.sip.in @@ -442,40 +442,6 @@ Returns the behavior of this item during exporting to layered exports (e.g. SVG .. seealso:: :py:func:`exportLayerName` .. versionadded:: 3.10 -%End - - QString exportLayerName() const; -%Docstring -Returns the name for this item during exporting to layered exports (e.g. SVG or GeoPDF). - -By default this is an empty string, which indicates that the item does not need to be placed in any specific -layer and will automatically be grouped with other items where possible. - -If the layer name is non-empty, then the item will be placed in a group with the corresponding name -during layered exports. - -.. seealso:: :py:func:`setExportLayerName` - -.. seealso:: :py:func:`exportLayerBehavior` - -.. versionadded:: 3.40 -%End - - void setExportLayerName( const QString &name ); -%Docstring -Sets the ``name`` for this item during exporting to layered exports (e.g. SVG or GeoPDF). - -If ``name`` is an empty string then the item does not need to be placed in any specific -layer and will automatically be grouped with other items where possible. - -If the layer ``name`` is non-empty, then the item will be placed in a group with the corresponding name -during layered exports. - -.. seealso:: :py:func:`exportLayerName` - -.. seealso:: :py:func:`exportLayerBehavior` - -.. versionadded:: 3.40 %End virtual int numberExportLayers() const /Deprecated/; diff --git a/src/core/layout/qgslayoutexporter.cpp b/src/core/layout/qgslayoutexporter.cpp index c835ec579fac..14c504c48f7f 100644 --- a/src/core/layout/qgslayoutexporter.cpp +++ b/src/core/layout/qgslayoutexporter.cpp @@ -628,7 +628,11 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdf( const QString &f p.end(); return layerExportResult; }; - result = handleLayeredExport( items, exportFunc ); + auto getExportGroupNameFunc = []( QgsLayoutItem * item )->QString + { + return item->customProperty( QStringLiteral( "pdfExportGroup" ) ).toString(); + }; + result = handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); if ( result != Success ) return result; @@ -1113,7 +1117,11 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToSvg( const QString &f { return renderToLayeredSvg( settings, width, height, i, bounds, fileName, layerId, layerDetail.name, svg, svgDocRoot, settings.exportMetadata ); }; - ExportResult res = handleLayeredExport( items, exportFunc ); + auto getExportGroupNameFunc = []( QgsLayoutItem * )->QString + { + return QString(); + }; + ExportResult res = handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); if ( res != Success ) return res; @@ -1771,7 +1779,8 @@ QString nameForLayerWithItems( const QList< QGraphicsItem * > &items, unsigned i } QgsLayoutExporter::ExportResult QgsLayoutExporter::handleLayeredExport( const QList &items, - const std::function &exportFunc ) + const std::function &exportFunc, + const std::function &getItemExportGroupFunc ) { LayoutItemHider itemHider( items ); ( void )itemHider; @@ -1793,7 +1802,7 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::handleLayeredExport( const QL if ( layoutItem ) { QgsLayoutItem::ExportLayerBehavior itemExportBehavior = layoutItem->exportLayerBehavior(); - thisItemExportGroupName = layoutItem->exportLayerName(); + thisItemExportGroupName = getItemExportGroupFunc( layoutItem ); if ( !thisItemExportGroupName.isEmpty() ) { if ( thisItemExportGroupName != previousItemGroup && !currentLayerItems.empty() ) diff --git a/src/core/layout/qgslayoutexporter.h b/src/core/layout/qgslayoutexporter.h index 51005785e4b0..633e89fc68c6 100644 --- a/src/core/layout/qgslayoutexporter.h +++ b/src/core/layout/qgslayoutexporter.h @@ -776,7 +776,10 @@ class CORE_EXPORT QgsLayoutExporter bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const; - ExportResult handleLayeredExport( const QList &items, const std::function &exportFunc ); + ExportResult handleLayeredExport( const QList &items, + const std::function &exportFunc, + const std::function &getItemExportGroupFunc + ); static QgsVectorSimplifyMethod createExportSimplifyMethod(); static QgsMaskRenderSettings createExportMaskSettings(); diff --git a/src/core/layout/qgslayoutitem.cpp b/src/core/layout/qgslayoutitem.cpp index 428ae0a1ec7e..ecc6ea489818 100644 --- a/src/core/layout/qgslayoutitem.cpp +++ b/src/core/layout/qgslayoutitem.cpp @@ -243,16 +243,6 @@ QgsLayoutItem::ExportLayerBehavior QgsLayoutItem::exportLayerBehavior() const return CanGroupWithAnyOtherItem; } -QString QgsLayoutItem::exportLayerName() const -{ - return mExportLayerName; -} - -void QgsLayoutItem::setExportLayerName( const QString &name ) -{ - mExportLayerName = name; -} - int QgsLayoutItem::numberExportLayers() const { return 0; @@ -643,7 +633,6 @@ bool QgsLayoutItem::writeXml( QDomElement &parentElement, QDomDocument &doc, con element.setAttribute( QStringLiteral( "size" ), mItemSize.encodeSize() ); element.setAttribute( QStringLiteral( "itemRotation" ), QString::number( mItemRotation ) ); element.setAttribute( QStringLiteral( "groupUuid" ), mParentGroupUuid ); - element.setAttribute( QStringLiteral( "exportLayer" ), mExportLayerName ); element.setAttribute( QStringLiteral( "zValue" ), QString::number( zValue() ) ); element.setAttribute( QStringLiteral( "visibility" ), isVisible() ); @@ -834,7 +823,6 @@ bool QgsLayoutItem::readXml( const QDomElement &element, const QDomDocument &doc mExcludeFromExports = element.attribute( QStringLiteral( "excludeFromExports" ), QStringLiteral( "0" ) ).toInt(); mEvaluatedExcludeFromExports = mExcludeFromExports; - mExportLayerName = element.attribute( QStringLiteral( "exportLayer" ) ); const bool result = readPropertiesFromElement( element, doc, context ); diff --git a/src/core/layout/qgslayoutitem.h b/src/core/layout/qgslayoutitem.h index be5301966803..509abbb9d741 100644 --- a/src/core/layout/qgslayoutitem.h +++ b/src/core/layout/qgslayoutitem.h @@ -475,36 +475,6 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt */ virtual ExportLayerBehavior exportLayerBehavior() const; - /** - * Returns the name for this item during exporting to layered exports (e.g. SVG or GeoPDF). - * - * By default this is an empty string, which indicates that the item does not need to be placed in any specific - * layer and will automatically be grouped with other items where possible. - * - * If the layer name is non-empty, then the item will be placed in a group with the corresponding name - * during layered exports. - * - * \see setExportLayerName() - * \see exportLayerBehavior() - * \since QGIS 3.40 - */ - QString exportLayerName() const; - - /** - * Sets the \a name for this item during exporting to layered exports (e.g. SVG or GeoPDF). - * - * If \a name is an empty string then the item does not need to be placed in any specific - * layer and will automatically be grouped with other items where possible. - * - * If the layer \a name is non-empty, then the item will be placed in a group with the corresponding name - * during layered exports. - * - * \see exportLayerName() - * \see exportLayerBehavior() - * \since QGIS 3.40 - */ - void setExportLayerName( const QString &name ); - /** * Returns the number of layers that this item requires for exporting during layered exports (e.g. SVG). * Returns 0 if this item is to be placed on the same layer as the previous item, @@ -1348,8 +1318,6 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt //! Whether item should be excluded in exports bool mExcludeFromExports = false; - QString mExportLayerName; - /** * Temporary evaluated item exclusion. Data defined properties may mean * this value differs from mExcludeFromExports. diff --git a/src/gui/layout/qgslayoutitemwidget.cpp b/src/gui/layout/qgslayoutitemwidget.cpp index 244b20e7920f..0b980124eb19 100644 --- a/src/gui/layout/qgslayoutitemwidget.cpp +++ b/src/gui/layout/qgslayoutitemwidget.cpp @@ -747,7 +747,7 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements() whileBlocking( mOpacityWidget )->setOpacity( mItem->itemOpacity() ); whileBlocking( mItemRotationSpinBox )->setValue( mItem->itemRotation() ); whileBlocking( mExcludeFromPrintsCheckBox )->setChecked( mItem->excludeFromExports() ); - whileBlocking( mExportGroupNameCombo )->setCurrentText( mItem->exportLayerName() ); + whileBlocking( mExportGroupNameCombo )->setCurrentText( mItem->customProperty( QStringLiteral( "pdfExportGroup" ) ).toString() ); } void QgsLayoutItemPropertiesWidget::initializeDataDefinedButtons() @@ -795,8 +795,9 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiElements() QStringList existingGroups; for ( const QgsLayoutItem *item : std::as_const( items ) ) { - if ( !item->exportLayerName().isEmpty() && !existingGroups.contains( item->exportLayerName() ) ) - existingGroups.append( item->exportLayerName() ); + const QString groupName = item->customProperty( QStringLiteral( "pdfExportGroup" ) ).toString(); + if ( !groupName.isEmpty() && !existingGroups.contains( groupName ) ) + existingGroups.append( groupName ); } std::sort( existingGroups.begin(), existingGroups.end(), [ = ]( const QString & a, const QString & b ) -> bool @@ -852,7 +853,7 @@ void QgsLayoutItemPropertiesWidget::exportGroupNameEditingFinished() if ( mItem ) { mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Export Group Name" ), QgsLayoutItem::UndoExportLayerName ); - mItem->setExportLayerName( mExportGroupNameCombo->currentText() ); + mItem->setCustomProperty( QStringLiteral( "pdfExportGroup" ), mExportGroupNameCombo->currentText() ); mItem->layout()->undoStack()->endCommand(); } } diff --git a/tests/src/core/testqgslayoutexporter.cpp b/tests/src/core/testqgslayoutexporter.cpp index 9756c0eaef20..831abf7af200 100644 --- a/tests/src/core/testqgslayoutexporter.cpp +++ b/tests/src/core/testqgslayoutexporter.cpp @@ -88,8 +88,13 @@ void TestQgsLayoutExporter::testHandleLayeredExport() return QgsLayoutExporter::Success; }; + auto getExportGroupNameFunc = []( QgsLayoutItem * )->QString + { + return QString(); + }; + QList< QGraphicsItem * > items; - QgsLayoutExporter::ExportResult res = exporter.handleLayeredExport( items, exportFunc ); + QgsLayoutExporter::ExportResult res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QVERIFY( layerIds.isEmpty() ); QVERIFY( layerNames.isEmpty() ); @@ -98,7 +103,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() // add two pages to a layout QgsLayoutItemPage *page1 = new QgsLayoutItemPage( &l ); items << page1; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Page" ) ); @@ -109,7 +114,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l ); items << page2; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) ); @@ -120,7 +125,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemLabel *label = new QgsLayoutItemLabel( &l ); items << label; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label" ) ); @@ -131,7 +136,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemShape *shape = new QgsLayoutItemShape( &l ); items << shape; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) ); @@ -142,7 +147,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemLabel *label2 = new QgsLayoutItemLabel( &l ); items << label2; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) ); @@ -154,7 +159,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() // add an item which can only be used with other similar items, should break the next label into a different layer QgsLayoutItemScaleBar *scaleBar = new QgsLayoutItemScaleBar( &l ); items << scaleBar; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) ); @@ -165,7 +170,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemLabel *label3 = new QgsLayoutItemLabel( &l ); items << label3; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) ); @@ -179,7 +184,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() items << scaleBar2; QgsLayoutItemScaleBar *scaleBar3 = new QgsLayoutItemScaleBar( &l ); items << scaleBar3; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebars" ) ); @@ -204,7 +209,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() map->setLayers( QList() << linesLayer ); items << map; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebars" ) << QStringLiteral( "Map 1: lines" ) ); @@ -215,7 +220,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() map->setFrameEnabled( true ); map->setBackgroundEnabled( true ); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebars" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) ); @@ -230,7 +235,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemLegend *legend2 = new QgsLayoutItemLegend( &l ); legend2->setId( QStringLiteral( "my legend 2" ) ); items << legend << legend2; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebars" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) << QStringLiteral( "my legend" ) << QStringLiteral( "my legend 2" ) ); @@ -241,7 +246,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemLabel *label4 = new QgsLayoutItemLabel( &l ); items << label4; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebars" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) << QStringLiteral( "my legend" ) << QStringLiteral( "my legend 2" ) << QStringLiteral( "Label" ) ); @@ -252,7 +257,7 @@ void TestQgsLayoutExporter::testHandleLayeredExport() QgsLayoutItemLabel *label5 = new QgsLayoutItemLabel( &l ); items << label5; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Labels, Shape" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebars" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) << QStringLiteral( "my legend" ) << QStringLiteral( "my legend 2" ) << QStringLiteral( "Labels" ) ); @@ -289,10 +294,14 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() return QgsLayoutExporter::Success; }; + auto getExportGroupNameFunc = []( QgsLayoutItem * item )->QString + { + return item->customProperty( QStringLiteral( "pdfExportGroup" ) ).toString(); + }; QList< QGraphicsItem * > items; QStringList expectedGroupNames; - QgsLayoutExporter::ExportResult res = exporter.handleLayeredExport( items, exportFunc ); + QgsLayoutExporter::ExportResult res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QVERIFY( layerIds.isEmpty() ); QVERIFY( layerNames.isEmpty() ); @@ -303,7 +312,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemPage *page1 = new QgsLayoutItemPage( &l ); items << page1; expectedGroupNames << QString(); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Page" ) ); @@ -316,7 +325,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l ); items << page2; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) ); @@ -330,7 +339,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemLabel *label = new QgsLayoutItemLabel( &l ); items << label; expectedGroupNames << QString(); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label" ) ); @@ -343,7 +352,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemShape *shape = new QgsLayoutItemShape( &l ); items << shape; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) ); @@ -355,10 +364,10 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() mapLayerIds.clear(); QgsLayoutItemLabel *label2 = new QgsLayoutItemLabel( &l ); - label2->setExportLayerName( QStringLiteral( "first group" ) ); + label2->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "first group" ) ); expectedGroupNames << QStringLiteral( "first group" ); items << label2; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) ); @@ -370,10 +379,10 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() // add an item which can only be used with other similar items, should break the next label into a different layer QgsLayoutItemScaleBar *scaleBar = new QgsLayoutItemScaleBar( &l ); - scaleBar->setExportLayerName( QStringLiteral( "first group" ) ); + scaleBar->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "first group" ) ); expectedGroupNames << QStringLiteral( "first group" ); items << scaleBar; - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) ); @@ -387,7 +396,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemLabel *label3 = new QgsLayoutItemLabel( &l ); items << label3; expectedGroupNames << QString(); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) ); @@ -399,14 +408,14 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() mapLayerIds.clear(); QgsLayoutItemScaleBar *scaleBar2 = new QgsLayoutItemScaleBar( &l ); - scaleBar2->setExportLayerName( QStringLiteral( "scales" ) ); + scaleBar2->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "scales" ) ); items << scaleBar2; expectedGroupNames << QStringLiteral( "scales" ); QgsLayoutItemScaleBar *scaleBar3 = new QgsLayoutItemScaleBar( &l ); - scaleBar3->setExportLayerName( QStringLiteral( "scales" ) ); + scaleBar3->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "scales" ) ); items << scaleBar3; expectedGroupNames << QStringLiteral( "scales" ); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Scalebar" ) ); @@ -434,7 +443,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() items << map; expectedGroupNames << QString(); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Map 1: lines" ) ); @@ -447,7 +456,7 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() map->setFrameEnabled( true ); map->setBackgroundEnabled( true ); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); expectedGroupNames << QString() << QString(); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 ); @@ -462,13 +471,13 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() // add two legends -- legends are complex and must be placed in an isolated layer QgsLayoutItemLegend *legend = new QgsLayoutItemLegend( &l ); legend->setId( QStringLiteral( "my legend" ) ); - legend->setExportLayerName( QStringLiteral( "second group" ) ); + legend->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "second group" ) ); QgsLayoutItemLegend *legend2 = new QgsLayoutItemLegend( &l ); legend2->setId( QStringLiteral( "my legend 2" ) ); - legend2->setExportLayerName( QStringLiteral( "second group" ) ); + legend2->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "second group" ) ); items << legend << legend2; expectedGroupNames << QStringLiteral( "second group" ) << QStringLiteral( "second group" ); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) << QStringLiteral( "my legend" ) << QStringLiteral( "my legend 2" ) ); @@ -481,9 +490,9 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemLabel *label4 = new QgsLayoutItemLabel( &l ); items << label4; - label4->setExportLayerName( QStringLiteral( "more labels" ) ); + label4->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "more labels" ) ); expectedGroupNames << QStringLiteral( "more labels" ); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) << QStringLiteral( "my legend" ) << QStringLiteral( "my legend 2" ) << QStringLiteral( "Label" ) ); @@ -496,9 +505,9 @@ void TestQgsLayoutExporter::testHandleLayeredExportCustomGroups() QgsLayoutItemLabel *label5 = new QgsLayoutItemLabel( &l ); items << label5; - label5->setExportLayerName( QStringLiteral( "more labels 2" ) ); + label5->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "more labels 2" ) ); expectedGroupNames << QStringLiteral( "more labels 2" ); - res = exporter.handleLayeredExport( items, exportFunc ); + res = exporter.handleLayeredExport( items, exportFunc, getExportGroupNameFunc ); QCOMPARE( res, QgsLayoutExporter::Success ); QCOMPARE( layerIds, QList< unsigned int >() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 ); QCOMPARE( layerNames, QStringList() << QStringLiteral( "Pages" ) << QStringLiteral( "Label, Shape" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Label" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Scalebar" ) << QStringLiteral( "Map 1: Background" ) << QStringLiteral( "Map 1: lines" ) << QStringLiteral( "Map 1: Frame" ) << QStringLiteral( "my legend" ) << QStringLiteral( "my legend 2" ) << QStringLiteral( "Label" ) << QStringLiteral( "Label" ) ); diff --git a/tests/src/core/testqgslayoutitem.cpp b/tests/src/core/testqgslayoutitem.cpp index a4635fec7454..837ab6bfbe98 100644 --- a/tests/src/core/testqgslayoutitem.cpp +++ b/tests/src/core/testqgslayoutitem.cpp @@ -170,7 +170,6 @@ class TestQgsLayoutItem: public QgsTest void blendMode(); void opacity(); void excludeFromExports(); - void exportName(); void setSceneRect(); void page(); void itemVariablesFunction(); @@ -1744,7 +1743,7 @@ void TestQgsLayoutItem::writeReadXmlProperties() original->setBlendMode( QPainter::CompositionMode_Darken ); original->setExcludeFromExports( true ); original->setItemOpacity( 0.75 ); - original->setExportLayerName( QStringLiteral( "_export_layer_" ) ); + original->setCustomProperty( QStringLiteral( "pdfExportGroup" ), QStringLiteral( "_export_layer_" ) ); std::unique_ptr< QgsLayoutItem > copy = createCopyViaXml( &l, original ); @@ -1774,7 +1773,7 @@ void TestQgsLayoutItem::writeReadXmlProperties() QCOMPARE( copy->blendMode(), QPainter::CompositionMode_Darken ); QVERIFY( copy->excludeFromExports( ) ); QCOMPARE( copy->itemOpacity(), 0.75 ); - QCOMPARE( copy->exportLayerName(), QStringLiteral( "_export_layer_" ) ); + QCOMPARE( copy->customProperty( QStringLiteral( "pdfExportGroup" ) ).toString(), QStringLiteral( "_export_layer_" ) ); delete original; } @@ -2099,18 +2098,6 @@ void TestQgsLayoutItem::excludeFromExports() QGSVERIFYLAYOUTCHECK( QStringLiteral( "layoutitem_excluded" ), &l, 0, 0, QSize( 400, 400 ) ); } -void TestQgsLayoutItem::exportName() -{ - QgsProject proj; - QgsLayout l( &proj ); - QgsLayoutItemShape *item = new QgsLayoutItemShape( &l ); - l.addLayoutItem( item ); - - QCOMPARE( item->exportLayerName(), QString() ); - item->setExportLayerName( QStringLiteral( "my export group" ) ); - QCOMPARE( item->exportLayerName(), QStringLiteral( "my export group" ) ); -} - std::unique_ptr TestQgsLayoutItem::createCopyViaXml( QgsLayout *layout, QgsLayoutItem *original ) { //save original item to xml