QGIS API Documentation  2.3.0-Master
 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 }
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:98
void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
A groupbox that collapses/expands when toggled and can save its collapsed and checked states...
bool deleteFeature(QgsFeatureId fid)
delete a feature from the layer (but does not commit it)
This class contains context information for attribute editor widgets.
void collapsedStateChanged(bool collapsed)
Signal emitted when groupbox collapsed/expanded state is changed, and when first shown.
virtual bool addFeature(QgsVectorLayer *layer, QgsAttributeMap defaultValues=QgsAttributeMap(), const QgsGeometry &defaultGeometry=QgsGeometry())=0
This method should/will be called, whenever a new feature will be added to the layer.
Container of fields for a vector layer.
Definition: qgsfield.h:164
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:211
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
Show a list of the features, where one can be chosen and the according attribute dialog will be prese...
Definition: qgsdualview.h:64
void init(QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const QgsFeatureRequest &request=QgsFeatureRequest(), QgsAttributeEditorContext context=QgsAttributeEditorContext())
Has to be called to initialize the dual view.
Definition: qgsdualview.cpp:67
virtual bool stopEditing(QgsVectorLayer *layer, bool allowCancel=true)=0
Will be called, when an editing session is ended and the features should be commited.
virtual bool startEditing(QgsVectorLayer *layer)=0
This will be called, whenever a vector layer should be switched to edit mode.
static QgsRelationEditorWidget * createRelationEditor(const QgsRelation &relation, const QgsFeature &feature, QgsAttributeEditorContext context, QWidget *parent=NULL)
Defines a relation between matchin fields of the two involved tables of a relation.
Definition: qgsrelation.h:38
Shows the features and attributes in a table layout.
Definition: qgsdualview.h:58
QgsVectorLayerTools * vectorLayerTools()
virtual int capabilities() const
Returns a bitmask containing the supported capabilities Note, some capabilities may change depending ...
QgsVectorLayer * referencingLayer() const
Access the referencing (child) layer This is the layer which has the field(s) which point to another ...
QgsRelationEditorWidget(const QgsRelation &relation, const QgsFeature &feature, QgsAttributeEditorContext context, QWidget *parent=NULL)
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Q_DECL_DEPRECATED bool changeAttributeValue(QgsFeatureId fid, int field, QVariant value, bool emitSignal)
Changes an attribute value (but does not commit it)
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:31
QgsFeatureRequest getRelatedFeaturesRequest(const QgsFeature &feature) const
Creates a request to return all the features on the referencing (child) layer which have a foreign ke...
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
Definition: qgsfield.h:223
This selection manager synchronizes a local set of selected features with an attribute table...
QgsAttributeTableModel * masterModel() const
Returns the model which has the information about all features (not only filtered) ...
Definition: qgsdualview.h:135
virtual bool isReadOnly() const
Returns true if the provider is in read-only mode.
virtual const QgsFeatureIds & selectedFeaturesIds() const
Return reference to identifiers of selected features.
const QString & referencingField() const
Get the name of the referencing field.
Definition: qgsrelation.h:50
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:230
void onCollapsedStateChanged(bool state)
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
QList< FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names f...
qint64 QgsFeatureId
Definition: qgsfeature.h:30
void on_mToggleEditingButton_toggled(bool state)
QgsGenericFeatureSelectionManager * mFeatureSelectionMgr
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
QgsVectorDataProvider * dataProvider()
Returns the data provider.
virtual bool isEditable() const
Returns true if the provider is in editing mode.
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
allows modification of attribute values
This widget is used to show the attributes of a set of features of a QgsVectorLayer.
Definition: qgsdualview.h:42
const QString & referencedField() const
Get the name of the referenced field.
Definition: qgsrelation.h:52
QgsAttributeEditorContext mEditorContext