|
| 1 | +/*************************************************************************** |
| 2 | + qgscomposerattributetable.cpp |
| 3 | + ----------------------------- |
| 4 | + begin : April 2010 |
| 5 | + copyright : (C) 2010 by Marco Hugentobler |
| 6 | + email : marco at hugis dot net |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgscomposerattributetable.h" |
| 19 | +#include "qgscomposermap.h" |
| 20 | +#include "qgsmaplayerregistry.h" |
| 21 | +#include "qgsvectorlayer.h" |
| 22 | + |
| 23 | +QgsComposerAttributeTable::QgsComposerAttributeTable( QgsComposition* composition ): QgsComposerTable( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), \ |
| 24 | + mMaximumNumberOfFeatures( 5 ), mShowOnlyVisibleFeatures( true ) |
| 25 | +{ |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +QgsComposerAttributeTable::~QgsComposerAttributeTable() |
| 30 | +{ |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +void QgsComposerAttributeTable::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) |
| 35 | +{ |
| 36 | + if ( mComposerMap && mComposerMap->isDrawing() ) |
| 37 | + { |
| 38 | + return; |
| 39 | + } |
| 40 | + QgsComposerTable::paint( painter, itemStyle, pWidget ); |
| 41 | +} |
| 42 | + |
| 43 | +void QgsComposerAttributeTable::initializeAliasMap() |
| 44 | +{ |
| 45 | + mFieldAliasMap.clear(); |
| 46 | + if ( mVectorLayer ) |
| 47 | + { |
| 48 | + QgsFieldMap fieldMap = mVectorLayer->pendingFields(); |
| 49 | + QgsFieldMap::const_iterator it = fieldMap.constBegin(); |
| 50 | + for ( ; it != fieldMap.constEnd(); ++it ) |
| 51 | + { |
| 52 | + QString currentAlias = mVectorLayer->attributeAlias( it.key() ); |
| 53 | + if ( !currentAlias.isEmpty() ) |
| 54 | + { |
| 55 | + mFieldAliasMap.insert( it.key(), currentAlias ); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void QgsComposerAttributeTable::setVectorLayer( QgsVectorLayer* vl ) |
| 62 | +{ |
| 63 | + if ( vl != mVectorLayer ) |
| 64 | + { |
| 65 | + mDisplayAttributes.clear(); |
| 66 | + mVectorLayer = vl; |
| 67 | + initializeAliasMap(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void QgsComposerAttributeTable::setComposerMap( const QgsComposerMap* map ) |
| 72 | +{ |
| 73 | + if ( mComposerMap ) |
| 74 | + { |
| 75 | + QObject::disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( repaint() ) ); |
| 76 | + } |
| 77 | + mComposerMap = map; |
| 78 | + if ( mComposerMap ) |
| 79 | + { |
| 80 | + QObject::connect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( repaint() ) ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributeMap>& attributes ) |
| 85 | +{ |
| 86 | + if ( !mVectorLayer ) |
| 87 | + { |
| 88 | + return false; |
| 89 | + } |
| 90 | + attributes.clear(); |
| 91 | + |
| 92 | + QgsRectangle selectionRect; |
| 93 | + if ( mComposerMap && mShowOnlyVisibleFeatures ) |
| 94 | + { |
| 95 | + selectionRect = mComposerMap->extent(); |
| 96 | + } |
| 97 | + |
| 98 | + if ( mDisplayAttributes.size() < 1 ) |
| 99 | + { |
| 100 | + mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), selectionRect, false, true ); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + mVectorLayer->select( mDisplayAttributes.toList(), selectionRect, false, true ); |
| 105 | + } |
| 106 | + QgsFeature f; |
| 107 | + int counter = 0; |
| 108 | + while ( mVectorLayer->nextFeature( f ) && counter < mMaximumNumberOfFeatures ) |
| 109 | + { |
| 110 | + attributes.push_back( f.attributeMap() ); |
| 111 | + ++counter; |
| 112 | + } |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +QMap<int, QString> QgsComposerAttributeTable::getHeaderLabels() const |
| 117 | +{ |
| 118 | + QMap<int, QString> header; |
| 119 | + if ( mVectorLayer ) |
| 120 | + { |
| 121 | + QgsFieldMap vectorFields = mVectorLayer->pendingFields(); |
| 122 | + QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin(); |
| 123 | + for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt ) |
| 124 | + { |
| 125 | + if ( mDisplayAttributes.size() > 0 && !mDisplayAttributes.contains( fieldIt.key() ) ) |
| 126 | + { |
| 127 | + continue; |
| 128 | + } |
| 129 | + header.insert( fieldIt.key(), attributeDisplayName( fieldIt.key(), fieldIt.value().name() ) ); |
| 130 | + } |
| 131 | + } |
| 132 | + return header; |
| 133 | +} |
| 134 | + |
| 135 | +QString QgsComposerAttributeTable::attributeDisplayName( int attributeIndex, const QString& name ) const |
| 136 | +{ |
| 137 | + QMap<int, QString>::const_iterator it = mFieldAliasMap.find( attributeIndex ); |
| 138 | + if ( it != mFieldAliasMap.constEnd() ) |
| 139 | + { |
| 140 | + return it.value(); |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + return name; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void QgsComposerAttributeTable::setSceneRect( const QRectF& rectangle ) |
| 149 | +{ |
| 150 | + double titleHeight = 2 * mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mHeaderFont ); |
| 151 | + double attributeHeight = mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mContentFont ); |
| 152 | + if (( rectangle.height() - titleHeight ) > 0 ) |
| 153 | + { |
| 154 | + mMaximumNumberOfFeatures = ( rectangle.height() - titleHeight ) / attributeHeight; |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + mMaximumNumberOfFeatures = 0; |
| 159 | + } |
| 160 | + QgsComposerItem::setSceneRect( rectangle ); |
| 161 | + emit maximumNumerOfFeaturesChanged( mMaximumNumberOfFeatures ); |
| 162 | +} |
| 163 | + |
| 164 | +bool QgsComposerAttributeTable::writeXML( QDomElement& elem, QDomDocument & doc ) const |
| 165 | +{ |
| 166 | + QDomElement composerTableElem = doc.createElement( "ComposerAttributeTable" ); |
| 167 | + composerTableElem.setAttribute( "showOnlyVisibleFeatures", mShowOnlyVisibleFeatures ); |
| 168 | + |
| 169 | + if ( mComposerMap ) |
| 170 | + { |
| 171 | + composerTableElem.setAttribute( "composerMap", mComposerMap->id() ); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + composerTableElem.setAttribute( "composerMap", -1 ); |
| 176 | + } |
| 177 | + if ( mVectorLayer ) |
| 178 | + { |
| 179 | + composerTableElem.setAttribute( "vectorLayer", mVectorLayer->getLayerID() ); |
| 180 | + } |
| 181 | + |
| 182 | + //display attributes |
| 183 | + QDomElement displayAttributesElem = doc.createElement( "displayAttributes" ); |
| 184 | + QSet<int>::const_iterator attIt = mDisplayAttributes.constBegin(); |
| 185 | + for ( ; attIt != mDisplayAttributes.constEnd(); ++attIt ) |
| 186 | + { |
| 187 | + QDomElement attributeIndexElem = doc.createElement( "attributeEntry" ); |
| 188 | + attributeIndexElem.setAttribute( "index", *attIt ); |
| 189 | + displayAttributesElem.appendChild( attributeIndexElem ); |
| 190 | + } |
| 191 | + composerTableElem.appendChild( displayAttributesElem ); |
| 192 | + |
| 193 | + //alias map |
| 194 | + QDomElement aliasMapElem = doc.createElement( "attributeAliasMap" ); |
| 195 | + QMap<int, QString>::const_iterator aliasIt = mFieldAliasMap.constBegin(); |
| 196 | + for ( ; aliasIt != mFieldAliasMap.constEnd(); ++aliasIt ) |
| 197 | + { |
| 198 | + QDomElement mapEntryElem = doc.createElement( "aliasEntry" ); |
| 199 | + mapEntryElem.setAttribute( "key", aliasIt.key() ); |
| 200 | + mapEntryElem.setAttribute( "value", aliasIt.value() ); |
| 201 | + aliasMapElem.appendChild( mapEntryElem ); |
| 202 | + } |
| 203 | + composerTableElem.appendChild( aliasMapElem ); |
| 204 | + bool ok = tableWriteXML( composerTableElem, doc ); |
| 205 | + elem.appendChild( composerTableElem ); |
| 206 | + return ok; |
| 207 | +} |
| 208 | + |
| 209 | +bool QgsComposerAttributeTable::readXML( const QDomElement& itemElem, const QDomDocument& doc ) |
| 210 | +{ |
| 211 | + if ( itemElem.isNull() ) |
| 212 | + { |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt(); |
| 217 | + mShowOnlyVisibleFeatures = itemElem.attribute( "showOnlyVisibleFeatures", "1" ).toInt(); |
| 218 | + |
| 219 | + //composer map |
| 220 | + int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt(); |
| 221 | + if ( composerMapId == -1 ) |
| 222 | + { |
| 223 | + mComposerMap = 0; |
| 224 | + } |
| 225 | + |
| 226 | + if ( composition() ) |
| 227 | + { |
| 228 | + mComposerMap = composition()->getComposerMapById( composerMapId ); |
| 229 | + } |
| 230 | + else |
| 231 | + { |
| 232 | + mComposerMap = 0; |
| 233 | + } |
| 234 | + |
| 235 | + //vector layer |
| 236 | + QString layerId = itemElem.attribute( "vectorLayer", "not_existing" ); |
| 237 | + if ( layerId == "not_existing" ) |
| 238 | + { |
| 239 | + mVectorLayer = 0; |
| 240 | + } |
| 241 | + else |
| 242 | + { |
| 243 | + QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId ); |
| 244 | + if ( ml ) |
| 245 | + { |
| 246 | + mVectorLayer = dynamic_cast<QgsVectorLayer*>( ml ); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + //restore display attribute map |
| 251 | + mDisplayAttributes.clear(); |
| 252 | + QDomNodeList displayAttributeList = itemElem.elementsByTagName( "displayAttributes" ); |
| 253 | + if ( displayAttributeList.size() > 0 ) |
| 254 | + { |
| 255 | + QDomElement displayAttributesElem = displayAttributeList.at( 0 ).toElement(); |
| 256 | + QDomNodeList attributeEntryList = displayAttributesElem.elementsByTagName( "attributeEntry" ); |
| 257 | + for ( int i = 0; i < attributeEntryList.size(); ++i ) |
| 258 | + { |
| 259 | + QDomElement attributeEntryElem = attributeEntryList.at( i ).toElement(); |
| 260 | + int index = attributeEntryElem.attribute( "index", "-1" ).toInt(); |
| 261 | + if ( index != -1 ) |
| 262 | + { |
| 263 | + mDisplayAttributes.insert( index ); |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + //restore alias map |
| 269 | + mFieldAliasMap.clear(); |
| 270 | + QDomNodeList aliasMapNodeList = itemElem.elementsByTagName( "attributeAliasMap" ); |
| 271 | + if ( aliasMapNodeList.size() > 0 ) |
| 272 | + { |
| 273 | + QDomElement attributeAliasMapElem = aliasMapNodeList.at( 0 ).toElement(); |
| 274 | + QDomNodeList aliasMepEntryList = attributeAliasMapElem.elementsByTagName( "aliasEntry" ); |
| 275 | + for ( int i = 0; i < aliasMepEntryList.size(); ++i ) |
| 276 | + { |
| 277 | + QDomElement aliasEntryElem = aliasMepEntryList.at( i ).toElement(); |
| 278 | + int key = aliasEntryElem.attribute( "key", "-1" ).toInt(); |
| 279 | + QString value = aliasEntryElem.attribute( "value", "" ); |
| 280 | + mFieldAliasMap.insert( key, value ); |
| 281 | + } |
| 282 | + } |
| 283 | + return tableReadXML( itemElem, doc ); |
| 284 | +} |
0 commit comments