Add ability to define QImage format for render jobs via map settings

- Defaults to QImage::Format_ARGB32_Premultiplied, as before
- Update labeling unit tests to use defined format
This commit is contained in:
Larry Shaffer 2014-03-31 21:51:21 -06:00
parent 34aeed7450
commit 72c33799d3
8 changed files with 42 additions and 16 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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()
@ -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() ) ) );
@ -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 );

View File

@ -27,6 +27,7 @@ QgsMapSettings::QgsMapSettings()
, mSelectionColor( Qt::yellow )
, mShowSelection( true )
, mFlags( Antialiasing | UseAdvancedEffects | DrawLabeling )
, mImageFormat( QImage::Format_ARGB32_Premultiplied )
{
updateDerived();

View File

@ -2,6 +2,7 @@
#define QGSMAPSETTINGS_H
#include <QColor>
#include <QImage>
#include <QSize>
#include <QStringList>
@ -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;
@ -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

View File

@ -233,6 +233,7 @@ class TestQgsPalLabeling(TestCase):
ms.setCrsTransformEnabled(oms.hasCrsTransformEnabled())
ms.setMapUnits(oms.mapUnits())
ms.setExtent(oms.extent())
ms.setOutputImageFormat(oms.outputImageFormat())
ms.setLayers(oms.layers())
return ms

View File

@ -123,19 +123,22 @@ class TestComposerBase(TestQgsPalLabeling):
# 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 = 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)
image = self._c.printPageAsRaster(0)
""":type: QImage"""
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, ''
@ -169,13 +172,18 @@ class TestComposerBase(TestQgsPalLabeling):
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()

View File

@ -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