QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrelationeditor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationeditor.cpp
3  --------------------------------------
4  Date : 17.5.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias dot kuhn at gmx dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsrelationeditor.h"
17 
19 
20 #include "qgsdistancearea.h"
21 #include "qgsvectordataprovider.h"
22 #include "qgsexpression.h"
23 #include "qgsfeature.h"
24 #include "qgsfeatureselectiondlg.h"
26 #include "qgsrelation.h"
27 #include "qgsvectorlayertools.h"
28 
29 #include <QHBoxLayout>
30 #include <QLabel>
31 
32 QgsRelationEditorWidget::QgsRelationEditorWidget( const QgsRelation& relation, const QgsFeature& feature, QgsAttributeEditorContext context, QWidget* parent )
33  : QgsCollapsibleGroupBox( relation.name(), parent )
34  , mDualView( NULL )
35  , mEditorContext( context )
36  , mRelation( relation )
37  , mFeature( feature )
38 {
39  setupUi( this );
40 
41  connect( relation.referencingLayer(), SIGNAL( editingStarted() ), this, SLOT( referencingLayerEditingToggled() ) );
42  connect( relation.referencingLayer(), SIGNAL( editingStopped() ), this, SLOT( referencingLayerEditingToggled() ) );
43  connect( this, SIGNAL( collapsedStateChanged( bool ) ), this, SIGNAL( onCollapsedStateChanged( bool ) ) );
44 
45  // Set initial state for add/remove etc. buttons
47 }
48 
50 {
51  QgsRelationEditorWidget* editor = new QgsRelationEditorWidget( relation, feature, context, parent );
52 
53  QgsDualView* dualView = new QgsDualView( editor );
54  QgsVectorLayer* lyr = relation.referencingLayer();
55 
56  bool canChangeAttributes = lyr->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
57 
58  editor->mToggleEditingButton->setEnabled( canChangeAttributes && !lyr->isReadOnly() );
59 
62 
63  editor->mBrowserWidget->layout()->addWidget( dualView );
64 
65  QgsFeatureRequest myRequest = relation.getRelatedFeaturesRequest( feature );
66 
67  dualView->init( relation.referencingLayer(), NULL, myRequest, context );
68 
69  editor->mDualView = dualView;
70 
71  editor->mViewModeButtonGroup->setId( editor->mTableViewButton, QgsDualView::AttributeTable );
72  editor->mViewModeButtonGroup->setId( editor->mFormViewButton, QgsDualView::AttributeEditor );
73 
74  connect( editor->mViewModeButtonGroup, SIGNAL( buttonClicked( int ) ), dualView, SLOT( setCurrentIndex( int ) ) );
75  connect( dualView, SIGNAL( currentChanged( int ) ), editor, SLOT( viewModeChanged( int ) ) );
76 
77  return editor;
78 }
79 
81 {
82  if ( state && !mDualView->masterModel() )
83  {
84  // TODO: Lazy init dual view if collapsed on init
85  }
86 }
87 
89 {
90  bool editable = mRelation.referencingLayer()->isEditable();
91 
92  mAddFeatureButton->setEnabled( editable );
93  mLinkFeatureButton->setEnabled( editable );
94  mDeleteFeatureButton->setEnabled( editable );
95  mUnlinkFeatureButton->setEnabled( editable );
96  mToggleEditingButton->setChecked( editable );
97 }
98 
100 {
101  mViewModeButtonGroup->button( mode )->click();
102 }
103 
105 {
106  QgsAttributeMap keyAttrs;
107 
109 
110  foreach ( QgsRelation::FieldPair fieldPair, mRelation.fieldPairs() )
111  {
112  keyAttrs.insert( fields.indexFromName( fieldPair.referencingField() ), mFeature.attribute( fieldPair.referencedField() ) );
113  }
114 
116 }
117 
119 {
120  QgsFeatureSelectionDlg selectionDlg( mRelation.referencingLayer(), this );
121 
122  if ( selectionDlg.exec() )
123  {
124  QMap<int, QVariant> keys;
125  foreach ( const QgsRelation::FieldPair fieldPair, mRelation.fieldPairs() )
126  {
127  int idx = mRelation.referencingLayer()->fieldNameIndex( fieldPair.referencingField() );
128  QVariant val = mFeature.attribute( fieldPair.referencedField() );
129  keys.insert( idx, val );
130  }
131 
132  foreach ( QgsFeatureId fid, selectionDlg.selectedFeatures() )
133  {
134  QMapIterator<int, QVariant> it( keys );
135  while ( it.hasNext() )
136  {
137  it.next();
138  mRelation.referencingLayer()->changeAttributeValue( fid, it.key(), it.value() );
139  }
140  }
141  }
142 }
143 
145 {
147  {
149  }
150 }
151 
153 {
154  QMap<int, QgsField> keyFields;
155  foreach ( const QgsRelation::FieldPair fieldPair, mRelation.fieldPairs() )
156  {
157  int idx = mRelation.referencingLayer()->fieldNameIndex( fieldPair.referencingField() );
159  keyFields.insert( idx, fld );
160  }
161 
163  {
164  QMapIterator<int, QgsField> it( keyFields );
165  while ( it.hasNext() )
166  {
167  it.next();
168  mRelation.referencingLayer()->changeAttributeValue( fid, it.key(), QVariant( it.value().type() ) );
169  }
170  }
171 }
172 
174 {
175  if ( state )
176  {
178  }
179  else
180  {
182  }
183 }