Skip to content

Commit

Permalink
Refactor composer table to a base class that has vector attribute tab…
Browse files Browse the repository at this point in the history
…le and normal text table as subclasses

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13316 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 15, 2010
1 parent 43d89d9 commit 9152a82
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 325 deletions.
10 changes: 5 additions & 5 deletions src/app/composer/qgscomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "qgscomposerscalebarwidget.h"
#include "qgscomposershape.h"
#include "qgscomposershapewidget.h"
#include "qgscomposertable.h"
#include "qgscomposerattributetable.h"
#include "qgscomposertablewidget.h"
#include "qgsexception.h"
#include "qgsproject.h"
Expand Down Expand Up @@ -283,7 +283,7 @@ void QgsComposer::connectSlots()
connect( mView, SIGNAL( composerPictureAdded( QgsComposerPicture* ) ), this, SLOT( addComposerPicture( QgsComposerPicture* ) ) );
connect( mView, SIGNAL( composerShapeAdded( QgsComposerShape* ) ), this, SLOT( addComposerShape( QgsComposerShape* ) ) );
connect( mView, SIGNAL( composerArrowAdded( QgsComposerArrow* ) ), this, SLOT( addComposerArrow( QgsComposerArrow* ) ) );
connect( mView, SIGNAL( composerTableAdded( QgsComposerTable* ) ), this, SLOT( addComposerTable( QgsComposerTable* ) ) );
connect( mView, SIGNAL( composerTableAdded( QgsComposerAttributeTable* ) ), this, SLOT( addComposerTable( QgsComposerAttributeTable* ) ) );
connect( mView, SIGNAL( actionFinished() ), this, SLOT( setSelectionTool() ) );
}

Expand Down Expand Up @@ -1263,11 +1263,11 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
}

//composer tables
QDomNodeList composerTableList = composerElem.elementsByTagName( "ComposerTable" );
QDomNodeList composerTableList = composerElem.elementsByTagName( "ComposerAttributeTable" );
for ( int i = 0; i < composerTableList.size(); ++i )
{
QDomElement currentTableElem = composerTableList.at( i ).toElement();
QgsComposerTable* newTable = new QgsComposerTable( mComposition );
QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( mComposition );
newTable->readXML( currentTableElem, doc );
addComposerTable( newTable );
mComposition->addItem( newTable );
Expand Down Expand Up @@ -1373,7 +1373,7 @@ void QgsComposer::addComposerShape( QgsComposerShape* shape )
mItemWidgetMap.insert( shape, sWidget );
}

void QgsComposer::addComposerTable( QgsComposerTable* table )
void QgsComposer::addComposerTable( QgsComposerAttributeTable* table )
{
if ( !table )
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/composer/qgscomposer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class QgsComposerMap;
class QgsComposerPicture;
class QgsComposerScaleBar;
class QgsComposerShape;
class QgsComposerTable;
class QgsComposerAttributeTable;
class QgsComposerView;
class QgsComposition;
class QgsMapCanvas;
Expand Down Expand Up @@ -223,7 +223,7 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
void addComposerShape( QgsComposerShape* shape );

/**Adds a composer table to the item/widget map and creates a configuration widget*/
void addComposerTable( QgsComposerTable* table );
void addComposerTable( QgsComposerAttributeTable* table );

/**Removes item from the item/widget map and deletes the configuration widget*/
void deleteItem( QgsComposerItem* item );
Expand Down
4 changes: 2 additions & 2 deletions src/app/composer/qgscomposertablewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#include "qgscomposertablewidget.h"
#include "qgsattributeselectiondialog.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposertable.h"
#include "qgscomposerattributetable.h"
#include "qgscomposermap.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"
#include <QColorDialog>
#include <QFontDialog>

QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerTable* table ): QWidget( 0 ), mComposerTable( table )
QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerAttributeTable* table ): QWidget( 0 ), mComposerTable( table )
{
setupUi( this );
//add widget for general composer item properties
Expand Down
6 changes: 3 additions & 3 deletions src/app/composer/qgscomposertablewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@

#include "ui_qgscomposertablewidgetbase.h"

class QgsComposerTable;
class QgsComposerAttributeTable;

class QgsComposerTableWidget: public QWidget, private Ui::QgsComposerTableWidgetBase
{
Q_OBJECT
public:
QgsComposerTableWidget( QgsComposerTable* table );
QgsComposerTableWidget( QgsComposerAttributeTable* table );
~QgsComposerTableWidget();

private:
QgsComposerTable* mComposerTable;
QgsComposerAttributeTable* mComposerTable;

/**Sets the GUI elements to the values of mComposerTable*/
void updateGuiElements();
Expand Down
4 changes: 3 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ SET(QGIS_CORE_SRCS
composer/qgscomposerpicture.cpp
composer/qgscomposermap.cpp
composer/qgscomposertable.cpp
composer/qgscomposerattributetable.cpp
composer/qgscomposertexttable.cpp
composer/qgscomposerscalebar.cpp
composer/qgscomposershape.cpp
composer/qgslegendmodel.cpp
Expand Down Expand Up @@ -238,7 +240,7 @@ SET(QGIS_CORE_MOC_HDRS
composer/qgscomposeritem.h
composer/qgscomposeritemgroup.h
composer/qgscomposershape.h
composer/qgscomposertable.h
composer/qgscomposerattributetable.h
composer/qgscomposition.h

composer/qgslegendmodel.h
Expand Down
284 changes: 284 additions & 0 deletions src/core/composer/qgscomposerattributetable.cpp
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 );
}
Loading

0 comments on commit 9152a82

Please sign in to comment.