Skip to content

Commit

Permalink
Some minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent 4495e8a commit 3f8ae8b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 23 deletions.
4 changes: 2 additions & 2 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -183,14 +183,14 @@ class QgsAuxiliaryLayer : QgsVectorLayer
:rtype: QgsPropertyDefinition
%End

static int createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *vlayer );
static int createProperty( QgsPalLayerSettings::Property property, QgsVectorLayer *vlayer, const QString &providerId = QString() );
%Docstring
Creates if necessary a new auxiliary field for a PAL property and
activate this property in settings.

\param property The property to create
\param providerId The id of the provider to use
\param vlayer The vector layer
\param providerId The id of the provider to use

:return: The index of the auxiliary field or -1
:rtype: int
Expand Down
6 changes: 4 additions & 2 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -774,13 +774,15 @@ Return the provider type for this layer
:rtype: str
%End

bool loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage );
bool loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage, const QString &key = QString() );
%Docstring
Loads the auxiliary layer for this vector layer. If there's no
corresponding table in the database, then nothing happens and false is
returned.
returned. The key is optional because if this layer has been read from
a XML document, then the key read in this document is used by default.

\param storage The auxiliary storage where to look for the table
\param key The key to use for joining.

:return: true if the auxiliary layer is well loaded, false otherwise

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoollabel.cpp
Expand Up @@ -744,7 +744,7 @@ bool QgsMapToolLabel::createAuxiliaryFields( LabelDetails &details, QgsPalIndexe
}
else
{
index = QgsAuxiliaryLayer::createProperty( p, providerId, vlayer );
index = QgsAuxiliaryLayer::createProperty( p, vlayer, providerId );
}

indexes[p] = index;
Expand Down
43 changes: 32 additions & 11 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -139,6 +139,7 @@ bool QgsAuxiliaryLayer::addAuxiliaryField( const QgsPropertyDefinition &definiti
const QgsField af = createAuxiliaryField( definition );
const bool rc = addAttribute( af );
updateFields();
mLayer->updateFields();

if ( rc )
{
Expand Down Expand Up @@ -214,7 +215,7 @@ bool QgsAuxiliaryLayer::save()
return rc;
}

int QgsAuxiliaryLayer::createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *layer )
int QgsAuxiliaryLayer::createProperty( QgsPalLayerSettings::Property property, QgsVectorLayer *layer, const QString &providerId )
{
int index = -1;

Expand Down Expand Up @@ -296,7 +297,7 @@ int QgsAuxiliaryLayer::propertyFromIndex( int index ) const
int p = -1;
QgsPropertyDefinition aDef = propertyDefinitionFromIndex( index );

if ( aDef.origin().compare( "labeling" ) == 0 )
if ( aDef.origin().compare( QStringLiteral( "labeling" ) ) == 0 )
{
const QgsPropertiesDefinition defs = QgsPalLayerSettings::propertyDefinitions();
QgsPropertiesDefinition::const_iterator it = defs.constBegin();
Expand All @@ -309,7 +310,7 @@ int QgsAuxiliaryLayer::propertyFromIndex( int index ) const
}
}
}
else if ( aDef.origin().compare( "symbol" ) == 0 )
else if ( aDef.origin().compare( QStringLiteral( "symbol" ) ) == 0 )
{
const QgsPropertiesDefinition defs = QgsSymbolLayer::propertyDefinitions();
QgsPropertiesDefinition::const_iterator it = defs.constBegin();
Expand All @@ -322,6 +323,19 @@ int QgsAuxiliaryLayer::propertyFromIndex( int index ) const
}
}
}
else if ( aDef.origin().compare( QStringLiteral( "diagram" ) ) == 0 )
{
const QgsPropertiesDefinition defs = QgsDiagramLayerSettings::propertyDefinitions();
QgsPropertiesDefinition::const_iterator it = defs.constBegin();
for ( ; it != defs.constEnd(); ++it )
{
if ( it->name().compare( aDef.name(), Qt::CaseInsensitive ) == 0 )
{
p = it.key();
break;
}
}
}

return p;
}
Expand Down Expand Up @@ -443,10 +457,13 @@ QgsPropertyDefinition QgsAuxiliaryLayer::propertyDefinitionFromField( const QgsF
}
}
}
else if ( origin.compare( "user", Qt::CaseInsensitive ) == 0 )
else
{
def.setOrigin( "user" );
def.setComment( propertyName );
def.setOrigin( origin );
def.setName( propertyName );

if ( parts.size() == 3 )
def.setComment( parts[2] );
}

return def;
Expand Down Expand Up @@ -475,12 +492,15 @@ QgsAuxiliaryStorage::QgsAuxiliaryStorage( const QgsProject &project, bool copy )
{
initTmpFileName();

const QFileInfo info = project.fileInfo();
const QString path = info.path() + QDir::separator() + info.baseName();
const QString asFileName = path + "." + QgsAuxiliaryStorage::extension();
mFileName = asFileName;
if ( !project.fileInfo().fileName().isEmpty() )
{
const QFileInfo info = project.fileInfo();
const QString path = info.path() + QDir::separator() + info.baseName();
const QString asFileName = path + "." + QgsAuxiliaryStorage::extension();
mFileName = asFileName;
}

sqlite3 *handler = open( asFileName );
sqlite3 *handler = open( mFileName );
close( handler );
}

Expand Down Expand Up @@ -551,6 +571,7 @@ QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &fi
}

alayer = new QgsAuxiliaryLayer( field.name(), currentFileName(), table, layer );
alayer->startEditing();
close( handler );
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -199,12 +199,12 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
* activate this property in settings.
*
* \param property The property to create
* \param providerId The id of the provider to use
* \param vlayer The vector layer
* \param providerId The id of the provider to use
*
* \returns The index of the auxiliary field or -1
*/
static int createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *vlayer );
static int createProperty( QgsPalLayerSettings::Property property, QgsVectorLayer *vlayer, const QString &providerId = QString() );

/**
* Creates if necessary a new auxiliary field for a diagram's property and
Expand Down
14 changes: 11 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -4269,15 +4269,19 @@ QString QgsVectorLayer::loadNamedStyle( const QString &theURI, bool &resultFlag
return loadNamedStyle( theURI, resultFlag, false );
}

bool QgsVectorLayer::loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage )
bool QgsVectorLayer::loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage, const QString &key )
{
bool rc = false;

if ( storage.isValid() && !mAuxiliaryLayerKey.isEmpty() )
QString joinKey = mAuxiliaryLayerKey;
if ( !key.isEmpty() )
joinKey = key;

if ( storage.isValid() && !joinKey.isEmpty() )
{
QgsAuxiliaryLayer *alayer = nullptr;

int idx = fields().lookupField( mAuxiliaryLayerKey );
int idx = fields().lookupField( joinKey );

if ( idx >= 0 )
{
Expand All @@ -4296,6 +4300,8 @@ bool QgsVectorLayer::loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage )

void QgsVectorLayer::setAuxiliaryLayer( QgsAuxiliaryLayer *alayer )
{
mAuxiliaryLayerKey.clear();

if ( mAuxiliaryLayer )
removeJoin( mAuxiliaryLayer->id() );

Expand All @@ -4305,6 +4311,8 @@ void QgsVectorLayer::setAuxiliaryLayer( QgsAuxiliaryLayer *alayer )

if ( !alayer->isEditable() )
alayer->startEditing();

mAuxiliaryLayerKey = alayer->joinInfo().targetFieldName();
}

mAuxiliaryLayer.reset( alayer );
Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayer.h
Expand Up @@ -788,15 +788,17 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/**
* Loads the auxiliary layer for this vector layer. If there's no
* corresponding table in the database, then nothing happens and false is
* returned.
* returned. The key is optional because if this layer has been read from
* a XML document, then the key read in this document is used by default.
*
* \param storage The auxiliary storage where to look for the table
* \param key The key to use for joining.
*
* \returns true if the auxiliary layer is well loaded, false otherwise
*
* \since QGIS 3.0
*/
bool loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage );
bool loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage, const QString &key = QString() );

/**
* Sets the current auxiliary layer. The auxiliary layer is automatically
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsnewauxiliaryfielddialog.cpp
Expand Up @@ -72,6 +72,7 @@ void QgsNewAuxiliaryFieldDialog::accept()
}

def.setOrigin( "user" );
def.setName( "custom" );
}

QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true );
Expand Down

0 comments on commit 3f8ae8b

Please sign in to comment.