Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
display a menu to choose the output format saving vector styles (fix #…
  • Loading branch information
brushtyler committed Jun 19, 2012
1 parent 6e855d8 commit 88187f1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
65 changes: 56 additions & 9 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -117,6 +117,12 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
actionDialog = new QgsAttributeActionDialog( layer->actions(), fields, actionOptionsFrame );
actionLayout->addWidget( actionDialog );

// Create the menu for the save style button to choose the output format
mSaveAsMenu = new QMenu( pbnSaveStyleAs );
mSaveAsMenu->addAction( tr( "QGIS Layer Style File" ) );
mSaveAsMenu->addAction( tr( "SLD File" ) );
QObject::connect( mSaveAsMenu, SIGNAL( triggered( QAction * ) ), this, SLOT( saveStyleAsMenuTriggered( QAction * ) ) );

reset();

if ( layer->dataProvider() )//enable spatial index button group if supported by provider
Expand Down Expand Up @@ -950,6 +956,7 @@ void QgsVectorLayerProperties::on_pbnLoadStyle_clicked()
{
QSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();

QString myFileName = QFileDialog::getOpenFileName( this, tr( "Load layer properties from style file" ), myLastUsedDir,
tr( "QGIS Layer Style File" ) + " (*.qml);;" + tr( "SLD File" ) + " (*.sld)" );
if ( myFileName.isNull() )
Expand Down Expand Up @@ -987,34 +994,65 @@ void QgsVectorLayerProperties::on_pbnLoadStyle_clicked()


void QgsVectorLayerProperties::on_pbnSaveStyleAs_clicked()
{
saveStyleAs( QML );
}

void QgsVectorLayerProperties::saveStyleAsMenuTriggered( QAction *action )
{
QMenu *menu = qobject_cast<QMenu *>( sender() );
if ( !menu )
return;

int index = mSaveAsMenu->actions().indexOf( action );
if ( index < 0 )
return;

saveStyleAs( (StyleType) index );
}

void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
{
QSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();
QString myOutputFileName = QFileDialog::getSaveFileName( this, tr( "Save layer properties as style file" ), myLastUsedDir,
tr( "QGIS Layer Style File" ) + " (*.qml);;" + tr( "SLD File" ) + " (*.sld)" );

QString format, extension;
if ( styleType == SLD )
{
format = tr( "SLD File" ) + " (*.sld)";
extension = ".sld";
}
else
{
format = tr( "QGIS Layer Style File" ) + " (*.qml)";
extension = ".qml";
}

QString myOutputFileName = QFileDialog::getSaveFileName( this, tr( "Save layer properties as style file" ),
myLastUsedDir, format );
if ( myOutputFileName.isNull() ) //dialog canceled
{
return;
}

apply(); // make sure the qml to save is uptodate
apply(); // make sure the style to save is uptodate

QString myMessage;
bool defaultLoadedFlag = false;

//ensure the user never omitted the extension from the file name
if ( myOutputFileName.endsWith( ".sld", Qt::CaseInsensitive ) )
if ( myOutputFileName.endsWith( extension, Qt::CaseInsensitive ) )
{
myOutputFileName += extension;
}

if ( styleType == SLD )
{
// convert to SLD
myMessage = layer->saveSldStyle( myOutputFileName, defaultLoadedFlag );
}
else
{
if ( !myOutputFileName.endsWith( ".qml", Qt::CaseInsensitive ) )
{
myOutputFileName += ".qml";
}

myMessage = layer->saveNamedStyle( myOutputFileName, defaultLoadedFlag );
}

Expand Down Expand Up @@ -1234,6 +1272,11 @@ void QgsVectorLayerProperties::updateSymbologyPage()
mRendererDialog = new QgsRendererV2PropertiesDialog( layer, QgsStyleV2::defaultStyle(), true );

connect( mRendererDialog, SIGNAL( useNewSymbology( bool ) ), this, SLOT( setUsingNewSymbology( bool ) ) );

// display the menu to choose the output format (fix #5136)
pbnSaveStyleAs->setText( tr( "Save Style" ) );
pbnSaveStyleAs->setMenu( mSaveAsMenu );
QObject::disconnect( pbnSaveStyleAs, SIGNAL( clicked() ), this, SLOT( on_pbnSaveStyleAs_clicked() ) );
}
else if ( layer->renderer() )
{
Expand Down Expand Up @@ -1261,6 +1304,10 @@ void QgsVectorLayerProperties::updateSymbologyPage()

QObject::connect( legendtypecombobox, SIGNAL( activated( const QString & ) ), this,
SLOT( alterLayerDialog( const QString & ) ) );

pbnSaveStyleAs->setText( tr( "Save Style..." ) );
pbnSaveStyleAs->setMenu( NULL );
QObject::connect( pbnSaveStyleAs, SIGNAL( clicked() ), this, SLOT( on_pbnSaveStyleAs_clicked() ) );
}
else
{
Expand Down
15 changes: 15 additions & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -42,6 +42,12 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
Q_OBJECT

public:
enum StyleType
{
QML = 0,
SLD,
};

QgsVectorLayerProperties( QgsVectorLayer *lyr = 0, QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
~QgsVectorLayerProperties();
/**Sets the legend type to "single symbol", "graduated symbol" or "continuous color"*/
Expand Down Expand Up @@ -138,8 +144,15 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope

void toggleEditing( QgsMapLayer * );

private slots:

/** save the style based on selected format from the menu */
void saveStyleAsMenuTriggered( QAction * );

protected:

void saveStyleAs( StyleType styleType );

void updateSymbologyPage();

enum attrColumns
Expand All @@ -159,6 +172,8 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope

bool mMetadataFilled;

QMenu *mSaveAsMenu;

/**Renderer dialog which is shown*/
QDialog* mRendererDialog;
/**Buffer renderer, which is assigned to the vector layer when apply is pressed*/
Expand Down

0 comments on commit 88187f1

Please sign in to comment.