Skip to content
Permalink
Browse files

Add GUI elements for N:M relation configuration

  • Loading branch information
m-kuhn committed Dec 8, 2015
1 parent c56588e commit 8246d960156812d4edea08dbb1c6b5d857c43af8
Showing with 53 additions and 4 deletions.
  1. +50 −4 src/app/qgsfieldsproperties.cpp
  2. +3 −0 src/app/qgsfieldsproperties.h
@@ -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( "" ) );
@@ -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 );

@@ -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;
}
}
@@ -894,7 +919,9 @@ void QgsFieldsProperties::apply()
}
}

//tabs and groups


// tabs and groups
mLayer->clearAttributeEditorWidgets();
for ( int t = 0; t < mDesignerTree->invisibleRootItem()->childCount(); t++ )
{
@@ -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
*/
@@ -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;
@@ -198,6 +200,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
RelLayerCol,
RelFieldCol,
RelIdCol,
RelNmCol,
RelColCount
};

0 comments on commit 8246d96

Please sign in to comment.
You can’t perform that action at this time.