Skip to content
Permalink
Browse files

Add export as SVG/PNG to style manager

  • Loading branch information
NathanW2 committed Jul 6, 2015
1 parent a360422 commit 217931f9372972c8be86469e920de53d7f0d2384
@@ -30,6 +30,7 @@
#include <QFile>
#include <QTextStream>
#include <QByteArray>
#include <QSvgGenerator>

#include <sqlite3.h>

@@ -135,6 +136,31 @@ 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,6 +167,9 @@ 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 );

@@ -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,38 @@ 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();
mStyle->exportSymbol( dir, name, 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 217931f

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