|
| 1 | +/*************************************************************************** |
| 2 | + qgscomposertable.cpp |
| 3 | + -------------------- |
| 4 | + begin : January 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 "qgscomposertable.h" |
| 19 | +#include "qgscomposermap.h" |
| 20 | +#include "qgslogger.h" |
| 21 | +#include "qgsmaplayerregistry.h" |
| 22 | +#include "qgsvectorlayer.h" |
| 23 | +#include <QPainter> |
| 24 | + |
| 25 | +QgsComposerTable::QgsComposerTable( QgsComposition* composition ): QgsComposerItem( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), mMaximumNumberOfFeatures( 5 ) |
| 26 | +{ |
| 27 | + mLineTextDistance = 1; |
| 28 | +} |
| 29 | + |
| 30 | +QgsComposerTable::~QgsComposerTable() |
| 31 | +{ |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +void QgsComposerTable::setComposerMap( const QgsComposerMap* map ) |
| 36 | +{ |
| 37 | + if ( mComposerMap ) |
| 38 | + { |
| 39 | + QObject::disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( repaint() ) ); |
| 40 | + } |
| 41 | + mComposerMap = map; |
| 42 | + if ( mComposerMap ) |
| 43 | + { |
| 44 | + QObject::connect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( repaint() ) ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) |
| 49 | +{ |
| 50 | + if ( !painter ) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if ( mComposerMap && mComposerMap->isDrawing() ) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + //getFeatureAttributes |
| 61 | + QList<QgsAttributeMap> attributeList; |
| 62 | + if ( !getFeatureAttributes( attributeList ) ) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + QMap<int, double> maxColumnWidthMap; |
| 68 | + //check how much space each column needs |
| 69 | + calculateMaxColumnWidths( maxColumnWidthMap, attributeList ); |
| 70 | + //adapt item fram to max width / height |
| 71 | + adaptItemFrame( maxColumnWidthMap, attributeList ); |
| 72 | + |
| 73 | + //now draw the text |
| 74 | + double currentX = 0; |
| 75 | + double currentY; |
| 76 | + |
| 77 | + QgsFieldMap vectorFields = mVectorLayer->pendingFields(); |
| 78 | + QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin(); |
| 79 | + for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt ) |
| 80 | + { |
| 81 | + currentY = 0; |
| 82 | + currentY += mLineTextDistance; |
| 83 | + currentY += fontAscentMillimeters( mHeaderFont ); |
| 84 | + currentX += mLineTextDistance; |
| 85 | + drawText( painter, currentX, currentY, fieldIt.value().name(), mHeaderFont ); |
| 86 | + |
| 87 | + currentY += mLineTextDistance; |
| 88 | + |
| 89 | + //draw the attribute values |
| 90 | + QList<QgsAttributeMap>::const_iterator attIt = attributeList.begin(); |
| 91 | + for ( ; attIt != attributeList.end(); ++attIt ) |
| 92 | + { |
| 93 | + currentY += fontAscentMillimeters( mContentFont ); |
| 94 | + currentY += mLineTextDistance; |
| 95 | + |
| 96 | + QgsAttributeMap currentAttributeMap = *attIt; |
| 97 | + QgsAttributeMap::const_iterator attMapIt = currentAttributeMap.find( fieldIt.key() ); |
| 98 | + if ( attMapIt != currentAttributeMap.constEnd() ) |
| 99 | + { |
| 100 | + drawText( painter, currentX, currentY, attMapIt.value().toString(), mContentFont ); |
| 101 | + } |
| 102 | + |
| 103 | + currentY += mLineTextDistance; |
| 104 | + } |
| 105 | + |
| 106 | + currentX += mLineTextDistance; |
| 107 | + currentX += maxColumnWidthMap[fieldIt.key()]; |
| 108 | + } |
| 109 | + |
| 110 | + //and the borders |
| 111 | + painter->setPen( mGridPen ); |
| 112 | + drawHorizontalGridLines( painter, attributeList.size() ); |
| 113 | + drawVerticalGridLines( painter, maxColumnWidthMap ); |
| 114 | + |
| 115 | + //draw frame and selection boxes if necessary |
| 116 | + drawFrame( painter ); |
| 117 | + if ( isSelected() ) |
| 118 | + { |
| 119 | + drawSelectionBoxes( painter ); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +bool QgsComposerTable::writeXML( QDomElement& elem, QDomDocument & doc ) const |
| 124 | +{ |
| 125 | + QDomElement composerTableElem = doc.createElement( "ComposerTable" ); |
| 126 | + composerTableElem.setAttribute( "maxFeatures", mMaximumNumberOfFeatures ); |
| 127 | + composerTableElem.setAttribute( "lineTextDist", mLineTextDistance ); |
| 128 | + composerTableElem.setAttribute( "headerFont", mHeaderFont.toString() ); |
| 129 | + composerTableElem.setAttribute( "contentFont", mContentFont.toString() ); |
| 130 | + if ( mComposerMap ) |
| 131 | + { |
| 132 | + composerTableElem.setAttribute( "composerMap", mComposerMap->id() ); |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + composerTableElem.setAttribute( "composerMap", -1 ); |
| 137 | + } |
| 138 | + if ( mVectorLayer ) |
| 139 | + { |
| 140 | + composerTableElem.setAttribute( "vectorLayer", mVectorLayer->getLayerID() ); |
| 141 | + } |
| 142 | + elem.appendChild( composerTableElem ); |
| 143 | + return _writeXML( composerTableElem, doc );; |
| 144 | +} |
| 145 | + |
| 146 | +bool QgsComposerTable::readXML( const QDomElement& itemElem, const QDomDocument& doc ) |
| 147 | +{ |
| 148 | + if ( itemElem.isNull() ) |
| 149 | + { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt(); |
| 154 | + mHeaderFont.fromString( itemElem.attribute( "headerFont", "" ) ); |
| 155 | + mContentFont.fromString( itemElem.attribute( "contentFont", "" ) ); |
| 156 | + mLineTextDistance = itemElem.attribute( "lineTextDist", "1.0" ).toDouble(); |
| 157 | + |
| 158 | + //composer map |
| 159 | + int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt(); |
| 160 | + if ( composerMapId == -1 ) |
| 161 | + { |
| 162 | + mComposerMap = 0; |
| 163 | + } |
| 164 | + |
| 165 | + if ( composition() ) |
| 166 | + { |
| 167 | + mComposerMap = composition()->getComposerMapById( composerMapId ); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + mComposerMap = 0; |
| 172 | + } |
| 173 | + |
| 174 | + //vector layer |
| 175 | + QString layerId = itemElem.attribute( "vectorLayer", "not_existing" ); |
| 176 | + if ( layerId == "not_existing" ) |
| 177 | + { |
| 178 | + mVectorLayer = 0; |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId ); |
| 183 | + if ( ml ) |
| 184 | + { |
| 185 | + mVectorLayer = dynamic_cast<QgsVectorLayer*>( ml ); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + //restore general composer item properties |
| 190 | + QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" ); |
| 191 | + if ( composerItemList.size() > 0 ) |
| 192 | + { |
| 193 | + QDomElement composerItemElem = composerItemList.at( 0 ).toElement(); |
| 194 | + _readXML( composerItemElem, doc ); |
| 195 | + } |
| 196 | + return true; |
| 197 | +} |
| 198 | + |
| 199 | +bool QgsComposerTable::getFeatureAttributes( QList<QgsAttributeMap>& attributes ) |
| 200 | +{ |
| 201 | + if ( !mVectorLayer ) |
| 202 | + { |
| 203 | + return false; |
| 204 | + } |
| 205 | + attributes.clear(); |
| 206 | + |
| 207 | + QgsRectangle selectionRect; |
| 208 | + if ( mComposerMap ) |
| 209 | + { |
| 210 | + selectionRect = mComposerMap->extent(); |
| 211 | + } |
| 212 | + |
| 213 | + mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), selectionRect, false, true ); |
| 214 | + QgsFeature f; |
| 215 | + int counter = 0; |
| 216 | + while ( mVectorLayer->nextFeature( f ) && counter < mMaximumNumberOfFeatures ) |
| 217 | + { |
| 218 | + attributes.push_back( f.attributeMap() ); |
| 219 | + ++counter; |
| 220 | + } |
| 221 | + return true; |
| 222 | +} |
| 223 | + |
| 224 | +bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeList ) const |
| 225 | +{ |
| 226 | + maxWidthMap.clear(); |
| 227 | + if ( !mVectorLayer ) |
| 228 | + { |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + QgsFieldMap vectorFields = mVectorLayer->pendingFields(); |
| 233 | + |
| 234 | + //initialize max width map with attribute names |
| 235 | + QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin(); |
| 236 | + for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt ) |
| 237 | + { |
| 238 | + maxWidthMap.insert( fieldIt.key(), textWidthMillimeters( mHeaderFont, fieldIt.value().name() ) ); |
| 239 | + } |
| 240 | + |
| 241 | + //go through all the attributes and adapt the max width values |
| 242 | + QList<QgsAttributeMap>::const_iterator attIt = attributeList.constBegin(); |
| 243 | + |
| 244 | + QgsAttributeMap currentAttributeMap; |
| 245 | + double currentAttributeTextWidth; |
| 246 | + |
| 247 | + for ( ; attIt != attributeList.constEnd(); ++attIt ) |
| 248 | + { |
| 249 | + currentAttributeMap = *attIt; |
| 250 | + QgsAttributeMap::const_iterator attMapIt = currentAttributeMap.constBegin(); |
| 251 | + for ( ; attMapIt != currentAttributeMap.constEnd(); ++attMapIt ) |
| 252 | + { |
| 253 | + currentAttributeTextWidth = textWidthMillimeters( mContentFont, attMapIt.value().toString() ); |
| 254 | + if ( currentAttributeTextWidth > maxWidthMap[attMapIt.key()] ) |
| 255 | + { |
| 256 | + maxWidthMap[attMapIt.key()] = currentAttributeTextWidth; |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + return true; |
| 261 | +} |
| 262 | + |
| 263 | +void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeList ) |
| 264 | +{ |
| 265 | + //calculate height |
| 266 | + double totalHeight = fontAscentMillimeters( mHeaderFont ) + attributeList.size() * fontAscentMillimeters( mContentFont ) + ( attributeList.size() + 1 ) * mLineTextDistance * 2; |
| 267 | + |
| 268 | + //adapt frame to total width |
| 269 | + double totalWidth = 0; |
| 270 | + QMap<int, double>::const_iterator maxColWidthIt = maxWidthMap.constBegin(); |
| 271 | + for ( ; maxColWidthIt != maxWidthMap.constEnd(); ++maxColWidthIt ) |
| 272 | + { |
| 273 | + totalWidth += maxColWidthIt.value(); |
| 274 | + } |
| 275 | + totalWidth += ( 2 * maxWidthMap.size() * mLineTextDistance ); |
| 276 | + QTransform t = transform(); |
| 277 | + setSceneRect( QRectF( t.dx(), t.dy(), totalWidth, totalHeight ) ); |
| 278 | +} |
| 279 | + |
| 280 | +void QgsComposerTable::drawHorizontalGridLines( QPainter* p, int nAttributes ) |
| 281 | +{ |
| 282 | + //horizontal lines |
| 283 | + double currentY = 0; |
| 284 | + p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) ); |
| 285 | + currentY += ( fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance ); |
| 286 | + for ( int i = 0; i < nAttributes; ++i ) |
| 287 | + { |
| 288 | + p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) ); |
| 289 | + currentY += ( fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance ); |
| 290 | + } |
| 291 | + p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) ); |
| 292 | +} |
| 293 | + |
| 294 | +void QgsComposerTable::drawVerticalGridLines( QPainter* p, const QMap<int, double>& maxWidthMap ) |
| 295 | +{ |
| 296 | + //vertical lines |
| 297 | + double currentX = 0; |
| 298 | + p->drawLine( QPointF( currentX, 0 ), QPointF( currentX, rect().height() ) ); |
| 299 | + QMap<int, double>::const_iterator maxColWidthIt = maxWidthMap.constBegin(); |
| 300 | + for ( ; maxColWidthIt != maxWidthMap.constEnd(); ++maxColWidthIt ) |
| 301 | + { |
| 302 | + currentX += ( maxColWidthIt.value() + 2 * mLineTextDistance ); |
| 303 | + p->drawLine( QPointF( currentX, 0 ), QPointF( currentX, rect().height() ) ); |
| 304 | + } |
| 305 | +} |
| 306 | + |
0 commit comments