Skip to content

Commit

Permalink
[composer] Implement min size method for frames. Minimum height for
Browse files Browse the repository at this point in the history
first frame in tables is set so that headers are always shown.
(Sponsored by City of Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent 37dbbd5 commit c833a93
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerattributetablev2.cpp
Expand Up @@ -128,6 +128,7 @@ QgsComposerAttributeTableV2::QgsComposerAttributeTableV2( QgsComposition* compos
//connect to atlas feature changes to update table rows
connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( refreshAttributes() ) );
}
refreshAttributes();
}

QgsComposerAttributeTableV2::~QgsComposerAttributeTableV2()
Expand Down
31 changes: 24 additions & 7 deletions src/core/composer/qgscomposerframe.cpp
Expand Up @@ -23,6 +23,11 @@ QgsComposerFrame::QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf
{
//repaint frame when multiframe content changes
connect( mf, SIGNAL( contentsChanged() ), this, SLOT( repaint() ) );
if ( mf )
{
//force recalculation of rect, so that multiframe specified sizes can be applied
setSceneRect( QRectF( pos().x(), pos().y(), rect().width(), rect().height() ) );
}
}

QgsComposerFrame::QgsComposerFrame()
Expand Down Expand Up @@ -80,14 +85,26 @@ QString QgsComposerFrame::displayName() const
void QgsComposerFrame::setSceneRect( const QRectF &rectangle )
{
QRectF fixedRect = rectangle;
QSizeF fixedSize = mMultiFrame->fixedFrameSize();
if ( fixedSize.width() > 0 )
{
fixedRect.setWidth( fixedSize.width() );
}
if ( fixedSize.height() > 0 )

if ( mMultiFrame )
{
fixedRect.setHeight( fixedSize.height() );
//calculate index of frame
int frameIndex = mMultiFrame->frameIndex( this );

QSizeF fixedSize = mMultiFrame->fixedFrameSize( frameIndex );
if ( fixedSize.width() > 0 )
{
fixedRect.setWidth( fixedSize.width() );
}
if ( fixedSize.height() > 0 )
{
fixedRect.setHeight( fixedSize.height() );
}

//check minimum size
QSizeF minSize = mMultiFrame->minFrameSize( frameIndex );
fixedRect.setWidth( qMax( minSize.width(), fixedRect.width() ) );
fixedRect.setHeight( qMax( minSize.height(), fixedRect.height() ) );
}

QgsComposerItem::setSceneRect( fixedRect );
Expand Down
16 changes: 16 additions & 0 deletions src/core/composer/qgscomposermultiframe.cpp
Expand Up @@ -176,6 +176,22 @@ void QgsComposerMultiFrame::recalculateFrameSizes()
}
}

void QgsComposerMultiFrame::recalculateFrameRects()
{
if ( mFrameItems.size() < 1 )
{
//no frames, nothing to do
return;
}

QList<QgsComposerFrame*>::iterator frameIt = mFrameItems.begin();
for ( ; frameIt != mFrameItems.end(); ++frameIt )
{
( *frameIt )->setSceneRect( QRectF(( *frameIt )->scenePos().x(), ( *frameIt )->scenePos().y(),
( *frameIt )->rect().width(), ( *frameIt )->rect().height() ) );
}
}

QgsComposerFrame* QgsComposerMultiFrame::createNewFrame( QgsComposerFrame* currentFrame, QPointF pos, QSizeF size )
{
if ( !currentFrame )
Expand Down
29 changes: 27 additions & 2 deletions src/core/composer/qgscomposermultiframe.h
Expand Up @@ -47,13 +47,29 @@ class CORE_EXPORT QgsComposerMultiFrame: public QgsComposerObject
virtual ~QgsComposerMultiFrame();
virtual QSizeF totalSize() const = 0;

/**Returns a fixed size for the frames, if desired.
/**Returns a fixed size for the frames, if desired. If the fixed frame size changes,
* the sizes of all frames can be recalculated by calling recalculateFrameRects().
* @param frameIndex frame number
* @returns fixed size for frames. If the size has a width or height of 0, then
* the frame size is not fixed in that direction and frames can have variable width
* or height accordingly.
* @note added in version 2.5
* @see minFrameSize
* @see recalculateFrameRects
*/
virtual QSizeF fixedFrameSize() const { return QSizeF( 0, 0 ); }
virtual QSizeF fixedFrameSize( const int frameIndex = -1 ) const { Q_UNUSED( frameIndex ); return QSizeF( 0, 0 ); }

/**Returns the minimum size size for the frames, if desired. If the minimum
* size changes, the sizes of all frames can be recalculated by calling
* recalculateFrameRects().
* @param frameIndex frame number
* @returns minimum size for frames. If the size has a width or height of 0, then
* the frame size has no minimum in that direction.
* @note added in version 2.5
* @see fixedFrameSize
* @see recalculateFrameRects
*/
virtual QSizeF minFrameSize( const int frameIndex = -1 ) const { Q_UNUSED( frameIndex ); return QSizeF( 0, 0 ); }

Q_DECL_DEPRECATED virtual void render( QPainter* p, const QRectF& renderExtent );

Expand Down Expand Up @@ -128,6 +144,15 @@ class CORE_EXPORT QgsComposerMultiFrame: public QgsComposerObject
*/
void recalculateFrameSizes();

/**Forces a recalculation of all the associated frame's scene rectangles. This
* method is useful for multiframes which implement a minFrameSize() or
* fixedFrameSize() method.
* @note added in version 2.5
* @see minFrameSize()
* @see fixedFrameSize()
*/
void recalculateFrameRects();

protected:
QList<QgsComposerFrame*> mFrameItems;
ResizeMode mResizeMode;
Expand Down
32 changes: 29 additions & 3 deletions src/core/composer/qgscomposertablev2.cpp
Expand Up @@ -31,7 +31,14 @@ QgsComposerTableV2::QgsComposerTableV2( QgsComposition *composition, bool create
, mGridStrokeWidth( 0.5 )
, mGridColor( Qt::black )
{

//get default composer font from settings
QSettings settings;
QString defaultFontString = settings.value( "/Composer/defaultFont" ).toString();
if ( !defaultFontString.isEmpty() )
{
mHeaderFont.setFamily( defaultFontString );
mContentFont.setFamily( defaultFontString );
}
}

QgsComposerTableV2::QgsComposerTableV2()
Expand All @@ -42,7 +49,8 @@ QgsComposerTableV2::QgsComposerTableV2()

QgsComposerTableV2::~QgsComposerTableV2()
{

qDeleteAll( mColumns );
mColumns.clear();
}

bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
Expand Down Expand Up @@ -398,11 +406,24 @@ QMap<int, QString> QgsComposerTableV2::headerLabels() const
return headers;
}

QSizeF QgsComposerTableV2::fixedFrameSize() const
QSizeF QgsComposerTableV2::fixedFrameSize( const int frameIndex ) const
{
Q_UNUSED( frameIndex );
return QSizeF( mTableSize.width(), 0 );
}

QSizeF QgsComposerTableV2::minFrameSize( const int frameIndex ) const
{
double height = 0;
if ( frameIndex == 0 )
{
//force first frame to be high enough for header
//TODO - handle different header modes
height = 2 * mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );
}
return QSizeF( 0, height );
}

void QgsComposerTableV2::refreshAttributes()
{
mMaxColumnWidthMap.clear();
Expand Down Expand Up @@ -542,4 +563,9 @@ void QgsComposerTableV2::drawVerticalGridLines( QPainter *painter, const QMap<in
void QgsComposerTableV2::adjustFrameToSize()
{
mTableSize = QSizeF( totalWidth(), totalHeight() );
//force recalculation of frame rects, so that they are set to the correct
//fixed and minimum frame sizes
recalculateFrameRects();

recalculateFrameSizes();
}
5 changes: 4 additions & 1 deletion src/core/composer/qgscomposertablev2.h
Expand Up @@ -236,7 +236,10 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
virtual bool getTableContents( QgsComposerTableContents &contents ) = 0;

//reimplemented to return fixed table width
virtual QSizeF fixedFrameSize() const;
virtual QSizeF fixedFrameSize( const int frameIndex = -1 ) const;

//reimplemented to return min frame height
virtual QSizeF minFrameSize( const int frameIndex = -1 ) const;

public slots:

Expand Down

0 comments on commit c833a93

Please sign in to comment.