Skip to content
Permalink
Browse files

Ask for confirmation before overwrite a layer style

  • Loading branch information
Emilio Loi authored and jef-n committed May 9, 2013
1 parent bfe2f9f commit 1b43de3111fe2b780d6fa601805e6191f41f285b
Showing with 347 additions and 339 deletions.
  1. +2 −0 src/app/qgsvectorlayerproperties.cpp
  2. +128 −129 src/core/qgsvectorlayer.cpp
  3. +217 −210 src/providers/postgres/qgspostgresprovider.cpp
@@ -728,6 +728,7 @@ void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
apply();

layer->saveStyleToDatabase( styleName, styleDesc, isDefault, uiFileContent, msgError );

if( !msgError.isNull() )
{
QMessageBox::warning( this, infoWindowTitle, msgError );
@@ -736,6 +737,7 @@ void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
{
QMessageBox::information( this, infoWindowTitle, tr( "Style saved" ) );
}

}
else
{
@@ -32,6 +32,7 @@
#include <QString>
#include <QDomNode>
#include <QVector>
#include <QMessageBox>

#include "qgsvectorlayer.h"

@@ -73,40 +74,39 @@
#include "qgsdiagramrendererv2.h"
#include "qgsstylev2.h"
#include "qgssymbologyv2conversion.h"
#include "qgspallabeling.h"

#ifdef TESTPROVIDERLIB
#include <dlfcn.h>
#endif

typedef bool saveStyle_t(
const QString& uri,
const QString& qmlStyle,
const QString& sldStyle,
const QString& styleName,
const QString& styleDescription,
const QString& uiFileContent,
bool useAsDefault,
QString& errCause
const QString& uri,
const QString& qmlStyle,
const QString& sldStyle,
const QString& styleName,
const QString& styleDescription,
const QString& uiFileContent,
bool useAsDefault,
QString& errCause
);

typedef QString loadStyle_t(
const QString& uri,
QString& errCause
const QString& uri,
QString& errCause
);

typedef int listStyles_t(
const QString& uri,
QStringList &ids,
QStringList &names,
QStringList &descriptions,
QString& errCause
const QString& uri,
QVector<QString> &ids,
QVector<QString> &names,
QVector<QString> &descriptions,
QString& errCause
);

typedef QString getStyleById_t(
const QString& uri,
QString styleID,
QString& errCause
const QString& uri,
QString styleID,
QString& errCause
);


@@ -3731,87 +3731,86 @@ QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
return elem;
}

int QgsVectorLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
int QgsVectorLayer::listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names, QVector<QString> &descriptions, QString &msgError )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return -1;
}
listStyles_t* listStylesExternalMethod = ( listStyles_t * ) cast_to_fptr( myLib->resolve( "listStyles" ) );
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return -1;
}
listStyles_t* listStylesExternalMethod = ( listStyles_t * ) cast_to_fptr(myLib->resolve("listStyles"));

if ( !listStylesExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey ).arg( "listStyles" );
return -1;
}
if ( !listStylesExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no listStyles method" ).arg( mProviderKey );
return -1;
}

return listStylesExternalMethod( mDataSource, ids, names, descriptions, msgError );
return listStylesExternalMethod(mDataSource, ids, names, descriptions, msgError);
}

QString QgsVectorLayer::getStyleFromDatabase( QString styleId, QString &msgError )
QString QgsVectorLayer::getStyleFromDatabase(QString styleId, QString &msgError)
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return QObject::tr( "" );
}
getStyleById_t* getStyleByIdMethod = ( getStyleById_t * ) cast_to_fptr( myLib->resolve( "getStyleById" ) );
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return QObject::tr( "" );
}
getStyleById_t* getStyleByIdMethod = ( getStyleById_t * ) cast_to_fptr(myLib->resolve("getStyleById"));

if ( !getStyleByIdMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey ).arg( "getStyleById" );
return QObject::tr( "" );
}
if ( !getStyleByIdMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no getStyleById method" ).arg( mProviderKey );
return QObject::tr( "" );
}

return getStyleByIdMethod( mDataSource, styleId, msgError );
return getStyleByIdMethod( mDataSource, styleId, msgError );
}


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

QString sldStyle, qmlStyle;
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return;
}
saveStyle_t* saveStyleExternalMethod = ( saveStyle_t * ) cast_to_fptr( myLib->resolve( "saveStyle" ) );
QString sldStyle, qmlStyle;
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return;
}
saveStyle_t* saveStyleExternalMethod = ( saveStyle_t * ) cast_to_fptr(myLib->resolve("saveStyle"));

if ( !saveStyleExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey ).arg( "saveStyle" );
return;
}
if ( !saveStyleExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no saveStyle method" ).arg( mProviderKey );
return;
}

QDomDocument qmlDocument, sldDocument;
this->exportNamedStyle( qmlDocument, msgError );
if ( !msgError.isNull() )
{
return;
}
qmlStyle = qmlDocument.toString();
QDomDocument qmlDocument, sldDocument;
this->exportNamedStyle(qmlDocument, msgError);
if( !msgError.isNull() )
{
return;
}
qmlStyle = qmlDocument.toString();

this->exportSldStyle( sldDocument, msgError );
if ( !msgError.isNull() )
{
return;
}
sldStyle = sldDocument.toString();
this->exportSldStyle(sldDocument, msgError);
if( !msgError.isNull() )
{
return;
}
sldStyle = sldDocument.toString();

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


@@ -3823,60 +3822,60 @@ QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFla

QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag , bool loadFromLocalDB )
{
QgsDataSourceURI dsUri( theURI );
if ( !loadFromLocalDB && !dsUri.database().isEmpty() )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( myLib )
QgsDataSourceURI dsUri( theURI );
if ( !loadFromLocalDB && !dsUri.database().isEmpty() )
{
loadStyle_t* loadStyleExternalMethod = ( loadStyle_t * ) cast_to_fptr( myLib->resolve( "loadStyle" ) );
if ( loadStyleExternalMethod )
{
QString qml, errorMsg;
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if ( !qml.isEmpty() )
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( myLib )
{
theResultFlag = this->applyNamedStyle( qml, errorMsg );
loadStyle_t* loadStyleExternalMethod = ( loadStyle_t * ) cast_to_fptr( myLib->resolve( "loadStyle" ) );
if ( loadStyleExternalMethod )
{
QString qml, errorMsg;
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if( !qml.isEmpty() )
{
theResultFlag = this->applyNamedStyle( qml, errorMsg );
}
}
}
}
}

}
if ( !theResultFlag )
{
return QgsMapLayer::loadNamedStyle( theURI, theResultFlag );
}
return QObject::tr( "Loaded from Provider" );
}
if( !theResultFlag )
{
return QgsMapLayer::loadNamedStyle( theURI, theResultFlag );
}
return QObject::tr( "Loaded from Provider" );
}

bool QgsVectorLayer::applyNamedStyle( QString namedStyle, QString errorMsg )
bool QgsVectorLayer::applyNamedStyle(QString namedStyle, QString errorMsg )
{
QDomDocument myDocument( "qgis" );
myDocument.setContent( namedStyle );
QDomDocument myDocument( "qgis" );
myDocument.setContent( namedStyle );

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

if ( myRoot.isNull() )
{
errorMsg = tr( "Error: qgis element could not be found" );
return false;
}
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );
if( myRoot.isNull() )
{
errorMsg = tr( "Error: qgis element could not be found" );
return false;
}
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
#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

return readSymbology( myRoot, errorMsg );
return readSymbology( myRoot, errorMsg );
}

0 comments on commit 1b43de3

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