28 : mCurrentSortColumn( 0 ), mAscending( true )
35 QVariant v1 = m1[mCurrentSortColumn];
36 QVariant v2 = m2[mCurrentSortColumn];
41 if ( v1.isNull() && v2.isNull() )
45 else if ( v1.isNull() )
49 else if ( v2.isNull() )
60 case QVariant::LongLong:
61 case QVariant::ULongLong:
62 less = v1.toLongLong() < v2.toLongLong();
65 case QVariant::Double:
66 less = v1.toDouble() < v2.toDouble();
70 less = v1.toDate() < v2.toDate();
73 case QVariant::DateTime:
74 less = v1.toDateTime() < v2.toDateTime();
78 less = v1.toTime() < v2.toTime();
83 less = v1.toString().localeAwareCompare( v2.toString() ) < 0;
88 return ( mAscending ? less : !less );
98 , mMaximumNumberOfFeatures( 5 )
99 , mShowOnlyVisibleFeatures( false )
100 , mFilterFeatures( false )
101 , mFeatureFilter(
"" )
105 QMap<QString, QgsMapLayer*>::const_iterator mapIt = layerMap.constBegin();
106 for ( ; mapIt != layerMap.constEnd(); ++mapIt )
137 if ( mComposerMap && mComposerMap->
isDrawing() )
150 if ( layer == mVectorLayer )
159 QObject::disconnect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT(
refreshAttributes() ) );
162 mVectorLayer = layer;
169 QObject::connect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT(
refreshAttributes() ) );
185 for (
int idx = 0; idx < fields.
count(); ++idx )
197 if ( map == mComposerMap )
206 QObject::disconnect( mComposerMap, SIGNAL( extentChanged() ),
this, SLOT(
refreshAttributes() ) );
212 QObject::connect( mComposerMap, SIGNAL( extentChanged() ),
this, SLOT(
refreshAttributes() ) );
219 if ( features == mMaximumNumberOfFeatures )
224 mMaximumNumberOfFeatures = features;
230 if ( visibleOnly == mShowOnlyVisibleFeatures )
235 mShowOnlyVisibleFeatures = visibleOnly;
241 if ( filter == mFilterFeatures )
246 mFilterFeatures = filter;
252 if ( expression == mFeatureFilter )
257 mFeatureFilter = expression;
263 return fieldsToDisplay().toSet();
281 QSet<int>::const_iterator attIt = attr.constBegin();
282 for ( ; attIt != attr.constEnd(); ++attIt )
284 int attrIdx = ( *attIt );
285 if ( !fields.
exists( attrIdx ) )
299 for (
int idx = 0; idx < fields.
count(); ++idx )
319 QList<QgsComposerTableColumn*>::const_iterator columnIt =
mColumns.constBegin();
320 for ( ; columnIt !=
mColumns.constEnd(); ++columnIt )
322 int attrIdx = mVectorLayer->
fieldNameIndex(( *columnIt )->attribute() );
323 fieldAliasMap.insert( attrIdx, ( *columnIt )->heading() );
328 void QgsComposerAttributeTable::restoreFieldAliasMap(
const QMap<int, QString>& map )
335 QList<QgsComposerTableColumn*>::const_iterator columnIt =
mColumns.constBegin();
336 for ( ; columnIt !=
mColumns.constEnd(); ++columnIt )
338 int attrIdx = mVectorLayer->
fieldNameIndex(( *columnIt )->attribute() );
339 if ( map.contains( attrIdx ) )
341 ( *columnIt )->setHeading( map.value( attrIdx ) );
353 restoreFieldAliasMap( map );
357 QList<int> QgsComposerAttributeTable::fieldsToDisplay()
const 362 QList<QgsComposerTableColumn*>::const_iterator columnIt =
mColumns.constBegin();
363 for ( ; columnIt !=
mColumns.constEnd(); ++columnIt )
365 int idx = mVectorLayer->
fieldNameIndex(( *columnIt )->attribute() );
366 fields.append( idx );
378 attributeMaps.clear();
381 QScopedPointer<QgsExpression> filterExpression;
382 bool activeFilter =
false;
383 if ( mFilterFeatures && !mFeatureFilter.isEmpty() )
385 filterExpression.reset(
new QgsExpression( mFeatureFilter ) );
386 if ( !filterExpression->hasParserError() )
393 if ( mComposerMap && mShowOnlyVisibleFeatures )
413 if ( !selectionRect.
isEmpty() )
422 while ( fit.
nextFeature( f ) && counter < mMaximumNumberOfFeatures )
425 if ( activeFilter && !filterExpression.isNull() )
427 QVariant result = filterExpression->evaluate( &f, mVectorLayer->
pendingFields() );
429 if ( !result.toBool() )
437 QList<QgsComposerTableColumn*>::const_iterator columnIt =
mColumns.constBegin();
439 for ( ; columnIt !=
mColumns.constEnd(); ++columnIt )
441 int idx = mVectorLayer->
fieldNameIndex(( *columnIt )->attribute() );
444 attributeMaps.last().insert( i, f.
attributes()[idx] );
452 QVariant value = expression->
evaluate( f );
453 attributeMaps.last().insert( i, value.toString() );
464 for (
int i = sortColumns.size() - 1; i >= 0; --i )
468 qStableSort( attributeMaps.begin(), attributeMaps.end(), c );
475 void QgsComposerAttributeTable::removeLayer( QString layerId )
479 if ( layerId == mVectorLayer->
id() )
503 QList<QgsComposerTableColumn*>::iterator columnIt =
mColumns.begin();
504 for ( ; columnIt !=
mColumns.end(); ++columnIt )
506 ( *columnIt )->setSortByRank( 0 );
510 QList< QPair<int, bool > >::const_iterator sortedColumnIt = att.constBegin();
512 for ( ; sortedColumnIt != att.constEnd(); ++sortedColumnIt )
514 if (( *sortedColumnIt ).first >=
mColumns.length() )
518 mColumns[( *sortedColumnIt ).first ]->setSortByRank( rank );
519 mColumns[( *sortedColumnIt ).first ]->setSortOrder(( *sortedColumnIt ).second ? Qt::AscendingOrder : Qt::DescendingOrder );
526 static bool columnsBySortRank( QPair<int, QgsComposerTableColumn* > a, QPair<int, QgsComposerTableColumn* > b )
528 return a.second->sortByRank() < b.second->sortByRank();
534 QList< QPair<int, QgsComposerTableColumn* > > sortedColumns;
535 QList<QgsComposerTableColumn*>::const_iterator columnIt =
mColumns.constBegin();
537 for ( ; columnIt !=
mColumns.constEnd(); ++columnIt )
539 if (( *columnIt )->sortByRank() > 0 )
541 sortedColumns.append( qMakePair( idx, *columnIt ) );
550 QList<QPair<int, bool> > attributesBySortRank;
551 QList< QPair<int, QgsComposerTableColumn* > >::const_iterator sortedColumnIt = sortedColumns.constBegin();
552 for ( ; sortedColumnIt != sortedColumns.constEnd(); ++sortedColumnIt )
555 attributesBySortRank.append( qMakePair(( *sortedColumnIt ).first,
556 ( *sortedColumnIt ).second->sortOrder() == Qt::AscendingOrder ) );
558 return attributesBySortRank;
563 QDomElement composerTableElem = doc.createElement(
"ComposerAttributeTable" );
564 composerTableElem.setAttribute(
"showOnlyVisibleFeatures", mShowOnlyVisibleFeatures );
565 composerTableElem.setAttribute(
"maxFeatures", mMaximumNumberOfFeatures );
566 composerTableElem.setAttribute(
"filterFeatures", mFilterFeatures ?
"true" :
"false" );
567 composerTableElem.setAttribute(
"featureFilter", mFeatureFilter );
571 composerTableElem.setAttribute(
"composerMap", mComposerMap->
id() );
575 composerTableElem.setAttribute(
"composerMap", -1 );
579 composerTableElem.setAttribute(
"vectorLayer", mVectorLayer->
id() );
582 elem.appendChild( composerTableElem );
589 if ( itemElem.isNull() )
600 mShowOnlyVisibleFeatures = itemElem.attribute(
"showOnlyVisibleFeatures",
"1" ).toInt();
601 mFilterFeatures = itemElem.attribute(
"filterFeatures",
"false" ) ==
"true" ?
true :
false;
602 mFeatureFilter = itemElem.attribute(
"featureFilter",
"" );
605 int composerMapId = itemElem.attribute(
"composerMap",
"-1" ).toInt();
606 if ( composerMapId == -1 )
623 QObject::connect( mComposerMap, SIGNAL( extentChanged() ),
this, SLOT(
refreshAttributes() ) );
627 QString layerId = itemElem.attribute(
"vectorLayer",
"not_existing" );
628 if ( layerId ==
"not_existing" )
641 QObject::connect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT(
refreshAttributes() ) );
648 QDomNodeList displayAttributeList = itemElem.elementsByTagName(
"displayAttributes" );
649 if ( displayAttributeList.size() > 0 )
651 QDomElement displayAttributesElem = displayAttributeList.at( 0 ).toElement();
652 QDomNodeList attributeEntryList = displayAttributesElem.elementsByTagName(
"attributeEntry" );
653 for (
int i = 0; i < attributeEntryList.size(); ++i )
655 QDomElement attributeEntryElem = attributeEntryList.at( i ).toElement();
656 int index = attributeEntryElem.attribute(
"index",
"-1" ).toInt();
659 displayAttributes.insert( index );
667 QDomNodeList aliasMapNodeList = itemElem.elementsByTagName(
"attributeAliasMap" );
668 if ( aliasMapNodeList.size() > 0 )
670 QDomElement attributeAliasMapElem = aliasMapNodeList.at( 0 ).toElement();
671 QDomNodeList aliasMepEntryList = attributeAliasMapElem.elementsByTagName(
"aliasEntry" );
672 for (
int i = 0; i < aliasMepEntryList.size(); ++i )
674 QDomElement aliasEntryElem = aliasMepEntryList.at( i ).toElement();
675 int key = aliasEntryElem.attribute(
"key",
"-1" ).toInt();
676 QString value = aliasEntryElem.attribute(
"value",
"" );
677 fieldAliasMap.insert( key, value );
679 restoreFieldAliasMap( fieldAliasMap );
683 QDomElement sortColumnsElem = itemElem.firstChildElement(
"sortColumns" );
684 if ( !sortColumnsElem.isNull() && mVectorLayer )
686 QDomNodeList
columns = sortColumnsElem.elementsByTagName(
"column" );
689 for (
int i = 0; i < columns.size(); ++i )
691 QDomElement columnElem = columns.
at( i ).toElement();
692 int attribute = columnElem.attribute(
"index" ).toInt();
693 Qt::SortOrder order = columnElem.attribute(
"ascending" ) ==
"true" ? Qt::AscendingOrder : Qt::DescendingOrder;
695 QList<QgsComposerTableColumn*>::iterator columnIt =
mColumns.begin();
696 for ( ; columnIt !=
mColumns.end(); ++columnIt )
698 if (( *columnIt )->attribute() == fields[attribute].name() )
700 ( *columnIt )->setSortByRank( i + 1 );
701 ( *columnIt )->setSortOrder( order );
709 mMaximumNumberOfFeatures = itemElem.attribute(
"maxFeatures",
"5" ).toInt();
Class for parsing and evaluation of expressions (formerly called "search strings").
void setSceneRect(const QRectF &rectangle) override
Adapts mMaximumNumberOfFeatures depending on the rectangle height.
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Base class for all map layer types.
QList< QPair< int, bool > > sortAttributes() const
Returns the attributes used to sort the table's features.
bool isEmpty() const
test if rectangle is empty.
void setAscending(bool asc)
Sets sort order for column sorting.
QMap< int, QVariant > QgsAttributeMap
void setAttribute(QString attribute)
Sets the attribute name or expression used for the column's values.
Use exact geometry intersection (slower) instead of bounding boxes.
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
Q_DECL_DEPRECATED QSet< int > displayAttributes() const
Returns the attributes fields which are shown by the table.
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
void itemChanged()
Emitted when the item changes.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
void setComposerMap(const QgsComposerMap *map)
Sets the composer map to use to limit the extent of features shown in the attribute table...
static QgsMapLayerRegistry * instance()
bool getFeatureAttributes(QList< QgsAttributeMap > &attributeMaps) override
Queries the attribute table's vector layer for attributes to show in the table.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
Container of fields for a vector layer.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
QList< QgsComposerTableColumn * > * columns()
Returns a pointer to the list of QgsComposerTableColumns shown in the table.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
void setHeading(QString heading)
Sets the heading for a column, which is the value displayed in the columns header cell...
void setDisplayAttributes(const QSet< int > &attr, bool refresh=true)
Sets the attributes to display in the table.
bool operator()(const QgsAttributeMap &m1, const QgsAttributeMap &m2)
void setFilterFeatures(bool filter)
Sets whether the feature filter is active for the attribute table.
void setDisplayOnlyVisibleFeatures(bool visibleOnly)
Sets attribute table to only show features which are visible in a composer map item.
A class to display feature attributes in the print composer.
bool isDrawing() const
True if a draw is already in progress.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Reads the properties specific to an attribute table from xml.
void setVectorLayer(QgsVectorLayer *layer)
Sets the vector layer from which to display feature attributes.
bool exists(int i) const
Return if a field index is valid.
Q_DECL_DEPRECATED void setSortAttributes(const QList< QPair< int, bool > > att)
Sets the attributes to use to sort the table's features.
QString attributeDisplayName(int attributeIndex) const
Convenience function that returns the attribute alias if defined or the field name else...
const QgsComposition * composition() const
Returns the composition the item is attached to.
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
static bool columnsBySortRank(QPair< int, QgsComposerTableColumn * > a, QPair< int, QgsComposerTableColumn * > b)
bool tableWriteXML(QDomElement &itemElem, QDomDocument &doc) const
Writes common table properties to xml for storage.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void setMaximumNumberOfFeatures(int features)
Sets the maximum number of features shown by the table.
const QgsAttributes & attributes() const
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
int count() const
Return number of items.
Q_DECL_DEPRECATED void setFieldAliasMap(const QMap< int, QString > &map)
Sets the attribute field aliases, which control how fields are named in the table's header row...
QgsComposerAttributeTable(QgsComposition *composition)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
Stores properties of a column in a QgsComposerTable.
void setCurrentRowNumber(int rowNumber)
Set the number for $rownum special column.
Graphics scene for map printing.
void resetColumns()
Resets the attribute table's columns to match the vector layer's fields.
Object representing map window.
QgsRectangle * currentMapExtent()
Returns a pointer to the current map extent, which is either the original user specified extent or th...
QgsComposerAttributeTableCompare()
QList< QgsComposerTableColumn * > mColumns
void setFeatureFilter(const QString &expression)
Sets the expression used for filtering features in the table.
int id() const
Get identification number.
QgsComposition * mComposition
virtual void refreshAttributes()
Refreshes the attributes shown in the table by querying the vector layer for new data.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
Q_DECL_DEPRECATED QMap< int, QString > fieldAliasMap() const
Returns the attribute field aliases, which control how fields are named in the table's header row...
bool tableReadXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads the table's common properties from xml.
virtual void adjustFrameToSize()
Adapts the size of the frame to match the content.
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Writes properties specific to attribute tables.
void setSortColumn(int col)
Sets column number to sort by.
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
const QMap< QString, QgsMapLayer * > & mapLayers()
Retrieve the mapLayers collection (mainly intended for use by projection)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
~QgsComposerAttributeTable()
QgsAtlasComposition & atlasComposition()
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
Custom exception class for Coordinate Reference System related exceptions.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
bool nextFeature(QgsFeature &f)
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
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.
QgsFeatureRequest & setFlags(Flags flags)
Set flags that affect how features will be fetched.
Helper class for sorting tables, takes into account sorting column and ascending / descending...
QgsFeatureRequest & setFilterRect(const QgsRectangle &rect)
Set rectangle from which features will be taken.