Skip to content

Commit 03b5ea4

Browse files
committedJul 7, 2015
Merge branch 'export_symbols'
2 parents 099faec + 659f0d6 commit 03b5ea4

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed
 

‎python/core/symbology-ng/qgssymbolv2.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class QgsSymbolV2
106106
//! @note customContext parameter added in 2.6
107107
void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = 0 );
108108

109+
//! export symbol as image format. PNG and SVG supported
110+
void exportImage( QString path, QString format, QSize size );
111+
112+
//! Generate symbol as image
109113
QImage asImage( QSize size, QgsRenderContext* customContext = 0 );
110114

111115
QImage bigSymbolPreviewImage();

‎src/core/symbology-ng/qgssymbolv2.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <QImage>
3535
#include <QPainter>
3636
#include <QSize>
37+
#include <QSvgGenerator>
3738

3839
#include <cmath>
3940

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

356+
void QgsSymbolV2::exportImage( QString path, QString format, QSize size )
357+
{
358+
if ( format.toLower() == "svg" )
359+
{
360+
QSvgGenerator generator;
361+
generator.setFileName( path );
362+
generator.setSize( size );
363+
generator.setViewBox( QRect( 0, 0, size.height(), size.height() ) );
364+
365+
QPainter painter( &generator );
366+
drawPreviewIcon( &painter, size );
367+
painter.end();
368+
}
369+
else
370+
{
371+
QImage image = asImage( size );
372+
image.save( path );
373+
}
374+
}
375+
355376
QImage QgsSymbolV2::asImage( QSize size, QgsRenderContext* customContext )
356377
{
357378
QImage image( size, QImage::Format_ARGB32_Premultiplied );

‎src/core/symbology-ng/qgssymbolv2.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ class CORE_EXPORT QgsSymbolV2
8787

8888
// symbol layers handling
8989

90-
/**Returns list of symbol layers contained in the symbol.
90+
/** Returns list of symbol layers contained in the symbol.
9191
* @returns symbol layers list
9292
* @note added in QGIS 2.7
9393
* @see symbolLayer
9494
* @see symbolLayerCount
9595
*/
9696
QgsSymbolLayerV2List symbolLayers() { return mLayers; }
9797

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

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

141+
//! export symbol as image format. PNG and SVG supported
142+
void exportImage( QString path, QString format, QSize size );
143+
144+
//! Generate symbol as image
141145
QImage asImage( QSize size, QgsRenderContext* customContext = 0 );
142146

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

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

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

203-
/**Symbol opacity (in the range 0 - 1)*/
207+
/** Symbol opacity (in the range 0 - 1)*/
204208
qreal mAlpha;
205209

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

399403
void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
400-
/**Calculates the bounds of a polygon including rings*/
404+
/** Calculates the bounds of a polygon including rings*/
401405
QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
402-
/**Translates the rings in a polygon by a set distance*/
406+
/** Translates the rings in a polygon by a set distance*/
403407
QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
404408
};
405409

‎src/gui/symbology-ng/qgsstylev2managerdialog.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "qgsapplication.h"
4141
#include "qgslogger.h"
4242

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

6867
QMenu *shareMenu = new QMenu( tr( "Share Menu" ), this );
68+
QAction *exportAsPNGAction = shareMenu->addAction( tr( "Export as PNG" ) );
69+
QAction *exportAsSVGAction = shareMenu->addAction( tr( "Export as SVG" ) );
6970
QAction *exportAction = shareMenu->addAction( tr( "Export" ) );
7071
QAction *importAction = shareMenu->addAction( tr( "Import" ) );
72+
exportAsPNGAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
73+
exportAsSVGAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
7174
exportAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
7275
importAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingImport.svg" ) ) );
76+
connect( exportAsPNGAction, SIGNAL( triggered() ), this, SLOT( exportItemsPNG() ) );
77+
connect( exportAsSVGAction, SIGNAL( triggered() ), this, SLOT( exportItemsSVG() ) );
7378
connect( exportAction, SIGNAL( triggered() ), this, SLOT( exportItems() ) );
7479
connect( importAction, SIGNAL( triggered() ), this, SLOT( importItems() ) );
7580
btnShare->setMenu( shareMenu );
@@ -730,6 +735,40 @@ void QgsStyleV2ManagerDialog::itemChanged( QStandardItem* item )
730735
}
731736
}
732737

738+
void QgsStyleV2ManagerDialog::exportItemsPNG()
739+
{
740+
QString dir = QFileDialog::getExistingDirectory( this, tr( "Exported selected symbols as PNG" ),
741+
QDir::home().absolutePath(),
742+
QFileDialog::ShowDirsOnly
743+
| QFileDialog::DontResolveSymlinks );
744+
exportSelectedItemsImages( dir, "png", QSize( 32, 32 ) );
745+
}
746+
747+
void QgsStyleV2ManagerDialog::exportItemsSVG()
748+
{
749+
QString dir = QFileDialog::getExistingDirectory( this, tr( "Exported selected symbols as SVG" ),
750+
QDir::home().absolutePath(),
751+
QFileDialog::ShowDirsOnly
752+
| QFileDialog::DontResolveSymlinks );
753+
exportSelectedItemsImages( dir, "svg", QSize( 32, 32 ) );
754+
}
755+
756+
757+
void QgsStyleV2ManagerDialog::exportSelectedItemsImages( QString dir, QString format, QSize size )
758+
{
759+
if ( dir.isEmpty() )
760+
return;
761+
762+
QModelIndexList indexes = listItems->selectionModel()->selection().indexes();
763+
foreach ( QModelIndex index, indexes )
764+
{
765+
QString name = index.data().toString();
766+
QString path = dir + "/" + name + "." + format;
767+
QgsSymbolV2 *sym = mStyle->symbol( name );
768+
sym->exportImage( path, format, size );
769+
}
770+
}
771+
733772
void QgsStyleV2ManagerDialog::exportItems()
734773
{
735774
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Export );

‎src/gui/symbology-ng/qgsstylev2managerdialog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
4141
void addItem();
4242
void editItem();
4343
void removeItem();
44+
void exportItemsSVG();
45+
void exportItemsPNG();
46+
void exportSelectedItemsImages( QString dir, QString format, QSize size );
4447
void exportItems();
4548
void importItems();
4649

0 commit comments

Comments
 (0)
Please sign in to comment.