Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[api] Add method for plugins to retrieve results of last layout export
operation from a layout designer window
  • Loading branch information
nyalldawson committed Jun 9, 2021
1 parent 2a57287 commit e627144
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
28 changes: 28 additions & 0 deletions python/gui/auto_generated/layout/qgslayoutdesignerinterface.sip.in
Expand Up @@ -413,6 +413,34 @@ Removes the specified ``dock`` widget from layout designer (without deleting it)
Activates a standard layout designer ``tool``.

.. versionadded:: 3.6
%End

class ExportResults
{
%Docstring(signature="appended")
Encapsulates the results of an export operation performed in the designer.

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgslayoutdesignerinterface.h"
%End
public:

QgsLayoutExporter::ExportResult result;

QMap< QString, QgsLabelingResults * > labelingResults;

};

virtual QgsLayoutDesignerInterface::ExportResults *lastExportResults() const = 0 /Factory/;
%Docstring
Returns the results of the last export operation performed in the designer.

May be ``None`` if no export has been performed in the designer.

.. versionadded:: 3.20
%End

public slots:
Expand Down
42 changes: 42 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -247,6 +247,11 @@ void QgsAppLayoutDesignerInterface::activateTool( QgsLayoutDesignerInterface::St
}
}

QgsLayoutDesignerInterface::ExportResults *QgsAppLayoutDesignerInterface::lastExportResults() const
{
return mDesigner->lastExportResults().release();
}

void QgsAppLayoutDesignerInterface::close()
{
mDesigner->close();
Expand Down Expand Up @@ -1010,6 +1015,9 @@ QgsLayoutDesignerDialog::~QgsLayoutDesignerDialog()
settings.remove( QStringLiteral( "LayoutDesigner/hiddenDocksTitle" ), QgsSettings::App );
settings.remove( QStringLiteral( "LayoutDesigner/hiddenDocksActive" ), QgsSettings::App );
}

qDeleteAll( mLastExportLabelingResults );
mLastExportLabelingResults.clear();
}

QgsAppLayoutDesignerInterface *QgsLayoutDesignerDialog::iface()
Expand Down Expand Up @@ -1103,6 +1111,11 @@ void QgsLayoutDesignerDialog::showGuideDock( bool show )
mGuideDock->setUserVisible( show );
}

std::unique_ptr<QgsLayoutDesignerInterface::ExportResults> QgsLayoutDesignerDialog::lastExportResults() const
{
return mLastExportResults ? std::make_unique< QgsLayoutDesignerInterface::ExportResults>( *mLastExportResults ) : nullptr;
}

QgsLayout *QgsLayoutDesignerDialog::currentLayout()
{
return mLayout;
Expand Down Expand Up @@ -2097,6 +2110,7 @@ void QgsLayoutDesignerDialog::print()
QgsLayoutExporter::ExportResult result = exporter.print( *p, printSettings );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result, &exporter );

switch ( result )
{
Expand Down Expand Up @@ -2211,6 +2225,7 @@ void QgsLayoutDesignerDialog::exportToRaster()
QgsLayoutExporter::ExportResult result = exporter.exportToImage( fileNExt.first, settings );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result, &exporter );

switch ( result )
{
Expand Down Expand Up @@ -2324,6 +2339,7 @@ void QgsLayoutDesignerDialog::exportToPdf()
QgsLayoutExporter::ExportResult result = exporter.exportToPdf( outputFileName, pdfSettings );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result, &exporter );

switch ( result )
{
Expand Down Expand Up @@ -2435,6 +2451,7 @@ void QgsLayoutDesignerDialog::exportToSvg()
QgsLayoutExporter::ExportResult result = exporter.exportToSvg( outputFileName, svgSettings );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result, &exporter );

switch ( result )
{
Expand Down Expand Up @@ -2709,6 +2726,7 @@ void QgsLayoutDesignerDialog::printAtlas()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::print( printAtlas, *p, printSettings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );

switch ( result )
{
Expand Down Expand Up @@ -2914,6 +2932,7 @@ void QgsLayoutDesignerDialog::exportAtlasToRaster()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToImage( printAtlas, fileName, fileExt, settings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );

cursorOverride.release();

Expand Down Expand Up @@ -3079,6 +3098,7 @@ void QgsLayoutDesignerDialog::exportAtlasToSvg()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToSvg( printAtlas, filename, svgSettings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );

cursorOverride.release();
switch ( result )
Expand Down Expand Up @@ -3317,6 +3337,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
}

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );

cursorOverride.release();
switch ( result )
Expand Down Expand Up @@ -3446,6 +3467,7 @@ void QgsLayoutDesignerDialog::exportReportToRaster()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToImage( static_cast< QgsReport * >( mMasterLayout ), fileName, fileNExt.second, settings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );
cursorOverride.release();

switch ( result )
Expand Down Expand Up @@ -3563,6 +3585,7 @@ void QgsLayoutDesignerDialog::exportReportToSvg()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToSvg( static_cast< QgsReport * >( mMasterLayout ), outFile, svgSettings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );
cursorOverride.release();
switch ( result )
{
Expand Down Expand Up @@ -3696,6 +3719,7 @@ void QgsLayoutDesignerDialog::exportReportToPdf()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToPdf( static_cast< QgsReport * >( mMasterLayout ), outputFileName, pdfSettings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );
cursorOverride.release();

switch ( result )
Expand Down Expand Up @@ -3804,6 +3828,7 @@ void QgsLayoutDesignerDialog::printReport()
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::print( static_cast< QgsReport * >( mMasterLayout ), *p, printSettings, error, feedback.get() );

proxyTask->finalize( result == QgsLayoutExporter::Success );
storeExportResults( result );

switch ( result )
{
Expand Down Expand Up @@ -4864,6 +4889,23 @@ void QgsLayoutDesignerDialog::backgroundTaskCountChanged( int total )
}
}

void QgsLayoutDesignerDialog::storeExportResults( QgsLayoutExporter::ExportResult result, QgsLayoutExporter *exporter )
{
mLastExportResults = std::make_unique< QgsLayoutDesignerInterface::ExportResults >();
mLastExportResults->result = result;

if ( exporter )
{
mLastExportLabelingResults = exporter->takeLabelingResults();
mLastExportResults->labelingResults = mLastExportLabelingResults;
}
else
{
qDeleteAll( mLastExportLabelingResults );
mLastExportLabelingResults.clear();
}
}

void QgsLayoutDesignerDialog::selectItems( const QList<QgsLayoutItem *> &items )
{
for ( QGraphicsItem *item : items )
Expand Down
12 changes: 11 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -81,7 +81,7 @@ class QgsAppLayoutDesignerInterface : public QgsLayoutDesignerInterface
void addDockWidget( Qt::DockWidgetArea area, QDockWidget *dock ) override;
void removeDockWidget( QDockWidget *dock ) override;
void activateTool( StandardTool tool ) override;

QgsLayoutDesignerInterface::ExportResults *lastExportResults() const override;
public slots:

void close() override;
Expand Down Expand Up @@ -203,6 +203,13 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB
*/
void showGuideDock( bool show );

/**
* Returns the results of the last export operation performed in the designer.
*
* May be NULLPTR if no export has been performed in the designer.
*/
std::unique_ptr< QgsLayoutDesignerInterface::ExportResults > lastExportResults() const;

public slots:

/**
Expand Down Expand Up @@ -492,6 +499,9 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB
QgsLayoutGuideWidget *mGuideWidget = nullptr;

bool mIsExportingAtlas = false;
void storeExportResults( QgsLayoutExporter::ExportResult result, QgsLayoutExporter *exporter = nullptr );
std::unique_ptr< QgsLayoutDesignerInterface::ExportResults> mLastExportResults;
QMap< QString, QgsLabelingResults *> mLastExportLabelingResults;

//! Save window state
void saveWindowState();
Expand Down
33 changes: 33 additions & 0 deletions src/gui/layout/qgslayoutdesignerinterface.h
Expand Up @@ -18,6 +18,7 @@

#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgslayoutexporter.h"
#include <QObject>

class QgsLayout;
Expand Down Expand Up @@ -351,6 +352,38 @@ class GUI_EXPORT QgsLayoutDesignerInterface: public QObject
*/
virtual void activateTool( StandardTool tool ) = 0;

/**
* \ingroup gui
* \brief Encapsulates the results of an export operation performed in the designer.
* \since QGIS 3.20
*/
class ExportResults
{
public:

/**
* Result/error code of export.
*/
QgsLayoutExporter::ExportResult result;

/**
* Returns the labeling results for all map items included in the export. Map keys are the item UUIDs (see QgsLayoutItem::uuid()).
*
* Ownership of the results remains with the layout designer.
*/
QMap< QString, QgsLabelingResults * > labelingResults;

};

/**
* Returns the results of the last export operation performed in the designer.
*
* May be NULLPTR if no export has been performed in the designer.
*
* \since QGIS 3.20
*/
virtual QgsLayoutDesignerInterface::ExportResults *lastExportResults() const = 0 SIP_FACTORY;

public slots:

/**
Expand Down

0 comments on commit e627144

Please sign in to comment.