Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add exportImage API to QgsSymbol
  • Loading branch information
NathanW2 committed Jul 7, 2015
1 parent 217931f commit 659f0d6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
4 changes: 4 additions & 0 deletions python/core/symbology-ng/qgssymbolv2.sip
Expand Up @@ -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();
Expand Down
26 changes: 0 additions & 26 deletions src/core/symbology-ng/qgsstylev2.cpp
Expand Up @@ -30,7 +30,6 @@
#include <QFile>
#include <QTextStream>
#include <QByteArray>
#include <QSvgGenerator>

#include <sqlite3.h>

Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 0 additions & 3 deletions src/core/symbology-ng/qgsstylev2.h
Expand Up @@ -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 );

Expand Down
21 changes: 21 additions & 0 deletions src/core/symbology-ng/qgssymbolv2.cpp
Expand Up @@ -34,6 +34,7 @@
#include <QImage>
#include <QPainter>
#include <QSize>
#include <QSvgGenerator>

#include <cmath>

Expand Down Expand Up @@ -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 );
Expand Down
20 changes: 12 additions & 8 deletions src/core/symbology-ng/qgssymbolv2.h
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down
4 changes: 3 additions & 1 deletion src/gui/symbology-ng/qgsstylev2managerdialog.cpp
Expand Up @@ -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 );
}
}

Expand Down

0 comments on commit 659f0d6

Please sign in to comment.