Skip to content
Permalink
Browse files

[composer] Begin work on calculating rows for QgsComposerTableV2

(sponsored by City of Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent d1e26f2 commit 37dbbd55a8ad3086c3abc3cbf6cfefefe8f35c2f
@@ -95,7 +95,8 @@ class QgsComposerHtml: QgsComposerMultiFrame
void setEvaluateExpressions( bool evaluateExpressions );

QSizeF totalSize() const;
void render( QPainter* p, const QRectF& renderExtent );

void render( QPainter* p, const QRectF& renderExtent, const int frameIndex );

bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const;
bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false );
@@ -28,7 +28,8 @@ class QgsComposerMultiFrame: QgsComposerObject
*/
virtual QSizeF fixedFrameSize() const;

virtual void render( QPainter* p, const QRectF& renderExtent ) = 0;
virtual void render( QPainter* p, const QRectF& renderExtent );
virtual void render( QPainter* p, const QRectF& renderExtent, const int frameIndex );

virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ) = 0;

@@ -106,7 +106,9 @@ void QgsComposerFrame::paint( QPainter* painter, const QStyleOptionGraphicsItem*
drawBackground( painter );
if ( mMultiFrame )
{
mMultiFrame->render( painter, mSection );
//calculate index of frame
int frameIndex = mMultiFrame->frameIndex( this );
mMultiFrame->render( painter, mSection, frameIndex );
}

drawFrame( painter );
@@ -252,8 +252,10 @@ QSizeF QgsComposerHtml::totalSize() const
return mSize;
}

void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent )
void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent, const int frameIndex )
{
Q_UNUSED( frameIndex );

if ( !mWebPage )
{
return;
@@ -118,7 +118,7 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
void setEvaluateExpressions( bool evaluateExpressions );

QSizeF totalSize() const;
void render( QPainter* p, const QRectF& renderExtent );
void render( QPainter* p, const QRectF& renderExtent, const int frameIndex );

bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const;
bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false );
@@ -40,6 +40,22 @@ QgsComposerMultiFrame::~QgsComposerMultiFrame()
deleteFrames();
}

void QgsComposerMultiFrame::render( QPainter *p, const QRectF &renderExtent )
{
//base implementation does nothing
Q_UNUSED( p );
Q_UNUSED( renderExtent );
}

void QgsComposerMultiFrame::render( QPainter *p, const QRectF &renderExtent, const int frameIndex )
{
Q_UNUSED( frameIndex );
//base implementation ignores frameIndex
Q_NOWARN_DEPRECATED_PUSH
render( p, renderExtent );
Q_NOWARN_DEPRECATED_POP
}

void QgsComposerMultiFrame::setResizeMode( ResizeMode mode )
{
if ( mode != mResizeMode )
@@ -307,6 +323,11 @@ QgsComposerFrame* QgsComposerMultiFrame::frame( int i ) const
return mFrameItems.at( i );
}

int QgsComposerMultiFrame::frameIndex( QgsComposerFrame *frame ) const
{
return mFrameItems.indexOf( frame );
}

bool QgsComposerMultiFrame::_writeXML( QDomElement& elem, QDomDocument& doc, bool ignoreFrames ) const
{
elem.setAttribute( "resizeMode", mResizeMode );
@@ -55,7 +55,9 @@ class CORE_EXPORT QgsComposerMultiFrame: public QgsComposerObject
*/
virtual QSizeF fixedFrameSize() const { return QSizeF( 0, 0 ); }

virtual void render( QPainter* p, const QRectF& renderExtent ) = 0;
Q_DECL_DEPRECATED virtual void render( QPainter* p, const QRectF& renderExtent );

virtual void render( QPainter* p, const QRectF& renderExtent, const int frameIndex );

virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ) = 0;

@@ -91,8 +93,16 @@ class CORE_EXPORT QgsComposerMultiFrame: public QgsComposerObject
@note added in 2.0, replaces nFrames
**/
int frameCount() const { return mFrameItems.size(); }

QgsComposerFrame* frame( int i ) const;

/**Returns the index of a frame within the multiframe
* @param frame frame to find index of
* @returns index for frame if found, -1 if frame not found in multiframe
* @note added in version 2.5
*/
int frameIndex( QgsComposerFrame *frame ) const;

/**Creates a new frame and adds it to the multi frame and composition.
* @param currentFrame an existing QgsComposerFrame from which to copy the size
* and general frame properties (eg frame style, background, rendering settings).
@@ -122,20 +122,55 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
QSizeF QgsComposerTableV2::totalSize() const
{
//TODO - handle multiple cell headers
//also check height calculation function

return mTableSize;
}

void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )

QPair< int, int > QgsComposerTableV2::rowRange( const QRectF extent, const int frameIndex ) const
{
//do this via rows
//eg, calculate how rows to->from
//and render them
//calculate row height
//TODO - handle different header modes
//TODO - need to traverse all previous frames to calculate what is visible in each
//as the entire height of a frame may not be used for content
double headerHeight = 0;
double firstHeaderHeight = 2 * mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );

//int frameNumber = mFrameItems.indexOf( this );
if ( frameIndex < 1 )
{
//currently only header on first
headerHeight = firstHeaderHeight;
}
else
{
headerHeight = mGridStrokeWidth;
}

//remaining height available for content rows
double contentHeight = extent.height() - headerHeight;
double rowHeight = mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mContentFont );

//using zero based indexes
int firstVisible = qMax( floor(( extent.top() - firstHeaderHeight ) / rowHeight ), 0.0 );
int rowsVisible = qMax( floor( contentHeight / rowHeight ), 0.0 );
int lastVisible = qMin( firstVisible + rowsVisible, mTableContents.length() );

return qMakePair( firstVisible, lastVisible );
}


void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const int frameIndex )
{
if ( !p )
{
return;
}

//calculate which rows to show in this frame
QPair< int, int > rowsToShow = rowRange( renderExtent, frameIndex );

if ( mComposition->plotStyle() == QgsComposition::Print ||
mComposition->plotStyle() == QgsComposition::Postscript )
{
@@ -160,43 +195,52 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )
double cellHeaderHeight = QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mCellMargin;
double cellBodyHeight = QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mCellMargin;
QRectF cell;

//TODO - should be controlled via a property, eg an enum with values
//always/never/first frame
bool drawHeader = frameIndex < 1;

for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
currentY = mGridStrokeWidth;
currentX += mCellMargin;

cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], cellHeaderHeight );

//calculate alignment of header
Qt::AlignmentFlag headerAlign = Qt::AlignLeft;
switch ( mHeaderHAlignment )
if ( drawHeader )
{
case FollowColumn:
headerAlign = ( *columnIt )->hAlignment();
break;
case HeaderLeft:
headerAlign = Qt::AlignLeft;
break;
case HeaderCenter:
headerAlign = Qt::AlignHCenter;
break;
case HeaderRight:
headerAlign = Qt::AlignRight;
break;
//draw the header
cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], cellHeaderHeight );

//calculate alignment of header
Qt::AlignmentFlag headerAlign = Qt::AlignLeft;
switch ( mHeaderHAlignment )
{
case FollowColumn:
headerAlign = ( *columnIt )->hAlignment();
break;
case HeaderLeft:
headerAlign = Qt::AlignLeft;
break;
case HeaderCenter:
headerAlign = Qt::AlignHCenter;
break;
case HeaderRight:
headerAlign = Qt::AlignRight;
break;
}


QgsComposerUtils::drawText( p, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );

currentY += cellHeaderHeight;
currentY += mGridStrokeWidth;
}

QgsComposerUtils::drawText( p, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );

currentY += cellHeaderHeight;
currentY += mGridStrokeWidth;

//draw the attribute values
QgsComposerTableContents::const_iterator attIt = mTableContents.begin();
for ( ; attIt != mTableContents.end(); ++attIt )
for ( int row = rowsToShow.first; row < rowsToShow.second; ++row )
{
cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], cellBodyHeight );

QVariant cellContents = ( *attIt ).at( col );
QVariant cellContents = mTableContents.at( row ).at( col );
QString str = cellContents.toString();
QgsComposerUtils::drawText( p, cell, str, mContentFont, mContentFontColor, ( *columnIt )->hAlignment(), Qt::AlignVCenter, Qt::TextDontClip );

@@ -210,6 +254,7 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )
col++;
}


//and the borders
if ( mShowGrid )
{
@@ -218,13 +263,12 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )
gridPen.setColor( mGridColor );
gridPen.setJoinStyle( Qt::MiterJoin );
p->setPen( gridPen );
drawHorizontalGridLines( p, mTableContents.size() );
drawVerticalGridLines( p, mMaxColumnWidthMap );
drawHorizontalGridLines( p, rowsToShow.second - rowsToShow.first, drawHeader );
drawVerticalGridLines( p, mMaxColumnWidthMap, rowsToShow.second - rowsToShow.first, drawHeader );
}

p->restore();


}

void QgsComposerTableV2::setCellMargin( const double margin )
@@ -438,14 +482,23 @@ double QgsComposerTableV2::totalHeight() const
return totalHeight;
}

void QgsComposerTableV2::drawHorizontalGridLines( QPainter *painter, const int rows ) const
void QgsComposerTableV2::drawHorizontalGridLines( QPainter *painter, const int rows, const bool drawHeaderLines ) const
{
//horizontal lines
if ( rows < 1 && !drawHeaderLines )
{
return;
}

double halfGridStrokeWidth = mGridStrokeWidth / 2.0;
double currentY = halfGridStrokeWidth;
painter->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( mTableSize.width() - halfGridStrokeWidth, currentY ) );
currentY += mGridStrokeWidth;
currentY += ( QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mCellMargin );
double currentY = 0;
currentY = halfGridStrokeWidth;
if ( drawHeaderLines )
{
painter->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( mTableSize.width() - halfGridStrokeWidth, currentY ) );
currentY += mGridStrokeWidth;
currentY += ( QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mCellMargin );
}
for ( int row = 0; row < rows; ++row )
{
painter->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( mTableSize.width() - halfGridStrokeWidth, currentY ) );
@@ -455,18 +508,33 @@ void QgsComposerTableV2::drawHorizontalGridLines( QPainter *painter, const int r
painter->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( mTableSize.width() - halfGridStrokeWidth, currentY ) );
}

void QgsComposerTableV2::drawVerticalGridLines( QPainter *painter, const QMap<int, double> &maxWidthMap ) const
void QgsComposerTableV2::drawVerticalGridLines( QPainter *painter, const QMap<int, double> &maxWidthMap, const int numberRows, const bool hasHeader ) const
{
//vertical lines
if ( numberRows < 1 && !hasHeader )
{
return;
}

//calculate height of table within frame
double tableHeight = 0;
if ( hasHeader )
{
tableHeight += mGridStrokeWidth + mCellMargin * 2 + QgsComposerUtils::fontAscentMM( mHeaderFont );
}

tableHeight += numberRows * ( mGridStrokeWidth + mCellMargin * 2 + QgsComposerUtils::fontAscentMM( mContentFont ) );
tableHeight += mGridStrokeWidth;

double halfGridStrokeWidth = mGridStrokeWidth / 2.0;
double currentX = halfGridStrokeWidth;
painter->drawLine( QPointF( currentX, halfGridStrokeWidth ), QPointF( currentX, mTableSize.height() - halfGridStrokeWidth ) );
painter->drawLine( QPointF( currentX, halfGridStrokeWidth ), QPointF( currentX, tableHeight - halfGridStrokeWidth ) );
currentX += mGridStrokeWidth;
QMap<int, double>::const_iterator maxColWidthIt = maxWidthMap.constBegin();
for ( ; maxColWidthIt != maxWidthMap.constEnd(); ++maxColWidthIt )
{
currentX += ( maxColWidthIt.value() + 2 * mCellMargin );
painter->drawLine( QPointF( currentX, halfGridStrokeWidth ), QPointF( currentX, mTableSize.height() - halfGridStrokeWidth ) );
painter->drawLine( QPointF( currentX, halfGridStrokeWidth ), QPointF( currentX, tableHeight - halfGridStrokeWidth ) );
currentX += mGridStrokeWidth;
}
}

0 comments on commit 37dbbd5

Please sign in to comment.
You can’t perform that action at this time.