Skip to content

Commit abd7533

Browse files
committed
composer table:
- fix api updates (fixes #7781) - store/restore geometry of attribute selection dialog
1 parent c735d1a commit abd7533

File tree

6 files changed

+68
-60
lines changed

6 files changed

+68
-60
lines changed

python/core/composer/qgscomposertable.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class QgsComposerTable: QgsComposerItem
4242

4343
protected:
4444
/**Retrieves feature attributes*/
45-
// virtual bool getFeatureAttributes( QList<QgsAttributeMap>& attributes );
45+
// virtual bool getFeatureAttributes( QList<QgsAttributeMap>& attributeMaps );
4646
virtual QMap<int, QString> getHeaderLabels() const;
4747
/**Calculate the maximum width values of the vector attributes*/
48-
// virtual bool calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeList ) const;
48+
// virtual bool calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeMaps ) const;
4949
/**Adapts the size of the item frame to match the content*/
50-
// void adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeList );
50+
// void adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeMaps );
5151
void drawHorizontalGridLines( QPainter* p, int nAttributes );
5252
// void drawVerticalGridLines( QPainter* p, const QMap<int, double>& maxWidthMap );
5353

src/app/composer/qgsattributeselectiondialog.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
#include <QLineEdit>
2525
#include <QPushButton>
2626
#include <QScrollArea>
27+
#include <QSettings>
2728

2829
QgsAttributeSelectionDialog::QgsAttributeSelectionDialog( const QgsVectorLayer* vLayer, const QSet<int>& enabledAttributes, const QMap<int, QString>& aliasMap,
2930
const QList< QPair<int, bool> >& sortColumns, QWidget* parent, Qt::WindowFlags f ): QDialog( parent, f ), mVectorLayer( vLayer )
3031
{
3132
setupUi( this );
33+
34+
QSettings settings;
35+
restoreGeometry( settings.value( "/Windows/AttributeSelectionDialog/geometry" ).toByteArray() );
36+
3237
if ( vLayer )
3338
{
3439
const QgsFields& fieldMap = vLayer->pendingFields();
@@ -79,7 +84,8 @@ QgsAttributeSelectionDialog::QgsAttributeSelectionDialog( const QgsVectorLayer*
7984

8085
QgsAttributeSelectionDialog::~QgsAttributeSelectionDialog()
8186
{
82-
87+
QSettings settings;
88+
settings.setValue( "/Windows/AttributeSelectionDialog/geometry", saveGeometry() );
8389
}
8490

8591
QSet<int> QgsAttributeSelectionDialog::enabledAttributes() const

src/core/composer/qgscomposerattributetable.cpp

+27-21
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
#include "qgsmaplayerregistry.h"
2121
#include "qgsvectorlayer.h"
2222

23-
QgsComposerAttributeTableCompare::QgsComposerAttributeTableCompare(): mCurrentSortColumn( 0 ), mAscending( true )
23+
QgsComposerAttributeTableCompare::QgsComposerAttributeTableCompare()
24+
: mCurrentSortColumn( 0 ), mAscending( true )
2425
{
2526
}
2627

2728

28-
bool QgsComposerAttributeTableCompare::operator()( const QgsAttributes& m1, const QgsAttributes& m2 )
29+
bool QgsComposerAttributeTableCompare::operator()( const QgsAttributeMap& m1, const QgsAttributeMap& m2 )
2930
{
3031
QVariant v1 = m1[mCurrentSortColumn];
3132
QVariant v2 = m2[mCurrentSortColumn];
@@ -44,8 +45,11 @@ bool QgsComposerAttributeTableCompare::operator()( const QgsAttributes& m1, cons
4445

4546

4647
QgsComposerAttributeTable::QgsComposerAttributeTable( QgsComposition* composition )
47-
: QgsComposerTable( composition ), mVectorLayer( 0 ), mComposerMap( 0 ),
48-
mMaximumNumberOfFeatures( 5 ), mShowOnlyVisibleFeatures( true )
48+
: QgsComposerTable( composition )
49+
, mVectorLayer( 0 )
50+
, mComposerMap( 0 )
51+
, mMaximumNumberOfFeatures( 5 )
52+
, mShowOnlyVisibleFeatures( true )
4953
{
5054
//set first vector layer from layer registry as default one
5155
QMap<QString, QgsMapLayer*> layerMap = QgsMapLayerRegistry::instance()->mapLayers();
@@ -64,7 +68,6 @@ QgsComposerAttributeTable::QgsComposerAttributeTable( QgsComposition* compositio
6468

6569
QgsComposerAttributeTable::~QgsComposerAttributeTable()
6670
{
67-
6871
}
6972

7073
void QgsComposerAttributeTable::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
@@ -116,13 +119,14 @@ void QgsComposerAttributeTable::setComposerMap( const QgsComposerMap* map )
116119
}
117120
}
118121

119-
bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributes>& attributes )
122+
bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributeMap> &attributeMaps )
120123
{
121124
if ( !mVectorLayer )
122125
{
123126
return false;
124127
}
125-
attributes.clear();
128+
129+
attributeMaps.clear();
126130

127131
QgsRectangle selectionRect;
128132
if ( mComposerMap && mShowOnlyVisibleFeatures )
@@ -147,19 +151,29 @@ bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributes>& attr
147151

148152
QgsFeatureRequest req;
149153
if ( !selectionRect.isEmpty() )
150-
{
151154
req.setFilterRect( selectionRect );
152-
}
155+
153156
req.setFlags( mShowOnlyVisibleFeatures ? QgsFeatureRequest::ExactIntersect : QgsFeatureRequest::NoGeometry );
154-
if ( mDisplayAttributes.size() > 0 )
157+
158+
if ( !mDisplayAttributes.isEmpty() )
155159
req.setSubsetOfAttributes( mDisplayAttributes.toList() );
156160

157161
QgsFeature f;
158162
int counter = 0;
159163
QgsFeatureIterator fit = mVectorLayer->getFeatures( req );
164+
160165
while ( fit.nextFeature( f ) && counter < mMaximumNumberOfFeatures )
161166
{
162-
attributes.push_back( f.attributes() );
167+
attributeMaps.push_back( QgsAttributeMap() );
168+
169+
for ( int i = 0; i < f.attributes().size(); i++ )
170+
{
171+
if ( !mDisplayAttributes.isEmpty() && !mDisplayAttributes.contains( i ) )
172+
continue;
173+
174+
attributeMaps.last().insert( i, f.attributes()[i] );
175+
}
176+
163177
++counter;
164178
}
165179

@@ -169,7 +183,7 @@ bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributes>& attr
169183
{
170184
c.setSortColumn( mSortInformation.at( i ).first );
171185
c.setAscending( mSortInformation.at( i ).second );
172-
qStableSort( attributes.begin(), attributes.end(), c );
186+
qStableSort( attributeMaps.begin(), attributeMaps.end(), c );
173187
}
174188
return true;
175189
}
@@ -194,15 +208,7 @@ QMap<int, QString> QgsComposerAttributeTable::getHeaderLabels() const
194208

195209
QString QgsComposerAttributeTable::attributeDisplayName( int attributeIndex, const QString& name ) const
196210
{
197-
QMap<int, QString>::const_iterator it = mFieldAliasMap.find( attributeIndex );
198-
if ( it != mFieldAliasMap.constEnd() )
199-
{
200-
return it.value();
201-
}
202-
else
203-
{
204-
return name;
205-
}
211+
return mFieldAliasMap.value( attributeIndex, name );
206212
}
207213

208214
void QgsComposerAttributeTable::removeLayer( QString layerId )

src/core/composer/qgscomposerattributetable.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CORE_EXPORT QgsComposerAttributeTableCompare
2828
{
2929
public:
3030
QgsComposerAttributeTableCompare();
31-
bool operator()( const QgsAttributes& m1, const QgsAttributes& m2 );
31+
bool operator()( const QgsAttributeMap& m1, const QgsAttributeMap& m2 );
3232
void setSortColumn( int col ) { mCurrentSortColumn = col; }
3333
void setAscending( bool asc ) { mAscending = asc; }
3434
private:
@@ -66,7 +66,7 @@ class CORE_EXPORT QgsComposerAttributeTable: public QgsComposerTable
6666
bool displayOnlyVisibleFeatures() const { return mShowOnlyVisibleFeatures; }
6767

6868
QSet<int> displayAttributes() const { return mDisplayAttributes; }
69-
void setDisplayAttributes( const QSet<int>& attr ) { mDisplayAttributes = attr;}
69+
void setDisplayAttributes( const QSet<int>& attr ) { mDisplayAttributes = attr; }
7070

7171
QMap<int, QString> fieldAliasMap() const { return mFieldAliasMap; }
7272
void setFieldAliasMap( const QMap<int, QString>& map ) { mFieldAliasMap = map; }
@@ -84,7 +84,7 @@ class CORE_EXPORT QgsComposerAttributeTable: public QgsComposerTable
8484
/**Retrieves feature attributes
8585
* @note not available in python bindings
8686
*/
87-
bool getFeatureAttributes( QList<QgsAttributes>& attributes );
87+
bool getFeatureAttributes( QList<QgsAttributeMap>& attributeMaps );
8888

8989
//! @note not available in python bindings
9090
QMap<int, QString> getHeaderLabels() const;

src/core/composer/qgscomposertable.cpp

+25-29
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
4343
}
4444

4545
//getFeatureAttributes
46-
QList<QgsAttributes> attributeList;
47-
if ( !getFeatureAttributes( attributeList ) )
46+
QList<QgsAttributeMap> attributeMaps;
47+
if ( !getFeatureAttributes( attributeMaps ) )
4848
{
4949
return;
5050
}
5151

5252
QMap<int, double> maxColumnWidthMap;
5353
//check how much space each column needs
54-
calculateMaxColumnWidths( maxColumnWidthMap, attributeList );
54+
calculateMaxColumnWidths( maxColumnWidthMap, attributeMaps );
5555
//adapt item frame to max width / height
56-
adaptItemFrame( maxColumnWidthMap, attributeList );
56+
adaptItemFrame( maxColumnWidthMap, attributeMaps );
5757

5858
drawBackground( painter );
5959
painter->setPen( Qt::SolidLine );
@@ -77,14 +77,14 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
7777
currentY += mGridStrokeWidth;
7878

7979
//draw the attribute values
80-
QList<QgsAttributes>::const_iterator attIt = attributeList.begin();
81-
for ( ; attIt != attributeList.end(); ++attIt )
80+
QList<QgsAttributeMap>::const_iterator attIt = attributeMaps.begin();
81+
for ( ; attIt != attributeMaps.end(); ++attIt )
8282
{
8383
currentY += fontAscentMillimeters( mContentFont );
8484
currentY += mLineTextDistance;
8585

86-
QgsAttributes currentAttributeMap = *attIt;
87-
QString str = currentAttributeMap.at( columnIt.key() ).toString();
86+
const QgsAttributeMap &currentAttributeMap = *attIt;
87+
QString str = currentAttributeMap[ columnIt.key()].toString();
8888
drawText( painter, currentX, currentY, str, mContentFont );
8989
currentY += mLineTextDistance;
9090
currentY += mGridStrokeWidth;
@@ -102,7 +102,7 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
102102
gridPen.setWidthF( mGridStrokeWidth );
103103
gridPen.setColor( mGridColor );
104104
painter->setPen( gridPen );
105-
drawHorizontalGridLines( painter, attributeList.size() );
105+
drawHorizontalGridLines( painter, attributeMaps.size() );
106106
drawVerticalGridLines( painter, maxColumnWidthMap );
107107
}
108108

@@ -116,17 +116,14 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
116116

117117
void QgsComposerTable::adjustFrameToSize()
118118
{
119-
QList<QgsAttributes> attributes;
119+
QList<QgsAttributeMap> attributes;
120120
if ( !getFeatureAttributes( attributes ) )
121-
{
122121
return;
123-
}
124122

125123
QMap<int, double> maxWidthMap;
126124
if ( !calculateMaxColumnWidths( maxWidthMap, attributes ) )
127-
{
128125
return;
129-
}
126+
130127
adaptItemFrame( maxWidthMap, attributes );
131128
}
132129

@@ -172,7 +169,7 @@ bool QgsComposerTable::tableReadXML( const QDomElement& itemElem, const QDomDocu
172169
return true;
173170
}
174171

175-
bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList<QgsAttributes>& attributeList ) const
172+
bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeMaps ) const
176173
{
177174
maxWidthMap.clear();
178175
QMap<int, QString> headerMap = getHeaderLabels();
@@ -183,34 +180,33 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
183180
}
184181

185182
//go through all the attributes and adapt the max width values
186-
QList<QgsAttributes>::const_iterator attIt = attributeList.constBegin();
183+
QList<QgsAttributeMap>::const_iterator attIt = attributeMaps.constBegin();
187184

188-
QgsAttributeMap currentAttributeMap;
189185
double currentAttributeTextWidth;
190186

191-
for ( ; attIt != attributeList.constEnd(); ++attIt )
187+
for ( ; attIt != attributeMaps.constEnd(); ++attIt )
192188
{
193-
for ( int i = 0; i < attIt->size(); ++i )
189+
QgsAttributeMap::const_iterator attIt2 = attIt->constBegin();
190+
for ( ; attIt2 != attIt->constEnd(); ++attIt2 )
194191
{
195-
currentAttributeTextWidth = textWidthMillimeters( mContentFont, attIt->at( i ).toString() );
196-
if ( currentAttributeTextWidth > maxWidthMap[i] )
192+
currentAttributeTextWidth = textWidthMillimeters( mContentFont, attIt2.value().toString() );
193+
if ( currentAttributeTextWidth > maxWidthMap[ attIt2.key()] )
197194
{
198-
maxWidthMap[i] = currentAttributeTextWidth;
195+
maxWidthMap[ attIt2.key()] = currentAttributeTextWidth;
199196
}
200197
}
201198
}
202199
return true;
203200
}
204201

205-
206-
207-
208-
209-
void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributes>& attributeList )
202+
void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeMaps )
210203
{
211204
//calculate height
212-
double totalHeight = fontAscentMillimeters( mHeaderFont ) + attributeList.size() * fontAscentMillimeters( mContentFont )
213-
+ ( attributeList.size() + 1 ) * mLineTextDistance * 2 + ( attributeList.size() + 2 ) * mGridStrokeWidth;
205+
int n = attributeMaps.size();
206+
double totalHeight = fontAscentMillimeters( mHeaderFont )
207+
+ n * fontAscentMillimeters( mContentFont )
208+
+ ( n + 1 ) * mLineTextDistance * 2
209+
+ ( n + 2 ) * mGridStrokeWidth;
214210

215211
//adapt frame to total width
216212
double totalWidth = 0;

src/core/composer/qgscomposertable.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class CORE_EXPORT QgsComposerTable: public QgsComposerItem
7676

7777
/**Retrieves feature attributes*/
7878
//! @note not available in python bindings
79-
virtual bool getFeatureAttributes( QList<QgsAttributes>& attributes ) { Q_UNUSED( attributes ); return false; }
79+
virtual bool getFeatureAttributes( QList<QgsAttributeMap>& attributeMaps ) { Q_UNUSED( attributeMaps ); return false; }
8080
virtual QMap<int, QString> getHeaderLabels() const { return QMap<int, QString>(); } //= 0;
8181
/**Calculate the maximum width values of the vector attributes*/
82-
virtual bool calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList<QgsAttributes>& attributeList ) const;
82+
virtual bool calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeMaps ) const;
8383
/**Adapts the size of the item frame to match the content*/
8484
//! @note not available in python bindings
85-
void adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributes>& attributeList );
85+
void adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeMaps );
8686
void drawHorizontalGridLines( QPainter* p, int nAttributes );
8787
//! @note not available in python bindings
8888
void drawVerticalGridLines( QPainter* p, const QMap<int, double>& maxWidthMap );

0 commit comments

Comments
 (0)