Skip to content

Commit

Permalink
[composer] Add function for getting a friendly display name for items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 28, 2014
1 parent 38d458c commit 3f076f0
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/composer/qgscomposeritem.sip
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
*/
QString uuid() const;

/**Get item display name. This is the item's id if set, and if
* not, a user-friendly string identifying item type.
* @returns display name for item
* @see id
* @see setId
* @note added in version 2.5
*/
virtual QString displayName() const;

/**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/qgscomposerlabel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class QgsComposerLabel : QgsComposerItem
* @param doc document
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

//Overriden to contain part of label's text
virtual QString displayName() const;

public slots:
/* Sets rotation for the label
Expand Down
3 changes: 3 additions & 0 deletions python/core/composer/qgscomposershape.sip
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class QgsComposerShape: QgsComposerItem
* QgsComposerItem as it needs to call updateBoundingRect after the shape's size changes
*/
void setSceneRect( const QRectF& rectangle );

//Overriden to return shape type
virtual QString displayName() const;

protected:
/* reimplement drawFrame, since it's not a rect, but a custom shape */
Expand Down
41 changes: 41 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,3 +1288,44 @@ void QgsComposerItem::setIsGroupMember( const bool isGroupMember )
mIsGroupMember = isGroupMember;
setFlag( QGraphicsItem::ItemIsSelectable, !isGroupMember ); //item in groups cannot be selected
}

QString QgsComposerItem::displayName() const
{
//return id, if it's not empty
if ( ! id().isEmpty() )
{
return id();
}

//for unnamed items, default to item type
//(note some item types override this method to provide their own defaults)
switch ( type() )
{
case ComposerArrow:
return tr( "<arrow>" );
case ComposerItemGroup:
return tr( "<group>" );
case ComposerLabel:
return tr( "<label>" );
case ComposerLegend:
return tr( "<legend>" );
case ComposerMap:
return tr( "<map>" );
case ComposerPicture:
return tr( "<picture>" );
case ComposerScaleBar:
return tr( "<scale bar>" );
case ComposerShape:
return tr( "<shape>" );
case ComposerTable:
return tr( "<table>" );
case ComposerAttributeTable:
return tr( "<attribute table>" );
case ComposerTextTable:
return tr( "<text table>" );
case ComposerFrame:
return tr( "<frame>" );
}

return tr( "<item>" );
}
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
*/
QString uuid() const { return mUuid; }

/**Get item display name. This is the item's id if set, and if
* not, a user-friendly string identifying item type.
* @returns display name for item
* @see id
* @see setId
* @note added in version 2.5
*/
virtual QString displayName() const;

/**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
19 changes: 19 additions & 0 deletions src/core/composer/qgscomposerlabel.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
return true;
}

QString QgsComposerLabel::displayName() const
{
if ( !id().isEmpty() )
{
return id();
}

//if no id, default to portion of label text
QString text = displayText();
if ( text.length() > 25 )
{
return QString( tr( "%1..." ) ).arg( text.left( 25 ) );
}
else
{
return text;
}
}

void QgsComposerLabel::itemShiftAdjustSize( double newWidth, double newHeight, double& xShift, double& yShift ) const
{
//keep alignment point constant
Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

//Overriden to contain part of label's text
virtual QString displayName() const;

public slots:
void refreshExpressionContext();

Expand Down
23 changes: 23 additions & 0 deletions src/core/composer/qgscomposershape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,26 @@ void QgsComposerShape::setSceneRect( const QRectF& rectangle )
updateBoundingRect();
update();
}

QString QgsComposerShape::displayName() const
{
if ( !id().isEmpty() )
{
return id();
}

switch ( mShape )
{
case Ellipse:
return tr( "<ellipse>" );
break;
case Rectangle:
return tr( "<rectangle>" );
break;
case Triangle:
return tr( "<triangle>" );
break;
}

return tr( "<shape>" );
}
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposershape.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class CORE_EXPORT QgsComposerShape: public QgsComposerItem
*/
void setSceneRect( const QRectF& rectangle );

//Overriden to return shape type
virtual QString displayName() const;

protected:
/* reimplement drawFrame, since it's not a rect, but a custom shape */
virtual void drawFrame( QPainter* p );
Expand Down

0 comments on commit 3f076f0

Please sign in to comment.