Skip to content

Commit

Permalink
[FEATURE] Add seperate controls for printing/exporting whole of atlas…
Browse files Browse the repository at this point in the history
… composition. Allows single features from an atlas to be printed/exported.
  • Loading branch information
nyalldawson committed Jan 1, 2014
1 parent 5cc408c commit 5a91aa7
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 13 deletions.
142 changes: 129 additions & 13 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -315,6 +315,21 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
atlasMenu->addAction( mActionAtlasPrev );
atlasMenu->addAction( mActionAtlasNext );
atlasMenu->addAction( mActionAtlasLast );
atlasMenu->addSeparator();
atlasMenu->addAction( mActionPrintAtlas );
atlasMenu->addAction( mActionExportAtlasAsImage );
atlasMenu->addAction( mActionExportAtlasAsSVG );
atlasMenu->addAction( mActionExportAtlasAsPDF );

QToolButton* atlasExportToolButton = new QToolButton( mAtlasToolbar );
atlasExportToolButton->setPopupMode( QToolButton::InstantPopup );
atlasExportToolButton->setAutoRaise( true );
atlasExportToolButton->setToolButtonStyle( Qt::ToolButtonIconOnly );
atlasExportToolButton->addAction( mActionExportAtlasAsImage );
atlasExportToolButton->addAction( mActionExportAtlasAsSVG );
atlasExportToolButton->addAction( mActionExportAtlasAsPDF );
atlasExportToolButton->setDefaultAction( mActionExportAtlasAsImage );
mAtlasToolbar->addWidget( atlasExportToolButton );

QMenu *settingsMenu = menuBar()->addMenu( tr( "Settings" ) );
settingsMenu->addAction( mActionOptions );
Expand Down Expand Up @@ -486,6 +501,10 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
mActionAtlasLast->setEnabled( false );
mActionAtlasNext->setEnabled( false );
mActionAtlasPrev->setEnabled( false );
mActionPrintAtlas->setEnabled( false );
mActionExportAtlasAsImage->setEnabled( false );
mActionExportAtlasAsSVG->setEnabled( false );
mActionExportAtlasAsPDF->setEnabled( false );

// Create size grip (needed by Mac OS X for QMainWindow if QStatusBar is not visible)
//should not be needed now that composer has a status bar?
Expand Down Expand Up @@ -787,6 +806,10 @@ void QgsComposer::toggleAtlasControls( bool atlasEnabled )
//preview defaults to unchecked
mActionAtlasPreview->setChecked( false );
mActionAtlasPreview->setEnabled( atlasEnabled );
mActionPrintAtlas->setEnabled( atlasEnabled );
mActionExportAtlasAsImage->setEnabled( atlasEnabled );
mActionExportAtlasAsSVG->setEnabled( atlasEnabled );
mActionExportAtlasAsPDF->setEnabled( atlasEnabled );
}

void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
Expand Down Expand Up @@ -1036,7 +1059,24 @@ void QgsComposer::toggleRulers( bool checked )
myQSettings.setValue( "/Composer/showRulers", checked );
}

void QgsComposer::on_mActionExportAtlasAsPDF_triggered()
{
exportCompositionAsPDF( QgsComposer::Atlas );

if ( mComposition->atlasPreviewEnabled() )
{
//after atlas output, jump back to preview first feature
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
atlasMap->firstFeature();
}
}

void QgsComposer::on_mActionExportAsPDF_triggered()
{
exportCompositionAsPDF( QgsComposer::Single );
}

void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
{
if ( !mComposition || !mView )
{
Expand Down Expand Up @@ -1072,16 +1112,25 @@ void QgsComposer::on_mActionExportAsPDF_triggered()
QString outputFileName;
QString outputDir;

if ( !hasAnAtlas || atlasOnASingleFile )
if ( mode == QgsComposer::Single || ( mode == QgsComposer::Atlas && atlasOnASingleFile ) )
{
QSettings myQSettings; // where we keep last used filter in persistent state
QString lastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
QFileInfo file( lastUsedFile );

if ( hasAnAtlas && !atlasOnASingleFile && ( mode == QgsComposer::Atlas || mComposition->atlasPreviewEnabled() ) )
{
outputFileName = QDir( file.path() ).filePath( atlasMap->currentFilename() ) + ".pdf";
}
else
{
outputFileName = file.path();
}

outputFileName = QFileDialog::getSaveFileName(
this,
tr( "Choose a file name to save the map as" ),
file.path(),
outputFileName,
tr( "PDF Format" ) + " (*.pdf *.PDF)" );
if ( outputFileName.isEmpty() )
{
Expand Down Expand Up @@ -1136,7 +1185,7 @@ void QgsComposer::on_mActionExportAsPDF_triggered()

mView->setPaintingEnabled( false );

if ( hasAnAtlas )
if ( mode == QgsComposer::Atlas )
{
QPrinter printer;

Expand Down Expand Up @@ -1233,6 +1282,18 @@ void QgsComposer::on_mActionExportAsPDF_triggered()
}

void QgsComposer::on_mActionPrint_triggered()
{
//print only current feature
printComposition( QgsComposer::Single );
}

void QgsComposer::on_mActionPrintAtlas_triggered()
{
//print whole atlas
printComposition( QgsComposer::Atlas );
}

void QgsComposer::printComposition( QgsComposer::OutputMode mode )
{
if ( !mComposition || !mView )
{
Expand Down Expand Up @@ -1272,7 +1333,7 @@ void QgsComposer::on_mActionPrint_triggered()
mView->setPaintingEnabled( false );

QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
if ( !atlasMap->enabled() )
if ( mode == QgsComposer::Single )
{
mComposition->print( mPrinter );
}
Expand Down Expand Up @@ -1341,7 +1402,24 @@ void QgsComposer::on_mActionPrint_triggered()
QApplication::restoreOverrideCursor();
}

void QgsComposer::on_mActionExportAtlasAsImage_triggered()
{
exportCompositionAsImage( QgsComposer::Atlas );

if ( mComposition->atlasPreviewEnabled() )
{
//after atlas output, jump back to preview first feature
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
atlasMap->firstFeature();
}
}

void QgsComposer::on_mActionExportAsImage_triggered()
{
exportCompositionAsImage( QgsComposer::Single );
}

void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
{
if ( !mComposition || !mView )
{
Expand All @@ -1353,6 +1431,8 @@ void QgsComposer::on_mActionExportAsImage_triggered()
showWMSPrintingWarning();
}

QSettings settings;

// Image size
int width = ( int )( mComposition->printResolution() * mComposition->paperWidth() / 25.4 );
int height = ( int )( mComposition-> printResolution() * mComposition->paperHeight() / 25.4 );
Expand All @@ -1375,9 +1455,17 @@ void QgsComposer::on_mActionExportAsImage_triggered()
}

QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
if ( !atlasMap->enabled() )
if ( mode == QgsComposer::Single )
{
QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
QString outputFileName = QString::null;

if ( atlasMap->enabled() && mComposition->atlasPreviewEnabled() )
{
QString lastUsedDir = settings.value( "/UI/lastSaveAsImageDir", "." ).toString();
outputFileName = QDir( lastUsedDir ).filePath( atlasMap->currentFilename() );
}

QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ), outputFileName );

if ( fileNExt.first.isEmpty() )
{
Expand Down Expand Up @@ -1591,8 +1679,24 @@ void QgsComposer::on_mActionExportAsImage_triggered()
}
}

void QgsComposer::on_mActionExportAtlasAsSVG_triggered()
{
exportCompositionAsSVG( QgsComposer::Atlas );

if ( mComposition->atlasPreviewEnabled() )
{
//after atlas output, jump back to preview first feature
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
atlasMap->firstFeature();
}
}

void QgsComposer::on_mActionExportAsSVG_triggered()
{
exportCompositionAsSVG( QgsComposer::Single );
}

void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
{
if ( containsWMSLayer() )
{
Expand Down Expand Up @@ -1626,20 +1730,28 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
}

QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
bool hasAnAtlas = atlasMap->enabled();

QString outputFileName;
QString outputDir;

if ( !hasAnAtlas )
if ( mode == QgsComposer::Single )
{
QString lastUsedFile = settings.value( "/UI/lastSaveAsSvgFile", "qgis.svg" ).toString();
QFileInfo file( lastUsedFile );

if ( atlasMap->enabled() )
{
outputFileName = QDir( file.path() ).filePath( atlasMap->currentFilename() ) + ".svg";
}
else
{
outputFileName = file.path();
}

outputFileName = QFileDialog::getSaveFileName(
this,
tr( "Choose a file name to save the map as" ),
file.path(),
outputFileName,
tr( "SVG Format" ) + " (*.svg *.SVG)" );
if ( outputFileName.isEmpty() )
return;
Expand Down Expand Up @@ -1694,7 +1806,7 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
mView->setPaintingEnabled( false );

int featureI = 0;
if ( hasAnAtlas )
if ( mode == QgsComposer::Atlas )
{
try
{
Expand All @@ -1714,7 +1826,7 @@ void QgsComposer::on_mActionExportAsSVG_triggered()

do
{
if ( hasAnAtlas )
if ( mode == QgsComposer::Atlas )
{
if ( atlasMap->numFeatures() == 0 )
break;
Expand Down Expand Up @@ -1772,9 +1884,9 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
}
featureI++;
}
while ( hasAnAtlas && featureI < atlasMap->numFeatures() );
while ( mode == QgsComposer::Atlas && featureI < atlasMap->numFeatures() );

if ( hasAnAtlas )
if ( mode == QgsComposer::Atlas )
atlasMap->endRender();

mView->setPaintingEnabled( true );
Expand Down Expand Up @@ -2497,6 +2609,10 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
mActionAtlasLast->setEnabled( false );
mActionAtlasNext->setEnabled( false );
mActionAtlasPrev->setEnabled( false );
mActionPrintAtlas->setEnabled( atlasMap->enabled() );
mActionExportAtlasAsImage->setEnabled( atlasMap->enabled() );
mActionExportAtlasAsSVG->setEnabled( atlasMap->enabled() );
mActionExportAtlasAsPDF->setEnabled( atlasMap->enabled() );
connect( atlasMap, SIGNAL( toggled( bool ) ), this, SLOT( toggleAtlasControls( bool ) ) );

setSelectionTool();
Expand Down
31 changes: 31 additions & 0 deletions src/app/composer/qgscomposer.h
Expand Up @@ -56,6 +56,13 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
Q_OBJECT

public:

enum OutputMode
{
Single = 0,
Atlas
};

QgsComposer( QgisApp *qgis, const QString& title );
~QgsComposer();

Expand Down Expand Up @@ -330,6 +337,18 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//!Last atlas feature
void on_mActionAtlasLast_triggered();

//! Print the atlas
void on_mActionPrintAtlas_triggered();

//! Print atlas as image
void on_mActionExportAtlasAsImage_triggered();

//! Print atlas as SVG
void on_mActionExportAtlasAsSVG_triggered();

//! Print atlas as PDF
void on_mActionExportAtlasAsPDF_triggered();

//! Save window state
void saveWindowState();

Expand Down Expand Up @@ -438,6 +457,18 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Updates the grid/guide action status based on compositions grid/guide settings
void restoreGridSettings();

//! Prints either the whole atlas or just the current feature, depending on mode
void printComposition( QgsComposer::OutputMode mode );

//! Exports either either the whole atlas or just the current feature as an image, depending on mode
void exportCompositionAsImage( QgsComposer::OutputMode mode );

//! Exports either either the whole atlas or just the current feature as an SVG, depending on mode
void exportCompositionAsSVG( QgsComposer::OutputMode mode );

//! Exports either either the whole atlas or just the current feature as a PDF, depending on mode
void exportCompositionAsPDF( QgsComposer::OutputMode mode );

/**Composer title*/
QString mTitle;

Expand Down
37 changes: 37 additions & 0 deletions src/ui/qgscomposerbase.ui
Expand Up @@ -144,6 +144,7 @@
<addaction name="mActionAtlasPrev"/>
<addaction name="mActionAtlasNext"/>
<addaction name="mActionAtlasLast"/>
<addaction name="mActionPrintAtlas"/>
</widget>
<action name="mActionPrint">
<property name="checkable">
Expand Down Expand Up @@ -890,6 +891,42 @@
<string>Preview Atlas</string>
</property>
</action>
<action name="mActionPrintAtlas">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFilePrint.png</normaloff>:/images/themes/default/mActionFilePrint.png</iconset>
</property>
<property name="text">
<string>Print Atlas...</string>
</property>
</action>
<action name="mActionExportAtlasAsImage">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSaveMapAsImage.png</normaloff>:/images/themes/default/mActionSaveMapAsImage.png</iconset>
</property>
<property name="text">
<string>Export Atlas as Images...</string>
</property>
</action>
<action name="mActionExportAtlasAsSVG">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSaveAsSVG.png</normaloff>:/images/themes/default/mActionSaveAsSVG.png</iconset>
</property>
<property name="text">
<string>Export Atlas as SVG...</string>
</property>
</action>
<action name="mActionExportAtlasAsPDF">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSaveAsPDF.png</normaloff>:/images/themes/default/mActionSaveAsPDF.png</iconset>
</property>
<property name="text">
<string>Export Atlas As PDF...</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down

0 comments on commit 5a91aa7

Please sign in to comment.