Skip to content
Permalink
Browse files

Merge branch 'export_symbols'

  • Loading branch information
NathanW2 committed Jul 7, 2015
2 parents 099faec + 659f0d6 commit 03b5ea4d792f5ca66fffafb0c220c1a02da37215
@@ -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();
@@ -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;
};

@@ -40,7 +40,6 @@
#include "qgsapplication.h"
#include "qgslogger.h"


QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* parent )
: QDialog( parent ), mStyle( style ), mModified( false )
{
@@ -66,10 +65,16 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
connect( btnRemoveItem, SIGNAL( clicked() ), this, SLOT( removeItem() ) );

QMenu *shareMenu = new QMenu( tr( "Share Menu" ), this );
QAction *exportAsPNGAction = shareMenu->addAction( tr( "Export as PNG" ) );
QAction *exportAsSVGAction = shareMenu->addAction( tr( "Export as SVG" ) );
QAction *exportAction = shareMenu->addAction( tr( "Export" ) );
QAction *importAction = shareMenu->addAction( tr( "Import" ) );
exportAsPNGAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
exportAsSVGAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
exportAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
importAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingImport.svg" ) ) );
connect( exportAsPNGAction, SIGNAL( triggered() ), this, SLOT( exportItemsPNG() ) );
connect( exportAsSVGAction, SIGNAL( triggered() ), this, SLOT( exportItemsSVG() ) );
connect( exportAction, SIGNAL( triggered() ), this, SLOT( exportItems() ) );
connect( importAction, SIGNAL( triggered() ), this, SLOT( importItems() ) );
btnShare->setMenu( shareMenu );
@@ -730,6 +735,40 @@ void QgsStyleV2ManagerDialog::itemChanged( QStandardItem* item )
}
}

void QgsStyleV2ManagerDialog::exportItemsPNG()
{
QString dir = QFileDialog::getExistingDirectory( this, tr( "Exported selected symbols as PNG" ),
QDir::home().absolutePath(),
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks );
exportSelectedItemsImages( dir, "png", QSize( 32, 32 ) );
}

void QgsStyleV2ManagerDialog::exportItemsSVG()
{
QString dir = QFileDialog::getExistingDirectory( this, tr( "Exported selected symbols as SVG" ),
QDir::home().absolutePath(),
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks );
exportSelectedItemsImages( dir, "svg", QSize( 32, 32 ) );
}


void QgsStyleV2ManagerDialog::exportSelectedItemsImages( QString dir, QString format, QSize size )
{
if ( dir.isEmpty() )
return;

QModelIndexList indexes = listItems->selectionModel()->selection().indexes();
foreach ( QModelIndex index, indexes )
{
QString name = index.data().toString();
QString path = dir + "/" + name + "." + format;
QgsSymbolV2 *sym = mStyle->symbol( name );
sym->exportImage( path, format, size );
}
}

void QgsStyleV2ManagerDialog::exportItems()
{
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Export );
@@ -41,6 +41,9 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
void addItem();
void editItem();
void removeItem();
void exportItemsSVG();
void exportItemsPNG();
void exportSelectedItemsImages( QString dir, QString format, QSize size );
void exportItems();
void importItems();

0 comments on commit 03b5ea4

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