Skip to content

Commit 88187f1

Browse files
committed
display a menu to choose the output format saving vector styles (fix #5136)
1 parent 6e855d8 commit 88187f1

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

src/app/qgsvectorlayerproperties.cpp

+56-9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
117117
actionDialog = new QgsAttributeActionDialog( layer->actions(), fields, actionOptionsFrame );
118118
actionLayout->addWidget( actionDialog );
119119

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

122128
if ( layer->dataProvider() )//enable spatial index button group if supported by provider
@@ -950,6 +956,7 @@ void QgsVectorLayerProperties::on_pbnLoadStyle_clicked()
950956
{
951957
QSettings myQSettings; // where we keep last used filter in persistent state
952958
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();
959+
953960
QString myFileName = QFileDialog::getOpenFileName( this, tr( "Load layer properties from style file" ), myLastUsedDir,
954961
tr( "QGIS Layer Style File" ) + " (*.qml);;" + tr( "SLD File" ) + " (*.sld)" );
955962
if ( myFileName.isNull() )
@@ -987,34 +994,65 @@ void QgsVectorLayerProperties::on_pbnLoadStyle_clicked()
987994

988995

989996
void QgsVectorLayerProperties::on_pbnSaveStyleAs_clicked()
997+
{
998+
saveStyleAs( QML );
999+
}
1000+
1001+
void QgsVectorLayerProperties::saveStyleAsMenuTriggered( QAction *action )
1002+
{
1003+
QMenu *menu = qobject_cast<QMenu *>( sender() );
1004+
if ( !menu )
1005+
return;
1006+
1007+
int index = mSaveAsMenu->actions().indexOf( action );
1008+
if ( index < 0 )
1009+
return;
1010+
1011+
saveStyleAs( (StyleType) index );
1012+
}
1013+
1014+
void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
9901015
{
9911016
QSettings myQSettings; // where we keep last used filter in persistent state
9921017
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();
993-
QString myOutputFileName = QFileDialog::getSaveFileName( this, tr( "Save layer properties as style file" ), myLastUsedDir,
994-
tr( "QGIS Layer Style File" ) + " (*.qml);;" + tr( "SLD File" ) + " (*.sld)" );
1018+
1019+
QString format, extension;
1020+
if ( styleType == SLD )
1021+
{
1022+
format = tr( "SLD File" ) + " (*.sld)";
1023+
extension = ".sld";
1024+
}
1025+
else
1026+
{
1027+
format = tr( "QGIS Layer Style File" ) + " (*.qml)";
1028+
extension = ".qml";
1029+
}
1030+
1031+
QString myOutputFileName = QFileDialog::getSaveFileName( this, tr( "Save layer properties as style file" ),
1032+
myLastUsedDir, format );
9951033
if ( myOutputFileName.isNull() ) //dialog canceled
9961034
{
9971035
return;
9981036
}
9991037

1000-
apply(); // make sure the qml to save is uptodate
1038+
apply(); // make sure the style to save is uptodate
10011039

10021040
QString myMessage;
10031041
bool defaultLoadedFlag = false;
10041042

10051043
//ensure the user never omitted the extension from the file name
1006-
if ( myOutputFileName.endsWith( ".sld", Qt::CaseInsensitive ) )
1044+
if ( myOutputFileName.endsWith( extension, Qt::CaseInsensitive ) )
1045+
{
1046+
myOutputFileName += extension;
1047+
}
1048+
1049+
if ( styleType == SLD )
10071050
{
10081051
// convert to SLD
10091052
myMessage = layer->saveSldStyle( myOutputFileName, defaultLoadedFlag );
10101053
}
10111054
else
10121055
{
1013-
if ( !myOutputFileName.endsWith( ".qml", Qt::CaseInsensitive ) )
1014-
{
1015-
myOutputFileName += ".qml";
1016-
}
1017-
10181056
myMessage = layer->saveNamedStyle( myOutputFileName, defaultLoadedFlag );
10191057
}
10201058

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

12361274
connect( mRendererDialog, SIGNAL( useNewSymbology( bool ) ), this, SLOT( setUsingNewSymbology( bool ) ) );
1275+
1276+
// display the menu to choose the output format (fix #5136)
1277+
pbnSaveStyleAs->setText( tr( "Save Style" ) );
1278+
pbnSaveStyleAs->setMenu( mSaveAsMenu );
1279+
QObject::disconnect( pbnSaveStyleAs, SIGNAL( clicked() ), this, SLOT( on_pbnSaveStyleAs_clicked() ) );
12371280
}
12381281
else if ( layer->renderer() )
12391282
{
@@ -1261,6 +1304,10 @@ void QgsVectorLayerProperties::updateSymbologyPage()
12611304

12621305
QObject::connect( legendtypecombobox, SIGNAL( activated( const QString & ) ), this,
12631306
SLOT( alterLayerDialog( const QString & ) ) );
1307+
1308+
pbnSaveStyleAs->setText( tr( "Save Style..." ) );
1309+
pbnSaveStyleAs->setMenu( NULL );
1310+
QObject::connect( pbnSaveStyleAs, SIGNAL( clicked() ), this, SLOT( on_pbnSaveStyleAs_clicked() ) );
12641311
}
12651312
else
12661313
{

src/app/qgsvectorlayerproperties.h

+15
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
4242
Q_OBJECT
4343

4444
public:
45+
enum StyleType
46+
{
47+
QML = 0,
48+
SLD,
49+
};
50+
4551
QgsVectorLayerProperties( QgsVectorLayer *lyr = 0, QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
4652
~QgsVectorLayerProperties();
4753
/**Sets the legend type to "single symbol", "graduated symbol" or "continuous color"*/
@@ -138,8 +144,15 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
138144

139145
void toggleEditing( QgsMapLayer * );
140146

147+
private slots:
148+
149+
/** save the style based on selected format from the menu */
150+
void saveStyleAsMenuTriggered( QAction * );
151+
141152
protected:
142153

154+
void saveStyleAs( StyleType styleType );
155+
143156
void updateSymbologyPage();
144157

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

160173
bool mMetadataFilled;
161174

175+
QMenu *mSaveAsMenu;
176+
162177
/**Renderer dialog which is shown*/
163178
QDialog* mRendererDialog;
164179
/**Buffer renderer, which is assigned to the vector layer when apply is pressed*/

0 commit comments

Comments
 (0)