Skip to content

Commit 4e52a8a

Browse files
author
Emilio Loi
committed
Added QMenu on loadStyle button with 2 options (load from filesystem, load from db)
1 parent 7aa831d commit 4e52a8a

File tree

4 files changed

+94
-21
lines changed

4 files changed

+94
-21
lines changed

src/app/qgsvectorlayerproperties.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,25 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
127127
mSaveAsMenu->addAction( tr( "QGIS Layer Style File" ) );
128128
mSaveAsMenu->addAction( tr( "SLD File" ) );
129129

130-
//Only if the provider support saving style to db add new choice
130+
//Only if the provider support loading & saving style to db add new choices
131131
if( layer->dataProvider()->isSavingStyleToDBSupported() )
132132
{
133+
//for loading
134+
mLoadStyleMenu = new QMenu();
135+
mLoadStyleMenu->addAction( tr( "Load from file" ) );
136+
mLoadStyleMenu->addAction( tr( "Load from database" ) );
137+
pbnLoadStyle->setContextMenuPolicy( Qt::PreventContextMenu );
138+
pbnLoadStyle->setMenu( mLoadStyleMenu );
139+
140+
QObject::connect( mLoadStyleMenu, SIGNAL( triggered( QAction * ) ),
141+
this, SLOT( loadStyleMenuTriggered( QAction * ) ) ) ;
142+
143+
//for saving
133144
mSaveAsMenu->addAction( tr( "Save on database (%1)" ).arg( layer->providerType() ) );
134145
}
135146

136-
QObject::connect( mSaveAsMenu, SIGNAL( triggered( QAction * ) ), this, SLOT( saveStyleAsMenuTriggered( QAction * ) ) );
147+
QObject::connect( mSaveAsMenu, SIGNAL( triggered( QAction * ) ),
148+
this, SLOT( saveStyleAsMenuTriggered( QAction * ) ) );
137149

138150
mFieldsPropertiesDialog = new QgsFieldsProperties( layer, mFieldsFrame );
139151
mFieldsFrame->setLayout( new QVBoxLayout( mFieldsFrame ) );
@@ -713,6 +725,22 @@ void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
713725
}
714726
}
715727

728+
void QgsVectorLayerProperties::loadStyleMenuTriggered( QAction *action )
729+
{
730+
QMenu *menu = qobject_cast<QMenu *>( sender() );
731+
if ( !menu )
732+
return;
733+
734+
int index = mLoadStyleMenu->actions().indexOf( action );
735+
736+
//Load from filesystem
737+
if ( index == 0 )
738+
{
739+
this->on_pbnLoadStyle_clicked();
740+
}
741+
742+
}
743+
716744
QList<QgsVectorOverlayPlugin*> QgsVectorLayerProperties::overlayPlugins() const
717745
{
718746
QList<QgsVectorOverlayPlugin*> pluginList;

src/app/qgsvectorlayerproperties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
134134
/** save the style based on selected format from the menu */
135135
void saveStyleAsMenuTriggered( QAction * );
136136

137+
/** called when is possible to choice if load the style from filesystem or from db */
138+
void loadStyleMenuTriggered( QAction * );
139+
137140
protected:
138141

139142
void saveStyleAs( StyleType styleType );
@@ -145,6 +148,7 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
145148
bool mMetadataFilled;
146149

147150
QMenu *mSaveAsMenu;
151+
QMenu *mLoadStyleMenu;
148152

149153
/**Renderer dialog which is shown*/
150154
QDialog* mRendererDialog;

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,7 +3251,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
32513251
res = conn->PQexec( createTabeQuery );
32523252
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
32533253
{
3254-
errCause = QObject::tr( "Unable to save layer style. Unable to create style table" );
3254+
errCause = QObject::tr( "Unable to save layer style. It's not possible to create the destination table on the database. Maybe you need to contact " );
32553255
conn->disconnect();
32563256
return false;
32573257
}
@@ -3262,23 +3262,25 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
32623262
f_table_name = dsUri.table();
32633263
f_geometry_column = dsUri.geometryColumn();
32643264
QString owner = dsUri.username();
3265+
QString isdef = (useAsDefault) ? QObject::tr( "true" ) : QObject::tr( "false" );
32653266

3266-
QString sql = QObject::tr( "INSERT INTO %10( f_table_catalog, "
3267+
QString sql = QObject::tr( "INSERT INTO %1 ( f_table_catalog, "
32673268
"f_table_schema, f_table_name, f_geometry_column, "
32683269
"styleName, styleQML, styleSLD, useAsDefault, "
32693270
"description, owner) "
3270-
"VALUES(%1,%2,%3,%4,%5,XMLPARSE(DOCUMENT %6),"
3271-
"XMLPARSE(DOCUMENT %7),true,%8,%9);" )
3271+
"VALUES(%2,%3,%4,%5,%6,XMLPARSE(DOCUMENT %7),"
3272+
"XMLPARSE(DOCUMENT %8),%9,%10,%11);" )
3273+
.arg( styleTableName )
32723274
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
32733275
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
32743276
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
32753277
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) )
32763278
.arg( QgsPostgresConn::quotedValue( styleName ) )
32773279
.arg( QgsPostgresConn::quotedValue( qmlStyle ) )
32783280
.arg( QgsPostgresConn::quotedValue( sldStyle ) )
3281+
.arg( isdef )
32793282
.arg( QgsPostgresConn::quotedValue( styleDescription ) )
3280-
.arg( QgsPostgresConn::quotedValue( owner ) )
3281-
.arg( styleTableName );
3283+
.arg( QgsPostgresConn::quotedValue( owner ) );
32823284

32833285
if( useAsDefault )
32843286
{
@@ -3288,15 +3290,15 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
32883290
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
32893291
.arg( QgsPostgresConn::quotedValue(f_table_name ) )
32903292
.arg( QgsPostgresConn::quotedValue(f_geometry_column ) );
3291-
sql = QObject::tr("BEGIN; %2 %1 COMMIT;")
3292-
.arg( sql ).arg( removeDefaultSql );
3293+
sql = QObject::tr("BEGIN; %1 %2 COMMIT;")
3294+
.arg( removeDefaultSql ).arg( sql );
32933295
}
32943296

32953297
res = conn->PQexec( sql );
32963298
conn->disconnect();
32973299
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
32983300
{
3299-
errCause = QObject::tr( "Unable to save layer style" );
3301+
errCause = QObject::tr( "Unable to save layer style. It's not possible to insert a new record in style table. " );
33003302
return false;
33013303
}
33023304
return true;

src/ui/qgsvectorlayerpropertiesbase.ui

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
</item>
4747
<item>
4848
<widget class="QPushButton" name="pbnSaveStyleAs">
49+
<property name="contextMenuPolicy">
50+
<enum>Qt::PreventContextMenu</enum>
51+
</property>
4952
<property name="text">
5053
<string>Save Style ...</string>
5154
</property>
@@ -177,7 +180,16 @@
177180
<string>Fields</string>
178181
</attribute>
179182
<layout class="QGridLayout" name="mFieldsGridLayout">
180-
<property name="margin">
183+
<property name="leftMargin">
184+
<number>0</number>
185+
</property>
186+
<property name="topMargin">
187+
<number>0</number>
188+
</property>
189+
<property name="rightMargin">
190+
<number>0</number>
191+
</property>
192+
<property name="bottomMargin">
181193
<number>0</number>
182194
</property>
183195
<item row="0" column="0">
@@ -201,7 +213,16 @@
201213
<string>General</string>
202214
</attribute>
203215
<layout class="QGridLayout" name="gridLayout_7">
204-
<property name="margin">
216+
<property name="leftMargin">
217+
<number>0</number>
218+
</property>
219+
<property name="topMargin">
220+
<number>0</number>
221+
</property>
222+
<property name="rightMargin">
223+
<number>0</number>
224+
</property>
225+
<property name="bottomMargin">
205226
<number>0</number>
206227
</property>
207228
<item row="0" column="0">
@@ -220,8 +241,8 @@
220241
<rect>
221242
<x>0</x>
222243
<y>0</y>
223-
<width>680</width>
224-
<height>393</height>
244+
<width>910</width>
245+
<height>732</height>
225246
</rect>
226247
</property>
227248
<layout class="QGridLayout" name="gridLayout_3">
@@ -297,7 +318,16 @@
297318
<bool>true</bool>
298319
</property>
299320
<layout class="QGridLayout">
300-
<property name="margin">
321+
<property name="leftMargin">
322+
<number>11</number>
323+
</property>
324+
<property name="topMargin">
325+
<number>11</number>
326+
</property>
327+
<property name="rightMargin">
328+
<number>11</number>
329+
</property>
330+
<property name="bottomMargin">
301331
<number>11</number>
302332
</property>
303333
<item row="0" column="4">
@@ -459,7 +489,16 @@ p, li { white-space: pre-wrap; }
459489
<string>Subset</string>
460490
</property>
461491
<layout class="QGridLayout">
462-
<property name="margin">
492+
<property name="leftMargin">
493+
<number>11</number>
494+
</property>
495+
<property name="topMargin">
496+
<number>11</number>
497+
</property>
498+
<property name="rightMargin">
499+
<number>11</number>
500+
</property>
501+
<property name="bottomMargin">
463502
<number>11</number>
464503
</property>
465504
<item row="0" column="0" colspan="2">
@@ -719,8 +758,8 @@ p, li { white-space: pre-wrap; }
719758
<rect>
720759
<x>0</x>
721760
<y>0</y>
722-
<width>94</width>
723-
<height>211</height>
761+
<width>892</width>
762+
<height>714</height>
724763
</rect>
725764
</property>
726765
<layout class="QVBoxLayout" name="verticalLayout">
@@ -831,8 +870,8 @@ p, li { white-space: pre-wrap; }
831870
<rect>
832871
<x>0</x>
833872
<y>0</y>
834-
<width>96</width>
835-
<height>112</height>
873+
<width>892</width>
874+
<height>714</height>
836875
</rect>
837876
</property>
838877
<layout class="QGridLayout" name="gridLayout_17">

0 commit comments

Comments
 (0)