Skip to content

Commit

Permalink
[composer] Fix scrollbars in composer by correctly calculating scene …
Browse files Browse the repository at this point in the history
…bounds (fix #7691)
  • Loading branch information
nyalldawson committed Feb 13, 2014
1 parent ee236d2 commit 9e948df
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
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
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

0 comments on commit 9e948df

Please sign in to comment.