Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add missing wordfile creation to revamped save as image
  • Loading branch information
nirvn committed May 2, 2017
1 parent fa7a3e7 commit a188d4f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsmaprenderertask.sip
Expand Up @@ -52,6 +52,11 @@ class QgsMapRendererTask : QgsTask
Adds ``decorations`` to be rendered on the map.
%End

void setSaveWorldFile( bool save );
%Docstring
Sets whether a world file will be created alongside an image file.
%End

virtual void cancel();


Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -5833,6 +5833,8 @@ void QgisApp::saveMapAsImage()
mapRendererTask->addDecorations( decorations );
}

mapRendererTask->setSaveWorldFile( dlg.saveWorldFile() );

connect( mapRendererTask, &QgsMapRendererTask::renderingComplete, this, [ = ]
{
messageBar()->pushSuccess( tr( "Save as image" ), tr( "Successfully saved map to image" ) );
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsmapsavedialog.cpp
Expand Up @@ -143,3 +143,8 @@ bool QgsMapSaveDialog::drawDecorations() const
{
return mDrawDecorations->isChecked();
}

bool QgsMapSaveDialog::saveWorldFile() const
{
return mSaveWorldFile->isChecked();
}
3 changes: 3 additions & 0 deletions src/app/qgsmapsavedialog.h
Expand Up @@ -56,6 +56,9 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog
//! returns whether the draw decorations element is checked
bool drawDecorations() const;

//! returns whether a world file will be created
bool saveWorldFile() const;

private:

void updateDpi( int dpi );
Expand Down
34 changes: 34 additions & 0 deletions src/core/qgsmaprenderertask.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgsannotationmanager.h"
#include "qgsmaprenderertask.h"

#include <QFile>
#include <QTextStream>

QgsMapRendererTask::QgsMapRendererTask( const QgsMapSettings &ms, const QString &fileName, const QString &fileFormat )
: QgsTask( tr( "Saving as image" ) )
Expand Down Expand Up @@ -154,6 +156,38 @@ bool QgsMapRendererTask::run()
mError = ImageSaveFail;
return false;
}

if ( mSaveWorldFile )
{
QString content;
// note: use 17 places of precision for all numbers output
//Pixel XDim
content += qgsDoubleToString( mMapSettings.mapUnitsPerPixel() ) + "\r\n";
//Rotation on y axis - hard coded
content += QLatin1String( "0 \r\n" );
//Rotation on x axis - hard coded
content += QLatin1String( "0 \r\n" );
//Pixel YDim - almost always negative - see
//http://en.wikipedia.org/wiki/World_file#cite_note-2
content += '-' + qgsDoubleToString( mMapSettings.mapUnitsPerPixel() ) + "\r\n";
//Origin X (center of top left cell)
content += qgsDoubleToString( mMapSettings.visibleExtent().xMinimum() + ( mMapSettings.mapUnitsPerPixel() / 2 ) ) + "\r\n";
//Origin Y (center of top left cell)
content += qgsDoubleToString( mMapSettings.visibleExtent().yMaximum() - ( mMapSettings.mapUnitsPerPixel() / 2 ) ) + "\r\n";

QFileInfo info = QFileInfo( mFileName );
// build the world file name
QString outputSuffix = info.suffix();
QString worldFileName = info.absolutePath() + '/' + info.baseName() + '.'
+ outputSuffix.at( 0 ) + outputSuffix.at( info.suffix().size() - 1 ) + 'w';
QFile worldFile( worldFileName );

if ( worldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) //don't use QIODevice::Text
{
QTextStream stream( &worldFile );
stream << content;
}
}
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsmaprenderertask.h
Expand Up @@ -73,6 +73,11 @@ class CORE_EXPORT QgsMapRendererTask : public QgsTask
*/
void addDecorations( QList< QgsMapDecoration * > decorations );

/**
* Sets whether a world file will be created alongside an image file.
*/
void setSaveWorldFile( bool save ) { mSaveWorldFile = save; }

void cancel() override;

signals:
Expand Down Expand Up @@ -103,6 +108,7 @@ class CORE_EXPORT QgsMapRendererTask : public QgsTask

QString mFileName;
QString mFileFormat;
bool mSaveWorldFile = false;

QList< QgsAnnotation * > mAnnotations;
QList< QgsMapDecoration * > mDecorations;
Expand Down
10 changes: 10 additions & 0 deletions src/ui/qgsmapsavedialog.ui
Expand Up @@ -16,6 +16,16 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="mSaveWorldFile">
<property name="text">
<string>Save world file</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">>
<widget class="QCheckBox" name="mDrawAnnotations">
<property name="text">
Expand Down

0 comments on commit a188d4f

Please sign in to comment.