Skip to content

Commit 2270603

Browse files
authored
Merge pull request #3788 from nyalldawson/rel_dialog
Add relation dialog improvements
2 parents 9679b6a + b4533cd commit 2270603

File tree

4 files changed

+72
-98
lines changed

4 files changed

+72
-98
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: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,86 +14,97 @@
1414
<string>Add relation</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17-
<item row="4" column="1" colspan="2">
18-
<widget class="QComboBox" name="mCbxReferencingField"/>
19-
</item>
20-
<item row="3" column="1" colspan="2">
21-
<widget class="QComboBox" name="mCbxReferencingLayer"/>
22-
</item>
23-
<item row="4" column="0">
24-
<widget class="QLabel" name="label_2">
17+
<item row="1" column="0">
18+
<widget class="QLabel" name="label_7">
2519
<property name="text">
26-
<string>Referencing Field</string>
20+
<string>Name</string>
2721
</property>
2822
</widget>
2923
</item>
30-
<item row="5" column="0">
31-
<widget class="QLabel" name="label_3">
24+
<item row="4" column="1" colspan="2">
25+
<widget class="QgsMapLayerComboBox" name="mCbxReferencingLayer"/>
26+
</item>
27+
<item row="9" column="0">
28+
<widget class="QLabel" name="label_5">
3229
<property name="text">
33-
<string>Referenced Layer (Parent)</string>
30+
<string>Id</string>
3431
</property>
3532
</widget>
3633
</item>
37-
<item row="6" column="0">
38-
<widget class="QLabel" name="label_4">
39-
<property name="text">
40-
<string>Referenced Field</string>
34+
<item row="15" column="0" colspan="3">
35+
<widget class="QDialogButtonBox" name="mButtonBox">
36+
<property name="orientation">
37+
<enum>Qt::Horizontal</enum>
38+
</property>
39+
<property name="standardButtons">
40+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
4141
</property>
4242
</widget>
4343
</item>
44-
<item row="5" column="1" colspan="2">
45-
<widget class="QComboBox" name="mCbxReferencedLayer"/>
46-
</item>
47-
<item row="6" column="1" colspan="2">
48-
<widget class="QComboBox" name="mCbxReferencedField"/>
49-
</item>
50-
<item row="3" column="0">
44+
<item row="4" column="0">
5145
<widget class="QLabel" name="label">
5246
<property name="text">
5347
<string>Referencing Layer (Child)</string>
5448
</property>
5549
</widget>
5650
</item>
57-
<item row="1" column="0">
58-
<widget class="QLabel" name="label_7">
51+
<item row="5" column="1" colspan="2">
52+
<widget class="QgsFieldComboBox" name="mCbxReferencingField"/>
53+
</item>
54+
<item row="5" column="0">
55+
<widget class="QLabel" name="label_2">
5956
<property name="text">
60-
<string>Name</string>
57+
<string>Referencing Field</string>
6158
</property>
6259
</widget>
6360
</item>
64-
<item row="14" column="0" colspan="3">
65-
<widget class="QDialogButtonBox" name="mButtonBox">
66-
<property name="orientation">
67-
<enum>Qt::Horizontal</enum>
68-
</property>
69-
<property name="standardButtons">
70-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
61+
<item row="2" column="0">
62+
<widget class="QLabel" name="label_3">
63+
<property name="text">
64+
<string>Referenced Layer (Parent)</string>
7165
</property>
7266
</widget>
7367
</item>
74-
<item row="8" column="0">
75-
<widget class="QLabel" name="label_5">
68+
<item row="3" column="0">
69+
<widget class="QLabel" name="label_4">
7670
<property name="text">
77-
<string>Id</string>
71+
<string>Referenced Field</string>
7872
</property>
7973
</widget>
8074
</item>
81-
<item row="8" column="1">
75+
<item row="3" column="1" colspan="2">
76+
<widget class="QgsFieldComboBox" name="mCbxReferencedField"/>
77+
</item>
78+
<item row="9" column="1" colspan="2">
8279
<widget class="QLineEdit" name="mTxtRelationId"/>
8380
</item>
84-
<item row="1" column="1">
81+
<item row="2" column="1" colspan="2">
82+
<widget class="QgsMapLayerComboBox" name="mCbxReferencedLayer"/>
83+
</item>
84+
<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>
91-
<tabstop>mCbxReferencingLayer</tabstop>
92-
<tabstop>mCbxReferencingField</tabstop>
93103
<tabstop>mCbxReferencedLayer</tabstop>
94104
<tabstop>mCbxReferencedField</tabstop>
105+
<tabstop>mCbxReferencingLayer</tabstop>
106+
<tabstop>mCbxReferencingField</tabstop>
95107
<tabstop>mTxtRelationId</tabstop>
96-
<tabstop>mButtonBox</tabstop>
97108
</tabstops>
98109
<resources/>
99110
<connections>

0 commit comments

Comments
 (0)