Skip to content

Commit 9152a82

Browse files
author
mhugent
committed
Refactor composer table to a base class that has vector attribute table and normal text table as subclasses
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13316 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 43d89d9 commit 9152a82

13 files changed

+561
-325
lines changed

src/app/composer/qgscomposer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "qgscomposerscalebarwidget.h"
3636
#include "qgscomposershape.h"
3737
#include "qgscomposershapewidget.h"
38-
#include "qgscomposertable.h"
38+
#include "qgscomposerattributetable.h"
3939
#include "qgscomposertablewidget.h"
4040
#include "qgsexception.h"
4141
#include "qgsproject.h"
@@ -283,7 +283,7 @@ void QgsComposer::connectSlots()
283283
connect( mView, SIGNAL( composerPictureAdded( QgsComposerPicture* ) ), this, SLOT( addComposerPicture( QgsComposerPicture* ) ) );
284284
connect( mView, SIGNAL( composerShapeAdded( QgsComposerShape* ) ), this, SLOT( addComposerShape( QgsComposerShape* ) ) );
285285
connect( mView, SIGNAL( composerArrowAdded( QgsComposerArrow* ) ), this, SLOT( addComposerArrow( QgsComposerArrow* ) ) );
286-
connect( mView, SIGNAL( composerTableAdded( QgsComposerTable* ) ), this, SLOT( addComposerTable( QgsComposerTable* ) ) );
286+
connect( mView, SIGNAL( composerTableAdded( QgsComposerAttributeTable* ) ), this, SLOT( addComposerTable( QgsComposerAttributeTable* ) ) );
287287
connect( mView, SIGNAL( actionFinished() ), this, SLOT( setSelectionTool() ) );
288288
}
289289

@@ -1263,11 +1263,11 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
12631263
}
12641264

12651265
//composer tables
1266-
QDomNodeList composerTableList = composerElem.elementsByTagName( "ComposerTable" );
1266+
QDomNodeList composerTableList = composerElem.elementsByTagName( "ComposerAttributeTable" );
12671267
for ( int i = 0; i < composerTableList.size(); ++i )
12681268
{
12691269
QDomElement currentTableElem = composerTableList.at( i ).toElement();
1270-
QgsComposerTable* newTable = new QgsComposerTable( mComposition );
1270+
QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( mComposition );
12711271
newTable->readXML( currentTableElem, doc );
12721272
addComposerTable( newTable );
12731273
mComposition->addItem( newTable );
@@ -1373,7 +1373,7 @@ void QgsComposer::addComposerShape( QgsComposerShape* shape )
13731373
mItemWidgetMap.insert( shape, sWidget );
13741374
}
13751375

1376-
void QgsComposer::addComposerTable( QgsComposerTable* table )
1376+
void QgsComposer::addComposerTable( QgsComposerAttributeTable* table )
13771377
{
13781378
if ( !table )
13791379
{

src/app/composer/qgscomposer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class QgsComposerMap;
2929
class QgsComposerPicture;
3030
class QgsComposerScaleBar;
3131
class QgsComposerShape;
32-
class QgsComposerTable;
32+
class QgsComposerAttributeTable;
3333
class QgsComposerView;
3434
class QgsComposition;
3535
class QgsMapCanvas;
@@ -223,7 +223,7 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
223223
void addComposerShape( QgsComposerShape* shape );
224224

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

228228
/**Removes item from the item/widget map and deletes the configuration widget*/
229229
void deleteItem( QgsComposerItem* item );

src/app/composer/qgscomposertablewidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#include "qgscomposertablewidget.h"
1919
#include "qgsattributeselectiondialog.h"
2020
#include "qgscomposeritemwidget.h"
21-
#include "qgscomposertable.h"
21+
#include "qgscomposerattributetable.h"
2222
#include "qgscomposermap.h"
2323
#include "qgsmaplayerregistry.h"
2424
#include "qgsvectorlayer.h"
2525
#include <QColorDialog>
2626
#include <QFontDialog>
2727

28-
QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerTable* table ): QWidget( 0 ), mComposerTable( table )
28+
QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerAttributeTable* table ): QWidget( 0 ), mComposerTable( table )
2929
{
3030
setupUi( this );
3131
//add widget for general composer item properties

src/app/composer/qgscomposertablewidget.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020

2121
#include "ui_qgscomposertablewidgetbase.h"
2222

23-
class QgsComposerTable;
23+
class QgsComposerAttributeTable;
2424

2525
class QgsComposerTableWidget: public QWidget, private Ui::QgsComposerTableWidgetBase
2626
{
2727
Q_OBJECT
2828
public:
29-
QgsComposerTableWidget( QgsComposerTable* table );
29+
QgsComposerTableWidget( QgsComposerAttributeTable* table );
3030
~QgsComposerTableWidget();
3131

3232
private:
33-
QgsComposerTable* mComposerTable;
33+
QgsComposerAttributeTable* mComposerTable;
3434

3535
/**Sets the GUI elements to the values of mComposerTable*/
3636
void updateGuiElements();

src/core/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ SET(QGIS_CORE_SRCS
9595
composer/qgscomposerpicture.cpp
9696
composer/qgscomposermap.cpp
9797
composer/qgscomposertable.cpp
98+
composer/qgscomposerattributetable.cpp
99+
composer/qgscomposertexttable.cpp
98100
composer/qgscomposerscalebar.cpp
99101
composer/qgscomposershape.cpp
100102
composer/qgslegendmodel.cpp
@@ -238,7 +240,7 @@ SET(QGIS_CORE_MOC_HDRS
238240
composer/qgscomposeritem.h
239241
composer/qgscomposeritemgroup.h
240242
composer/qgscomposershape.h
241-
composer/qgscomposertable.h
243+
composer/qgscomposerattributetable.h
242244
composer/qgscomposition.h
243245

244246
composer/qgslegendmodel.h
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
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

Comments
 (0)