5 changes: 5 additions & 0 deletions python/core/composer/qgscomposition.sip
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ class QgsComposition : QGraphicsScene
public slots:
/**Casts object to the proper subclass type and calls corresponding itemAdded signal*/
void sendItemAddedSignal( QgsComposerItem* item );

/**Updates the scene bounds of the composition
@note added in version 2.2*/
void updateBounds();

signals:
void paperSizeChanged();
void nPagesChanged();
Expand Down
9 changes: 1 addition & 8 deletions src/app/composer/qgscomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,14 +957,7 @@ void QgsComposer::zoomFull( void )
{
if ( mView )
{
int nPages = mComposition->numPages();
if ( nPages < 1 )
{
return;
}
double height = mComposition->paperHeight() * nPages + mComposition->spaceBetweenPages() * ( nPages - 1 );
mView->fitInView( 0, 0, mComposition->paperWidth() + 1, height + 1, Qt::KeepAspectRatio );
mView->centerOn(( mComposition->paperWidth() + 1 ) / 2, ( height + 1 ) / 2 );
mView->fitInView( mComposition->sceneRect(), Qt::KeepAspectRatio );
}
}

Expand Down
80 changes: 79 additions & 1 deletion src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
createDefaultPageStyleSymbol();
addPaperItem();

updateBounds();

//add mouse selection handles to composition, and initially hide
mSelectionHandles = new QgsComposerMouseHandles( this );
addItem( mSelectionHandles );
Expand Down Expand Up @@ -154,6 +156,36 @@ void QgsComposition::loadDefaults()
mAlignmentSnapTolerance = settings.value( "/Composer/defaultSnapGuideTolerance", 2 ).toDouble();
}

void QgsComposition::updateBounds()
{
setSceneRect( compositionBounds() );
}

QRectF QgsComposition::compositionBounds() const
{
//start with an empty rectangle
QRectF bounds = QRectF( 0, 0, 0, 0 );

//add all QgsComposerItems and QgsPaperItems which are in the composition
QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
const QgsComposerItem* composerItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
const QgsPaperItem* paperItem = dynamic_cast<const QgsPaperItem*>( *itemIt );
if (( composerItem || paperItem ) )
{
//expand bounds with current item's bounds
bounds = bounds.united(( *itemIt )->sceneBoundingRect() );
}
}

//finally, expand bounds out by 5% page size to give a bit of a margin
bounds.adjust( -mPageWidth * 0.05, -mPageWidth * 0.05, mPageWidth * 0.05, mPageWidth * 0.05 );

return bounds;
}

void QgsComposition::setPaperSize( double width, double height )
{
mPageWidth = width;
Expand All @@ -164,6 +196,7 @@ void QgsComposition::setPaperSize( double width, double height )
mPages.at( i )->setSceneRect( QRectF( 0, currentY, width, height ) );
currentY += ( height + mSpaceBetweenPages );
}
updateBounds();
emit paperSizeChanged();
}

Expand Down Expand Up @@ -198,9 +231,11 @@ void QgsComposition::setNumPages( int pages )
}
}

// update the corresponding variable
//update the corresponding variable
QgsExpression::setSpecialColumn( "$numpages", QVariant(( int )numPages() ) );

updateBounds();

emit nPagesChanged();
}

Expand Down Expand Up @@ -637,6 +672,8 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu

updatePaperItems();

updateBounds();

return true;
}

Expand Down Expand Up @@ -1948,22 +1985,34 @@ void QgsComposition::endMultiFrameCommand()
void QgsComposition::addMultiFrame( QgsComposerMultiFrame* multiFrame )
{
mMultiFrames.insert( multiFrame );

updateBounds();
}

void QgsComposition::removeMultiFrame( QgsComposerMultiFrame* multiFrame )
{
mMultiFrames.remove( multiFrame );

updateBounds();
}

void QgsComposition::addComposerArrow( QgsComposerArrow* arrow )
{
addItem( arrow );

updateBounds();
connect( arrow, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerArrowAdded( arrow );
}

void QgsComposition::addComposerLabel( QgsComposerLabel* label )
{
addItem( label );

updateBounds();
connect( label, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerLabelAdded( label );
}

Expand All @@ -1981,42 +2030,69 @@ void QgsComposition::addComposerMap( QgsComposerMap* map, bool setDefaultPreview
map->cache();
}

updateBounds();
connect( map, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerMapAdded( map );
}

void QgsComposition::addComposerScaleBar( QgsComposerScaleBar* scaleBar )
{
addItem( scaleBar );

updateBounds();
connect( scaleBar, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerScaleBarAdded( scaleBar );
}

void QgsComposition::addComposerLegend( QgsComposerLegend* legend )
{
addItem( legend );

updateBounds();
connect( legend, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerLegendAdded( legend );
}

void QgsComposition::addComposerPicture( QgsComposerPicture* picture )
{
addItem( picture );

updateBounds();
connect( picture, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerPictureAdded( picture );
}

void QgsComposition::addComposerShape( QgsComposerShape* shape )
{
addItem( shape );

updateBounds();
connect( shape, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerShapeAdded( shape );
}

void QgsComposition::addComposerTable( QgsComposerAttributeTable* table )
{
addItem( table );

updateBounds();
connect( table, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerTableAdded( table );
}

void QgsComposition::addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame )
{
addItem( frame );

updateBounds();
connect( frame, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

emit composerHtmlFrameAdded( html, frame );
}

Expand Down Expand Up @@ -2090,6 +2166,8 @@ void QgsComposition::removeComposerItem( QgsComposerItem* item, bool createComma
}
}
}

updateBounds();
}

void QgsComposition::pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state )
Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
/**Casts object to the proper subclass type and calls corresponding itemAdded signal*/
void sendItemAddedSignal( QgsComposerItem* item );

/**Updates the scene bounds of the composition
@note added in version 2.2*/
void updateBounds();

private:
/**Pointer to map renderer of QGIS main map*/
QgsMapRenderer* mMapRenderer;
Expand Down Expand Up @@ -513,6 +517,9 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene

QgsComposition(); //default constructor is forbidden

/**Calculates the bounds of all non-gui items in the composition. Ignores snap lines and mouse handles*/
QRectF compositionBounds() const;

/**Reset z-values of items based on position in z list*/
void updateZValues( bool addUndoCommands = true );

Expand Down
17 changes: 15 additions & 2 deletions src/ui/qgscompositionwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
Expand Down Expand Up @@ -189,7 +198,11 @@
</widget>
</item>
<item row="4" column="1">
<widget class="QSpinBox" name="mNumPagesSpinBox"/>
<widget class="QSpinBox" name="mNumPagesSpinBox">
<property name="minimum">
<number>1</number>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="textLabel7">
Expand Down