Skip to content

Commit 8b1e057

Browse files
committedDec 17, 2017
Make QgsLayoutExporter::generateFileName virtual, so exporter
subclasses can be made which customise the generated file names
1 parent 48b6e02 commit 8b1e057

File tree

4 files changed

+102
-11
lines changed

4 files changed

+102
-11
lines changed
 

‎python/core/layout/qgslayoutexporter.sip

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,42 @@ class QgsLayoutExporter
2121
%End
2222
public:
2323

24+
struct PageExportDetails
25+
{
26+
QString directory;
27+
%Docstring
28+
Target folder
29+
%End
30+
31+
QString baseName;
32+
%Docstring
33+
Base part of filename (i.e. file name without extension or '.')
34+
%End
35+
36+
QString extension;
37+
%Docstring
38+
File suffix/extension (without the leading '.')
39+
%End
40+
41+
int page;
42+
%Docstring
43+
Page number, where 0 = first page.
44+
%End
45+
};
46+
2447
QgsLayoutExporter( QgsLayout *layout );
2548
%Docstring
2649
Constructor for QgsLayoutExporter, for the specified ``layout``.
2750
%End
2851

52+
virtual ~QgsLayoutExporter();
53+
54+
QgsLayout *layout() const;
55+
%Docstring
56+
Returns the layout linked to this exporter.
57+
:rtype: QgsLayout
58+
%End
59+
2960
void renderPage( QPainter *painter, int page ) const;
3061
%Docstring
3162
Renders a full page to a destination ``painter``.
@@ -207,6 +238,15 @@ Resolution to export layout at
207238
The ``dpi`` argument can be set to the actual DPI of exported file, or left as -1 to use the layout's default DPI.
208239
%End
209240

241+
protected:
242+
243+
virtual QString generateFileName( const PageExportDetails &details ) const;
244+
%Docstring
245+
Generates the file name for a page during export.
246+
247+
Subclasses can override this method to customise page file naming.
248+
:rtype: str
249+
%End
210250

211251
};
212252

‎src/core/layout/qgslayoutexporter.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ QgsLayoutExporter::QgsLayoutExporter( QgsLayout *layout )
3131

3232
}
3333

34+
QgsLayout *QgsLayoutExporter::layout() const
35+
{
36+
return mLayout;
37+
}
38+
3439
void QgsLayoutExporter::renderPage( QPainter *painter, int page ) const
3540
{
3641
if ( !mLayout )
@@ -169,9 +174,11 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( const QString
169174
}
170175

171176
QFileInfo fi( filePath );
172-
QString path = fi.path();
173-
QString baseName = fi.baseName();
174-
QString extension = fi.completeSuffix();
177+
178+
PageExportDetails pageDetails;
179+
pageDetails.directory = fi.path();
180+
pageDetails.baseName = fi.baseName();
181+
pageDetails.extension = fi.completeSuffix();
175182

176183
LayoutContextSettingsRestorer dpiRestorer( mLayout );
177184
( void )dpiRestorer;
@@ -207,15 +214,16 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( const QString
207214
if ( skip )
208215
continue; // should skip this page, e.g. null size
209216

210-
QString outputFilePath = generateFileName( path, baseName, extension, page );
217+
pageDetails.page = page;
218+
QString outputFilePath = generateFileName( pageDetails );
211219

212220
if ( image.isNull() )
213221
{
214222
mErrorFileName = outputFilePath;
215223
return MemoryError;
216224
}
217225

218-
if ( !saveImage( image, outputFilePath, extension ) )
226+
if ( !saveImage( image, outputFilePath, pageDetails.extension ) )
219227
{
220228
mErrorFileName = outputFilePath;
221229
return FileError;
@@ -509,15 +517,15 @@ QImage QgsLayoutExporter::createImage( const QgsLayoutExporter::ImageExportSetti
509517
}
510518
}
511519

512-
QString QgsLayoutExporter::generateFileName( const QString &path, const QString &baseName, const QString &suffix, int page ) const
520+
QString QgsLayoutExporter::generateFileName( const PageExportDetails &details ) const
513521
{
514-
if ( page == 0 )
522+
if ( details.page == 0 )
515523
{
516-
return path + '/' + baseName + '.' + suffix;
524+
return details.directory + '/' + details.baseName + '.' + details.extension;
517525
}
518526
else
519527
{
520-
return path + '/' + baseName + '_' + QString::number( page + 1 ) + '.' + suffix;
528+
return details.directory + '/' + details.baseName + '_' + QString::number( details.page + 1 ) + '.' + details.extension;
521529
}
522530
}
523531

‎src/core/layout/qgslayoutexporter.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,34 @@ class CORE_EXPORT QgsLayoutExporter
3838

3939
public:
4040

41+
//! Contains details of a page being exported by the class
42+
struct PageExportDetails
43+
{
44+
//! Target folder
45+
QString directory;
46+
47+
//! Base part of filename (i.e. file name without extension or '.')
48+
QString baseName;
49+
50+
//! File suffix/extension (without the leading '.')
51+
QString extension;
52+
53+
//! Page number, where 0 = first page.
54+
int page = 0;
55+
};
56+
4157
/**
4258
* Constructor for QgsLayoutExporter, for the specified \a layout.
4359
*/
4460
QgsLayoutExporter( QgsLayout *layout );
4561

62+
virtual ~QgsLayoutExporter() = default;
63+
64+
/**
65+
* Returns the layout linked to this exporter.
66+
*/
67+
QgsLayout *layout() const;
68+
4669
/**
4770
* Renders a full page to a destination \a painter.
4871
*
@@ -219,6 +242,14 @@ class CORE_EXPORT QgsLayoutExporter
219242
*/
220243
void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
221244

245+
protected:
246+
247+
/**
248+
* Generates the file name for a page during export.
249+
*
250+
* Subclasses can override this method to customise page file naming.
251+
*/
252+
virtual QString generateFileName( const PageExportDetails &details ) const;
222253

223254
private:
224255

@@ -228,8 +259,6 @@ class CORE_EXPORT QgsLayoutExporter
228259

229260
QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
230261

231-
QString generateFileName( const QString &path, const QString &baseName, const QString &suffix, int page ) const;
232-
233262
/**
234263
* Saves an image to a file, possibly using format specific options (e.g. LZW compression for tiff)
235264
*/

‎tests/src/python/test_qgslayoutexporter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,20 @@ def testExportWorldFile(self):
317317
self.assertAlmostEqual(values[4], 1925.000000000000, 2)
318318
self.assertAlmostEqual(values[5], 3050.000000000000, 2)
319319

320+
def testPageFileName(self):
321+
l = QgsLayout(QgsProject.instance())
322+
exporter = QgsLayoutExporter(l)
323+
details = QgsLayoutExporter.PageExportDetails()
324+
details.directory = '/tmp/output'
325+
details.baseName = 'my_maps'
326+
details.extension = 'png'
327+
details.page = 0
328+
self.assertEqual(exporter.generateFileName(details), '/tmp/output/my_maps.png')
329+
details.page = 1
330+
self.assertEqual(exporter.generateFileName(details), '/tmp/output/my_maps_2.png')
331+
details.page = 2
332+
self.assertEqual(exporter.generateFileName(details), '/tmp/output/my_maps_3.png')
333+
320334

321335
if __name__ == '__main__':
322336
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.