-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor composer table to a base class that has vector attribute tab…
…le and normal text table as subclasses git-svn-id: http://svn.osgeo.org/qgis/trunk@13316 c8812cc2-4d05-0410-92ff-de0c093fc19c
- Loading branch information
mhugent
committed
Apr 15, 2010
1 parent
ed9fd12
commit 0bc85b2
Showing
13 changed files
with
561 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
/*************************************************************************** | ||
qgscomposerattributetable.cpp | ||
----------------------------- | ||
begin : April 2010 | ||
copyright : (C) 2010 by Marco Hugentobler | ||
email : marco at hugis dot net | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgscomposerattributetable.h" | ||
#include "qgscomposermap.h" | ||
#include "qgsmaplayerregistry.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
QgsComposerAttributeTable::QgsComposerAttributeTable( QgsComposition* composition ): QgsComposerTable( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), \ | ||
mMaximumNumberOfFeatures( 5 ), mShowOnlyVisibleFeatures( true ) | ||
{ | ||
|
||
} | ||
|
||
QgsComposerAttributeTable::~QgsComposerAttributeTable() | ||
{ | ||
|
||
} | ||
|
||
void QgsComposerAttributeTable::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) | ||
{ | ||
if ( mComposerMap && mComposerMap->isDrawing() ) | ||
{ | ||
return; | ||
} | ||
QgsComposerTable::paint( painter, itemStyle, pWidget ); | ||
} | ||
|
||
void QgsComposerAttributeTable::initializeAliasMap() | ||
{ | ||
mFieldAliasMap.clear(); | ||
if ( mVectorLayer ) | ||
{ | ||
QgsFieldMap fieldMap = mVectorLayer->pendingFields(); | ||
QgsFieldMap::const_iterator it = fieldMap.constBegin(); | ||
for ( ; it != fieldMap.constEnd(); ++it ) | ||
{ | ||
QString currentAlias = mVectorLayer->attributeAlias( it.key() ); | ||
if ( !currentAlias.isEmpty() ) | ||
{ | ||
mFieldAliasMap.insert( it.key(), currentAlias ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void QgsComposerAttributeTable::setVectorLayer( QgsVectorLayer* vl ) | ||
{ | ||
if ( vl != mVectorLayer ) | ||
{ | ||
mDisplayAttributes.clear(); | ||
mVectorLayer = vl; | ||
initializeAliasMap(); | ||
} | ||
} | ||
|
||
void QgsComposerAttributeTable::setComposerMap( const QgsComposerMap* map ) | ||
{ | ||
if ( mComposerMap ) | ||
{ | ||
QObject::disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( repaint() ) ); | ||
} | ||
mComposerMap = map; | ||
if ( mComposerMap ) | ||
{ | ||
QObject::connect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( repaint() ) ); | ||
} | ||
} | ||
|
||
bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributeMap>& attributes ) | ||
{ | ||
if ( !mVectorLayer ) | ||
{ | ||
return false; | ||
} | ||
attributes.clear(); | ||
|
||
QgsRectangle selectionRect; | ||
if ( mComposerMap && mShowOnlyVisibleFeatures ) | ||
{ | ||
selectionRect = mComposerMap->extent(); | ||
} | ||
|
||
if ( mDisplayAttributes.size() < 1 ) | ||
{ | ||
mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), selectionRect, false, true ); | ||
} | ||
else | ||
{ | ||
mVectorLayer->select( mDisplayAttributes.toList(), selectionRect, false, true ); | ||
} | ||
QgsFeature f; | ||
int counter = 0; | ||
while ( mVectorLayer->nextFeature( f ) && counter < mMaximumNumberOfFeatures ) | ||
{ | ||
attributes.push_back( f.attributeMap() ); | ||
++counter; | ||
} | ||
return true; | ||
} | ||
|
||
QMap<int, QString> QgsComposerAttributeTable::getHeaderLabels() const | ||
{ | ||
QMap<int, QString> header; | ||
if ( mVectorLayer ) | ||
{ | ||
QgsFieldMap vectorFields = mVectorLayer->pendingFields(); | ||
QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin(); | ||
for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt ) | ||
{ | ||
if ( mDisplayAttributes.size() > 0 && !mDisplayAttributes.contains( fieldIt.key() ) ) | ||
{ | ||
continue; | ||
} | ||
header.insert( fieldIt.key(), attributeDisplayName( fieldIt.key(), fieldIt.value().name() ) ); | ||
} | ||
} | ||
return header; | ||
} | ||
|
||
QString QgsComposerAttributeTable::attributeDisplayName( int attributeIndex, const QString& name ) const | ||
{ | ||
QMap<int, QString>::const_iterator it = mFieldAliasMap.find( attributeIndex ); | ||
if ( it != mFieldAliasMap.constEnd() ) | ||
{ | ||
return it.value(); | ||
} | ||
else | ||
{ | ||
return name; | ||
} | ||
} | ||
|
||
void QgsComposerAttributeTable::setSceneRect( const QRectF& rectangle ) | ||
{ | ||
double titleHeight = 2 * mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mHeaderFont ); | ||
double attributeHeight = mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mContentFont ); | ||
if (( rectangle.height() - titleHeight ) > 0 ) | ||
{ | ||
mMaximumNumberOfFeatures = ( rectangle.height() - titleHeight ) / attributeHeight; | ||
} | ||
else | ||
{ | ||
mMaximumNumberOfFeatures = 0; | ||
} | ||
QgsComposerItem::setSceneRect( rectangle ); | ||
emit maximumNumerOfFeaturesChanged( mMaximumNumberOfFeatures ); | ||
} | ||
|
||
bool QgsComposerAttributeTable::writeXML( QDomElement& elem, QDomDocument & doc ) const | ||
{ | ||
QDomElement composerTableElem = doc.createElement( "ComposerAttributeTable" ); | ||
composerTableElem.setAttribute( "showOnlyVisibleFeatures", mShowOnlyVisibleFeatures ); | ||
|
||
if ( mComposerMap ) | ||
{ | ||
composerTableElem.setAttribute( "composerMap", mComposerMap->id() ); | ||
} | ||
else | ||
{ | ||
composerTableElem.setAttribute( "composerMap", -1 ); | ||
} | ||
if ( mVectorLayer ) | ||
{ | ||
composerTableElem.setAttribute( "vectorLayer", mVectorLayer->getLayerID() ); | ||
} | ||
|
||
//display attributes | ||
QDomElement displayAttributesElem = doc.createElement( "displayAttributes" ); | ||
QSet<int>::const_iterator attIt = mDisplayAttributes.constBegin(); | ||
for ( ; attIt != mDisplayAttributes.constEnd(); ++attIt ) | ||
{ | ||
QDomElement attributeIndexElem = doc.createElement( "attributeEntry" ); | ||
attributeIndexElem.setAttribute( "index", *attIt ); | ||
displayAttributesElem.appendChild( attributeIndexElem ); | ||
} | ||
composerTableElem.appendChild( displayAttributesElem ); | ||
|
||
//alias map | ||
QDomElement aliasMapElem = doc.createElement( "attributeAliasMap" ); | ||
QMap<int, QString>::const_iterator aliasIt = mFieldAliasMap.constBegin(); | ||
for ( ; aliasIt != mFieldAliasMap.constEnd(); ++aliasIt ) | ||
{ | ||
QDomElement mapEntryElem = doc.createElement( "aliasEntry" ); | ||
mapEntryElem.setAttribute( "key", aliasIt.key() ); | ||
mapEntryElem.setAttribute( "value", aliasIt.value() ); | ||
aliasMapElem.appendChild( mapEntryElem ); | ||
} | ||
composerTableElem.appendChild( aliasMapElem ); | ||
bool ok = tableWriteXML( composerTableElem, doc ); | ||
elem.appendChild( composerTableElem ); | ||
return ok; | ||
} | ||
|
||
bool QgsComposerAttributeTable::readXML( const QDomElement& itemElem, const QDomDocument& doc ) | ||
{ | ||
if ( itemElem.isNull() ) | ||
{ | ||
return false; | ||
} | ||
|
||
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt(); | ||
mShowOnlyVisibleFeatures = itemElem.attribute( "showOnlyVisibleFeatures", "1" ).toInt(); | ||
|
||
//composer map | ||
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt(); | ||
if ( composerMapId == -1 ) | ||
{ | ||
mComposerMap = 0; | ||
} | ||
|
||
if ( composition() ) | ||
{ | ||
mComposerMap = composition()->getComposerMapById( composerMapId ); | ||
} | ||
else | ||
{ | ||
mComposerMap = 0; | ||
} | ||
|
||
//vector layer | ||
QString layerId = itemElem.attribute( "vectorLayer", "not_existing" ); | ||
if ( layerId == "not_existing" ) | ||
{ | ||
mVectorLayer = 0; | ||
} | ||
else | ||
{ | ||
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId ); | ||
if ( ml ) | ||
{ | ||
mVectorLayer = dynamic_cast<QgsVectorLayer*>( ml ); | ||
} | ||
} | ||
|
||
//restore display attribute map | ||
mDisplayAttributes.clear(); | ||
QDomNodeList displayAttributeList = itemElem.elementsByTagName( "displayAttributes" ); | ||
if ( displayAttributeList.size() > 0 ) | ||
{ | ||
QDomElement displayAttributesElem = displayAttributeList.at( 0 ).toElement(); | ||
QDomNodeList attributeEntryList = displayAttributesElem.elementsByTagName( "attributeEntry" ); | ||
for ( int i = 0; i < attributeEntryList.size(); ++i ) | ||
{ | ||
QDomElement attributeEntryElem = attributeEntryList.at( i ).toElement(); | ||
int index = attributeEntryElem.attribute( "index", "-1" ).toInt(); | ||
if ( index != -1 ) | ||
{ | ||
mDisplayAttributes.insert( index ); | ||
} | ||
} | ||
} | ||
|
||
//restore alias map | ||
mFieldAliasMap.clear(); | ||
QDomNodeList aliasMapNodeList = itemElem.elementsByTagName( "attributeAliasMap" ); | ||
if ( aliasMapNodeList.size() > 0 ) | ||
{ | ||
QDomElement attributeAliasMapElem = aliasMapNodeList.at( 0 ).toElement(); | ||
QDomNodeList aliasMepEntryList = attributeAliasMapElem.elementsByTagName( "aliasEntry" ); | ||
for ( int i = 0; i < aliasMepEntryList.size(); ++i ) | ||
{ | ||
QDomElement aliasEntryElem = aliasMepEntryList.at( i ).toElement(); | ||
int key = aliasEntryElem.attribute( "key", "-1" ).toInt(); | ||
QString value = aliasEntryElem.attribute( "value", "" ); | ||
mFieldAliasMap.insert( key, value ); | ||
} | ||
} | ||
return tableReadXML( itemElem, doc ); | ||
} |
Oops, something went wrong.