Skip to content

Commit

Permalink
[FEATURE] Allow to specify custom prefix for joins
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jan 22, 2015
1 parent 2310046 commit 60712cd
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 37 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -103,6 +103,11 @@ struct QgsVectorJoinInfo

bool operator==( const QgsVectorJoinInfo& other ) const;

/** An optional prefix. If it is a Null string "{layername}_" will be used
* @note Added in 2.8
*/
QString prefix;

/** Set subset of fields to be used from joined layer. Takes ownership of the passed pointer. Null pointer tells to use all fields.
@note added in 2.6 */
void setJoinFieldNamesSubset( QStringList* fieldNamesSubset /Transfer/ );
Expand Down
22 changes: 14 additions & 8 deletions src/app/qgsaddjoindialog.cpp
Expand Up @@ -117,15 +117,21 @@ QStringList QgsAddJoinDialog::joinFieldsSubset() const
return lst;
}

bool QgsAddJoinDialog::hasCustomPrefix() const
{
return mUseCustomPrefix;
}

const QString QgsAddJoinDialog::customPrefix() const
{
return mCustomPrefix->text();
}

void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
{
mJoinFieldComboBox->clear();
QString layerId = mJoinLayerComboBox->itemData( index ).toString();
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( !layer )
{
return;
}
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>( layer );
if ( !vLayer )
{
Expand Down Expand Up @@ -157,9 +163,9 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
}

mJoinFieldsSubsetView->setModel( subsetModel );
}

void QgsAddJoinDialog::on_mUseJoinFieldsSubset_clicked()
{
mJoinFieldsSubsetView->setEnabled( mUseJoinFieldsSubset->isChecked() );
if ( !mUseCustomPrefix->isChecked() )
{
mCustomPrefix->setText( layer->name() + "_" );
}
}
19 changes: 11 additions & 8 deletions src/app/qgsaddjoindialog.h
Expand Up @@ -30,24 +30,27 @@ class APP_EXPORT QgsAddJoinDialog: public QDialog, private Ui::QgsAddJoinDialogB

//retrieve results

/**Get the id of the layer to join*/
/** Get the id of the layer to join*/
QString joinedLayerId() const;
/**Returns the name of the join field*/
/** Returns the name of the join field*/
QString joinFieldName() const;
/**Returns the name of the target field (join-to field)*/
/** Returns the name of the target field (join-to field)*/
QString targetFieldName() const;
/**True if joined layer should be cached in virtual memory*/
/** True if joined layer should be cached in virtual memory*/
bool cacheInMemory() const;
/**Returns true if user wants to create an attribute index on the join field*/
/** Returns true if user wants to create an attribute index on the join field*/
bool createAttributeIndex() const;
/**True if onle a subset of fields of joined layer should be used*/
/** True if onle a subset of fields of joined layer should be used*/
bool hasJoinFieldsSubset() const;
/**Return list of checked fields from joined layer to be used in join*/
/** Return list of checked fields from joined layer to be used in join*/
QStringList joinFieldsSubset() const;
/** Return if the user selected a custom prefix*/
bool hasCustomPrefix() const;
/** The custom prefix the user defined*/
const QString customPrefix() const;

private slots:
void on_mJoinLayerComboBox_currentIndexChanged( int index );
void on_mUseJoinFieldsSubset_clicked();

private:
/**Target layer*/
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -1042,6 +1042,10 @@ void QgsVectorLayerProperties::on_mButtonAddJoin_clicked()
joinLayer->dataProvider()->createAttributeIndex( joinLayer->pendingFields().indexFromName( info.joinFieldName ) );
}
}
if ( d.hasCustomPrefix() )
info.prefix = d.customPrefix();
else
info.prefix = QString::null;

layer->addJoin( info );
addJoinToTreeWidget( info );
Expand Down
24 changes: 15 additions & 9 deletions src/core/qgsvectorlayer.h
Expand Up @@ -164,31 +164,37 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement

struct CORE_EXPORT QgsVectorJoinInfo
{
/**Join field in the target layer*/
/** Join field in the target layer*/
QString targetFieldName;
/**Source layer*/
/** Source layer*/
QString joinLayerId;
/**Join field in the source layer*/
/** Join field in the source layer*/
QString joinFieldName;
/**True if the join is cached in virtual memory*/
/** True if the join is cached in virtual memory*/
bool memoryCache;
/**Cache for joined attributes to provide fast lookup (size is 0 if no memory caching)
/** Cache for joined attributes to provide fast lookup (size is 0 if no memory caching)
@note not available in python bindings
*/
QHash< QString, QgsAttributes> cachedAttributes;

/**Join field index in the target layer. For backward compatibility with 1.x (x>=7)*/
/** Join field index in the target layer. For backward compatibility with 1.x (x>=7)*/
int targetFieldIndex;
/**Join field index in the source layer. For backward compatibility with 1.x (x>=7)*/
/** Join field index in the source layer. For backward compatibility with 1.x (x>=7)*/
int joinFieldIndex;

/** An optional prefix. If it is a Null string "{layername}_" will be used
* @note Added in 2.8
*/
QString prefix;

bool operator==( const QgsVectorJoinInfo& other ) const
{
return targetFieldName == other.targetFieldName &&
joinLayerId == other.joinLayerId &&
joinFieldName == other.joinFieldName &&
joinFieldsSubset == other.joinFieldsSubset &&
memoryCache == other.memoryCache;
memoryCache == other.memoryCache &&
prefix == other.prefix;
}

/** Set subset of fields to be used from joined layer. Takes ownership of the passed pointer. Null pointer tells to use all fields.
Expand All @@ -199,7 +205,7 @@ struct CORE_EXPORT QgsVectorJoinInfo
QStringList* joinFieldNamesSubset() const { return joinFieldsSubset.data(); }

protected:
/**Subset of fields to use from joined layer. null = use all fields*/
/** Subset of fields to use from joined layer. null = use all fields*/
QSharedPointer<QStringList> joinFieldsSubset;
};

Expand Down
24 changes: 23 additions & 1 deletion src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -195,6 +195,8 @@ QVector<int> QgsVectorLayerJoinBuffer::joinSubsetIndices( QgsVectorLayer* joinLa

void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
{
QString prefix;

QList< QgsVectorJoinInfo>::const_iterator joinIt = mVectorJoins.constBegin();
for ( int joinIdx = 0 ; joinIt != mVectorJoins.constEnd(); ++joinIt, ++joinIdx )
{
Expand All @@ -219,6 +221,15 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
subset = QSet<QString>::fromList( *joinIt->joinFieldNamesSubset() );
}

if ( joinIt->prefix.isNull() )
{
prefix = joinLayer->name() + "_";
}
else
{
prefix = joinIt->prefix;
}

for ( int idx = 0; idx < joinFields.count(); ++idx )
{
// if using just a subset of fields, filter some of them out
Expand All @@ -229,7 +240,7 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
if ( joinFields[idx].name() != joinFieldName )
{
QgsField f = joinFields[idx];
f.setName( joinLayer->name() + "_" + f.name() );
f.setName( prefix + f.name() );
fields.append( f, QgsFields::OriginJoin, idx + ( joinIdx*1000 ) );
}
}
Expand Down Expand Up @@ -285,6 +296,12 @@ void QgsVectorLayerJoinBuffer::writeXml( QDomNode& layer_node, QDomDocument& doc
joinElem.appendChild( subsetElem );
}

if ( !joinIt->prefix.isNull() )
{
joinElem.setAttribute( "customPrefix", joinIt->prefix );
joinElem.setAttribute( "hasCustomPrefix", 1 );
}

vectorJoinsElem.appendChild( joinElem );
}
}
Expand Down Expand Up @@ -318,6 +335,11 @@ void QgsVectorLayerJoinBuffer::readXml( const QDomNode& layer_node )
info.setJoinFieldNamesSubset( fieldNames );
}

if ( infoElem.attribute( "hasCustomPrefix" ).toInt() )
info.prefix = infoElem.attribute( "customPrefix" );
else
info.prefix = QString::null;

addJoin( info );
}
}
Expand Down
73 changes: 62 additions & 11 deletions src/ui/qgsaddjoindialogbase.ui
Expand Up @@ -6,14 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>301</width>
<height>327</height>
<width>505</width>
<height>460</height>
</rect>
</property>
<property name="windowTitle">
<string>Add vector join</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="mJoinLayerLabel">
<property name="text">
Expand Down Expand Up @@ -44,7 +44,7 @@
<item row="2" column="1">
<widget class="QComboBox" name="mTargetFieldComboBox"/>
</item>
<item row="3" column="0" colspan="2">
<item row="3" column="0">
<widget class="QCheckBox" name="mCacheInMemoryCheckBox">
<property name="text">
<string>Cache join layer in virtual memory</string>
Expand All @@ -59,20 +59,52 @@
</widget>
</item>
<item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="mUseJoinFieldsSubset">
<property name="text">
<widget class="QgsCollapsibleGroupBox" name="mUseJoinFieldsSubset">
<property name="title">
<string>Choose which fields are joined</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="collapsed">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QListView" name="mJoinFieldsSubsetView">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="6" column="0" colspan="2">
<widget class="QListView" name="mJoinFieldsSubsetView">
<property name="enabled">
<widget class="QgsCollapsibleGroupBox" name="mUseCustomPrefix">
<property name="title">
<string>Custom field name prefix</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="collapsed">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLineEdit" name="mCustomPrefix"/>
</item>
</layout>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -82,16 +114,35 @@
</property>
</widget>
</item>
<item row="7" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mJoinLayerComboBox</tabstop>
<tabstop>mJoinFieldComboBox</tabstop>
<tabstop>mTargetFieldComboBox</tabstop>
<tabstop>mCacheInMemoryCheckBox</tabstop>
<tabstop>mCreateIndexCheckBox</tabstop>
<tabstop>mUseJoinFieldsSubset</tabstop>
<tabstop>mJoinFieldsSubsetView</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
Expand Down

0 comments on commit 60712cd

Please sign in to comment.