Skip to content
Permalink
Browse files

reapply some cleanup to 'styles_to_db'

  • Loading branch information
jef-n committed May 9, 2013
1 parent a905b55 commit 576102b2603c0390a525d21332fb09e4b5d41275

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

#include "qgsvectorlayer.h"

@@ -74,39 +73,40 @@
#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,
QVector<QString> &ids,
QVector<QString> &names,
QVector<QString> &descriptions,
QString& errCause
const QString& uri,
QStringList &ids,
QStringList &names,
QStringList &descriptions,
QString& errCause
);

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


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

int QgsVectorLayer::listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names, QVector<QString> &descriptions, QString &msgError )
int QgsVectorLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &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 listStyles method" ).arg( mProviderKey );
return -1;
}
if ( !listStylesExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey ).arg( "listStyles" );
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 getStyleById method" ).arg( mProviderKey );
return QObject::tr( "" );
}
if ( !getStyleByIdMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey ).arg( "getStyleById" );
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 saveStyle method" ).arg( mProviderKey );
return;
}
if ( !saveStyleExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey ).arg( "saveStyle" );
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 );
}



QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
{
return loadNamedStyle( theURI, theResultFlag, false );
return loadNamedStyle( theURI, theResultFlag, false );
}

QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag , bool loadFromLocalDB )
{
QgsDataSourceURI dsUri( theURI );
if ( !loadFromLocalDB && !dsUri.database().isEmpty() )
QgsDataSourceURI dsUri( theURI );
if ( !loadFromLocalDB && !dsUri.database().isEmpty() )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( myLib )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( myLib )
loadStyle_t* loadStyleExternalMethod = ( loadStyle_t * ) cast_to_fptr( myLib->resolve( "loadStyle" ) );
if ( loadStyleExternalMethod )
{
QString qml, errorMsg;
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if ( !qml.isEmpty() )
{
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 );
}
}
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 );
}
@@ -22,7 +22,6 @@
#include <QSet>
#include <QList>
#include <QStringList>
#include <QVector>

#include "qgis.h"
#include "qgsmaplayer.h"
@@ -707,14 +706,14 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer

/**
* Lists all the style in db split into related to the layer and not related to
* @param ids the QVector in which will be stored the style db ids
* @param names the QVector in which will be stored the style names
* @param descriptions the QVector in which will be stored the style descriptions
* @param ids the list in which will be stored the style db ids
* @param names the list in which will be stored the style names
* @param descriptions the list in which will be stored the style descriptions
* @param msgError
* @return the number of styles related to current layer
*/
virtual int listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names,
QVector<QString> &descriptions, QString &msgError );
virtual int listStylesInDatabase( QStringList &ids, QStringList &names,
QStringList &descriptions, QString &msgError );

/**
* Will return the named style corresponding to style id provided
@@ -735,7 +734,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag );

virtual bool applyNamedStyle(QString namedStyle , QString errorMsg);
virtual bool applyNamedStyle( QString namedStyle , QString errorMsg );

/** convert a saved attribute editor element into a AttributeEditor structure as it's used internally.
* @param elem the DOM element
@@ -1281,15 +1280,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
signals:

/**
* This signal is emited when selection was changed
* This signal is emitted when selection was changed
*
* @param selected Newly selected feature ids
* @param deselected Ids of all features which have previously been selected but are not any more
* @param clearAndSelect In case this is set to true, the old selection was dismissed and the new selection corresponds to selected
*/
void selectionChanged( const QgsFeatureIds selected, const QgsFeatureIds deselected, const bool clearAndSelect );

/** This signal is emited when selection was changed */
/** This signal is emitted when selection was changed */
void selectionChanged();

/** This signal is emitted when modifications has been done on layer */
@@ -1298,7 +1297,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Is emitted, when editing on this layer has started*/
void editingStarted();

/** Is emitted, when edited changes succesfully have been written to the data provider */
/** Is emitted, when edited changes successfully have been written to the data provider */
void editingStopped();

/** Is emitted, before changes are commited to the data provider */

0 comments on commit 576102b

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