Skip to content

Commit

Permalink
Add ability to define QImage format for render jobs via map settings
Browse files Browse the repository at this point in the history
- Defaults to QImage::Format_ARGB32_Premultiplied, as before
- Update labeling unit tests to use defined format
  • Loading branch information
dakcarto committed Jun 13, 2014
1 parent 34aeed7 commit 72c3379
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsmapsettings.sip
Expand Up @@ -72,6 +72,11 @@ class QgsMapSettings
Flags flags() const;
bool testFlag( Flag flag ) const;

//! sets format of internal QImage
void setOutputImageFormat( QImage::Format format );
//! format of internal QImage, default QImage::Format_ARGB32_Premultiplied
QImage::Format outputImageFormat() const;

bool hasValidSettings() const;
QgsRectangle visibleExtent() const;
double mapUnitsPerPixel() const;
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -186,6 +186,7 @@ void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const
jobMapSettings.setMapUnits( ms.mapUnits() );
jobMapSettings.setBackgroundColor( Qt::transparent );
jobMapSettings.setShowSelection( false );
jobMapSettings.setOutputImageFormat( ms.outputImageFormat() );

//set layers to render
QStringList theLayerSet = layersToRender();
Expand Down
7 changes: 4 additions & 3 deletions src/core/qgsmaprendererjob.cpp
Expand Up @@ -50,7 +50,7 @@ QgsMapRendererSequentialJob::QgsMapRendererSequentialJob( const QgsMapSettings&
{
QgsDebugMsg( "SEQUENTIAL construct" );

mImage = QImage( mSettings.outputSize(), QImage::Format_ARGB32_Premultiplied );
mImage = QImage( mSettings.outputSize(), mSettings.outputImageFormat() );
}

QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob()
Expand Down Expand Up @@ -639,7 +639,8 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter* painter, QgsPalLabelin
// Flattened image for drawing when a blending mode is set
QImage * mypFlattenedImage = 0;
mypFlattenedImage = new QImage( mSettings.outputSize().width(),
mSettings.outputSize().height(), QImage::Format_ARGB32_Premultiplied );
mSettings.outputSize().height(),
mSettings.outputImageFormat() );
if ( mypFlattenedImage->isNull() )
{
mErrors.append( Error( layerId, "Insufficient memory for image " + QString::number( mSettings.outputSize().width() ) + "x" + QString::number( mSettings.outputSize().height() ) ) );
Expand Down Expand Up @@ -915,7 +916,7 @@ void QgsMapRendererParallelJob::renderLabelsStatic( QgsMapRendererParallelJob* s

QImage QgsMapRendererJob::composeImage( const QgsMapSettings& settings, const LayerRenderJobs& jobs )
{
QImage image( settings.outputSize(), QImage::Format_ARGB32_Premultiplied );
QImage image( settings.outputSize(), settings.outputImageFormat() );
image.fill( settings.backgroundColor().rgb() );

QPainter painter( &image );
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmapsettings.cpp
Expand Up @@ -27,6 +27,7 @@ QgsMapSettings::QgsMapSettings()
, mSelectionColor( Qt::yellow )
, mShowSelection( true )
, mFlags( Antialiasing | UseAdvancedEffects | DrawLabeling )
, mImageFormat( QImage::Format_ARGB32_Premultiplied )
{
updateDerived();

Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsmapsettings.h
Expand Up @@ -2,6 +2,7 @@
#define QGSMAPSETTINGS_H

#include <QColor>
#include <QImage>
#include <QSize>
#include <QStringList>

Expand Down Expand Up @@ -88,6 +89,11 @@ class CORE_EXPORT QgsMapSettings
Flags flags() const;
bool testFlag( Flag flag ) const;

//! sets format of internal QImage
void setOutputImageFormat( QImage::Format format ) { mImageFormat = format; }
//! format of internal QImage, default QImage::Format_ARGB32_Premultiplied
QImage::Format outputImageFormat() const { return mImageFormat; }

bool hasValidSettings() const;
QgsRectangle visibleExtent() const;
double mapUnitsPerPixel() const;
Expand Down Expand Up @@ -178,6 +184,8 @@ class CORE_EXPORT QgsMapSettings

Flags mFlags;

QImage::Format mImageFormat;

// derived properties
bool mValid; //!< whether the actual settings are valid (set in updateDerived())
QgsRectangle mVisibleExtent; //!< extent with some additional white space that matches the output aspect ratio
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -233,6 +233,7 @@ def cloneMapSettings(self, oms):
ms.setCrsTransformEnabled(oms.hasCrsTransformEnabled())
ms.setMapUnits(oms.mapUnits())
ms.setExtent(oms.extent())
ms.setOutputImageFormat(oms.outputImageFormat())

ms.setLayers(oms.layers())
return ms
Expand Down
36 changes: 22 additions & 14 deletions tests/src/python/test_qgspallabeling_composer.py
Expand Up @@ -123,19 +123,22 @@ def _set_up_composition(self, width, height, dpi):

# noinspection PyUnusedLocal
def _get_composer_image(self, width, height, dpi):
# image = QImage(QSize(width, height), QImage.Format_ARGB32)
# image.fill(QColor(152, 219, 249).rgb())
# image.setDotsPerMeterX(dpi / 25.4 * 1000)
# image.setDotsPerMeterY(dpi / 25.4 * 1000)
#
# p = QPainter(image)
# p.setRenderHint(QPainter.Antialiasing, False)
# p.setRenderHint(QPainter.HighQualityAntialiasing, False)
# self._c.renderPage(p, 0)
# p.end()

image = self._c.printPageAsRaster(0)
""":type: QImage"""
image = QImage(QSize(width, height),
self._TestMapSettings.outputImageFormat())
image.fill(QColor(152, 219, 249).rgb())
image.setDotsPerMeterX(dpi / 25.4 * 1000)
image.setDotsPerMeterY(dpi / 25.4 * 1000)

p = QPainter(image)
p.setRenderHint(
QPainter.Antialiasing,
self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)
)
self._c.renderPage(p, 0)
p.end()

# image = self._c.printPageAsRaster(0)
# """:type: QImage"""

if image.isNull():
return False, ''
Expand Down Expand Up @@ -169,13 +172,18 @@ def _get_composer_svg_image(self, width, height, dpi):
if temp_size == os.path.getsize(svgpath):
return False, ''

image = QImage(width, height, QImage.Format_ARGB32)
image = QImage(width, height, self._TestMapSettings.outputImageFormat())
image.fill(QColor(152, 219, 249).rgb())
image.setDotsPerMeterX(dpi / 25.4 * 1000)
image.setDotsPerMeterY(dpi / 25.4 * 1000)

svgr = QSvgRenderer(svgpath)
p = QPainter(image)
p.setRenderHint(
QPainter.Antialiasing,
self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)
)
p.setRenderHint(QPainter.TextAntialiasing)
svgr.render(p)
p.end()

Expand Down
1 change: 1 addition & 0 deletions tests/src/python/utilities.py
Expand Up @@ -302,6 +302,7 @@ def mapSettingsString(ms):
ms.testFlag(QgsMapSettings.DrawLabeling))
s += ' flag.DrawEditingInfo: {0}\n'.format(
ms.testFlag(QgsMapSettings.DrawEditingInfo))
s += ' outputImageFormat(): {0}\n'.format(ms.outputImageFormat())
return s


Expand Down

0 comments on commit 72c3379

Please sign in to comment.