Skip to content
Permalink
Browse files

[FEATURE] Add copy to clipboard function to save as image (#4914)

Sponsored by Andreas Neumann.
  • Loading branch information
nirvn committed Jul 25, 2017
1 parent cb088a2 commit 778e84ba7b1065a430d7d7ee1c0ce7bb62f6c134
@@ -40,7 +40,7 @@ class QgsMapRendererTask : QgsTask
QgsMapRendererTask( const QgsMapSettings &ms,
QPainter *p );
%Docstring
Constructor for QgsMapRendererTask to render a map to a painter object.
Constructor for QgsMapRendererTask to render a map to a QPainter object.
%End

void addAnnotations( QList< QgsAnnotation * > annotations );
@@ -29,6 +29,7 @@
#include "qgsproject.h"
#include "qgssettings.h"

#include <QClipboard>
#include <QCheckBox>
#include <QFileDialog>
#include <QImage>
@@ -110,6 +111,12 @@ QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, QL

this->setWindowTitle( tr( "Save map as PDF" ) );
}
else
{
QPushButton *button = new QPushButton( tr( "Copy to clipboard" ) );
buttonBox->addButton( button, QDialogButtonBox::ResetRole );
connect( button, &QPushButton::clicked, this, &QgsMapSaveDialog::copyToClipboard );
}

connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsMapSaveDialog::accepted );
}
@@ -309,6 +316,64 @@ void QgsMapSaveDialog::lockChanged( const bool locked )
}
}

void QgsMapSaveDialog::copyToClipboard()
{
QgsMapSettings ms = QgsMapSettings();
applyMapSettings( ms );

QPainter *p;
QImage *img;

img = new QImage( ms.outputSize(), QImage::Format_ARGB32 );
if ( img->isNull() )
{
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as image" ), tr( "Could not allocate required memory for image" ) );
return;
}

img->setDotsPerMeterX( 1000 * ms.outputDpi() / 25.4 );
img->setDotsPerMeterY( 1000 * ms.outputDpi() / 25.4 );

p = new QPainter( img );

QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask( ms, p );

if ( drawAnnotations() )
{
mapRendererTask->addAnnotations( mAnnotations );
}

if ( drawDecorations() )
{
mapRendererTask->addDecorations( mDecorations );
}

connect( mapRendererTask, &QgsMapRendererTask::renderingComplete, [ = ]
{
QApplication::clipboard()->setImage( *img, QClipboard::Clipboard );
QApplication::restoreOverrideCursor();
QgisApp::instance()->messageBar()->pushSuccess( tr( "Save as image" ), tr( "Successfully copied map to clipboard" ) );

delete p;
delete img;
setEnabled( true );
} );
connect( mapRendererTask, &QgsMapRendererTask::errorOccurred, [ = ]( int )
{
QApplication::restoreOverrideCursor();
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as PDF" ), tr( "Could not copy the map to clipboard" ) );

delete p;
delete img;
setEnabled( true );
} );

setEnabled( false );

QApplication::setOverrideCursor( Qt::WaitCursor );
QgsApplication::taskManager()->addTask( mapRendererTask );
}

void QgsMapSaveDialog::accepted()
{
if ( mDialogType == Image )
@@ -387,7 +452,7 @@ void QgsMapSaveDialog::accepted()
} );
connect( mapRendererTask, &QgsMapRendererTask::errorOccurred, [ = ]( int )
{
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as PDF" ), tr( "Could not save the map to PDF..." ) );
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as PDF" ), tr( "Could not save the map to PDF" ) );
} );

QgsApplication::taskManager()->addTask( mapRendererTask );
@@ -79,6 +79,7 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog

void lockChanged( const bool locked );
void accepted();
void copyToClipboard();

void updateDpi( int dpi );
void updateOutputWidth( int width );
@@ -59,7 +59,7 @@ class CORE_EXPORT QgsMapRendererTask : public QgsTask
const bool forceRaster = false );

/**
* Constructor for QgsMapRendererTask to render a map to a painter object.
* Constructor for QgsMapRendererTask to render a map to a QPainter object.
*/
QgsMapRendererTask( const QgsMapSettings &ms,
QPainter *p );

0 comments on commit 778e84b

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