Skip to content
Permalink
Browse files

[composer] Fix calculation of visible row ranges in QgsComposerTableV2

(Sponsored by City of Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent 7d63ade commit 817152598de0cafbfe48ccdacf5445a32407241f
@@ -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;
@@ -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
{
@@ -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() );

@@ -50,7 +50,6 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
{
Q_OBJECT


public:

/*! Controls how headers are horizontally aligned in a table
@@ -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

0 comments on commit 8171525

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