Skip to content

Commit

Permalink
Use standard map layer and field combo boxes in relation add dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 19, 2016
1 parent cac8de5 commit b4533cd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 61 deletions.
65 changes: 19 additions & 46 deletions src/app/qgsrelationadddlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "qgsrelationadddlg.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayerproxymodel.h"

#include <QPushButton>

Expand All @@ -24,45 +25,41 @@ QgsRelationAddDlg::QgsRelationAddDlg( QWidget *parent )
{
setupUi( this );

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

connect( mCbxReferencingLayer, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
connect( mCbxReferencingField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
connect( mCbxReferencedLayer, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
connect( mCbxReferencedField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( checkDefinitionValid() ) );
}

void QgsRelationAddDlg::addLayers( const QList< QgsVectorLayer* >& layers )
{
mCbxReferencingLayer->addItem( QLatin1String( "" ), "" );
mCbxReferencedLayer->addItem( QLatin1String( "" ), "" );
mCbxReferencingLayer->setFilters( QgsMapLayerProxyModel::VectorLayer );
mCbxReferencingField->setLayer( mCbxReferencingLayer->currentLayer() );
mCbxReferencedLayer->setFilters( QgsMapLayerProxyModel::VectorLayer );
mCbxReferencedField->setLayer( mCbxReferencedLayer->currentLayer() );

Q_FOREACH ( QgsVectorLayer* layer, layers )
{
mCbxReferencingLayer->addItem( layer->name(), layer->id() );
mCbxReferencedLayer->addItem( layer->name(), layer->id() );
mTxtRelationId->setPlaceholderText( tr( "[Generated automatically]" ) );
checkDefinitionValid();

mLayers.insert( layer->id(), layer );
}
connect( mCbxReferencingLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
connect( mCbxReferencingField, &QgsFieldComboBox::fieldChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
connect( mCbxReferencedLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
connect( mCbxReferencedField, &QgsFieldComboBox::fieldChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
}

QString QgsRelationAddDlg::referencingLayerId()
{
return mCbxReferencingLayer->currentData().toString();
return mCbxReferencingLayer->currentLayer()->id();
}

QString QgsRelationAddDlg::referencedLayerId()
{
return mCbxReferencedLayer->currentData().toString();
return mCbxReferencedLayer->currentLayer()->id();
}

QList< QPair< QString, QString > > QgsRelationAddDlg::references()
{
QList< QPair< QString, QString > > references;

QString referencingField = mCbxReferencingField->currentData().toString();
QString referencedField = mCbxReferencedField->currentData().toString();
QString referencingField = mCbxReferencingField->currentField();
QString referencedField = mCbxReferencedField->currentField();

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

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

void QgsRelationAddDlg::on_mCbxReferencingLayer_currentIndexChanged( int index )
{
loadLayerAttributes( mCbxReferencingField, mLayers[mCbxReferencingLayer->itemData( index ).toString()] );
}

void QgsRelationAddDlg::on_mCbxReferencedLayer_currentIndexChanged( int index )
{
loadLayerAttributes( mCbxReferencedField, mLayers[mCbxReferencedLayer->itemData( index ).toString()] );
}

void QgsRelationAddDlg::checkDefinitionValid()
{
Expand All @@ -96,18 +84,3 @@ void QgsRelationAddDlg::checkDefinitionValid()
&& mCbxReferencingLayer->currentIndex() != -1
&& mCbxReferencingField->currentIndex() != -1 );
}

void QgsRelationAddDlg::loadLayerAttributes( QComboBox* cbx, QgsVectorLayer* layer )
{
cbx->clear();

if ( !layer )
{
return;
}

Q_FOREACH ( const QgsField& fld, layer->fields().toList() )
{
cbx->addItem( fld.name(), fld.name() );
}
}
9 changes: 0 additions & 9 deletions src/app/qgsrelationadddlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class APP_EXPORT QgsRelationAddDlg : public QDialog, private Ui::QgsRelationAddD
public:
explicit QgsRelationAddDlg( QWidget *parent = nullptr );

void addLayers( const QList<QgsVectorLayer*>& layers );

QString referencingLayerId();
QString referencedLayerId();
QList< QPair< QString, QString > > references();
Expand All @@ -37,16 +35,9 @@ class APP_EXPORT QgsRelationAddDlg : public QDialog, private Ui::QgsRelationAddD


private slots:
void on_mCbxReferencingLayer_currentIndexChanged( int index );
void on_mCbxReferencedLayer_currentIndexChanged( int index );

void checkDefinitionValid();

private:
void loadLayerAttributes( QComboBox* cbx, QgsVectorLayer* layer );

QMap< QString, QgsVectorLayer* > mLayers;

};

#endif // QGSRELATIONADDDLG_H
3 changes: 1 addition & 2 deletions src/app/qgsrelationmanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ void QgsRelationManagerDialog::addRelation( const QgsRelation &rel )
void QgsRelationManagerDialog::on_mBtnAddRelation_clicked()
{
QgsRelationAddDlg addDlg;
addDlg.addLayers( mLayers );

if ( addDlg.exec() )
{
Expand Down Expand Up @@ -164,4 +163,4 @@ QList< QgsRelation > QgsRelationManagerDialog::relations()
void QgsRelationManagerDialog::onSelectionChanged()
{
mBtnRemoveRelation->setEnabled( mRelationsTable->selectionModel()->hasSelection() );
}
}
20 changes: 16 additions & 4 deletions src/ui/qgsrelationadddlgbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</widget>
</item>
<item row="4" column="1" colspan="2">
<widget class="QComboBox" name="mCbxReferencingLayer"/>
<widget class="QgsMapLayerComboBox" name="mCbxReferencingLayer"/>
</item>
<item row="9" column="0">
<widget class="QLabel" name="label_5">
Expand All @@ -49,7 +49,7 @@
</widget>
</item>
<item row="5" column="1" colspan="2">
<widget class="QComboBox" name="mCbxReferencingField"/>
<widget class="QgsFieldComboBox" name="mCbxReferencingField"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_2">
Expand All @@ -73,19 +73,31 @@
</widget>
</item>
<item row="3" column="1" colspan="2">
<widget class="QComboBox" name="mCbxReferencedField"/>
<widget class="QgsFieldComboBox" name="mCbxReferencedField"/>
</item>
<item row="9" column="1" colspan="2">
<widget class="QLineEdit" name="mTxtRelationId"/>
</item>
<item row="2" column="1" colspan="2">
<widget class="QComboBox" name="mCbxReferencedLayer"/>
<widget class="QgsMapLayerComboBox" name="mCbxReferencedLayer"/>
</item>
<item row="1" column="1" colspan="2">
<widget class="QLineEdit" name="mTxtRelationName"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsMapLayerComboBox</class>
<extends>QComboBox</extends>
<header location="global">qgsmaplayercombobox.h</header>
</customwidget>
<customwidget>
<class>QgsFieldComboBox</class>
<extends>QComboBox</extends>
<header location="global">qgsfieldcombobox.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mTxtRelationName</tabstop>
<tabstop>mCbxReferencedLayer</tabstop>
Expand Down

0 comments on commit b4533cd

Please sign in to comment.