Skip to content
Permalink
Browse files

save screenshots to sub-folders

  • Loading branch information
3nids committed Oct 4, 2018
1 parent b0e1cf5 commit 18f656289832dd4b1ef0e774764ca91c66486922
Showing with 47 additions and 15 deletions.
  1. +9 −0 scripts/create_screenshots.sh
  2. +36 −13 src/app/qgsappscreenshots.cpp
  3. +2 −2 src/app/qgsappscreenshots.h
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -e

FILE=$1

QGIS_SRC=~/opt/qgis/QGIS
OUT_PATH=~/Documents/temp/quick
CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
@@ -80,11 +80,13 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect

if ( !crop.isNull() )
{
qreal dpr = scr->devicePixelRatio();
if ( crop.height() == 0 )
crop.setHeight( pixmap.height() );
crop.setHeight( static_cast<int>( pixmap.height() / dpr ) );
if ( crop.width() == 0 )
crop.setWidth( pixmap.width() );
crop.setWidth( static_cast<int>( pixmap.width() / dpr ) );
}

if ( !crop.isEmpty() )
{
qreal dpr = scr->devicePixelRatio();
@@ -95,7 +97,6 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
pixmap = pixmap.copy( crop );
}


if ( gradient )
{
QImage img = pixmap.toImage();
@@ -113,17 +114,35 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
return pixmap;
}

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

void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name )
void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, const QString &folder )
{
const QString &fileName = mSaveDirectory + "/" + name + ".png";
pixmap.save( fileName );
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
const QDir topDirectory( mSaveDirectory );
if ( !topDirectory.exists() )
{
QgsMessageLog::logMessage( QString( "Directory does not exist: %1" ).arg( mSaveDirectory ), QString(), Qgis::Critical );
return;
}

const QDir directory( topDirectory.absolutePath() + "/" + folder );
const QString fileName = directory.absolutePath() + "/" + name + ".png";
if ( !directory.exists() )
{
if ( !topDirectory.mkpath( folder ) )
{
QgsMessageLog::logMessage( QString( "Could not create directory %1 in %2" ).arg( folder, mSaveDirectory ), QString(), Qgis::Critical );
return;
}
}
if ( pixmap.save( fileName ) )
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
else
QgsMessageLog::logMessage( QString( "Failed to save screenshot: %1" ).arg( fileName ), QString(), Qgis::Critical );
}

void QgsAppScreenShots::moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference )
@@ -187,6 +206,7 @@ void QgsAppScreenShots::setGradientSize( int size )

void QgsAppScreenShots::takeVectorLayerProperties()
{
QString folder = QLatin1String( "working_with_vector/img/auto_generated" );
QString rootName = QLatin1String( "vectorlayerproperties_" );
QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties( mLineLayer, QgisApp::instance() );
dlg->show();
@@ -199,7 +219,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
QCoreApplication::processEvents();
QString name = dlg->mOptionsListWidget->item( row )[0].text().toLower();
name.replace( " ", "_" );
takeScreenshot( rootName + name, dlg );
takeScreenshot( rootName + name, folder, dlg );
}
// ------------------
// style menu clicked
@@ -209,7 +229,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
QCoreApplication::processEvents();
dlg->mBtnStyle->click();
QCoreApplication::processEvents();
takeScreenshot( rootName + "style_menu", dlg );
takeScreenshot( rootName + "style_menu", folder, dlg );
QCoreApplication::processEvents();
dlg->mBtnStyle->menu()->hide();
QCoreApplication::processEvents();
@@ -219,8 +239,11 @@ void QgsAppScreenShots::takeVectorLayerProperties()
dlg->deleteLater();
}

//---------------

void QgsAppScreenShots::take25dSymbol()
{
QString folder = QLatin1String( "working_with_vector/img/auto_generated" );
QString rootName = QLatin1String( "vectorlayerproperties_" );
QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties( mPolygonLayer, QgisApp::instance() );
dlg->show();
@@ -239,9 +262,9 @@ void QgsAppScreenShots::take25dSymbol()
QCoreApplication::processEvents();
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" ) );
saveScreenshot( pixmap, rootName + QLatin1String( "25dsymbol" ), folder );

// exit properly
// exit properly
dlg->close();
dlg->deleteLater();
}
@@ -65,7 +65,7 @@ class QgsAppScreenShots
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 );
void takeScreenshot( const QString &name, const QString &folder, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
//! take screenshot and return pixmap
QPixmap takeScreenshot( QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame, QRect crop = QRect(), bool gradient = false );

@@ -76,7 +76,7 @@ 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 );
void saveScreenshot( QPixmap &pixmap, const QString &name, const QString &folder );

void takeVectorLayerProperties();
void take25dSymbol();

0 comments on commit 18f6562

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