34 #include <QHBoxLayout> 40 QVBoxLayout *topLayout =
new QVBoxLayout(
this );
41 topLayout->setContentsMargins( 0, 9, 0, 0 );
42 setLayout( topLayout );
45 QHBoxLayout *buttonLayout =
new QHBoxLayout();
46 buttonLayout->setContentsMargins( 0, 0, 0, 0 );
48 mToggleEditingButton =
new QToolButton(
this );
49 mToggleEditingButton->setObjectName( QStringLiteral(
"mToggleEditingButton" ) );
51 mToggleEditingButton->setText( tr(
"Toggle editing" ) );
52 mToggleEditingButton->setEnabled(
false );
53 mToggleEditingButton->setCheckable(
true );
54 mToggleEditingButton->setToolTip( tr(
"Toggle editing mode for child layer" ) );
55 buttonLayout->addWidget( mToggleEditingButton );
57 mSaveEditsButton =
new QToolButton(
this );
59 mSaveEditsButton->setText( tr(
"Save child layer edits" ) );
60 mSaveEditsButton->setToolTip( tr(
"Save child layer edits" ) );
61 mSaveEditsButton->setEnabled(
true );
62 buttonLayout->addWidget( mSaveEditsButton );
64 mAddFeatureButton =
new QToolButton(
this );
66 mAddFeatureButton->setText( tr(
"Add child feature" ) );
67 mAddFeatureButton->setToolTip( tr(
"Add child feature" ) );
68 mAddFeatureButton->setObjectName( QStringLiteral(
"mAddFeatureButton" ) );
69 buttonLayout->addWidget( mAddFeatureButton );
71 mDuplicateFeatureButton =
new QToolButton(
this );
73 mDuplicateFeatureButton->setText( tr(
"Duplicate child feature" ) );
74 mDuplicateFeatureButton->setToolTip( tr(
"Duplicate child feature" ) );
75 mDuplicateFeatureButton->setObjectName( QStringLiteral(
"mDuplicateFeatureButton" ) );
76 buttonLayout->addWidget( mDuplicateFeatureButton );
78 mDeleteFeatureButton =
new QToolButton(
this );
80 mDeleteFeatureButton->setText( tr(
"Delete child feature" ) );
81 mDeleteFeatureButton->setToolTip( tr(
"Delete child feature" ) );
82 mDeleteFeatureButton->setObjectName( QStringLiteral(
"mDeleteFeatureButton" ) );
83 buttonLayout->addWidget( mDeleteFeatureButton );
85 mLinkFeatureButton =
new QToolButton(
this );
87 mLinkFeatureButton->setText( tr(
"Link existing features" ) );
88 mLinkFeatureButton->setToolTip( tr(
"Link existing child features" ) );
89 mLinkFeatureButton->setObjectName( QStringLiteral(
"mLinkFeatureButton" ) );
90 buttonLayout->addWidget( mLinkFeatureButton );
92 mUnlinkFeatureButton =
new QToolButton(
this );
94 mUnlinkFeatureButton->setText( tr(
"Unlink feature" ) );
95 mUnlinkFeatureButton->setToolTip( tr(
"Unlink child feature" ) );
96 mUnlinkFeatureButton->setObjectName( QStringLiteral(
"mUnlinkFeatureButton" ) );
97 buttonLayout->addWidget( mUnlinkFeatureButton );
99 mZoomToFeatureButton =
new QToolButton(
this );
101 mZoomToFeatureButton->setText( tr(
"Zoom To Feature" ) );
102 mZoomToFeatureButton->setToolTip( tr(
"Zoom to child feature" ) );
103 mZoomToFeatureButton->setObjectName( QStringLiteral(
"mZoomToFeatureButton" ) );
104 buttonLayout->addWidget( mZoomToFeatureButton );
106 buttonLayout->addItem(
new QSpacerItem( 0, 0, QSizePolicy::Expanding ) );
108 mFormViewButton =
new QToolButton(
this );
109 mFormViewButton->setText( tr(
"Form view" ) );
110 mFormViewButton->setToolTip( tr(
"Switch to form view" ) );
112 mFormViewButton->setCheckable(
true );
114 buttonLayout->addWidget( mFormViewButton );
116 mTableViewButton =
new QToolButton(
this );
117 mTableViewButton->setText( tr(
"Table view" ) );
118 mTableViewButton->setToolTip( tr(
"Switch to table view" ) );
120 mTableViewButton->setCheckable(
true );
122 buttonLayout->addWidget( mTableViewButton );
124 mViewModeButtonGroup =
new QButtonGroup(
this );
129 topLayout->addLayout( buttonLayout );
131 mRelationLayout =
new QGridLayout();
132 mRelationLayout->setContentsMargins( 0, 0, 0, 0 );
133 topLayout->addLayout( mRelationLayout );
136 mDualView->
setView( mViewMode );
140 mRelationLayout->addWidget( mDualView );
143 connect( mViewModeButtonGroup,
static_cast<void ( QButtonGroup::* )(
int )
>( &QButtonGroup::buttonClicked ),
145 connect( mToggleEditingButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::toggleEditing );
146 connect( mSaveEditsButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::saveEdits );
147 connect( mAddFeatureButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::addFeature );
148 connect( mDuplicateFeatureButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::duplicateFeature );
149 connect( mDeleteFeatureButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::deleteSelectedFeatures );
150 connect( mLinkFeatureButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::linkFeature );
151 connect( mUnlinkFeatureButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::unlinkSelectedFeatures );
152 connect( mZoomToFeatureButton, &QAbstractButton::clicked,
this, &QgsRelationEditorWidget::zoomToSelectedFeatures );
169 mRelation = relation;
176 setTitle( relation.
name() );
181 if ( canChangeAttributes && !lyr->readOnly() )
183 mToggleEditingButton->setEnabled(
true );
188 mToggleEditingButton->setEnabled(
false );
191 setObjectName( QStringLiteral(
"referenced/" ) + mRelation.name() );
200 mDualView->
init( mRelation.referencingLayer(),
nullptr, myRequest, mEditorContext );
218 mRelation = relation;
219 mNmRelation = nmrelation;
221 if ( !mRelation.isValid() )
224 mToggleEditingButton->setVisible(
true );
227 for (
auto it = transactionGroups.constBegin(); it != transactionGroups.constEnd(); ++it )
229 if ( it.value()->layers().contains( mRelation.referencingLayer() ) )
231 mToggleEditingButton->setVisible(
false );
232 mSaveEditsButton->setVisible(
false );
245 setTitle( relation.
name() );
250 if ( canChangeAttributes && !lyr->readOnly() )
252 mToggleEditingButton->setEnabled(
true );
257 mToggleEditingButton->setEnabled(
false );
260 setObjectName( QStringLiteral(
"referenced/" ) + mRelation.name() );
267 mEditorContext = context;
272 return mFeatureSelectionMgr;
288 void QgsRelationEditorWidget::updateButtons()
290 bool editable =
false;
291 bool linkable =
false;
305 mAddFeatureButton->setEnabled( editable );
306 mDuplicateFeatureButton->setEnabled( editable && selectionNotEmpty );
307 mLinkFeatureButton->setEnabled( linkable );
308 mDeleteFeatureButton->setEnabled( editable && selectionNotEmpty );
309 mUnlinkFeatureButton->setEnabled( linkable && selectionNotEmpty );
311 mZoomToFeatureButton->setVisible(
327 mZoomToFeatureButton->setEnabled( selectionNotEmpty );
329 mToggleEditingButton->setChecked( editable );
330 mSaveEditsButton->setEnabled( editable );
333 void QgsRelationEditorWidget::addFeature()
355 int index = fields.
indexOf( fieldPair.first );
356 linkAttributes.insert( index, mFeature.
attribute( fieldPair.second ) );
361 int index = fields.
indexOf( fieldPair.first );
362 linkAttributes.insert( index, f.
attribute( fieldPair.second ) );
384 void QgsRelationEditorWidget::linkFeature()
395 if ( selectionDlg.exec() )
417 int index = fields.
indexOf( fieldPair.first );
418 linkAttributes.insert( index, mFeature.
attribute( fieldPair.second ) );
425 int index = fields.
indexOf( fieldPair.first );
426 linkAttributes.insert( index, relatedFeature.
attribute( fieldPair.second ) );
430 newFeatures << linkFeature;
435 Q_FOREACH (
const QgsFeature &f, newFeatures )
444 QMap<int, QVariant> keys;
449 keys.insert( idx, val );
454 QMapIterator<int, QVariant> it( keys );
455 while ( it.hasNext() )
465 void QgsRelationEditorWidget::duplicateFeature()
478 void QgsRelationEditorWidget::deleteFeature(
const QgsFeatureId featureid )
483 void QgsRelationEditorWidget::deleteSelectedFeatures()
488 void QgsRelationEditorWidget::deleteFeatures(
const QgsFeatureIds &featureids )
495 void QgsRelationEditorWidget::unlinkFeature(
const QgsFeatureId featureid )
500 void QgsRelationEditorWidget::unlinkSelectedFeatures()
505 void QgsRelationEditorWidget::zoomToSelectedFeatures()
517 void QgsRelationEditorWidget::unlinkFeatures(
const QgsFeatureIds &featureids )
523 .setFilterFids( featureids )
535 QString filter = QStringLiteral(
"(%1) AND (%2)" ).arg(
537 filters.join( QStringLiteral(
" OR " ) ) );
541 .setFilterExpression( filter ) );
557 QMap<int, QgsField> keyFields;
567 keyFields.insert( idx, fld );
572 QMapIterator<int, QgsField> it( keyFields );
573 while ( it.hasNext() )
582 void QgsRelationEditorWidget::toggleEditing(
bool state )
598 void QgsRelationEditorWidget::saveEdits()
605 void QgsRelationEditorWidget::onCollapsedStateChanged(
bool collapsed )
614 void QgsRelationEditorWidget::updateUi()
635 filters << filter.prepend(
'(' ).append(
')' );
653 return mLinkFeatureButton->isVisible();
658 mLinkFeatureButton->setVisible( showLinkButton );
663 return mUnlinkFeatureButton->isVisible();
668 mUnlinkFeatureButton->setVisible( showUnlinkButton );
680 if ( mShowLabel && mRelation.
isValid() )
681 setTitle( mRelation.
name() );
683 setTitle( QString() );
690 QAction *qAction =
nullptr;
693 connect( qAction, &QAction::triggered,
this, [
this, fid]() { deleteFeature( fid ); } );
696 connect( qAction, &QAction::triggered,
this, [
this, fid]() { unlinkFeature( fid ); } );
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
Wrapper for iterator of features from vector data provider or vector layer.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
static QgsFeature createFeature(QgsVectorLayer *layer, const QgsGeometry &geometry=QgsGeometry(), const QgsAttributeMap &attributes=QgsAttributeMap(), QgsExpressionContext *context=nullptr)
Creates a new feature ready for insertion into a layer.
void init(QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const QgsFeatureRequest &request=QgsFeatureRequest(), const QgsAttributeEditorContext &context=QgsAttributeEditorContext(), bool loadFeatures=true)
Has to be called to initialize the dual view.
bool collapsed
The collapsed state of this group box.
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
QgsAttributeTableModel * masterModel() const
Returns the model which has the information about all features (not only filtered) ...
void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
Set the feature selection model.
A groupbox that collapses/expands when toggled and can save its collapsed and checked states...
int selectedFeatureCount() override
Returns the number of features that are selected in this layer.
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features...
QSet< QgsFeatureId > QgsFeatureIds
This class contains context information for attribute editor widgets.
QList< QgsFeature > QgsFeatureList
QgsVectorLayer referencingLayer
ViewMode
The view modes, in which this widget can present information.
void collapsedStateChanged(bool collapsed)
Signal emitted when groupbox collapsed/expanded state is changed, and when first shown.
#define FID_TO_STRING(fid)
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
bool deleteFeatures(const QgsFeatureIds &fids)
Deletes a set of features from the layer (but does not commit it)
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void selectByIds(const QgsFeatureIds &ids, SelectBehavior behavior=SetSelection)
Select matching features using a list of feature IDs.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
This signal is emitted when selection was changed.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool isEditable() const override
Returns true if the provider is in editing mode.
Map canvas is a class for displaying all GIS data types on a canvas.
void setView(ViewMode view)
Change the current view mode.
const QgsFeatureIds & selectedFeatures()
Gets the selected features.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QgsExpression * filterExpression() const
Returns the filter expression if set.
Show a list of the features, where one can be chosen and the according attribute dialog will be prese...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QgsAttributeList referencedFields() const
Returns a list of attributes used to form the referenced fields (most likely primary key) on the refe...
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsFeatureRequest getReferencedFeatureRequest(const QgsAttributes &attributes) const
Creates a request to return the feature on the referenced (parent) layer which is referenced by the p...
Defines a relation between matching fields of the two involved tables of a relation.
QgsFields fields() const override
Returns the list of fields of this layer.
Shows the features and attributes in a table layout.
#define QgsDebugMsgLevel(str, level)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QMap< int, QVariant > QgsAttributeMap
void editingStopped()
Is emitted, when edited changes successfully have been written to the data provider.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsVectorLayer referencedLayer
Encapsulate a field in an attribute table or data source.
QgsFeatureRequest getRelatedFeaturesRequest(const QgsFeature &feature) const
Creates a request to return all the features on the referencing (child) layer which have a foreign ke...
void editingStarted()
Is emitted, when editing on this layer has started.
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
QString expression() const
Returns the original, unmodified expression string.
This selection manager synchronizes a local set of selected features with an attribute table...
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void showContextMenuExternally(QgsActionMenu *menu, QgsFeatureId fid)
Emitted when selecting context menu on the feature list to create the context menu individually...
void zoomToFeatureIds(QgsVectorLayer *layer, const QgsFeatureIds &ids)
Set canvas extent to the bounding box of a set of features.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
QMap< QPair< QString, QString >, QgsTransactionGroup * > transactionGroups()
Map of transaction groups.
static QgsFeature duplicateFeature(QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, int depth, QgsDuplicateFeatureContext &duplicateFeatureContext)
Duplicates a feature and it's children (one level deep).
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString referencedField() const
Gets the name of the referenced (parent) field.
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) override
Adds a list of features to the sink.
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant(), bool skipDefaultValues=false)
Changes an attribute value for a feature (but does not immediately commit the changes).
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Is an interface class to abstract feature selection handling.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QString referencingField() const
Gets the name of the referencing (child) field.
QgsMapCanvas * mapCanvas() const
Returns the associated map canvas (e.g.
Allows modification of attribute values.
const QgsFeatureIds & selectedFeatureIds() const override
Returns reference to identifiers of selected features.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a single feature to the sink.
This widget is used to show the attributes of a set of features of a QgsVectorLayer.