Skip to content
Permalink
Browse files

[composer] Add function for getting a friendly display name for items

  • Loading branch information
nyalldawson committed Jul 28, 2014
1 parent 38d458c commit 3f076f0d30c404d291b715d85b686bda5949a110
@@ -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
@@ -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
@@ -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 */
@@ -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>" );
}
@@ -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
@@ -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
@@ -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();

@@ -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>" );
}
@@ -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 );

0 comments on commit 3f076f0

Please sign in to comment.
You can’t perform that action at this time.