Skip to content

Commit

Permalink
Port display name handling from composer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent ee0e9ff commit 0c858d2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
9 changes: 9 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -113,6 +113,15 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
.. seealso:: uuid()
%End

virtual QString displayName() const;
%Docstring
Get item display name. This is the item's id if set, and if
not, a user-friendly string identifying item type.
.. seealso:: id()
.. seealso:: setId()
:rtype: str
%End

virtual void setSelected( bool selected );
%Docstring
Sets whether the item should be selected.
Expand Down
24 changes: 24 additions & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -70,6 +70,23 @@ QgsLayoutItem::~QgsLayoutItem()
}
}

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

//for unnamed items, default to item type
if ( QgsLayoutItemAbstractMetadata *metadata = QgsApplication::layoutItemRegistry()->itemMetadata( type() ) )
{
return tr( "<%1>" ).arg( metadata->visibleName() );
}

return tr( "<item>" );
}

int QgsLayoutItem::type() const
{
return QgsLayoutItemRegistry::LayoutItem;
Expand All @@ -82,7 +99,14 @@ void QgsLayoutItem::setId( const QString &id )
return;
}

if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->beginCommand( this, tr( "Change item ID" ) );

mId = id;

if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->endCommand();

setToolTip( id );

//inform model that id data has changed
Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -134,6 +134,14 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
*/
virtual void setId( const QString &id );

/**
* Get item display name. This is the item's id if set, and if
* not, a user-friendly string identifying item type.
* \see id()
* \see setId()
*/
virtual QString displayName() const;

/**
* Sets whether the item should be selected.
*/
Expand Down
6 changes: 0 additions & 6 deletions src/core/layout/qgslayoutmodel.cpp
Expand Up @@ -132,10 +132,7 @@ QVariant QgsLayoutModel::data( const QModelIndex &index, int role ) const
case Qt::DisplayRole:
if ( index.column() == ItemId )
{
#if 0 // TODO
return item->displayName();
#endif
return "my item";
}
else
{
Expand Down Expand Up @@ -972,10 +969,7 @@ bool QgsLayoutProxyModel::lessThan( const QModelIndex &left, const QModelIndex &
if ( !item2 )
return true;

#if 0 //TODO
return QString::localeAwareCompare( item1->displayName(), item2->displayName() ) < 0;
#endif
return QString::localeAwareCompare( QString(), QString() ) < 0;
}

QgsLayoutItem *QgsLayoutProxyModel::itemFromSourceIndex( const QModelIndex &sourceIndex ) const
Expand Down
12 changes: 11 additions & 1 deletion tests/src/python/test_qgslayoutitem.py
Expand Up @@ -17,12 +17,14 @@
from qgis.core import (QgsProject,
QgsLayout,
QgsLayoutItemMap,
QgsLayoutItemEllipseShape,
QgsLayoutObject,
QgsProperty,
QgsLayoutMeasurement,
QgsUnitTypes,
QgsLayoutPoint,
QgsLayoutSize)
QgsLayoutSize,
QgsApplication)
from qgis.PyQt.QtCore import QRectF
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtTest import QSignalSpy
Expand Down Expand Up @@ -135,6 +137,14 @@ def testRectWithFrame(self):
item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(item.rectWithFrame(), QRectF(-50.0, -50.0, 118.0, 112.0))

def testDisplayName(self):
layout = QgsLayout(QgsProject.instance())
item = QgsLayoutItemEllipseShape(layout)
self.assertEqual(item.displayName(), '<Ellipse>')
item.setId('a')
self.assertEqual(item.displayName(), 'a')
self.assertEqual(item.id(), 'a')


if __name__ == '__main__':
unittest.main()

0 comments on commit 0c858d2

Please sign in to comment.