Skip to content

Commit

Permalink
[composer] Fix calculation of visible row ranges in 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 7d63ade commit 8171525
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposerframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class CORE_EXPORT QgsComposerFrame: public QgsComposerItem
//Overriden to handle fixed frame sizes set by multi frame
void setSceneRect( const QRectF& rectangle );

/**Returns the visible portion of the multi frame's content which
* is shown in this frame.
* @returns extent of visible portion
* @note added in QGIS 2.5
*/
QRectF extent() const { return mSection; }

private:
QgsComposerFrame(); //forbidden
QgsComposerMultiFrame* mMultiFrame;
Expand Down
52 changes: 46 additions & 6 deletions src/core/composer/qgscomposertablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,60 @@ QSizeF QgsComposerTableV2::totalSize() const
return mTableSize;
}

int QgsComposerTableV2::rowsVisible( const int frameIndex ) const
{
//get frame extent
if ( frameIndex >= frameCount() )
{
return 0;
}
QRectF frameExtent = frame( frameIndex )->extent();

//calculate header height
double headerHeight = 0;
if (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) )
{
//frame has a header
headerHeight = 2 * ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );
}
else
{
//frame has no header text, just the stroke
headerHeight = ( mShowGrid ? mGridStrokeWidth : 0 );
}

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

//calculate number of visible rows
double rowHeight = ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mContentFont );
return qMax( floor( contentHeight / rowHeight ), 0.0 );
}

QPair< int, int > QgsComposerTableV2::rowRange( const QRectF extent, const int frameIndex ) const
{
//calculate row height
//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 * ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );
if ( frameIndex >= frameCount() )
{
//bad frame index
return qMakePair( 0 , 0 );
}

//loop through all previous frames to calculate how many rows are visible in each
//as the entire height of a frame may not be utilised for content rows
int rowsAlreadyShown = 0;
for ( int idx = 0; idx < frameIndex; ++idx )
{
rowsAlreadyShown += rowsVisible( idx );
}

double headerHeight = 0;
if (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) )
{
//frame has a header
headerHeight = firstHeaderHeight;
headerHeight = 2 * ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );
}
else
{
Expand All @@ -161,7 +201,7 @@ QPair< int, int > QgsComposerTableV2::rowRange( const QRectF extent, const int f
double rowHeight = ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mContentFont );

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

Expand Down
7 changes: 6 additions & 1 deletion src/core/composer/qgscomposertablev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
{
Q_OBJECT


public:

/*! Controls how headers are horizontally aligned in a table
Expand Down Expand Up @@ -324,6 +323,12 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame

double totalHeight() const;

/**Calculates how many content rows are visible within a given frame
* @param frameIndex index number for frame
* @returns number of visible content rows (excludes header rows)
*/
int rowsVisible( const int frameIndex ) const;

/**Calculates a range of rows which should be visible in a given
* rectangle.
* @param extent visible extent
Expand Down

0 comments on commit 8171525

Please sign in to comment.