Skip to content
Permalink
Browse files

Add exportImage API to QgsSymbol

  • Loading branch information
NathanW2 committed Jul 7, 2015
1 parent 217931f commit 659f0d661cf8eea52b23ca6b018ed04cd8848adc
@@ -106,6 +106,10 @@ class QgsSymbolV2
//! @note customContext parameter added in 2.6
void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = 0 );

//! export symbol as image format. PNG and SVG supported
void exportImage( QString path, QString format, QSize size );

//! Generate symbol as image
QImage asImage( QSize size, QgsRenderContext* customContext = 0 );

QImage bigSymbolPreviewImage();
@@ -30,7 +30,6 @@
#include <QFile>
#include <QTextStream>
#include <QByteArray>
#include <QSvgGenerator>

#include <sqlite3.h>

@@ -136,31 +135,6 @@ bool QgsStyleV2::saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QSt
return true;
}

void QgsStyleV2::exportSymbol( QString folder, QString name, QString format, QSize size )
{
QString fullname = folder + "/" + name + "." + format;
QgsSymbolV2 *sym = symbol( name );
if ( format.toLower() == "svg" )
{
QSvgGenerator generator;
generator.setFileName( fullname );
generator.setSize( size );
generator.setViewBox( QRect( 0, 0, size.height(), size.height() ) );
generator.setTitle( name );
generator.setDescription( tr( "Exported symbol from QGIS" ) );


QPainter painter( &generator );
sym->drawPreviewIcon( &painter, size );
painter.end();
}
else
{
QImage image = sym->asImage( size );
image.save( fullname );
}
}

bool QgsStyleV2::removeSymbol( QString name )
{
QgsSymbolV2 *symbol = mSymbols.take( name );
@@ -167,9 +167,6 @@ class CORE_EXPORT QgsStyleV2 : public QObject
*/
bool detagSymbol( StyleEntity type, QString symbol, QStringList tags );

//! export symbol as image
void exportSymbol( QString path, QString name, QString format, QSize size );

//! remove symbol from style (and delete it)
bool removeSymbol( QString name );

@@ -34,6 +34,7 @@
#include <QImage>
#include <QPainter>
#include <QSize>
#include <QSvgGenerator>

#include <cmath>

@@ -352,6 +353,26 @@ void QgsSymbolV2::drawPreviewIcon( QPainter* painter, QSize size, QgsRenderConte
}
}

void QgsSymbolV2::exportImage( QString path, QString format, QSize size )
{
if ( format.toLower() == "svg" )
{
QSvgGenerator generator;
generator.setFileName( path );
generator.setSize( size );
generator.setViewBox( QRect( 0, 0, size.height(), size.height() ) );

QPainter painter( &generator );
drawPreviewIcon( &painter, size );
painter.end();
}
else
{
QImage image = asImage( size );
image.save( path );
}
}

QImage QgsSymbolV2::asImage( QSize size, QgsRenderContext* customContext )
{
QImage image( size, QImage::Format_ARGB32_Premultiplied );
@@ -87,15 +87,15 @@ class CORE_EXPORT QgsSymbolV2

// symbol layers handling

/**Returns list of symbol layers contained in the symbol.
/** Returns list of symbol layers contained in the symbol.
* @returns symbol layers list
* @note added in QGIS 2.7
* @see symbolLayer
* @see symbolLayerCount
*/
QgsSymbolLayerV2List symbolLayers() { return mLayers; }

/**Returns a specific symbol layers contained in the symbol.
/** Returns a specific symbol layers contained in the symbol.
* @param layer layer number
* @returns corresponding symbol layer
* @note added in QGIS 2.7
@@ -104,7 +104,7 @@ class CORE_EXPORT QgsSymbolV2
*/
QgsSymbolLayerV2* symbolLayer( int layer );

/**Returns total number of symbol layers contained in the symbol.
/** Returns total number of symbol layers contained in the symbol.
* @returns count of symbol layers
* @note added in QGIS 2.7
* @see symbolLayers
@@ -138,6 +138,10 @@ class CORE_EXPORT QgsSymbolV2
//! @note customContext parameter added in 2.6
void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = 0 );

//! export symbol as image format. PNG and SVG supported
void exportImage( QString path, QString format, QSize size );

//! Generate symbol as image
QImage asImage( QSize size, QgsRenderContext* customContext = 0 );

QImage bigSymbolPreviewImage();
@@ -162,7 +166,7 @@ class CORE_EXPORT QgsSymbolV2
void setRenderHints( int hints ) { mRenderHints = hints; }
int renderHints() const { return mRenderHints; }

/**Sets whether features drawn by the symbol should be clipped to the render context's
/** Sets whether features drawn by the symbol should be clipped to the render context's
* extent. If this option is enabled then features which are partially outside the extent
* will be clipped. This speeds up rendering of the feature, but may have undesirable
* side effects for certain symbol types.
@@ -172,7 +176,7 @@ class CORE_EXPORT QgsSymbolV2
*/
void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }

/**Returns whether features drawn by the symbol will be clipped to the render context's
/** Returns whether features drawn by the symbol will be clipped to the render context's
* extent. If this option is enabled then features which are partially outside the extent
* will be clipped. This speeds up rendering of the feature, but may have undesirable
* side effects for certain symbol types.
@@ -200,7 +204,7 @@ class CORE_EXPORT QgsSymbolV2
SymbolType mType;
QgsSymbolLayerV2List mLayers;

/**Symbol opacity (in the range 0 - 1)*/
/** Symbol opacity (in the range 0 - 1)*/
qreal mAlpha;

int mRenderHints;
@@ -397,9 +401,9 @@ class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
private:

void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
/**Calculates the bounds of a polygon including rings*/
/** Calculates the bounds of a polygon including rings*/
QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
/**Translates the rings in a polygon by a set distance*/
/** Translates the rings in a polygon by a set distance*/
QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
};

@@ -763,7 +763,9 @@ void QgsStyleV2ManagerDialog::exportSelectedItemsImages( QString dir, QString fo
foreach ( QModelIndex index, indexes )
{
QString name = index.data().toString();
mStyle->exportSymbol( dir, name, format, size );
QString path = dir + "/" + name + "." + format;
QgsSymbolV2 *sym = mStyle->symbol( name );
sym->exportImage( path, format, size );
}
}

0 comments on commit 659f0d6

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