Skip to content

Commit

Permalink
[composer] Begin work on calculating rows for QgsComposerTableV2
Browse files Browse the repository at this point in the history
(sponsored by City of Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent d1e26f2 commit 37dbbd5
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 49 deletions.
3 changes: 2 additions & 1 deletion python/core/composer/qgscomposerhtml.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion python/core/composer/qgscomposermultiframe.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion src/core/composer/qgscomposerframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 3 additions & 1 deletion src/core/composer/qgscomposerhtml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerhtml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
21 changes: 21 additions & 0 deletions src/core/composer/qgscomposermultiframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down
12 changes: 11 additions & 1 deletion src/core/composer/qgscomposermultiframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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).
Expand Down
148 changes: 108 additions & 40 deletions src/core/composer/qgscomposertablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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 );

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


//and the borders
if ( mShowGrid )
{
Expand All @@ -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 )
Expand Down Expand Up @@ -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 ) );
Expand All @@ -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;
}
}
Expand Down
Loading

0 comments on commit 37dbbd5

Please sign in to comment.