Skip to content

Commit

Permalink
[composer] Implement total height mechanisms 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 8171525 commit 407a05c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/app/composer/qgscomposerattributetablewidget.cpp
Expand Up @@ -40,6 +40,12 @@ QgsComposerAttributeTableWidget::QgsComposerAttributeTableWidget( QgsComposerAtt
mainLayout->addWidget( itemPropertiesWidget );

blockAllSignals( true );

mResizeModeComboBox->addItem( tr( "Use existing frames" ), QgsComposerMultiFrame::UseExistingFrames );
mResizeModeComboBox->addItem( tr( "Extend to next page" ), QgsComposerMultiFrame::ExtendToNextPage );
mResizeModeComboBox->addItem( tr( "Repeat on every page" ), QgsComposerMultiFrame::RepeatOnEveryPage );
mResizeModeComboBox->addItem( tr( "Repeat until finished" ), QgsComposerMultiFrame::RepeatUntilFinished );

mLayerComboBox->setFilters( QgsMapLayerProxyModel::VectorLayer );
connect( mLayerComboBox, SIGNAL( layerChanged( QgsMapLayer* ) ), this, SLOT( changeLayer( QgsMapLayer* ) ) );

Expand Down Expand Up @@ -468,6 +474,9 @@ void QgsComposerAttributeTableWidget::updateGuiElements()
mHeaderHAlignmentComboBox->setCurrentIndex(( int )mComposerTable->headerHAlignment() );
mHeaderModeComboBox->setCurrentIndex(( int )mComposerTable->headerMode() );

mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mComposerTable->resizeMode() ) );
mAddFramePushButton->setEnabled( mComposerTable->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );

blockAllSignals( false );
}

Expand All @@ -487,6 +496,7 @@ void QgsComposerAttributeTableWidget::blockAllSignals( bool b )
mHeaderModeComboBox->blockSignals( b );
mHeaderFontColorButton->blockSignals( b );
mContentFontColorButton->blockSignals( b );
mResizeModeComboBox->blockSignals( b );
}

void QgsComposerAttributeTableWidget::setMaximumNumberOfFeatures( int n )
Expand Down Expand Up @@ -699,3 +709,21 @@ void QgsComposerAttributeTableWidget::on_mAddFramePushButton_clicked()
composition->setSelectedItem( newFrame );
}
}

void QgsComposerAttributeTableWidget::on_mResizeModeComboBox_currentIndexChanged( int index )
{
if ( !mComposerTable )
{
return;
}

QgsComposition* composition = mComposerTable->composition();
if ( composition )
{
composition->beginMultiFrameCommand( mComposerTable, tr( "Change resize mode" ) );
mComposerTable->setResizeMode(( QgsComposerMultiFrame::ResizeMode )mResizeModeComboBox->itemData( index ).toInt() );
composition->endMultiFrameCommand();
}

mAddFramePushButton->setEnabled( mComposerTable->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
}
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerattributetablewidget.h
Expand Up @@ -63,6 +63,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void on_mHeaderModeComboBox_currentIndexChanged( int index );
void changeLayer( QgsMapLayer* layer );
void on_mAddFramePushButton_clicked();
void on_mResizeModeComboBox_currentIndexChanged( int index );

/**Inserts a new maximum number of features into the spin box (without the spinbox emitting a signal)*/
void setMaximumNumberOfFeatures( int n );
Expand Down
53 changes: 46 additions & 7 deletions src/core/composer/qgscomposertablev2.cpp
Expand Up @@ -133,6 +133,10 @@ QSizeF QgsComposerTableV2::totalSize() const
//TODO - handle multiple cell headers
//also check height calculation function



//calculate total size

return mTableSize;
}

Expand Down Expand Up @@ -571,13 +575,48 @@ double QgsComposerTableV2::totalWidth()

double QgsComposerTableV2::totalHeight() const
{
//calculate height
int n = mTableContents.size();
double totalHeight = QgsComposerUtils::fontAscentMM( mHeaderFont )
+ n * QgsComposerUtils::fontAscentMM( mContentFont )
+ ( n + 1 ) * mCellMargin * 2
+ ( n + 2 ) * ( mShowGrid ? mGridStrokeWidth : 0 );
return totalHeight;
double height = 0;

//loop through all existing 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;
int numberExistingFrames = frameCount();
int rowsVisibleInLastFrame = 0;
double heightOfLastFrame = 0;
for ( int idx = 0; idx < numberExistingFrames; ++idx )
{
rowsVisibleInLastFrame = rowsVisible( idx );
heightOfLastFrame = frame( idx )->rect().height();
rowsAlreadyShown += rowsVisibleInLastFrame;
height += heightOfLastFrame;
if ( rowsAlreadyShown >= mTableContents.length() )
{
//shown entire contents of table, nothing remaining
return height;
}
}

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

if ( remainingRows <= 0 )
{
//no remaining rows
return height;
}

if ( rowsVisibleInLastFrame < 1 )
{
//if no rows are visible in the last frame, calculation of missing frames
//is impossible. So just return total height of existing frames
return height;
}

//rows remain unshown -- how many extra frames would we need to complete the table?
//assume all added frames are same size as final frame
int numberFramesMissing = ceil(( double )remainingRows / ( double )rowsVisibleInLastFrame );
height += heightOfLastFrame * numberFramesMissing;
return height;
}

void QgsComposerTableV2::drawHorizontalGridLines( QPainter *painter, const int rows, const bool drawHeaderLines ) const
Expand Down

0 comments on commit 407a05c

Please sign in to comment.