Skip to content

Commit

Permalink
composer table:
Browse files Browse the repository at this point in the history
- fix api updates (fixes #7781)
- store/restore geometry of attribute selection dialog
  • Loading branch information
jef-n committed Jul 8, 2013
1 parent c735d1a commit abd7533
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 60 deletions.
6 changes: 3 additions & 3 deletions python/core/composer/qgscomposertable.sip
Expand Up @@ -42,12 +42,12 @@ class QgsComposerTable: QgsComposerItem

protected:
/**Retrieves feature attributes*/
// virtual bool getFeatureAttributes( QList<QgsAttributeMap>& attributes );
// virtual bool getFeatureAttributes( QList<QgsAttributeMap>& attributeMaps );
virtual QMap<int, QString> getHeaderLabels() const;
/**Calculate the maximum width values of the vector attributes*/
// virtual bool calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeList ) const;
// virtual bool calculateMaxColumnWidths( QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeMaps ) const;
/**Adapts the size of the item frame to match the content*/
// void adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeList );
// void adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList< QMap<int, QVariant> >& attributeMaps );
void drawHorizontalGridLines( QPainter* p, int nAttributes );
// void drawVerticalGridLines( QPainter* p, const QMap<int, double>& maxWidthMap );

Expand Down
8 changes: 7 additions & 1 deletion src/app/composer/qgsattributeselectiondialog.cpp
Expand Up @@ -24,11 +24,16 @@
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
#include <QSettings>

QgsAttributeSelectionDialog::QgsAttributeSelectionDialog( const QgsVectorLayer* vLayer, const QSet<int>& enabledAttributes, const QMap<int, QString>& aliasMap,
const QList< QPair<int, bool> >& sortColumns, QWidget* parent, Qt::WindowFlags f ): QDialog( parent, f ), mVectorLayer( vLayer )
{
setupUi( this );

QSettings settings;
restoreGeometry( settings.value( "/Windows/AttributeSelectionDialog/geometry" ).toByteArray() );

if ( vLayer )
{
const QgsFields& fieldMap = vLayer->pendingFields();
Expand Down Expand Up @@ -79,7 +84,8 @@ QgsAttributeSelectionDialog::QgsAttributeSelectionDialog( const QgsVectorLayer*

QgsAttributeSelectionDialog::~QgsAttributeSelectionDialog()
{

QSettings settings;
settings.setValue( "/Windows/AttributeSelectionDialog/geometry", saveGeometry() );
}

QSet<int> QgsAttributeSelectionDialog::enabledAttributes() const
Expand Down
48 changes: 27 additions & 21 deletions src/core/composer/qgscomposerattributetable.cpp
Expand Up @@ -20,12 +20,13 @@
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"

QgsComposerAttributeTableCompare::QgsComposerAttributeTableCompare(): mCurrentSortColumn( 0 ), mAscending( true )
QgsComposerAttributeTableCompare::QgsComposerAttributeTableCompare()
: mCurrentSortColumn( 0 ), mAscending( true )
{
}


bool QgsComposerAttributeTableCompare::operator()( const QgsAttributes& m1, const QgsAttributes& m2 )
bool QgsComposerAttributeTableCompare::operator()( const QgsAttributeMap& m1, const QgsAttributeMap& m2 )
{
QVariant v1 = m1[mCurrentSortColumn];
QVariant v2 = m2[mCurrentSortColumn];
Expand All @@ -44,8 +45,11 @@ bool QgsComposerAttributeTableCompare::operator()( const QgsAttributes& m1, cons


QgsComposerAttributeTable::QgsComposerAttributeTable( QgsComposition* composition )
: QgsComposerTable( composition ), mVectorLayer( 0 ), mComposerMap( 0 ),
mMaximumNumberOfFeatures( 5 ), mShowOnlyVisibleFeatures( true )
: QgsComposerTable( composition )
, mVectorLayer( 0 )
, mComposerMap( 0 )
, mMaximumNumberOfFeatures( 5 )
, mShowOnlyVisibleFeatures( true )
{
//set first vector layer from layer registry as default one
QMap<QString, QgsMapLayer*> layerMap = QgsMapLayerRegistry::instance()->mapLayers();
Expand All @@ -64,7 +68,6 @@ QgsComposerAttributeTable::QgsComposerAttributeTable( QgsComposition* compositio

QgsComposerAttributeTable::~QgsComposerAttributeTable()
{

}

void QgsComposerAttributeTable::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
Expand Down Expand Up @@ -116,13 +119,14 @@ void QgsComposerAttributeTable::setComposerMap( const QgsComposerMap* map )
}
}

bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributes>& attributes )
bool QgsComposerAttributeTable::getFeatureAttributes( QList<QgsAttributeMap> &attributeMaps )
{
if ( !mVectorLayer )
{
return false;
}
attributes.clear();

attributeMaps.clear();

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

QgsFeatureRequest req;
if ( !selectionRect.isEmpty() )
{
req.setFilterRect( selectionRect );
}

req.setFlags( mShowOnlyVisibleFeatures ? QgsFeatureRequest::ExactIntersect : QgsFeatureRequest::NoGeometry );
if ( mDisplayAttributes.size() > 0 )

if ( !mDisplayAttributes.isEmpty() )
req.setSubsetOfAttributes( mDisplayAttributes.toList() );

QgsFeature f;
int counter = 0;
QgsFeatureIterator fit = mVectorLayer->getFeatures( req );

while ( fit.nextFeature( f ) && counter < mMaximumNumberOfFeatures )
{
attributes.push_back( f.attributes() );
attributeMaps.push_back( QgsAttributeMap() );

for ( int i = 0; i < f.attributes().size(); i++ )
{
if ( !mDisplayAttributes.isEmpty() && !mDisplayAttributes.contains( i ) )
continue;

attributeMaps.last().insert( i, f.attributes()[i] );
}

++counter;
}

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

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;
}
return mFieldAliasMap.value( attributeIndex, name );
}

void QgsComposerAttributeTable::removeLayer( QString layerId )
Expand Down
6 changes: 3 additions & 3 deletions src/core/composer/qgscomposerattributetable.h
Expand Up @@ -28,7 +28,7 @@ class CORE_EXPORT QgsComposerAttributeTableCompare
{
public:
QgsComposerAttributeTableCompare();
bool operator()( const QgsAttributes& m1, const QgsAttributes& m2 );
bool operator()( const QgsAttributeMap& m1, const QgsAttributeMap& m2 );
void setSortColumn( int col ) { mCurrentSortColumn = col; }
void setAscending( bool asc ) { mAscending = asc; }
private:
Expand Down Expand Up @@ -66,7 +66,7 @@ class CORE_EXPORT QgsComposerAttributeTable: public QgsComposerTable
bool displayOnlyVisibleFeatures() const { return mShowOnlyVisibleFeatures; }

QSet<int> displayAttributes() const { return mDisplayAttributes; }
void setDisplayAttributes( const QSet<int>& attr ) { mDisplayAttributes = attr;}
void setDisplayAttributes( const QSet<int>& attr ) { mDisplayAttributes = attr; }

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

//! @note not available in python bindings
QMap<int, QString> getHeaderLabels() const;
Expand Down
54 changes: 25 additions & 29 deletions src/core/composer/qgscomposertable.cpp
Expand Up @@ -43,17 +43,17 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
}

//getFeatureAttributes
QList<QgsAttributes> attributeList;
if ( !getFeatureAttributes( attributeList ) )
QList<QgsAttributeMap> attributeMaps;
if ( !getFeatureAttributes( attributeMaps ) )
{
return;
}

QMap<int, double> maxColumnWidthMap;
//check how much space each column needs
calculateMaxColumnWidths( maxColumnWidthMap, attributeList );
calculateMaxColumnWidths( maxColumnWidthMap, attributeMaps );
//adapt item frame to max width / height
adaptItemFrame( maxColumnWidthMap, attributeList );
adaptItemFrame( maxColumnWidthMap, attributeMaps );

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

//draw the attribute values
QList<QgsAttributes>::const_iterator attIt = attributeList.begin();
for ( ; attIt != attributeList.end(); ++attIt )
QList<QgsAttributeMap>::const_iterator attIt = attributeMaps.begin();
for ( ; attIt != attributeMaps.end(); ++attIt )
{
currentY += fontAscentMillimeters( mContentFont );
currentY += mLineTextDistance;

QgsAttributes currentAttributeMap = *attIt;
QString str = currentAttributeMap.at( columnIt.key() ).toString();
const QgsAttributeMap &currentAttributeMap = *attIt;
QString str = currentAttributeMap[ columnIt.key()].toString();
drawText( painter, currentX, currentY, str, mContentFont );
currentY += mLineTextDistance;
currentY += mGridStrokeWidth;
Expand All @@ -102,7 +102,7 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
gridPen.setWidthF( mGridStrokeWidth );
gridPen.setColor( mGridColor );
painter->setPen( gridPen );
drawHorizontalGridLines( painter, attributeList.size() );
drawHorizontalGridLines( painter, attributeMaps.size() );
drawVerticalGridLines( painter, maxColumnWidthMap );
}

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

void QgsComposerTable::adjustFrameToSize()
{
QList<QgsAttributes> attributes;
QList<QgsAttributeMap> attributes;
if ( !getFeatureAttributes( attributes ) )
{
return;
}

QMap<int, double> maxWidthMap;
if ( !calculateMaxColumnWidths( maxWidthMap, attributes ) )
{
return;
}

adaptItemFrame( maxWidthMap, attributes );
}

Expand Down Expand Up @@ -172,7 +169,7 @@ bool QgsComposerTable::tableReadXML( const QDomElement& itemElem, const QDomDocu
return true;
}

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

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

QgsAttributeMap currentAttributeMap;
double currentAttributeTextWidth;

for ( ; attIt != attributeList.constEnd(); ++attIt )
for ( ; attIt != attributeMaps.constEnd(); ++attIt )
{
for ( int i = 0; i < attIt->size(); ++i )
QgsAttributeMap::const_iterator attIt2 = attIt->constBegin();
for ( ; attIt2 != attIt->constEnd(); ++attIt2 )
{
currentAttributeTextWidth = textWidthMillimeters( mContentFont, attIt->at( i ).toString() );
if ( currentAttributeTextWidth > maxWidthMap[i] )
currentAttributeTextWidth = textWidthMillimeters( mContentFont, attIt2.value().toString() );
if ( currentAttributeTextWidth > maxWidthMap[ attIt2.key()] )
{
maxWidthMap[i] = currentAttributeTextWidth;
maxWidthMap[ attIt2.key()] = currentAttributeTextWidth;
}
}
}
return true;
}





void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributes>& attributeList )
void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeMaps )
{
//calculate height
double totalHeight = fontAscentMillimeters( mHeaderFont ) + attributeList.size() * fontAscentMillimeters( mContentFont )
+ ( attributeList.size() + 1 ) * mLineTextDistance * 2 + ( attributeList.size() + 2 ) * mGridStrokeWidth;
int n = attributeMaps.size();
double totalHeight = fontAscentMillimeters( mHeaderFont )
+ n * fontAscentMillimeters( mContentFont )
+ ( n + 1 ) * mLineTextDistance * 2
+ ( n + 2 ) * mGridStrokeWidth;

//adapt frame to total width
double totalWidth = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/core/composer/qgscomposertable.h
Expand Up @@ -76,13 +76,13 @@ class CORE_EXPORT QgsComposerTable: public QgsComposerItem

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

0 comments on commit abd7533

Please sign in to comment.