Skip to content

Commit

Permalink
[composer] Tweak calculation of total height of table (Sponsored by
Browse files Browse the repository at this point in the history
City of Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent a0db663 commit 993aa83
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/core/composer/qgscomposertablev2.cpp
Expand Up @@ -155,10 +155,20 @@ int QgsComposerTableV2::rowsVisible( const int frameIndex ) const
}
QRectF frameExtent = frame( frameIndex )->extent();

//calculate header height
double headerHeight = 0;
bool includeHeader = false;
if (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) )
{
includeHeader = true;
}
return rowsVisible( frameExtent.height(), includeHeader );
}

int QgsComposerTableV2::rowsVisible( const double frameHeight, const bool includeHeader ) const
{
//calculate header height
double headerHeight = 0;
if ( includeHeader )
{
//frame has a header
headerHeight = 2 * ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );
Expand All @@ -170,7 +180,7 @@ int QgsComposerTableV2::rowsVisible( const int frameIndex ) const
}

//remaining height available for content rows
double contentHeight = frameExtent.height() - headerHeight;
double contentHeight = frameHeight - headerHeight;

//calculate number of visible rows
double rowHeight = ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mContentFont );
Expand Down Expand Up @@ -519,7 +529,8 @@ void QgsComposerTableV2::refreshAttributes()

//since contents have changed, we also need to recalculate the column widths
//and size of table
adjustFrameToSize();
//gettablecontents does this!
// adjustFrameToSize();
}

bool QgsComposerTableV2::calculateMaxColumnWidths()
Expand Down Expand Up @@ -602,6 +613,14 @@ double QgsComposerTableV2::totalHeight() const
}
}

if ( mResizeMode == QgsComposerMultiFrame::ExtendToNextPage )
{
heightOfLastFrame = mComposition->paperHeight();
bool hasHeader = (( mHeaderMode == QgsComposerTableV2::FirstFrame && numberExistingFrames < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) );
rowsVisibleInLastFrame = rowsVisible( heightOfLastFrame, hasHeader );
}

//calculate how many rows left to show
int remainingRows = mTableContents.length() - rowsAlreadyShown;

Expand Down Expand Up @@ -685,9 +704,10 @@ void QgsComposerTableV2::drawVerticalGridLines( QPainter *painter, const QMap<in
void QgsComposerTableV2::adjustFrameToSize()
{
mTableSize = QSizeF( totalWidth(), totalHeight() );

recalculateFrameSizes();

//force recalculation of frame rects, so that they are set to the correct
//fixed and minimum frame sizes
recalculateFrameRects();

recalculateFrameSizes();
}
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposertablev2.h
Expand Up @@ -329,6 +329,8 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
*/
int rowsVisible( const int frameIndex ) const;

int rowsVisible( const double frameHeight, const bool includeHeader ) const;

/**Calculates a range of rows which should be visible in a given
* rectangle.
* @param extent visible extent
Expand All @@ -337,7 +339,6 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
*/
QPair<int, int> rowRange( const QRectF extent, const int frameIndex ) const;


/**Draws the horizontal grid lines for the table.
* @param painter destination painter for grid lines
* @param rows number of rows shown in table
Expand Down
26 changes: 26 additions & 0 deletions tests/src/core/testqgscomposertablev2.cpp
Expand Up @@ -47,6 +47,7 @@ class TestQgsComposerTableV2: public QObject
void attributeTableRender(); //test rendering attribute table

void attributeTableExtend();
void attributeTableRepeat();

private:
QgsComposition* mComposition;
Expand Down Expand Up @@ -314,7 +315,32 @@ void TestQgsComposerTableV2::attributeTableExtend()
//now auto remove extra created frames
mComposerAttributeTable->setMaximumNumberOfFeatures( 1 );
bool result = checker.testComposition( mReport, 1 );
}

void TestQgsComposerTableV2::attributeTableRepeat()
{
mComposerAttributeTable->setResizeMode( QgsComposerMultiFrame::UseExistingFrames );
//remove extra frames
for ( int idx = mComposerAttributeTable->frameCount(); idx > 0; --idx )
{
mComposerAttributeTable->removeFrame( idx - 1 );
}

mComposerAttributeTable->setMaximumNumberOfFeatures( 1 );

//force auto creation of some new frames
mComposerAttributeTable->setResizeMode( QgsComposerMultiFrame::RepeatUntilFinished );

for ( int features = 0; features < 50; ++features )
{
mComposerAttributeTable->setMaximumNumberOfFeatures( features );
}

//and then the reverse....
for ( int features = 50; features > 1; --features )
{
mComposerAttributeTable->setMaximumNumberOfFeatures( features );
}
}

QTEST_MAIN( TestQgsComposerTableV2 )
Expand Down

0 comments on commit 993aa83

Please sign in to comment.