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

This commit is contained in:
Nyall Dawson 2014-07-28 19:44:43 +10:00
parent 38d458ce5c
commit 3f076f0d30
9 changed files with 113 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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 */

View File

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

View File

@ -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

19
src/core/composer/qgscomposerlabel.cpp Executable file → Normal file
View File

@ -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

View File

@ -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();

View File

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

View File

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