Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Joins: optionally use just a subset of fields from the join…
…ed layer
  • Loading branch information
wonder-sk committed Sep 13, 2014
1 parent ffcd07b commit 9945a6d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
36 changes: 36 additions & 0 deletions src/app/qgsaddjoindialog.cpp
Expand Up @@ -21,6 +21,8 @@
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"

#include <QStandardItemModel>

QgsAddJoinDialog::QgsAddJoinDialog( QgsVectorLayer* layer, QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mLayer( layer )
{
setupUi( this );
Expand Down Expand Up @@ -94,6 +96,27 @@ bool QgsAddJoinDialog::createAttributeIndex() const
return mCreateIndexCheckBox->isChecked();
}

bool QgsAddJoinDialog::hasJoinFieldsSubset() const
{
return mUseJoinFieldsSubset->isChecked();
}

QStringList QgsAddJoinDialog::joinFieldsSubset() const
{
QStringList lst;
QAbstractItemModel* model = mJoinFieldsSubsetView->model();
if ( !model )
return lst;

for ( int i = 0; i < model->rowCount(); ++i )
{
QModelIndex index = model->index( i, 0 );
if ( model->data( index, Qt::CheckStateRole ).toInt() == Qt::Checked )
lst << model->data( index ).toString();
}
return lst;
}

void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
{
mJoinFieldComboBox->clear();
Expand All @@ -109,10 +132,16 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
return;
}

QStandardItemModel* subsetModel = new QStandardItemModel( this );

const QgsFields& layerFields = vLayer->pendingFields();
for ( int idx = 0; idx < layerFields.count(); ++idx )
{
mJoinFieldComboBox->addItem( layerFields[idx].name(), idx );
QStandardItem* subsetItem = new QStandardItem( layerFields[idx].name() );
subsetItem->setCheckable( true );
//subsetItem->setFlags( subsetItem->flags() | Qt::ItemIsUserCheckable );
subsetModel->appendRow( subsetItem );
}

//does provider support creation of attribute indices?
Expand All @@ -126,4 +155,11 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
mCreateIndexCheckBox->setEnabled( false );
mCreateIndexCheckBox->setChecked( false );
}

mJoinFieldsSubsetView->setModel( subsetModel );
}

void QgsAddJoinDialog::on_mUseJoinFieldsSubset_clicked()
{
mJoinFieldsSubsetView->setEnabled( mUseJoinFieldsSubset->isChecked() );
}
5 changes: 5 additions & 0 deletions src/app/qgsaddjoindialog.h
Expand Up @@ -40,9 +40,14 @@ class APP_EXPORT QgsAddJoinDialog: public QDialog, private Ui::QgsAddJoinDialogB
bool cacheInMemory() const;
/**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*/
bool hasJoinFieldsSubset() const;
/**Return list of checked fields from joined layer to be used in join*/
QStringList joinFieldsSubset() const;

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

private:
/**Target layer*/
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -980,6 +980,8 @@ void QgsVectorLayerProperties::on_mButtonAddJoin_clicked()
info.joinLayerId = d.joinedLayerId();
info.joinFieldName = d.joinFieldName();
info.memoryCache = d.cacheInMemory();
if ( d.hasJoinFieldsSubset() )
info.setJoinFieldNamesSubset( new QStringList( d.joinFieldsSubset() ) );
if ( layer )
{
//create attribute index if possible
Expand Down
56 changes: 40 additions & 16 deletions src/ui/qgsaddjoindialogbase.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>281</width>
<height>160</height>
<width>301</width>
<height>327</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -44,13 +44,10 @@
<item row="2" column="1">
<widget class="QComboBox" name="mTargetFieldComboBox"/>
</item>
<item row="5" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="mCacheInMemoryCheckBox">
<property name="text">
<string>Cache join layer in virtual memory</string>
</property>
</widget>
</item>
Expand All @@ -61,15 +58,42 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="mCacheInMemoryCheckBox">
<item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="mUseJoinFieldsSubset">
<property name="text">
<string>Cache join layer in virtual memory</string>
<string>Choose which fields are joined</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<widget class="QListView" name="mJoinFieldsSubsetView">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<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/>
<connections>
<connection>
Expand All @@ -79,8 +103,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
<x>252</x>
<y>322</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
Expand All @@ -95,8 +119,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
<x>296</x>
<y>322</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
Expand Down

0 comments on commit 9945a6d

Please sign in to comment.