Skip to content
Permalink
Browse files

cropping and gradient capabilities

  • Loading branch information
3nids committed Sep 29, 2018
1 parent 2f93ef1 commit a4ccc954123cd5e41b90db02a099f4894c933465
Showing with 57 additions and 8 deletions.
  1. +43 −7 src/app/qgsappscreenshots.cpp
  2. +14 −1 src/app/qgsappscreenshots.h
@@ -19,6 +19,7 @@
#include <QScreen>
#include <QImageWriter>
#include <QIcon>
#include <QImage>

#include "qgsappscreenshots.h"

@@ -45,7 +46,7 @@ QgsAppScreenShots::QgsAppScreenShots( const QString &saveDirectory )
<< mPolygonLayer );
}

void QgsAppScreenShots::saveScreenshot( const QString &name, QWidget *widget, GrabMode mode )
QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
{
QPixmap pix;
QRect geom;
@@ -76,11 +77,45 @@ void QgsAppScreenShots::saveScreenshot( const QString &name, QWidget *widget, Gr
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 )
crop.setHeight( pixmap.height() );
if ( crop.width() == 0 )
crop.setWidth( pixmap.width() );
}
if ( !crop.isEmpty() )
pixmap = pixmap.copy( crop );


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

// create image and fill it with gradient
QImage image( pixmap.width(), pixmap.height(), QImage::Format_ARGB32 );
QPainter painter( &img );
painter.fillRect( img.rect(), linearGrad );
pixmap = QPixmap::fromImage( img );
}

const QString &fileName = mSaveDirectory + "/" + name + ".png";
pix.save( fileName );
QMetaEnum metaEnum = QMetaEnum::fromType<GrabMode>();
QgsMessageLog::logMessage( QString( "Screenshot saved: %1 (%2)" ).arg( fileName, metaEnum.key( mode ) ) );
pixmap.save( fileName );
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
}

void QgsAppScreenShots::moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference )
@@ -151,7 +186,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
QCoreApplication::processEvents();
QString name = dlg->mOptionsListWidget->item( row )[0].text().toLower();
name.replace( " ", "_" );
saveScreenshot( rootName + name, dlg );
takeScreenshot( rootName + name, dlg );
}
// ------------------
// style menu clicked
@@ -161,7 +196,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
QCoreApplication::processEvents();
dlg->mBtnStyle->click();
QCoreApplication::processEvents();
saveScreenshot( rootName + "style_menu", dlg );
takeScreenshot( rootName + "style_menu", dlg );
QCoreApplication::processEvents();
dlg->mBtnStyle->menu()->hide();
QCoreApplication::processEvents();
@@ -189,7 +224,8 @@ void QgsAppScreenShots::take25dSymbol()
QCoreApplication::processEvents();
dlg->adjustSize();
QCoreApplication::processEvents();
saveScreenshot( rootName + QLatin1String( "25dsymbol" ), dlg );
QPixmap pixmap = takeScreenshot( dlg );
saveScreenshot( pixmap, rootName + QLatin1String( "25dsymbol" ), QRect( 0, 0, 0, 800 ), true );

// exit properly
dlg->close();
@@ -18,6 +18,7 @@
#define QGSAPPSCREENSHOTS_H

#include <QObject>
#include <QRect>

class QScreen;
class QgsVectorLayer;
@@ -60,7 +61,19 @@ class QgsAppScreenShots
private:
QScreen *screen( QWidget *widget = nullptr );
void moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference = Screen );
void saveScreenshot( const QString &name, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
//! 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 );

/**
* save screenshot from pixmap
* @param pixmap
* @param name
* @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 takeVectorLayerProperties();
void take25dSymbol();

0 comments on commit a4ccc95

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