Skip to content
Permalink
Browse files
More options for composer table
git-svn-id: http://svn.osgeo.org/qgis/trunk@12688 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jan 7, 2010
1 parent 615e82a commit 41c888caaa39aa54d49f24e806bccfdc3077b415
@@ -21,6 +21,7 @@
#include "qgscomposermap.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"
#include <QColorDialog>
#include <QFontDialog>

QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerTable* table ): QWidget( 0 ), mComposerTable( table )
@@ -167,6 +168,50 @@ void QgsComposerTableWidget::on_mContentFontPushButton_clicked()
}
}

void QgsComposerTableWidget::on_mGridStrokeWidthSpinBox_valueChanged( double d )
{
if ( !mComposerTable )
{
return;
}
mComposerTable->setGridStrokeWidth( d );
mComposerTable->update();
}

void QgsComposerTableWidget::on_mGridColorButton_clicked()
{
if ( !mComposerTable )
{
return;
}

QColor newColor = QColorDialog::getColor( mComposerTable->gridColor(), 0, tr( "Select grid color" ) );
if ( !newColor.isValid() )
{
return;
}
mGridColorButton->setColor( newColor );
mComposerTable->setGridColor( newColor );
mComposerTable->update();
}

void QgsComposerTableWidget::on_mShowGridCheckBox_stateChanged( int state )
{
if ( !mComposerTable )
{
return;
}

bool showGrid = false;
if ( state == Qt::Checked )
{
showGrid = true;
}
mComposerTable->setShowGrid( showGrid );
mComposerTable->update();
}


void QgsComposerTableWidget::updateGuiElements()
{
if ( !mComposerTable )
@@ -199,6 +244,16 @@ void QgsComposerTableWidget::updateGuiElements()
}
mMaximumColumnsSpinBox->setValue( mComposerTable->maximumNumberOfFeatures() );
mMarginSpinBox->setValue( mComposerTable->lineTextDistance() );
mGridStrokeWidthSpinBox->setValue( mComposerTable->gridStrokeWidth() );
mGridColorButton->setColor( mComposerTable->gridColor() );
if ( mComposerTable->showGrid() )
{
mShowGridCheckBox->setCheckState( Qt::Checked );
}
else
{
mShowGridCheckBox->setCheckState( Qt::Unchecked );
}
blockAllSignals( false );
}

@@ -208,6 +263,9 @@ void QgsComposerTableWidget::blockAllSignals( bool b )
mComposerMapComboBox->blockSignals( b );
mMaximumColumnsSpinBox->blockSignals( b );
mMarginSpinBox->blockSignals( b );
mGridColorButton->blockSignals( b );
mGridStrokeWidthSpinBox->blockSignals( b );
mShowGridCheckBox->blockSignals( b );
}


@@ -42,8 +42,11 @@ class QgsComposerTableWidget: public QWidget, private Ui::QgsComposerTableWidget
void on_mComposerMapComboBox_currentIndexChanged( int index );
void on_mMaximumColumnsSpinBox_valueChanged( int i );
void on_mMarginSpinBox_valueChanged( double d );
void on_mGridStrokeWidthSpinBox_valueChanged( double d );
void on_mGridColorButton_clicked();
void on_mHeaderFontPushButton_clicked();
void on_mContentFontPushButton_clicked();
void on_mShowGridCheckBox_stateChanged( int state );
};

#endif // QGSCOMPOSERTABLEWIDGET_H
@@ -22,9 +22,10 @@
#include "qgsvectorlayer.h"
#include <QPainter>

QgsComposerTable::QgsComposerTable( QgsComposition* composition ): QgsComposerItem( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), mMaximumNumberOfFeatures( 5 )
QgsComposerTable::QgsComposerTable( QgsComposition* composition ): QgsComposerItem( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), \
mMaximumNumberOfFeatures( 5 ), mLineTextDistance( 1.0 ), mShowGrid( true ), mGridStrokeWidth( 0.5 ), mGridColor( QColor( 0, 0, 0 ) )
{
mLineTextDistance = 1;

}

QgsComposerTable::~QgsComposerTable()
@@ -70,21 +71,24 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
//adapt item fram to max width / height
adaptItemFrame( maxColumnWidthMap, attributeList );

drawBackground( painter );

//now draw the text
double currentX = 0;
double currentX = mGridStrokeWidth;
double currentY;

QgsFieldMap vectorFields = mVectorLayer->pendingFields();
QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin();
for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt )
{
currentY = 0;
currentY = mGridStrokeWidth;
currentY += mLineTextDistance;
currentY += fontAscentMillimeters( mHeaderFont );
currentX += mLineTextDistance;
drawText( painter, currentX, currentY, fieldIt.value().name(), mHeaderFont );

currentY += mLineTextDistance;
currentY += mGridStrokeWidth;

//draw the attribute values
QList<QgsAttributeMap>::const_iterator attIt = attributeList.begin();
@@ -99,18 +103,25 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
{
drawText( painter, currentX, currentY, attMapIt.value().toString(), mContentFont );
}

currentY += mLineTextDistance;
currentY += mGridStrokeWidth;
}

currentX += mLineTextDistance;
currentX += maxColumnWidthMap[fieldIt.key()];
currentX += mLineTextDistance;
currentX += mGridStrokeWidth;
}

//and the borders
painter->setPen( mGridPen );
drawHorizontalGridLines( painter, attributeList.size() );
drawVerticalGridLines( painter, maxColumnWidthMap );
if ( mShowGrid )
{
QPen gridPen;
gridPen.setWidthF( mGridStrokeWidth );
gridPen.setColor( mGridColor );
painter->setPen( gridPen );
drawHorizontalGridLines( painter, attributeList.size() );
drawVerticalGridLines( painter, maxColumnWidthMap );
}

//draw frame and selection boxes if necessary
drawFrame( painter );
@@ -127,6 +138,12 @@ bool QgsComposerTable::writeXML( QDomElement& elem, QDomDocument & doc ) const
composerTableElem.setAttribute( "lineTextDist", mLineTextDistance );
composerTableElem.setAttribute( "headerFont", mHeaderFont.toString() );
composerTableElem.setAttribute( "contentFont", mContentFont.toString() );
composerTableElem.setAttribute( "gridStrokeWidth", mGridStrokeWidth );
composerTableElem.setAttribute( "gridColorRed", mGridColor.red() );
composerTableElem.setAttribute( "gridColorGreen", mGridColor.green() );
composerTableElem.setAttribute( "gridColorBlue", mGridColor.blue() );
composerTableElem.setAttribute( "showGrid", mShowGrid );

if ( mComposerMap )
{
composerTableElem.setAttribute( "composerMap", mComposerMap->id() );
@@ -154,6 +171,14 @@ bool QgsComposerTable::readXML( const QDomElement& itemElem, const QDomDocument&
mHeaderFont.fromString( itemElem.attribute( "headerFont", "" ) );
mContentFont.fromString( itemElem.attribute( "contentFont", "" ) );
mLineTextDistance = itemElem.attribute( "lineTextDist", "1.0" ).toDouble();
mGridStrokeWidth = itemElem.attribute( "gridStrokeWidth", "0.5" ).toDouble();
mShowGrid = itemElem.attribute( "showGrid", "1" ).toInt();

//grid color
int gridRed = itemElem.attribute( "gridColorRed", "0" ).toInt();
int gridGreen = itemElem.attribute( "gridColorGreen", "0" ).toInt();
int gridBlue = itemElem.attribute( "gridColorBlue", "0" ).toInt();
mGridColor = QColor( gridRed, gridGreen, gridBlue );

//composer map
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt();
@@ -263,7 +288,8 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeList )
{
//calculate height
double totalHeight = fontAscentMillimeters( mHeaderFont ) + attributeList.size() * fontAscentMillimeters( mContentFont ) + ( attributeList.size() + 1 ) * mLineTextDistance * 2;
double totalHeight = fontAscentMillimeters( mHeaderFont ) + attributeList.size() * fontAscentMillimeters( mContentFont ) \
+ ( attributeList.size() + 1 ) * mLineTextDistance * 2 + ( attributeList.size() + 2 ) * mGridStrokeWidth;

//adapt frame to total width
double totalWidth = 0;
@@ -273,34 +299,41 @@ void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, con
totalWidth += maxColWidthIt.value();
}
totalWidth += ( 2 * maxWidthMap.size() * mLineTextDistance );
totalWidth += ( maxWidthMap.size() + 1 ) * mGridStrokeWidth;
QTransform t = transform();
setSceneRect( QRectF( t.dx(), t.dy(), totalWidth, totalHeight ) );
}

void QgsComposerTable::drawHorizontalGridLines( QPainter* p, int nAttributes )
{
//horizontal lines
double currentY = 0;
p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) );
double halfGridStrokeWidth = mGridStrokeWidth / 2.0;
double currentY = halfGridStrokeWidth;
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
currentY += mGridStrokeWidth;
currentY += ( fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance );
for ( int i = 0; i < nAttributes; ++i )
{
p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) );
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
currentY += mGridStrokeWidth;
currentY += ( fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance );
}
p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) );
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
}

void QgsComposerTable::drawVerticalGridLines( QPainter* p, const QMap<int, double>& maxWidthMap )
{
//vertical lines
double currentX = 0;
p->drawLine( QPointF( currentX, 0 ), QPointF( currentX, rect().height() ) );
double halfGridStrokeWidth = mGridStrokeWidth / 2.0;
double currentX = halfGridStrokeWidth;
p->drawLine( QPointF( currentX, halfGridStrokeWidth ), QPointF( currentX, rect().height() - halfGridStrokeWidth ) );
currentX += mGridStrokeWidth;
QMap<int, double>::const_iterator maxColWidthIt = maxWidthMap.constBegin();
for ( ; maxColWidthIt != maxWidthMap.constEnd(); ++maxColWidthIt )
{
currentX += ( maxColWidthIt.value() + 2 * mLineTextDistance );
p->drawLine( QPointF( currentX, 0 ), QPointF( currentX, rect().height() ) );
p->drawLine( QPointF( currentX, halfGridStrokeWidth ), QPointF( currentX, rect().height() - halfGridStrokeWidth ) );
currentX += mGridStrokeWidth;
}
}

@@ -55,6 +55,15 @@ class QgsComposerTable: public QgsComposerItem
void setContentFont( const QFont& f ) { mContentFont = f; }
QFont contentFont() const { return mContentFont; }

void setShowGrid( bool show ) { mShowGrid = show;}
bool showGrid() const { return mShowGrid; }

void setGridStrokeWidth( double w ) { mGridStrokeWidth = w; }
double gridStrokeWidth() const { return mGridStrokeWidth; }

void setGridColor( const QColor& c ) { mGridColor = c; }
QColor gridColor() const { return mGridColor; }

private:
/**Associated vector layer*/
QgsVectorLayer* mVectorLayer;
@@ -64,10 +73,14 @@ class QgsComposerTable: public QgsComposerItem
int mMaximumNumberOfFeatures;
/**Distance between table lines and text*/
double mLineTextDistance;
QPen mGridPen;

QFont mHeaderFont;
QFont mContentFont;

bool mShowGrid;
double mGridStrokeWidth;
QColor mGridColor;

/**Retrieves feature attributes*/
bool getFeatureAttributes( QList<QgsAttributeMap>& attributes );
/**Calculate the maximum width values of the vector attributes*/

0 comments on commit 41c888c

Please sign in to comment.