Skip to content

Commit b4533cd

Browse files
committedNov 19, 2016
Use standard map layer and field combo boxes in relation add dialog
1 parent cac8de5 commit b4533cd

File tree

4 files changed

+36
-61
lines changed

4 files changed

+36
-61
lines changed
 

‎src/app/qgsrelationadddlg.cpp

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgsrelationadddlg.h"
1818
#include "qgsvectorlayer.h"
19+
#include "qgsmaplayerproxymodel.h"
1920

2021
#include <QPushButton>
2122

@@ -24,45 +25,41 @@ QgsRelationAddDlg::QgsRelationAddDlg( QWidget *parent )
2425
{
2526
setupUi( this );
2627

27-
mTxtRelationId->setPlaceholderText( tr( "[Generated automatically]" ) );
28-
checkDefinitionValid();
28+
connect( mCbxReferencingLayer, &QgsMapLayerComboBox::layerChanged, this, [=]( QgsMapLayer * layer ) { mCbxReferencingField->setLayer( layer ); }
29+
);
30+
connect( mCbxReferencedLayer, &QgsMapLayerComboBox::layerChanged, this, [=]( QgsMapLayer * layer ) { mCbxReferencedField->setLayer( layer ); }
31+
);
2932

30-
connect( mCbxReferencingLayer, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
31-
connect( mCbxReferencingField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
32-
connect( mCbxReferencedLayer, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
33-
connect( mCbxReferencedField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
34-
}
35-
36-
void QgsRelationAddDlg::addLayers( const QList< QgsVectorLayer* >& layers )
37-
{
38-
mCbxReferencingLayer->addItem( QLatin1String( "" ), "" );
39-
mCbxReferencedLayer->addItem( QLatin1String( "" ), "" );
33+
mCbxReferencingLayer->setFilters( QgsMapLayerProxyModel::VectorLayer );
34+
mCbxReferencingField->setLayer( mCbxReferencingLayer->currentLayer() );
35+
mCbxReferencedLayer->setFilters( QgsMapLayerProxyModel::VectorLayer );
36+
mCbxReferencedField->setLayer( mCbxReferencedLayer->currentLayer() );
4037

41-
Q_FOREACH ( QgsVectorLayer* layer, layers )
42-
{
43-
mCbxReferencingLayer->addItem( layer->name(), layer->id() );
44-
mCbxReferencedLayer->addItem( layer->name(), layer->id() );
38+
mTxtRelationId->setPlaceholderText( tr( "[Generated automatically]" ) );
39+
checkDefinitionValid();
4540

46-
mLayers.insert( layer->id(), layer );
47-
}
41+
connect( mCbxReferencingLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
42+
connect( mCbxReferencingField, &QgsFieldComboBox::fieldChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
43+
connect( mCbxReferencedLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
44+
connect( mCbxReferencedField, &QgsFieldComboBox::fieldChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
4845
}
4946

5047
QString QgsRelationAddDlg::referencingLayerId()
5148
{
52-
return mCbxReferencingLayer->currentData().toString();
49+
return mCbxReferencingLayer->currentLayer()->id();
5350
}
5451

5552
QString QgsRelationAddDlg::referencedLayerId()
5653
{
57-
return mCbxReferencedLayer->currentData().toString();
54+
return mCbxReferencedLayer->currentLayer()->id();
5855
}
5956

6057
QList< QPair< QString, QString > > QgsRelationAddDlg::references()
6158
{
6259
QList< QPair< QString, QString > > references;
6360

64-
QString referencingField = mCbxReferencingField->currentData().toString();
65-
QString referencedField = mCbxReferencedField->currentData().toString();
61+
QString referencingField = mCbxReferencingField->currentField();
62+
QString referencedField = mCbxReferencedField->currentField();
6663

6764
references.append( QPair<QString, QString> ( referencingField, referencedField ) );
6865

@@ -79,15 +76,6 @@ QString QgsRelationAddDlg::relationName()
7976
return mTxtRelationName->text();
8077
}
8178

82-
void QgsRelationAddDlg::on_mCbxReferencingLayer_currentIndexChanged( int index )
83-
{
84-
loadLayerAttributes( mCbxReferencingField, mLayers[mCbxReferencingLayer->itemData( index ).toString()] );
85-
}
86-
87-
void QgsRelationAddDlg::on_mCbxReferencedLayer_currentIndexChanged( int index )
88-
{
89-
loadLayerAttributes( mCbxReferencedField, mLayers[mCbxReferencedLayer->itemData( index ).toString()] );
90-
}
9179

9280
void QgsRelationAddDlg::checkDefinitionValid()
9381
{
@@ -96,18 +84,3 @@ void QgsRelationAddDlg::checkDefinitionValid()
9684
&& mCbxReferencingLayer->currentIndex() != -1
9785
&& mCbxReferencingField->currentIndex() != -1 );
9886
}
99-
100-
void QgsRelationAddDlg::loadLayerAttributes( QComboBox* cbx, QgsVectorLayer* layer )
101-
{
102-
cbx->clear();
103-
104-
if ( !layer )
105-
{
106-
return;
107-
}
108-
109-
Q_FOREACH ( const QgsField& fld, layer->fields().toList() )
110-
{
111-
cbx->addItem( fld.name(), fld.name() );
112-
}
113-
}

‎src/app/qgsrelationadddlg.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class APP_EXPORT QgsRelationAddDlg : public QDialog, private Ui::QgsRelationAddD
2727
public:
2828
explicit QgsRelationAddDlg( QWidget *parent = nullptr );
2929

30-
void addLayers( const QList<QgsVectorLayer*>& layers );
31-
3230
QString referencingLayerId();
3331
QString referencedLayerId();
3432
QList< QPair< QString, QString > > references();
@@ -37,16 +35,9 @@ class APP_EXPORT QgsRelationAddDlg : public QDialog, private Ui::QgsRelationAddD
3735

3836

3937
private slots:
40-
void on_mCbxReferencingLayer_currentIndexChanged( int index );
41-
void on_mCbxReferencedLayer_currentIndexChanged( int index );
4238

4339
void checkDefinitionValid();
4440

45-
private:
46-
void loadLayerAttributes( QComboBox* cbx, QgsVectorLayer* layer );
47-
48-
QMap< QString, QgsVectorLayer* > mLayers;
49-
5041
};
5142

5243
#endif // QGSRELATIONADDDLG_H

‎src/app/qgsrelationmanagerdialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ void QgsRelationManagerDialog::addRelation( const QgsRelation &rel )
8484
void QgsRelationManagerDialog::on_mBtnAddRelation_clicked()
8585
{
8686
QgsRelationAddDlg addDlg;
87-
addDlg.addLayers( mLayers );
8887

8988
if ( addDlg.exec() )
9089
{
@@ -164,4 +163,4 @@ QList< QgsRelation > QgsRelationManagerDialog::relations()
164163
void QgsRelationManagerDialog::onSelectionChanged()
165164
{
166165
mBtnRemoveRelation->setEnabled( mRelationsTable->selectionModel()->hasSelection() );
167-
}
166+
}

‎src/ui/qgsrelationadddlgbase.ui

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</widget>
2323
</item>
2424
<item row="4" column="1" colspan="2">
25-
<widget class="QComboBox" name="mCbxReferencingLayer"/>
25+
<widget class="QgsMapLayerComboBox" name="mCbxReferencingLayer"/>
2626
</item>
2727
<item row="9" column="0">
2828
<widget class="QLabel" name="label_5">
@@ -49,7 +49,7 @@
4949
</widget>
5050
</item>
5151
<item row="5" column="1" colspan="2">
52-
<widget class="QComboBox" name="mCbxReferencingField"/>
52+
<widget class="QgsFieldComboBox" name="mCbxReferencingField"/>
5353
</item>
5454
<item row="5" column="0">
5555
<widget class="QLabel" name="label_2">
@@ -73,19 +73,31 @@
7373
</widget>
7474
</item>
7575
<item row="3" column="1" colspan="2">
76-
<widget class="QComboBox" name="mCbxReferencedField"/>
76+
<widget class="QgsFieldComboBox" name="mCbxReferencedField"/>
7777
</item>
7878
<item row="9" column="1" colspan="2">
7979
<widget class="QLineEdit" name="mTxtRelationId"/>
8080
</item>
8181
<item row="2" column="1" colspan="2">
82-
<widget class="QComboBox" name="mCbxReferencedLayer"/>
82+
<widget class="QgsMapLayerComboBox" name="mCbxReferencedLayer"/>
8383
</item>
8484
<item row="1" column="1" colspan="2">
8585
<widget class="QLineEdit" name="mTxtRelationName"/>
8686
</item>
8787
</layout>
8888
</widget>
89+
<customwidgets>
90+
<customwidget>
91+
<class>QgsMapLayerComboBox</class>
92+
<extends>QComboBox</extends>
93+
<header location="global">qgsmaplayercombobox.h</header>
94+
</customwidget>
95+
<customwidget>
96+
<class>QgsFieldComboBox</class>
97+
<extends>QComboBox</extends>
98+
<header location="global">qgsfieldcombobox.h</header>
99+
</customwidget>
100+
</customwidgets>
89101
<tabstops>
90102
<tabstop>mTxtRelationName</tabstop>
91103
<tabstop>mCbxReferencedLayer</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.