Skip to content

Commit

Permalink
Added feature to save vector layers styles to postgis (when provider …
Browse files Browse the repository at this point in the history
…is postgre$
  • Loading branch information
Emilio Loi committed Apr 12, 2013
1 parent c3bbfee commit e21ace6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 63 deletions.
30 changes: 16 additions & 14 deletions src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,25 +625,27 @@ void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
if( styleType == DB )
{
QString infoWindowTitle = QObject::tr( "Save style to Postgres" );
QString msgError, pluto;
QString msgError;

QgsSaveStyleToDbDialog askToUser;
//TODO retrieve user username
askToUser.setOwner( QObject::tr( "Pippo!" ) );

if( askToUser.exec() == QDialog::Accepted )
{
layer->saveStyleToDatabase( askToUser.getName(), askToUser.getOwner(),
askToUser.getDescription(), askToUser.isDefault(),
msgError );
if( !msgError.isNull() )
{
QMessageBox::warning( this, infoWindowTitle, msgError );
}
else
{
QMessageBox::information(this, infoWindowTitle, tr( "Successful!" ));
}
QString styleName = askToUser.getName();
QString styleDesc = askToUser.getDescription();
bool isDefault = askToUser.isDefault();

apply();

layer->saveStyleToDatabase( styleName, styleDesc, isDefault, msgError );
if( !msgError.isNull() )
{
QMessageBox::warning( this, infoWindowTitle, msgError );
}
else
{
QMessageBox::information( this, infoWindowTitle, tr( "Style saved" ) );
}
}
else
{
Expand Down
84 changes: 42 additions & 42 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ typedef bool saveStyle_t(
const QString& sldStyle,
const QString& styleName,
const QString& styleDescription,
const QString& owner,
bool useAsDefault,
QString& errCause
);
Expand Down Expand Up @@ -3718,7 +3717,7 @@ QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
return elem;
}

void QgsVectorLayer::saveStyleToDatabase(QString name, QString owner, QString description, bool useAsDefault, QString &msgError){
void QgsVectorLayer::saveStyleToDatabase(QString name, QString description, bool useAsDefault, QString &msgError){

QString sldStyle, qmlStyle;
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
Expand Down Expand Up @@ -3752,56 +3751,57 @@ void QgsVectorLayer::saveStyleToDatabase(QString name, QString owner, QString de
}
sldStyle = sldDocument.toString();

saveStyleExternalMethod(mDataSource, qmlStyle, sldStyle, name, description,
owner, useAsDefault, msgError);
saveStyleExternalMethod(mDataSource, qmlStyle, sldStyle, name, description, useAsDefault, msgError);
}

QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
bool styleFound = false;
if ( myLib )
//TODO find a better way to check if uri is pointing to db
if ( theURI.contains( tr( "dbname=" ) ) )
{
loadStyle_t* loadStyleExternalMethod = ( loadStyle_t * ) cast_to_fptr( myLib->resolve( "loadStyle" ) );
if ( loadStyleExternalMethod )
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( myLib )
{
QString qml, errorMsg;
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if( qml.compare( tr( "" ) ) )
{
QDomDocument myDocument( "qgis" );
myDocument.setContent( qml );

QDomElement myRoot = myDocument.firstChildElement( "qgis" );

if( myRoot.isNull() )
loadStyle_t* loadStyleExternalMethod = ( loadStyle_t * ) cast_to_fptr( myLib->resolve( "loadStyle" ) );
if ( loadStyleExternalMethod )
{
QString qml, errorMsg;
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if( qml.compare( tr( "" ) ) )
{
theResultFlag = false;
return tr( "Error: qgis element could not be found in %1" ).arg( theURI );;
QDomDocument myDocument( "qgis" );
myDocument.setContent( qml );

QDomElement myRoot = myDocument.firstChildElement( "qgis" );

if( myRoot.isNull() )
{
theResultFlag = false;
return tr( "Error: qgis element could not be found in %1" ).arg( theURI );;
}
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );

#if 0
//read transparency level
QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
if ( ! transparencyNode.isNull() )
{
// set transparency level only if it's in project
// (otherwise it sets the layer transparent)
QDomElement myElement = transparencyNode.toElement();
setTransparency( myElement.text().toInt() );
}
#endif

theResultFlag = readSymbology( myRoot, errorMsg );
}
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );

#if 0
//read transparency level
QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
if ( ! transparencyNode.isNull() )
{
// set transparency level only if it's in project
// (otherwise it sets the layer transparent)
QDomElement myElement = transparencyNode.toElement();
setTransparency( myElement.text().toInt() );
}
#endif

styleFound = readSymbology( myRoot, errorMsg );
return errorMsg;
}
}
}
}
if( !styleFound )
if( !theResultFlag )
{
return QgsMapLayer::loadNamedStyle( theURI, theResultFlag );
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
virtual bool writeXml( QDomNode & layer_node, QDomDocument & doc );

virtual void saveStyleToDatabase( QString name, QString owner, QString description,
virtual void saveStyleToDatabase( QString name, QString description,
bool useAsDefault, QString &msgError );

virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag );
Expand Down
14 changes: 8 additions & 6 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3226,7 +3226,7 @@ QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause )

QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QString& sldStyle,
const QString& styleName, const QString& styleDescription,
const QString& owner, bool useAsDefault, QString& errCause )
bool useAsDefault, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column;
Expand Down Expand Up @@ -3261,6 +3261,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
f_table_schema = dsUri.schema();
f_table_name = dsUri.table();
f_geometry_column = dsUri.geometryColumn();
QString owner = dsUri.username();

QString sql = QObject::tr( "INSERT INTO %10( f_table_catalog, "
"f_table_schema, f_table_name, f_geometry_column, "
Expand All @@ -3281,13 +3282,14 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS

if( useAsDefault )
{
QString removeDefaultSql = QObject::tr( "UPDATE %1 SET useAsDefault=false WHERE f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5; COMMIT;")
QString removeDefaultSql = QObject::tr( "UPDATE %1 SET useAsDefault=false WHERE f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5;")
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue(f_table_name ) )
.arg( QgsPostgresConn::quotedValue(f_geometry_column ) );
sql = QObject::tr("BEGIN; %2 %1").arg( sql ).arg( removeDefaultSql );
sql = QObject::tr("BEGIN; %2 %1 COMMIT;")
.arg( sql ).arg( removeDefaultSql );
}

res = conn->PQexec( sql );
Expand All @@ -3310,8 +3312,8 @@ QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return false;
errCause = QObject::tr( "Connection to database failed" );
return QObject::tr( "" );
}

f_table_catalog = dsUri.database();
Expand All @@ -3333,5 +3335,5 @@ QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )
return QObject::tr( c );;
}

return tr( "" );
return QObject::tr( "" );
}

0 comments on commit e21ace6

Please sign in to comment.