Skip to content

Commit

Permalink
Add GUI elements for N:M relation configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 8, 2015
1 parent c56588e commit 8246d96
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -113,7 +113,9 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
mRelationsList->setHorizontalHeaderItem( RelNameCol, new QTableWidgetItem( tr( "Name" ) ) );
mRelationsList->setHorizontalHeaderItem( RelLayerCol, new QTableWidgetItem( tr( "Layer" ) ) );
mRelationsList->setHorizontalHeaderItem( RelFieldCol, new QTableWidgetItem( tr( "Field" ) ) );
mRelationsList->setHorizontalHeaderItem( RelNmCol, new QTableWidgetItem( tr( "Cardinality" ) ) );
mRelationsList->verticalHeader()->hide();
mRelationsList->horizontalHeader()->setStretchLastSection( true );

// Init function stuff
mInitCodeSourceComboBox->addItem( tr( "" ) );
Expand Down Expand Up @@ -331,11 +333,11 @@ void QgsFieldsProperties::loadRelations()
{
mRelationsList->setRowCount( 0 );

QList<QgsRelation> relations = QgsProject::instance()->relationManager()->referencedRelations( mLayer );
mRelations = QgsProject::instance()->relationManager()->referencedRelations( mLayer );

int idx = 0;

Q_FOREACH ( const QgsRelation& relation, relations )
Q_FOREACH ( const QgsRelation& relation, mRelations )
{
mRelationsList->insertRow( idx );

Expand All @@ -357,6 +359,29 @@ void QgsFieldsProperties::loadRelations()
item->setFlags( Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mRelationsList->setItem( idx, RelIdCol, item );

QComboBox* nmCombo = new QComboBox( mRelationsList );
nmCombo->addItem( tr( "Many to one relation" ) );
Q_FOREACH ( const QgsRelation& nmrel, QgsProject::instance()->relationManager()->referencingRelations( relation.referencingLayer() ) )
{
if ( nmrel.fieldPairs().first().referencingField() != relation.fieldPairs().first().referencingField() )
nmCombo->addItem( QString( "%1 (%2)" ).arg( nmrel.referencedLayer()->name() ).arg( nmrel.fieldPairs().first().referencedField() ), nmrel.id() );

QgsEditorWidgetConfig cfg = mLayer->editFormConfig()->widgetConfig( relation.id() );

QVariant nmrelcfg = cfg.value( "nm-rel" );

int idx = nmCombo->findData( nmrelcfg.toString() );

if ( idx != -1 )
nmCombo->setCurrentIndex( idx );
}

if ( nmCombo->count() == 1 )
{
nmCombo->setEnabled( false );
}

mRelationsList->setCellWidget( idx, RelNmCol, nmCombo );
++idx;
}
}
Expand Down Expand Up @@ -894,7 +919,9 @@ void QgsFieldsProperties::apply()
}
}

//tabs and groups


// tabs and groups
mLayer->clearAttributeEditorWidgets();
for ( int t = 0; t < mDesignerTree->invisibleRootItem()->childCount(); t++ )
{
Expand All @@ -917,8 +944,27 @@ void QgsFieldsProperties::apply()

mLayer->setExcludeAttributesWMS( excludeAttributesWMS );
mLayer->setExcludeAttributesWFS( excludeAttributesWFS );
}

// relations
for ( int i = 0; i < mRelationsList->rowCount(); ++i )
{
QgsEditorWidgetConfig cfg;

QComboBox* cb = qobject_cast<QComboBox*>( mRelationsList->cellWidget( i, RelNmCol ) );
QVariant otherRelation = cb->itemData( cb->currentIndex() );

if ( otherRelation.isValid() )
{
cfg["nm-rel"] = otherRelation.toString();
}

DesignerTreeItemData itemData = mRelationsList->item( i, RelNameCol )->data( DesignerTreeRole ).value<DesignerTreeItemData>();

QString relationName = itemData.name();

mLayer->editFormConfig()->setWidgetConfig( relationName, cfg );
}
}
/*
* FieldConfig implementation
*/
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsfieldsproperties.h
Expand Up @@ -166,6 +166,8 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
FieldConfig configForRow( int row );
void setConfigForRow( int row, const FieldConfig& cfg );

QList<QgsRelation> mRelations;

QgsVectorLayer* mLayer;
DesignerTree* mDesignerTree;
DragList* mFieldsList;
Expand Down Expand Up @@ -198,6 +200,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
RelLayerCol,
RelFieldCol,
RelIdCol,
RelNmCol,
RelColCount
};

Expand Down

0 comments on commit 8246d96

Please sign in to comment.