Add missing wordfile creation to revamped save as image

This commit is contained in:
nirvn 2017-05-01 12:09:55 +07:00
parent fa7a3e7aff
commit a188d4f32a
7 changed files with 65 additions and 0 deletions

View File

@ -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();

View File

@ -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" ) );

View File

@ -143,3 +143,8 @@ bool QgsMapSaveDialog::drawDecorations() const
{
return mDrawDecorations->isChecked();
}
bool QgsMapSaveDialog::saveWorldFile() const
{
return mSaveWorldFile->isChecked();
}

View File

@ -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 );

View File

@ -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" ) )
@ -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;

View File

@ -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:
@ -103,6 +108,7 @@ class CORE_EXPORT QgsMapRendererTask : public QgsTask
QString mFileName;
QString mFileFormat;
bool mSaveWorldFile = false;
QList< QgsAnnotation * > mAnnotations;
QList< QgsMapDecoration * > mDecorations;

View File

@ -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">