Skip to content

Commit

Permalink
use absolute positioning for cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Oct 4, 2018
1 parent a4ccc95 commit b0e1cf5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
56 changes: 35 additions & 21 deletions src/app/qgsappscreenshots.cpp
Expand Up @@ -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 );
Expand All @@ -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 )
{
Expand All @@ -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 )
Expand All @@ -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 );

Expand All @@ -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 ) );
Expand Down Expand Up @@ -168,6 +176,11 @@ void QgsAppScreenShots::takePicturesOf( Categories categories )
takeVectorLayerProperties();
}

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


// ----------------------
// !!!!! SCREENSHOTS !!!!
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions src/app/qgsappscreenshots.h
Expand Up @@ -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
Expand All @@ -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;
};
Expand Down

0 comments on commit b0e1cf5

Please sign in to comment.