Skip to content
Permalink
Browse files

use absolute positioning for cropping

  • Loading branch information
3nids committed Sep 29, 2018
1 parent a4ccc95 commit b0e1cf5fe3acba2179cc5b43bd4b59527246737e
Showing with 41 additions and 23 deletions.
  1. +35 −21 src/app/qgsappscreenshots.cpp
  2. +6 −2 src/app/qgsappscreenshots.h
@@ -46,9 +46,9 @@ QgsAppScreenShots::QgsAppScreenShots( const QString &saveDirectory )
<< mPolygonLayer );
}

QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect crop, bool gradient )
{
QPixmap pix;
QPixmap pixmap;
QRect geom;

QScreen *scr = screen( widget );
@@ -57,7 +57,7 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
widget->raise();
if ( mode == GrabWidget )
{
pix = widget->grab();
pixmap = widget->grab();
}
else if ( mode == GrabWidgetAndFrame )
{
@@ -67,27 +67,17 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
if ( !widget || mode != GrabWidget )
{
WId wid = widget ? widget->winId() : 0;
pix = scr->grabWindow( wid );
pixmap = scr->grabWindow( wid );
if ( !geom.isEmpty() )
{
qreal dpr = scr->devicePixelRatio();
pix = pix.copy( static_cast<int>( geom.x() * dpr ),
static_cast<int>( geom.y() * dpr ),
static_cast<int>( geom.width() * dpr ),
static_cast<int>( geom.height() * dpr ) );
pixmap = pixmap.copy( static_cast<int>( geom.x() * dpr ),
static_cast<int>( geom.y() * dpr ),
static_cast<int>( geom.width() * dpr ),
static_cast<int>( geom.height() * dpr ) );
}
}
return pix;
}

void QgsAppScreenShots::takeScreenshot( const QString &name, QWidget *widget, QgsAppScreenShots::GrabMode mode )
{
QPixmap pixmap = takeScreenshot( widget, mode );
saveScreenshot( pixmap, name );
}

void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, QRect crop, bool gradient )
{
if ( !crop.isNull() )
{
if ( crop.height() == 0 )
@@ -96,13 +86,20 @@ void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, QR
crop.setWidth( pixmap.width() );
}
if ( !crop.isEmpty() )
{
qreal dpr = scr->devicePixelRatio();
crop = QRect( static_cast<int>( crop.x() * dpr ),
static_cast<int>( crop.y() * dpr ),
static_cast<int>( crop.width() * dpr ),
static_cast<int>( crop.height() * dpr ) );
pixmap = pixmap.copy( crop );
}


if ( gradient )
{
QImage img = pixmap.toImage();
QLinearGradient linearGrad( QPointF( 0, pixmap.height() - 200 ), QPointF( 0, pixmap.height() - 20 ) );
QLinearGradient linearGrad( QPointF( 0, pixmap.height() - mGradientSize ), QPointF( 0, pixmap.height() - mGradientSize / 10 ) );
linearGrad.setColorAt( 0, Qt::transparent );
linearGrad.setColorAt( 1, Qt::white );

@@ -113,6 +110,17 @@ void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, QR
pixmap = QPixmap::fromImage( img );
}

return pixmap;
}

void QgsAppScreenShots::takeScreenshot( const QString &name, QWidget *widget, QgsAppScreenShots::GrabMode mode )
{
QPixmap pixmap = takeScreenshot( widget, mode );
saveScreenshot( pixmap, name );
}

void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name )
{
const QString &fileName = mSaveDirectory + "/" + name + ".png";
pixmap.save( fileName );
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
@@ -168,6 +176,11 @@ void QgsAppScreenShots::takePicturesOf( Categories categories )
takeVectorLayerProperties();
}

void QgsAppScreenShots::setGradientSize( int size )
{
mGradientSize = size;
}


// ----------------------
// !!!!! SCREENSHOTS !!!!
@@ -224,8 +237,9 @@ void QgsAppScreenShots::take25dSymbol()
QCoreApplication::processEvents();
dlg->adjustSize();
QCoreApplication::processEvents();
QPixmap pixmap = takeScreenshot( dlg );
saveScreenshot( pixmap, rootName + QLatin1String( "25dsymbol" ), QRect( 0, 0, 0, 800 ), true );
int cropHeight = w->mAdvancedConfigurationBox->mapTo( dlg, w->mAdvancedConfigurationBox->frameGeometry().bottomLeft() ).y();
QPixmap pixmap = takeScreenshot( dlg, GrabWidgetAndFrame, QRect( 0, 0, 0, cropHeight ), true );
saveScreenshot( pixmap, rootName + QLatin1String( "25dsymbol" ) );

// exit properly
dlg->close();
@@ -58,13 +58,16 @@ class QgsAppScreenShots
//! if categories is null, then takes all categories
void takePicturesOf( Categories categories = nullptr );

//! set gradient size
void setGradientSize( int size );

private:
QScreen *screen( QWidget *widget = nullptr );
void moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference = Screen );
//! take and directly save screenshot
void takeScreenshot( const QString &name, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
//! take screenshot and return pixmap
QPixmap takeScreenshot( QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
QPixmap takeScreenshot( QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame, QRect crop = QRect(), bool gradient = false );

/**
* save screenshot from pixmap
@@ -73,12 +76,13 @@ class QgsAppScreenShots
* @param crop the crop can have only one dimension (empty but not null rect)
* @param gradient
*/
void saveScreenshot( QPixmap &pixmap, const QString &name, QRect crop = QRect(), bool gradient = false );
void saveScreenshot( QPixmap &pixmap, const QString &name );

void takeVectorLayerProperties();
void take25dSymbol();

QString mSaveDirectory;
int mGradientSize = 200;
QgsVectorLayer *mLineLayer = nullptr;
QgsVectorLayer *mPolygonLayer = nullptr;
};

0 comments on commit b0e1cf5

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