QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
69 return tr(
"<Attribute table frame>" );
74 if ( layer == mVectorLayer.
get() )
112 QgsRelation relation =
mLayout->project()->relationManager()->relation( mRelationId );
134 void QgsLayoutItemAttributeTable::atlasLayerChanged(
QgsVectorLayer *layer )
143 if ( mCurrentAtlasLayer )
149 const bool mustRebuildColumns =
static_cast< bool >( mCurrentAtlasLayer ) ||
mColumns.empty();
150 mCurrentAtlasLayer = layer;
152 if ( mustRebuildColumns )
179 for (
const auto &field : sourceFields )
181 QString currentAlias =
source->attributeDisplayName( idx );
190 void QgsLayoutItemAttributeTable::disconnectCurrentMap()
199 disconnect( mMap, &QObject::destroyed,
this, &QgsLayoutItemAttributeTable::disconnectCurrentMap );
205 return mUseConditionalStyling;
227 disconnectCurrentMap();
235 connect( mMap, &QObject::destroyed,
this, &QgsLayoutItemAttributeTable::disconnectCurrentMap );
243 if ( features == mMaximumNumberOfFeatures )
248 mMaximumNumberOfFeatures = features;
255 if ( uniqueOnly == mShowUniqueRowsOnly )
260 mShowUniqueRowsOnly = uniqueOnly;
267 if ( visibleOnly == mShowOnlyVisibleFeatures )
272 mShowOnlyVisibleFeatures = visibleOnly;
279 if ( filterToAtlas == mFilterToAtlasIntersection )
284 mFilterToAtlasIntersection = filterToAtlas;
291 if ( filter == mFilterFeatures )
296 mFilterFeatures = filter;
303 if ( expression == mFeatureFilter )
308 mFeatureFilter = expression;
326 if ( !fields.isEmpty() )
328 for (
const QString &field : fields )
334 QString currentAlias =
source->attributeDisplayName( attrIdx );
345 for (
const QgsField &field : layerFields )
347 QString currentAlias =
source->attributeDisplayName( idx );
362 void QgsLayoutItemAttributeTable::restoreFieldAliasMap(
const QMap<int, QString> &map )
370 for (
int i = 0; i <
mColumns.count(); i++ )
372 int attrIdx =
source->fields().lookupField(
mColumns[i].attribute() );
373 if (
map.contains( attrIdx ) )
379 mColumns[i].setHeading(
source->attributeDisplayName( attrIdx ) );
404 std::unique_ptr<QgsExpression> filterExpression;
405 bool activeFilter =
false;
406 if ( mFilterFeatures && !mFeatureFilter.isEmpty() )
408 filterExpression = qgis::make_unique< QgsExpression >( mFeatureFilter );
409 if ( !filterExpression->hasParserError() )
418 std::unique_ptr< QgsGeometryEngine > visibleMapEngine;
419 if ( mMap && mShowOnlyVisibleFeatures )
436 visibleMapEngine->prepareGeometry();
440 std::unique_ptr< QgsGeometryEngine > atlasGeometryEngine;
441 if ( mFilterToAtlasIntersection )
443 atlasGeometry =
mLayout->reportContext().currentGeometry( layer->
crs() );
444 if ( !atlasGeometry.
isNull() )
446 if ( selectionRect.
isNull() )
456 atlasGeometryEngine->prepareGeometry();
462 QgsRelation relation =
mLayout->project()->relationManager()->relation( mRelationId );
467 if ( !selectionRect.
isEmpty() )
481 req.
addOrderBy( column.attribute(), column.sortOrder() == Qt::AscendingOrder );
488 mConditionalStyles.clear();
490 QVector< QVector< QPair< QVariant, QgsConditionalStyle > > > tempContents;
493 while ( fit.
nextFeature( f ) && counter < mMaximumNumberOfFeatures )
497 if ( activeFilter && filterExpression )
499 QVariant result = filterExpression->evaluate( &context );
501 if ( !result.toBool() )
508 if ( visibleMapEngine )
518 if ( mFilterToAtlasIntersection )
531 if ( mUseConditionalStyling )
542 QVector< QPair< QVariant, QgsConditionalStyle > > currentRow;
543 currentRow.reserve(
mColumns.count() );
545 rowContents.reserve(
mColumns.count() );
555 const QVariant val = f.
attributes().at( idx );
557 if ( mUseConditionalStyling )
561 styles.insert( 0, rowStyle );
565 QVariant v = replaceWrapChar( val );
566 currentRow << qMakePair( v, style );
572 std::unique_ptr< QgsExpression > expression = qgis::make_unique< QgsExpression >( column.attribute() );
574 expression->prepare( &context );
575 QVariant value = expression->evaluate( &context );
577 currentRow << qMakePair( value, rowStyle );
578 rowContents << value;
582 if ( mShowUniqueRowsOnly )
588 tempContents << currentRow;
589 existingContents << rowContents;
594 contents.reserve( tempContents.size() );
595 mConditionalStyles.reserve( tempContents.size() );
596 for (
auto it = tempContents.constBegin(); it != tempContents.constEnd(); ++it )
599 QList< QgsConditionalStyle > rowStyles;
600 row.reserve( it->size() );
601 rowStyles.reserve( it->size() );
603 for (
auto cellIt = it->constBegin(); cellIt != it->constEnd(); ++cellIt )
605 row << cellIt->first;
606 rowStyles << cellIt->second;
609 mConditionalStyles << rowStyles;
618 if ( row >= mConditionalStyles.size() )
621 return mConditionalStyles.at( row ).at( column );
639 if ( !mMap && !mMapUuid.isEmpty() &&
mLayout )
641 mMap = qobject_cast< QgsLayoutItemMap *>(
mLayout->itemByUuid( mMapUuid,
true ) );
658 mDataDefinedVectorLayer =
nullptr;
660 QString currentLayerIdentifier;
662 currentLayerIdentifier = currentLayer->id();
667 mDataDefinedVectorLayer = ddLayer;
673 QVariant QgsLayoutItemAttributeTable::replaceWrapChar(
const QVariant &variant )
const
676 if ( mWrapString.isEmpty() || !variant.toString().contains( mWrapString ) )
679 QString replaced = variant.toString();
680 replaced.replace( mWrapString, QLatin1String(
"\n" ) );
689 return mLayout->reportContext().layer();
692 if ( mDataDefinedVectorLayer )
693 return mDataDefinedVectorLayer;
695 return mVectorLayer.
get();
699 QgsRelation relation =
mLayout->project()->relationManager()->relation( mRelationId );
706 void QgsLayoutItemAttributeTable::removeLayer(
const QString &layerId )
710 if ( layerId == mVectorLayer->
id() )
736 tableElem.setAttribute( QStringLiteral(
"source" ), QString::number(
static_cast< int >( mSource ) ) );
737 tableElem.setAttribute( QStringLiteral(
"relationId" ), mRelationId );
738 tableElem.setAttribute( QStringLiteral(
"showUniqueRowsOnly" ), mShowUniqueRowsOnly );
739 tableElem.setAttribute( QStringLiteral(
"showOnlyVisibleFeatures" ), mShowOnlyVisibleFeatures );
740 tableElem.setAttribute( QStringLiteral(
"filterToAtlasIntersection" ), mFilterToAtlasIntersection );
741 tableElem.setAttribute( QStringLiteral(
"maxFeatures" ), mMaximumNumberOfFeatures );
742 tableElem.setAttribute( QStringLiteral(
"filterFeatures" ), mFilterFeatures ? QStringLiteral(
"true" ) : QStringLiteral(
"false" ) );
743 tableElem.setAttribute( QStringLiteral(
"featureFilter" ), mFeatureFilter );
744 tableElem.setAttribute( QStringLiteral(
"wrapString" ), mWrapString );
745 tableElem.setAttribute( QStringLiteral(
"useConditionalStyling" ), mUseConditionalStyling );
749 tableElem.setAttribute( QStringLiteral(
"mapUuid" ), mMap->
uuid() );
754 tableElem.setAttribute( QStringLiteral(
"vectorLayer" ), mVectorLayer.
layerId );
755 tableElem.setAttribute( QStringLiteral(
"vectorLayerName" ), mVectorLayer.
name );
756 tableElem.setAttribute( QStringLiteral(
"vectorLayerSource" ), mVectorLayer.
source );
757 tableElem.setAttribute( QStringLiteral(
"vectorLayerProvider" ), mVectorLayer.
provider );
774 mRelationId = itemElem.attribute( QStringLiteral(
"relationId" ), QString() );
778 mCurrentAtlasLayer =
mLayout->reportContext().layer();
781 mShowUniqueRowsOnly = itemElem.attribute( QStringLiteral(
"showUniqueRowsOnly" ), QStringLiteral(
"0" ) ).toInt();
782 mShowOnlyVisibleFeatures = itemElem.attribute( QStringLiteral(
"showOnlyVisibleFeatures" ), QStringLiteral(
"1" ) ).toInt();
783 mFilterToAtlasIntersection = itemElem.attribute( QStringLiteral(
"filterToAtlasIntersection" ), QStringLiteral(
"0" ) ).toInt();
784 mFilterFeatures = itemElem.attribute( QStringLiteral(
"filterFeatures" ), QStringLiteral(
"false" ) ) == QLatin1String(
"true" );
785 mFeatureFilter = itemElem.attribute( QStringLiteral(
"featureFilter" ), QString() );
786 mMaximumNumberOfFeatures = itemElem.attribute( QStringLiteral(
"maxFeatures" ), QStringLiteral(
"5" ) ).toInt();
787 mWrapString = itemElem.attribute( QStringLiteral(
"wrapString" ) );
788 mUseConditionalStyling = itemElem.attribute( QStringLiteral(
"useConditionalStyling" ), QStringLiteral(
"0" ) ).toInt();
791 mMapUuid = itemElem.attribute( QStringLiteral(
"mapUuid" ) );
801 QString layerId = itemElem.attribute( QStringLiteral(
"vectorLayer" ) );
802 QString layerName = itemElem.attribute( QStringLiteral(
"vectorLayerName" ) );
803 QString layerSource = itemElem.attribute( QStringLiteral(
"vectorLayerSource" ) );
804 QString layerProvider = itemElem.attribute( QStringLiteral(
"vectorLayerProvider" ) );
805 mVectorLayer =
QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
829 if ( newLayer != prevLayer )
841 mCurrentAtlasLayer = newLayer;
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
void layerWillBeRemoved(const QString &layerId)
Emitted when a layer is about to be removed from the registry.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsCoordinateReferenceSystem crs
const QgsLayout * layout() const
Returns the layout the object is attached to.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setDisplayedFields(const QStringList &fields, bool refresh=true)
Sets the attributes to display in the table.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
void setFilterToAtlasFeature(bool filterToAtlas)
Sets attribute table to only show features which intersect the current atlas feature.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsFeatureRequest & addOrderBy(const QString &expression, bool ascending=true)
Adds a new OrderByClause, appending it as the least important one.
void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties) override
Refreshes a data defined property for the multi frame by reevaluating the property's value and redraw...
QVector< QgsLayoutTableRow > QgsLayoutTableContents
TYPE * resolveWeakly(const QgsProject *project, MatchType matchType=MatchType::All)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
QgsVectorLayer * sourceLayer() const
Returns the source layer for the table, considering the table source mode.
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
void layerModified()
Emitted when modifications has been done on layer.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the multi frame by reevaluating the property's value and redraw...
void extentChanged()
Emitted when the map's extent changes.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
QIcon icon() const override
Returns the item's icon.
QString relationId() const
Returns the relation id which the table displays child features from.
QgsPropertyCollection mDataDefinedProperties
void setFilterFeatures(bool filter)
Sets whether the feature filter is active for the attribute table.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
bool writePropertiesToElement(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Stores multiframe state within an XML DOM element.
@ AttributeTableSourceLayer
Attribute table source layer.
The QgsConditionalLayerStyles class holds conditional style information for a layer....
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
void changed()
Emitted when the object's properties change.
void setFeatureFilter(const QString &expression)
Sets the expression used for filtering features in the table.
void setUniqueRowsOnly(bool uniqueOnly)
Sets attribute table to only show unique rows.
void setUseConditionalStyling(bool enabled)
Sets whether the attribute table will be rendered using the conditional styling properties of the lin...
static QgsConditionalStyle compressStyles(const QList< QgsConditionalStyle > &styles)
Compress a list of styles into a single style.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets feature ID that should be fetched.
QgsLayoutItemAttributeTable(QgsLayout *layout)
Constructor for QgsLayoutItemAttributeTable, attached to the specified layout.
QgsVectorLayer referencingLayer
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
QgsLayoutTableSortColumns mSortColumns
Columns to sort the table.
QString layerId
Original layer ID.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
bool readPropertiesFromElement(const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context) override
Sets multiframe state from a DOM element.
@ AllProperties
All properties for item.
bool useConditionalStyling() const
Returns true if the attribute table will be rendered using the conditional styling properties of the ...
QString displayName() const override
Returns the multiframe display name.
QgsLayoutTableContents & contents()
Returns the current contents of the table.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString provider
Weak reference to layer provider.
@ RelationChildren
Table shows attributes from related child features.
@ LayerAttributes
Table shows attributes from features in a vector layer.
QString name
Weak reference to layer name.
int type() const override
Returns unique multiframe type id.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
ContentSource
Specifies the content source for the attribute table.
void setAttribute(const QString &attribute)
Sets the attribute name or expression used for the column's values.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
A class to display a table in the print layout, and allow the table to span over multiple frames.
QString wrapString() const
Returns the string used to wrap the contents of the table cells by.
virtual void refreshAttributes()
Refreshes the contents shown in the table by querying for new data.
static QList< QgsConditionalStyle > matchingConditionalStyles(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching styles for the value and feature.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
QgsConditionalStyle conditionalCellStyle(int row, int column) const override
Returns the conditional style to use for the cell at row, column.
void recalculateTableSize()
Recalculates and updates the size of the table and all table frames.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setHeading(const QString &heading)
Sets the heading for a column, which is the value displayed in the column's header cell.
Layout graphical items for displaying a map.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProject *project)
Resolves a string into a map layer from a given project.
QgsConditionalStyles rowStyles() const
Returns a list of row styles associated with the layer.
static QgsLayoutItemAttributeTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemAttributeTable for the specified parent layout.
void setMaximumNumberOfFeatures(int features)
Sets the maximum number of features shown by the table.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine.
virtual QString uuid() const
Returns the item identification string.
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 setMap(QgsLayoutItemMap *map)
Sets a layout map to use to limit the extent of features shown in the attribute table.
void setWrapString(const QString &wrapString)
Sets a string to wrap the contents of the table cells by.
QPointer< QgsLayout > mLayout
bool nextFeature(QgsFeature &f)
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
QString source
Weak reference to layer public source.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
bool getTableContents(QgsLayoutTableContents &contents) override
Queries the attribute table's vector layer for attributes to show in the table.
@ AtlasFeature
Table shows attributes from the current atlas feature.
QgsConditionalLayerStyles * conditionalStyles() const
Returns the conditional styles that are set for this layer.
bool contentsContainsRow(const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row) const
Checks whether a table contents contains a given row.
void setRelationId(const QString &id)
Sets the relation id from which to display child features.
_LayerRef< QgsVectorLayer > QgsVectorLayerRef
void setVectorLayer(QgsVectorLayer *layer)
Sets the vector layer from which to display feature attributes.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName) const
Returns the conditional styles set for the field with matching fieldName.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QVector< QVariant > QgsLayoutTableRow
QgsLayoutTableColumns mColumns
Columns to show in table.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Single variable definition for use within a QgsExpressionContextScope.
bool readPropertiesFromElement(const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context) override
Sets multiframe state from a DOM element.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
void setDisplayOnlyVisibleFeatures(bool visibleOnly)
Sets the attribute table to only show features which are visible in a map item.
@ LayoutAttributeTable
Attribute table.
bool isEmpty() const
Returns true if the rectangle is empty.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
bool writePropertiesToElement(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Stores multiframe state within an XML DOM element.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
void resetColumns()
Resets the attribute table's columns to match the vector layer's fields.
ContentSource source() const
Returns the source for attributes shown in the table body.
QgsLayoutItemMap * map() const
Returns the layout map whose extents are controlling the features shown in the table.
void setSource(ContentSource source)
Sets the source for attributes to show in table body.
DataDefinedProperty
Data defined properties for different item types.
QPolygonF visibleExtentPolygon() const
Returns a polygon representing the current visible map extent, considering map extents and rotation.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.