Skip to content
Permalink
Browse files

Hookup filename generation for reports

  • Loading branch information
nyalldawson committed Dec 29, 2017
1 parent 935dfa3 commit 767075a3d466035f452bf8849581f5a704b67809
@@ -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();

@@ -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 )
@@ -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 )
@@ -2406,7 +2408,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
return;
}

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

mView->setPaintingEnabled( false );
@@ -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;
@@ -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;
@@ -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;
@@ -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\"")
@@ -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')
@@ -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')
@@ -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')
@@ -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())


0 comments on commit 767075a

Please sign in to comment.
You can’t perform that action at this time.