Skip to content

Commit

Permalink
[FEATURE] Joins: optionally use just a subset of fields from the join…
Browse files Browse the repository at this point in the history
…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 "qgsvectordataprovider.h"
#include "qgsvectorlayer.h" #include "qgsvectorlayer.h"


#include <QStandardItemModel>

QgsAddJoinDialog::QgsAddJoinDialog( QgsVectorLayer* layer, QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mLayer( layer ) QgsAddJoinDialog::QgsAddJoinDialog( QgsVectorLayer* layer, QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mLayer( layer )
{ {
setupUi( this ); setupUi( this );
Expand Down Expand Up @@ -94,6 +96,27 @@ bool QgsAddJoinDialog::createAttributeIndex() const
return mCreateIndexCheckBox->isChecked(); 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 ) void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
{ {
mJoinFieldComboBox->clear(); mJoinFieldComboBox->clear();
Expand All @@ -109,10 +132,16 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
return; return;
} }


QStandardItemModel* subsetModel = new QStandardItemModel( this );

const QgsFields& layerFields = vLayer->pendingFields(); const QgsFields& layerFields = vLayer->pendingFields();
for ( int idx = 0; idx < layerFields.count(); ++idx ) for ( int idx = 0; idx < layerFields.count(); ++idx )
{ {
mJoinFieldComboBox->addItem( layerFields[idx].name(), 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? //does provider support creation of attribute indices?
Expand All @@ -126,4 +155,11 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
mCreateIndexCheckBox->setEnabled( false ); mCreateIndexCheckBox->setEnabled( false );
mCreateIndexCheckBox->setChecked( 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; 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; 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: private slots:
void on_mJoinLayerComboBox_currentIndexChanged( int index ); void on_mJoinLayerComboBox_currentIndexChanged( int index );
void on_mUseJoinFieldsSubset_clicked();


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

0 comments on commit 9945a6d

Please sign in to comment.