mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
[FEATURE] Add copy to clipboard function to save as image (#4914)
Sponsored by Andreas Neumann.
This commit is contained in:
parent
cb088a2a0c
commit
778e84ba7b
@ -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 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user