Skip to content

Commit 9945a6d

Browse files
committedSep 13, 2014
[FEATURE] Joins: optionally use just a subset of fields from the joined layer
1 parent ffcd07b commit 9945a6d

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed
 

‎src/app/qgsaddjoindialog.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "qgsvectordataprovider.h"
2222
#include "qgsvectorlayer.h"
2323

24+
#include <QStandardItemModel>
25+
2426
QgsAddJoinDialog::QgsAddJoinDialog( QgsVectorLayer* layer, QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mLayer( layer )
2527
{
2628
setupUi( this );
@@ -94,6 +96,27 @@ bool QgsAddJoinDialog::createAttributeIndex() const
9496
return mCreateIndexCheckBox->isChecked();
9597
}
9698

99+
bool QgsAddJoinDialog::hasJoinFieldsSubset() const
100+
{
101+
return mUseJoinFieldsSubset->isChecked();
102+
}
103+
104+
QStringList QgsAddJoinDialog::joinFieldsSubset() const
105+
{
106+
QStringList lst;
107+
QAbstractItemModel* model = mJoinFieldsSubsetView->model();
108+
if ( !model )
109+
return lst;
110+
111+
for ( int i = 0; i < model->rowCount(); ++i )
112+
{
113+
QModelIndex index = model->index( i, 0 );
114+
if ( model->data( index, Qt::CheckStateRole ).toInt() == Qt::Checked )
115+
lst << model->data( index ).toString();
116+
}
117+
return lst;
118+
}
119+
97120
void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
98121
{
99122
mJoinFieldComboBox->clear();
@@ -109,10 +132,16 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
109132
return;
110133
}
111134

135+
QStandardItemModel* subsetModel = new QStandardItemModel( this );
136+
112137
const QgsFields& layerFields = vLayer->pendingFields();
113138
for ( int idx = 0; idx < layerFields.count(); ++idx )
114139
{
115140
mJoinFieldComboBox->addItem( layerFields[idx].name(), idx );
141+
QStandardItem* subsetItem = new QStandardItem( layerFields[idx].name() );
142+
subsetItem->setCheckable( true );
143+
//subsetItem->setFlags( subsetItem->flags() | Qt::ItemIsUserCheckable );
144+
subsetModel->appendRow( subsetItem );
116145
}
117146

118147
//does provider support creation of attribute indices?
@@ -126,4 +155,11 @@ void QgsAddJoinDialog::on_mJoinLayerComboBox_currentIndexChanged( int index )
126155
mCreateIndexCheckBox->setEnabled( false );
127156
mCreateIndexCheckBox->setChecked( false );
128157
}
158+
159+
mJoinFieldsSubsetView->setModel( subsetModel );
160+
}
161+
162+
void QgsAddJoinDialog::on_mUseJoinFieldsSubset_clicked()
163+
{
164+
mJoinFieldsSubsetView->setEnabled( mUseJoinFieldsSubset->isChecked() );
129165
}

‎src/app/qgsaddjoindialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ class APP_EXPORT QgsAddJoinDialog: public QDialog, private Ui::QgsAddJoinDialogB
4040
bool cacheInMemory() const;
4141
/**Returns true if user wants to create an attribute index on the join field*/
4242
bool createAttributeIndex() const;
43+
/**True if onle a subset of fields of joined layer should be used*/
44+
bool hasJoinFieldsSubset() const;
45+
/**Return list of checked fields from joined layer to be used in join*/
46+
QStringList joinFieldsSubset() const;
4347

4448
private slots:
4549
void on_mJoinLayerComboBox_currentIndexChanged( int index );
50+
void on_mUseJoinFieldsSubset_clicked();
4651

4752
private:
4853
/**Target layer*/

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,8 @@ void QgsVectorLayerProperties::on_mButtonAddJoin_clicked()
980980
info.joinLayerId = d.joinedLayerId();
981981
info.joinFieldName = d.joinFieldName();
982982
info.memoryCache = d.cacheInMemory();
983+
if ( d.hasJoinFieldsSubset() )
984+
info.setJoinFieldNamesSubset( new QStringList( d.joinFieldsSubset() ) );
983985
if ( layer )
984986
{
985987
//create attribute index if possible

‎src/ui/qgsaddjoindialogbase.ui

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>281</width>
10-
<height>160</height>
9+
<width>301</width>
10+
<height>327</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -44,13 +44,10 @@
4444
<item row="2" column="1">
4545
<widget class="QComboBox" name="mTargetFieldComboBox"/>
4646
</item>
47-
<item row="5" column="0" colspan="2">
48-
<widget class="QDialogButtonBox" name="buttonBox">
49-
<property name="orientation">
50-
<enum>Qt::Horizontal</enum>
51-
</property>
52-
<property name="standardButtons">
53-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
47+
<item row="3" column="0" colspan="2">
48+
<widget class="QCheckBox" name="mCacheInMemoryCheckBox">
49+
<property name="text">
50+
<string>Cache join layer in virtual memory</string>
5451
</property>
5552
</widget>
5653
</item>
@@ -61,15 +58,42 @@
6158
</property>
6259
</widget>
6360
</item>
64-
<item row="3" column="0" colspan="2">
65-
<widget class="QCheckBox" name="mCacheInMemoryCheckBox">
61+
<item row="5" column="0" colspan="2">
62+
<widget class="QCheckBox" name="mUseJoinFieldsSubset">
6663
<property name="text">
67-
<string>Cache join layer in virtual memory</string>
64+
<string>Choose which fields are joined</string>
65+
</property>
66+
</widget>
67+
</item>
68+
<item row="6" column="0" colspan="2">
69+
<widget class="QListView" name="mJoinFieldsSubsetView">
70+
<property name="enabled">
71+
<bool>false</bool>
72+
</property>
73+
</widget>
74+
</item>
75+
<item row="7" column="0" colspan="2">
76+
<widget class="QDialogButtonBox" name="buttonBox">
77+
<property name="orientation">
78+
<enum>Qt::Horizontal</enum>
79+
</property>
80+
<property name="standardButtons">
81+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
6882
</property>
6983
</widget>
7084
</item>
7185
</layout>
7286
</widget>
87+
<tabstops>
88+
<tabstop>mJoinLayerComboBox</tabstop>
89+
<tabstop>mJoinFieldComboBox</tabstop>
90+
<tabstop>mTargetFieldComboBox</tabstop>
91+
<tabstop>mCacheInMemoryCheckBox</tabstop>
92+
<tabstop>mCreateIndexCheckBox</tabstop>
93+
<tabstop>mUseJoinFieldsSubset</tabstop>
94+
<tabstop>mJoinFieldsSubsetView</tabstop>
95+
<tabstop>buttonBox</tabstop>
96+
</tabstops>
7397
<resources/>
7498
<connections>
7599
<connection>
@@ -79,8 +103,8 @@
79103
<slot>accept()</slot>
80104
<hints>
81105
<hint type="sourcelabel">
82-
<x>248</x>
83-
<y>254</y>
106+
<x>252</x>
107+
<y>322</y>
84108
</hint>
85109
<hint type="destinationlabel">
86110
<x>157</x>
@@ -95,8 +119,8 @@
95119
<slot>reject()</slot>
96120
<hints>
97121
<hint type="sourcelabel">
98-
<x>316</x>
99-
<y>260</y>
122+
<x>296</x>
123+
<y>322</y>
100124
</hint>
101125
<hint type="destinationlabel">
102126
<x>286</x>

0 commit comments

Comments
 (0)
Please sign in to comment.