Skip to content

Commit b0e1cf5

Browse files
committed
use absolute positioning for cropping
1 parent a4ccc95 commit b0e1cf5

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

src/app/qgsappscreenshots.cpp

+35-21
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ QgsAppScreenShots::QgsAppScreenShots( const QString &saveDirectory )
4646
<< mPolygonLayer );
4747
}
4848

49-
QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
49+
QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect crop, bool gradient )
5050
{
51-
QPixmap pix;
51+
QPixmap pixmap;
5252
QRect geom;
5353

5454
QScreen *scr = screen( widget );
@@ -57,7 +57,7 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
5757
widget->raise();
5858
if ( mode == GrabWidget )
5959
{
60-
pix = widget->grab();
60+
pixmap = widget->grab();
6161
}
6262
else if ( mode == GrabWidgetAndFrame )
6363
{
@@ -67,27 +67,17 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode )
6767
if ( !widget || mode != GrabWidget )
6868
{
6969
WId wid = widget ? widget->winId() : 0;
70-
pix = scr->grabWindow( wid );
70+
pixmap = scr->grabWindow( wid );
7171
if ( !geom.isEmpty() )
7272
{
7373
qreal dpr = scr->devicePixelRatio();
74-
pix = pix.copy( static_cast<int>( geom.x() * dpr ),
75-
static_cast<int>( geom.y() * dpr ),
76-
static_cast<int>( geom.width() * dpr ),
77-
static_cast<int>( geom.height() * dpr ) );
74+
pixmap = pixmap.copy( static_cast<int>( geom.x() * dpr ),
75+
static_cast<int>( geom.y() * dpr ),
76+
static_cast<int>( geom.width() * dpr ),
77+
static_cast<int>( geom.height() * dpr ) );
7878
}
7979
}
80-
return pix;
81-
}
8280

83-
void QgsAppScreenShots::takeScreenshot( const QString &name, QWidget *widget, QgsAppScreenShots::GrabMode mode )
84-
{
85-
QPixmap pixmap = takeScreenshot( widget, mode );
86-
saveScreenshot( pixmap, name );
87-
}
88-
89-
void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, QRect crop, bool gradient )
90-
{
9181
if ( !crop.isNull() )
9282
{
9383
if ( crop.height() == 0 )
@@ -96,13 +86,20 @@ void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, QR
9686
crop.setWidth( pixmap.width() );
9787
}
9888
if ( !crop.isEmpty() )
89+
{
90+
qreal dpr = scr->devicePixelRatio();
91+
crop = QRect( static_cast<int>( crop.x() * dpr ),
92+
static_cast<int>( crop.y() * dpr ),
93+
static_cast<int>( crop.width() * dpr ),
94+
static_cast<int>( crop.height() * dpr ) );
9995
pixmap = pixmap.copy( crop );
96+
}
10097

10198

10299
if ( gradient )
103100
{
104101
QImage img = pixmap.toImage();
105-
QLinearGradient linearGrad( QPointF( 0, pixmap.height() - 200 ), QPointF( 0, pixmap.height() - 20 ) );
102+
QLinearGradient linearGrad( QPointF( 0, pixmap.height() - mGradientSize ), QPointF( 0, pixmap.height() - mGradientSize / 10 ) );
106103
linearGrad.setColorAt( 0, Qt::transparent );
107104
linearGrad.setColorAt( 1, Qt::white );
108105

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

113+
return pixmap;
114+
}
115+
116+
void QgsAppScreenShots::takeScreenshot( const QString &name, QWidget *widget, QgsAppScreenShots::GrabMode mode )
117+
{
118+
QPixmap pixmap = takeScreenshot( widget, mode );
119+
saveScreenshot( pixmap, name );
120+
}
121+
122+
void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name )
123+
{
116124
const QString &fileName = mSaveDirectory + "/" + name + ".png";
117125
pixmap.save( fileName );
118126
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
@@ -168,6 +176,11 @@ void QgsAppScreenShots::takePicturesOf( Categories categories )
168176
takeVectorLayerProperties();
169177
}
170178

179+
void QgsAppScreenShots::setGradientSize( int size )
180+
{
181+
mGradientSize = size;
182+
}
183+
171184

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

230244
// exit properly
231245
dlg->close();

src/app/qgsappscreenshots.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ class QgsAppScreenShots
5858
//! if categories is null, then takes all categories
5959
void takePicturesOf( Categories categories = nullptr );
6060

61+
//! set gradient size
62+
void setGradientSize( int size );
63+
6164
private:
6265
QScreen *screen( QWidget *widget = nullptr );
6366
void moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference = Screen );
6467
//! take and directly save screenshot
6568
void takeScreenshot( const QString &name, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
6669
//! take screenshot and return pixmap
67-
QPixmap takeScreenshot( QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
70+
QPixmap takeScreenshot( QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame, QRect crop = QRect(), bool gradient = false );
6871

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

7881
void takeVectorLayerProperties();
7982
void take25dSymbol();
8083

8184
QString mSaveDirectory;
85+
int mGradientSize = 200;
8286
QgsVectorLayer *mLineLayer = nullptr;
8387
QgsVectorLayer *mPolygonLayer = nullptr;
8488
};

0 commit comments

Comments
 (0)