Skip to content

Commit

Permalink
[composer] Add api method for toggling visibility of composer items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 28, 2014
1 parent 53e1aba commit 200a343
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
9 changes: 9 additions & 0 deletions python/core/composer/qgscomposeritem.sip
Expand Up @@ -517,6 +517,15 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
*/
virtual QString displayName() const;

/**Sets visibility for item.
* @param visible set to true to show item, false to hide item
* @note QGraphicsItem::setVisible should not be called directly
* on a QgsComposerItem, as some item types (eg groups) need to override
* the visibility toggle.
* @note added in version 2.5
*/
virtual void setVisibility( const bool visible );

/**Returns whether this item is part of a group
* @returns true if item is in a group
* @note added in version 2.5
Expand Down
3 changes: 3 additions & 0 deletions python/core/composer/qgscomposeritemgroup.sip
Expand Up @@ -20,6 +20,9 @@ class QgsComposerItemGroup: QgsComposerItem
/**Sets this items bound in scene coordinates such that 1 item size units
corresponds to 1 scene size unit*/
void setSceneRect( const QRectF& rectangle );

//overridden to also hide grouped items
virtual void setVisibility( const bool visible );

/** stores state in Dom node
* @param elem is Dom element corresponding to 'Composer' tag
Expand Down
12 changes: 10 additions & 2 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -185,7 +185,7 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
composerItemElem.setAttribute( "frame", "false" );
}

//frame
//background
if ( mBackground )
{
composerItemElem.setAttribute( "background", "true" );
Expand All @@ -211,6 +211,7 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
composerItemElem.setAttribute( "itemRotation", QString::number( mItemRotation ) );
composerItemElem.setAttribute( "uuid", mUuid );
composerItemElem.setAttribute( "id", mId );
composerItemElem.setAttribute( "visibility", isVisible() );
//position lock for mouse moves/resizes
if ( mItemPositionLocked )
{
Expand All @@ -223,7 +224,6 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons

composerItemElem.setAttribute( "lastValidViewScaleFactor", QString::number( mLastValidViewScaleFactor ) );


//frame color
QDomElement frameColorElem = doc.createElement( "FrameColor" );
QColor frameColor = pen().color();
Expand Down Expand Up @@ -310,6 +310,9 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
setPositionLock( false );
}

//visibility
setVisibility( itemElem.attribute( "visibility", "1" ) != "0" );

//position
int page;
double x, y, pagex, pagey, width, height;
Expand Down Expand Up @@ -1329,3 +1332,8 @@ QString QgsComposerItem::displayName() const

return tr( "<item>" );
}

void QgsComposerItem::setVisibility( const bool visible )
{
QGraphicsItem::setVisible( visible );
}
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -473,6 +473,15 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
*/
virtual QString displayName() const;

/**Sets visibility for item.
* @param visible set to true to show item, false to hide item
* @note QGraphicsItem::setVisible should not be called directly
* on a QgsComposerItem, as some item types (eg groups) need to override
* the visibility toggle.
* @note added in version 2.5
*/
virtual void setVisibility( const bool visible );

/**Returns whether this item is part of a group
* @returns true if item is in a group
* @note added in version 2.5
Expand Down
12 changes: 12 additions & 0 deletions src/core/composer/qgscomposeritemgroup.cpp 100755 → 100644
Expand Up @@ -144,6 +144,18 @@ void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
QgsComposerItem::setSceneRect( rectangle );
}

void QgsComposerItemGroup::setVisibility( const bool visible )
{
//also set visibility for all items within the group
QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
for ( ; item_it != mItems.end(); ++item_it )
{
( *item_it )->setVisibility( visible );
}
//lastly set visibility for group item itself
QgsComposerItem::setVisibility( visible );
}

void QgsComposerItemGroup::drawFrame( QPainter* p )
{
if ( !mComposition )
Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposeritemgroup.h
Expand Up @@ -42,6 +42,9 @@ class CORE_EXPORT QgsComposerItemGroup: public QgsComposerItem
corresponds to 1 scene size unit*/
void setSceneRect( const QRectF& rectangle );

//overridden to also hide grouped items
virtual void setVisibility( const bool visible );

/** stores state in Dom node
* @param elem is Dom element corresponding to 'Composer' tag
* @param doc is the Dom document
Expand Down

0 comments on commit 200a343

Please sign in to comment.