Skip to content

Commit

Permalink
Hookup filename generation for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 935dfa3 commit 767075a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
4 changes: 1 addition & 3 deletions python/core/layout/qgsabstractreportsection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ implementations.

virtual int count();

virtual QString filePath( const QString &baseFilePath, const QString &extension );



virtual QString filePath( const QString &baseFilePath, const QString &extension );

virtual QgsLayout *layout();

Expand Down
8 changes: 5 additions & 3 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,8 @@ void QgsLayoutDesignerDialog::exportAtlasToRaster()
feedback->cancel();
} );

QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToImage( printAtlas, dir, fileExt, settings, error, feedback.get() );
QString fileName = QDir( dir ).filePath( QStringLiteral( "atlas" ) ); // filename is overridden by atlas
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToImage( printAtlas, fileName, fileExt, settings, error, feedback.get() );
QApplication::restoreOverrideCursor();

switch ( result )
Expand Down Expand Up @@ -2231,7 +2232,8 @@ void QgsLayoutDesignerDialog::exportAtlasToSvg()
feedback->cancel();
} );

QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToSvg( printAtlas, dir, svgSettings, error, feedback.get() );
QString filename = QDir( dir ).filePath( QStringLiteral( "atlas" ) ); // filename is overridden by atlas
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToSvg( printAtlas, filename, svgSettings, error, feedback.get() );

QApplication::restoreOverrideCursor();
switch ( result )
Expand Down Expand Up @@ -2406,7 +2408,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
return;
}

outputFileName = dir;
outputFileName = QDir( dir ).filePath( QStringLiteral( "atlas" ) ); // filename is overridden by atlas
}

mView->setPaintingEnabled( false );
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgsabstractreportsection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ QgsAbstractReportSection::~QgsAbstractReportSection()

QString QgsAbstractReportSection::filePath( const QString &baseFilePath, const QString &extension )
{
QString base = QDir( baseFilePath ).filePath( "report_" ) + QString::number( mSectionNumber );
QString base = QStringLiteral( "%1_%2" ).arg( baseFilePath ).arg( mSectionNumber, 4, 10, QChar( '0' ) );
if ( !extension.startsWith( '.' ) )
base += '.';
base += extension;
Expand Down
5 changes: 1 addition & 4 deletions src/core/layout/qgsabstractreportsection.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ class CORE_EXPORT QgsAbstractReportSection : public QgsAbstractLayoutIterator
// TODO - how to handle this?
int count() override { return -1; }

//TODO - baseFilePath should be a filename, not directory
QString filePath( const QString &baseFilePath, const QString &extension ) override;


#if 0 //TODO
virtual void setContext( const QgsLayoutReportContext &context ) = 0;
#endif

QString filePath( const QString &baseFilePath, const QString &extension ) override;
QgsLayout *layout() override;
bool beginRender() override;
bool next() override;
Expand Down
4 changes: 3 additions & 1 deletion src/core/layout/qgslayoutatlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ int QgsLayoutAtlas::count()

QString QgsLayoutAtlas::filePath( const QString &baseFilePath, const QString &extension )
{
QString base = QDir( baseFilePath ).filePath( mCurrentFilename );
QFileInfo fi( baseFilePath );
QDir dir = fi.dir(); // ignore everything except the directory
QString base = dir.filePath( mCurrentFilename );
if ( !extension.startsWith( '.' ) )
base += '.';
base += extension;
Expand Down
10 changes: 5 additions & 5 deletions tests/src/python/test_qgslayoutatlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,19 @@ def testFileName(self):
self.assertEqual(atlas.count(), 4)
atlas.first()
self.assertEqual(atlas.currentFilename(), 'output_Basse-Normandie')
self.assertEqual(atlas.filePath('/tmp/output', 'png'), '/tmp/output/output_Basse-Normandie.png')
self.assertEqual(atlas.filePath('/tmp/output', '.png'), '/tmp/output/output_Basse-Normandie.png')
self.assertEqual(atlas.filePath('/tmp/output/', 'png'), '/tmp/output/output_Basse-Normandie.png')
self.assertEqual(atlas.filePath('/tmp/output/', '.png'), '/tmp/output/output_Basse-Normandie.png')
self.assertEqual(atlas.filePath('/tmp/output/', 'svg'), '/tmp/output/output_Basse-Normandie.svg')

atlas.next()
self.assertEqual(atlas.currentFilename(), 'output_Bretagne')
self.assertEqual(atlas.filePath('/tmp/output', 'png'), '/tmp/output/output_Bretagne.png')
self.assertEqual(atlas.filePath('/tmp/output/', 'png'), '/tmp/output/output_Bretagne.png')
atlas.next()
self.assertEqual(atlas.currentFilename(), 'output_Pays de la Loire')
self.assertEqual(atlas.filePath('/tmp/output', 'png'), '/tmp/output/output_Pays de la Loire.png')
self.assertEqual(atlas.filePath('/tmp/output/', 'png'), '/tmp/output/output_Pays de la Loire.png')
atlas.next()
self.assertEqual(atlas.currentFilename(), 'output_Centre')
self.assertEqual(atlas.filePath('/tmp/output', 'png'), '/tmp/output/output_Centre.png')
self.assertEqual(atlas.filePath('/tmp/output/', 'png'), '/tmp/output/output_Centre.png')

# try changing expression, filename should be updated instantly
atlas.setFilenameExpression("'export_' || \"NAME_1\"")
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgslayoutexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def testIteratorToImages(self):
settings = QgsLayoutExporter.ImageExportSettings()
settings.dpi = 80

result, error = QgsLayoutExporter.exportToImage(atlas, self.basetestpath, 'png', settings)
result, error = QgsLayoutExporter.exportToImage(atlas, self.basetestpath + '/', 'png', settings)
self.assertEqual(result, QgsLayoutExporter.Success, error)

page1_path = os.path.join(self.basetestpath, 'test_exportiteratortoimage_Basse-Normandie.png')
Expand All @@ -654,7 +654,7 @@ def testIteratorToSvgs(self):
settings.dpi = 80
settings.forceVectorOutput = False

result, error = QgsLayoutExporter.exportToSvg(atlas, self.basetestpath, settings)
result, error = QgsLayoutExporter.exportToSvg(atlas, self.basetestpath + '/', settings)
self.assertEqual(result, QgsLayoutExporter.Success, error)

page1_path = os.path.join(self.basetestpath, 'test_exportiteratortosvg_Basse-Normandie.svg')
Expand All @@ -681,7 +681,7 @@ def testIteratorToPdfs(self):
settings.rasterizeWholeImage = False
settings.forceVectorOutput = False

result, error = QgsLayoutExporter.exportToPdfs(atlas, self.basetestpath, settings)
result, error = QgsLayoutExporter.exportToPdfs(atlas, self.basetestpath + '/', settings)
self.assertEqual(result, QgsLayoutExporter.Success, error)

page1_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Basse-Normandie.pdf')
Expand Down
9 changes: 9 additions & 0 deletions tests/src/python/test_qgsreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,31 @@ def testIteration(self):
self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0001.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_header)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0002.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_body)
self.assertEqual(r.filePath('/tmp/myreport', '.png'), '/tmp/myreport_0003.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_footer)
self.assertEqual(r.filePath('/tmp/myreport', 'jpg'), '/tmp/myreport_0004.jpg')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2_header)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0005.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2a_header)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0006.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2a_footer)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0007.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2_footer)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0008.png')
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_footer)
self.assertEqual(r.filePath('/tmp/myreport', 'png'), '/tmp/myreport_0009.png')
self.assertFalse(r.next())


Expand Down

0 comments on commit 767075a

Please sign in to comment.