Skip to content

Commit

Permalink
Fix layout items appear in random order in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent 2ec31d8 commit b41fea9
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 80 deletions.
20 changes: 10 additions & 10 deletions python/gui/layout/qgslayoutitemguiregistry.sip
Expand Up @@ -174,10 +174,10 @@ class QgsLayoutItemGuiRegistry : QObject
~QgsLayoutItemGuiRegistry();


QgsLayoutItemAbstractGuiMetadata *itemMetadata( const QString &uuid ) const;
QgsLayoutItemAbstractGuiMetadata *itemMetadata( int metadataId ) const;
%Docstring
Returns the metadata for the specified item ``uuid``. Returns None if
a corresponding uuid was not found in the registry.
Returns the metadata for the specified item ``metadataId``. Returns None if
a corresponding ``metadataId`` was not found in the registry.
:rtype: QgsLayoutItemAbstractGuiMetadata
%End

Expand Down Expand Up @@ -206,9 +206,9 @@ class QgsLayoutItemGuiRegistry : QObject
:rtype: QgsLayoutItemGuiGroup
%End

QgsLayoutItem *createItem( const QString &uuid, QgsLayout *layout ) const /Factory/;
QgsLayoutItem *createItem( int metadataId, QgsLayout *layout ) const /Factory/;
%Docstring
Creates a new instance of a layout item given the item metadata ``uuid``, target ``layout``.
Creates a new instance of a layout item given the item metadata ``metadataId``, target ``layout``.
:rtype: QgsLayoutItem
%End

Expand All @@ -219,18 +219,18 @@ class QgsLayoutItemGuiRegistry : QObject
%End


QList< QString > itemUuids() const;
QList< int > itemMetadataIds() const;
%Docstring
Returns a list of available item metadata uuids handled by the registry.
:rtype: list of str
Returns a list of available item metadata ids handled by the registry.
:rtype: list of int
%End

signals:

void typeAdded( const QString &uuid );
void typeAdded( int metadataId );
%Docstring
Emitted whenever a new item type is added to the registry, with the specified
``uuid``.
``metadataId``.
%End

private:
Expand Down
16 changes: 8 additions & 8 deletions python/gui/layout/qgslayoutviewtooladditem.sip
Expand Up @@ -22,21 +22,21 @@ class QgsLayoutViewToolAddItem : QgsLayoutViewTool

QgsLayoutViewToolAddItem( QgsLayoutView *view /TransferThis/ );

QString itemMetadataUuid() const;
int itemMetadataId() const;
%Docstring
Returns the item metadata uuid for items created by the tool.
.. seealso:: setItemMetadataUuid()
:rtype: str
Returns the item metadata id for items created by the tool.
.. seealso:: setItemMetadataId()
:rtype: int
%End

void setItemMetadataUuid( const QString &uuid );
void setItemMetadataId( int metadataId );
%Docstring
Sets the item metadata ``uuid`` for items created by the tool.
Sets the item metadata ``metadataId`` for items created by the tool.

The \uuid associates the current tool behavior with a metadata entry
The ``metadataId`` associates the current tool behavior with a metadata entry
from QgsLayoutItemGuiRegistry.

.. seealso:: itemMetadataUuid()
.. seealso:: itemMetadataId()
%End

virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );
Expand Down
26 changes: 13 additions & 13 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -169,10 +169,10 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
connect( mActionClose, &QAction::triggered, this, &QWidget::close );

// populate with initial items...
const QStringList itemUuids = QgsGui::layoutItemGuiRegistry()->itemUuids();
for ( const QString &uuid : itemUuids )
const QList< int > itemMetadataIds = QgsGui::layoutItemGuiRegistry()->itemMetadataIds();
for ( int id : itemMetadataIds )
{
itemTypeAdded( uuid );
itemTypeAdded( id );
}
//..and listen out for new item types
connect( QgsGui::layoutItemGuiRegistry(), &QgsLayoutItemGuiRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );
Expand Down Expand Up @@ -840,13 +840,13 @@ void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * )
saveWindowState();
}

void QgsLayoutDesignerDialog::itemTypeAdded( const QString &uuid )
void QgsLayoutDesignerDialog::itemTypeAdded( int id )
{
if ( QgsGui::layoutItemGuiRegistry()->itemMetadata( uuid )->flags() & QgsLayoutItemAbstractGuiMetadata::FlagNoCreationTools )
if ( QgsGui::layoutItemGuiRegistry()->itemMetadata( id )->flags() & QgsLayoutItemAbstractGuiMetadata::FlagNoCreationTools )
return;

QString name = QgsGui::layoutItemGuiRegistry()->itemMetadata( uuid )->visibleName();
QString groupId = QgsGui::layoutItemGuiRegistry()->itemMetadata( uuid )->groupId();
QString name = QgsGui::layoutItemGuiRegistry()->itemMetadata( id )->visibleName();
QString groupId = QgsGui::layoutItemGuiRegistry()->itemMetadata( id )->groupId();
QToolButton *groupButton = nullptr;
QMenu *itemSubmenu = nullptr;
if ( !groupId.isEmpty() )
Expand Down Expand Up @@ -891,8 +891,8 @@ void QgsLayoutDesignerDialog::itemTypeAdded( const QString &uuid )
QAction *action = new QAction( tr( "Add %1" ).arg( name ), this );
action->setToolTip( tr( "Adds a new %1 to the layout" ).arg( name ) );
action->setCheckable( true );
action->setData( uuid );
action->setIcon( QgsGui::layoutItemGuiRegistry()->itemMetadata( uuid )->creationIcon() );
action->setData( id );
action->setIcon( QgsGui::layoutItemGuiRegistry()->itemMetadata( id )->creationIcon() );

mToolsActionGroup->addAction( action );
if ( itemSubmenu )
Expand All @@ -905,9 +905,9 @@ void QgsLayoutDesignerDialog::itemTypeAdded( const QString &uuid )
else
mToolsToolbar->addAction( action );

connect( action, &QAction::triggered, this, [this, uuid]()
connect( action, &QAction::triggered, this, [this, id]()
{
activateNewItemCreationTool( uuid );
activateNewItemCreationTool( id );
} );
}

Expand Down Expand Up @@ -1084,9 +1084,9 @@ void QgsLayoutDesignerDialog::restoreWindowState()
}
}

void QgsLayoutDesignerDialog::activateNewItemCreationTool( const QString &uuid )
void QgsLayoutDesignerDialog::activateNewItemCreationTool( int id )
{
mAddItemTool->setItemMetadataUuid( uuid );
mAddItemTool->setItemMetadataId( id );
if ( mView )
{
mView->setTool( mAddItemTool );
Expand Down
6 changes: 3 additions & 3 deletions src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -226,7 +226,7 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner

private slots:

void itemTypeAdded( const QString &uuid );
void itemTypeAdded( int id );
void statusZoomCombo_currentIndexChanged( int index );
void statusZoomCombo_zoomEntered();
void sliderZoomChanged( int value );
Expand Down Expand Up @@ -312,8 +312,8 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
//! Restore the window and toolbar state
void restoreWindowState();

//! Switch to new item creation tool, for a new item of the specified \a type.
void activateNewItemCreationTool( const QString &uuid );
//! Switch to new item creation tool, for a new item of the specified \a id.
void activateNewItemCreationTool( int id );

void createLayoutPropertiesWidget();

Expand Down
26 changes: 13 additions & 13 deletions src/gui/layout/qgslayoutitemguiregistry.cpp
Expand Up @@ -40,19 +40,19 @@ QgsLayoutItemGuiRegistry::~QgsLayoutItemGuiRegistry()
qDeleteAll( mMetadata );
}

QgsLayoutItemAbstractGuiMetadata *QgsLayoutItemGuiRegistry::itemMetadata( const QString &uuid ) const
QgsLayoutItemAbstractGuiMetadata *QgsLayoutItemGuiRegistry::itemMetadata( int metadataId ) const
{
return mMetadata.value( uuid );
return mMetadata.value( metadataId );
}

bool QgsLayoutItemGuiRegistry::addLayoutItemGuiMetadata( QgsLayoutItemAbstractGuiMetadata *metadata )
{
if ( !metadata )
return false;

QString uuid = QUuid::createUuid().toString();
mMetadata[uuid] = metadata;
emit typeAdded( uuid );
int id = mMetadata.count();
mMetadata[id] = metadata;
emit typeAdded( id );
return true;
}

Expand All @@ -70,16 +70,16 @@ const QgsLayoutItemGuiGroup &QgsLayoutItemGuiRegistry::itemGroup( const QString
return mItemGroups[ id ];
}

QgsLayoutItem *QgsLayoutItemGuiRegistry::createItem( const QString &uuid, QgsLayout *layout ) const
QgsLayoutItem *QgsLayoutItemGuiRegistry::createItem( int metadataId, QgsLayout *layout ) const
{
if ( !mMetadata.contains( uuid ) )
if ( !mMetadata.contains( metadataId ) )
return nullptr;

std::unique_ptr< QgsLayoutItem > item( mMetadata.value( uuid )->createItem( layout ) );
std::unique_ptr< QgsLayoutItem > item( mMetadata.value( metadataId )->createItem( layout ) );
if ( item )
return item.release();

int type = mMetadata.value( uuid )->type();
int type = mMetadata.value( metadataId )->type();
return QgsApplication::layoutItemRegistry()->createItem( type, layout );
}

Expand All @@ -97,15 +97,15 @@ QgsLayoutItemBaseWidget *QgsLayoutItemGuiRegistry::createItemWidget( QgsLayoutIt
return nullptr;
}

QgsLayoutViewRubberBand *QgsLayoutItemGuiRegistry::createItemRubberBand( const QString &uuid, QgsLayoutView *view ) const
QgsLayoutViewRubberBand *QgsLayoutItemGuiRegistry::createItemRubberBand( int metadataId, QgsLayoutView *view ) const
{
if ( !mMetadata.contains( uuid ) )
if ( !mMetadata.contains( metadataId ) )
return nullptr;

return mMetadata[uuid]->createRubberBand( view );
return mMetadata[metadataId]->createRubberBand( view );
}

QList<QString> QgsLayoutItemGuiRegistry::itemUuids() const
QList<int> QgsLayoutItemGuiRegistry::itemMetadataIds() const
{
return mMetadata.keys();
}
Expand Down
24 changes: 12 additions & 12 deletions src/gui/layout/qgslayoutitemguiregistry.h
Expand Up @@ -289,10 +289,10 @@ class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
QgsLayoutItemGuiRegistry &operator=( const QgsLayoutItemGuiRegistry &rh ) = delete;

/**
* Returns the metadata for the specified item \a uuid. Returns nullptr if
* a corresponding uuid was not found in the registry.
* Returns the metadata for the specified item \a metadataId. Returns nullptr if
* a corresponding \a metadataId was not found in the registry.
*/
QgsLayoutItemAbstractGuiMetadata *itemMetadata( const QString &uuid ) const;
QgsLayoutItemAbstractGuiMetadata *itemMetadata( int metadataId ) const;

/**
* Registers the gui metadata for a new layout item type. Takes ownership of the metadata instance.
Expand All @@ -317,40 +317,40 @@ class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
const QgsLayoutItemGuiGroup &itemGroup( const QString &id );

/**
* Creates a new instance of a layout item given the item metadata \a uuid, target \a layout.
* Creates a new instance of a layout item given the item metadata \a metadataId, target \a layout.
*/
QgsLayoutItem *createItem( const QString &uuid, QgsLayout *layout ) const SIP_FACTORY;
QgsLayoutItem *createItem( int metadataId, QgsLayout *layout ) const SIP_FACTORY;

/**
* Creates a new instance of a layout item configuration widget for the specified \a item.
*/
QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const SIP_FACTORY;

/**
* Creates a new rubber band item for the specified item \a type and destination \a view.
* Creates a new rubber band item for the specified item \a metadataId and destination \a view.
* \note not available from Python bindings
*/
QgsLayoutViewRubberBand *createItemRubberBand( const QString &uuid, QgsLayoutView *view ) const SIP_SKIP;
QgsLayoutViewRubberBand *createItemRubberBand( int metadataId, QgsLayoutView *view ) const SIP_SKIP;

/**
* Returns a list of available item metadata uuids handled by the registry.
* Returns a list of available item metadata ids handled by the registry.
*/
QList< QString > itemUuids() const;
QList< int > itemMetadataIds() const;

signals:

/**
* Emitted whenever a new item type is added to the registry, with the specified
* \a uuid.
* \a metadataId.
*/
void typeAdded( const QString &uuid );
void typeAdded( int metadataId );

private:
#ifdef SIP_RUN
QgsLayoutItemGuiRegistry( const QgsLayoutItemGuiRegistry &rh );
#endif

QMap<QString, QgsLayoutItemAbstractGuiMetadata *> mMetadata;
QMap< int, QgsLayoutItemAbstractGuiMetadata *> mMetadata;

QMap< QString, QgsLayoutItemGuiGroup > mItemGroups;

Expand Down
12 changes: 6 additions & 6 deletions src/gui/layout/qgslayoutviewtooladditem.cpp
Expand Up @@ -39,9 +39,9 @@ QgsLayoutViewToolAddItem::QgsLayoutViewToolAddItem( QgsLayoutView *view )
setCursor( QCursor( crosshairQPixmap, 8, 8 ) );
}

void QgsLayoutViewToolAddItem::setItemMetadataUuid( const QString &uuid )
void QgsLayoutViewToolAddItem::setItemMetadataId( int metadataId )
{
mItemMetadataUuid = uuid;
mItemMetadataId = metadataId;
}

void QgsLayoutViewToolAddItem::layoutPressEvent( QgsLayoutViewMouseEvent *event )
Expand All @@ -54,7 +54,7 @@ void QgsLayoutViewToolAddItem::layoutPressEvent( QgsLayoutViewMouseEvent *event

mDrawing = true;
mMousePressStartPos = event->pos();
mRubberBand.reset( QgsGui::layoutItemGuiRegistry()->createItemRubberBand( mItemMetadataUuid, view() ) );
mRubberBand.reset( QgsGui::layoutItemGuiRegistry()->createItemRubberBand( mItemMetadataId, view() ) );
if ( mRubberBand )
{
mRubberBand->start( event->snappedPoint(), event->modifiers() );
Expand Down Expand Up @@ -84,7 +84,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even

QRectF rect = mRubberBand->finish( event->snappedPoint(), event->modifiers() );

QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataUuid, layout() );
QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
if ( !item )
return;

Expand Down Expand Up @@ -135,7 +135,7 @@ void QgsLayoutViewToolAddItem::deactivate()
QgsLayoutViewTool::deactivate();
}

QString QgsLayoutViewToolAddItem::itemMetadataUuid() const
int QgsLayoutViewToolAddItem::itemMetadataId() const
{
return mItemMetadataUuid;
return mItemMetadataId;
}
16 changes: 8 additions & 8 deletions src/gui/layout/qgslayoutviewtooladditem.h
Expand Up @@ -37,20 +37,20 @@ class GUI_EXPORT QgsLayoutViewToolAddItem : public QgsLayoutViewTool
QgsLayoutViewToolAddItem( QgsLayoutView *view SIP_TRANSFERTHIS );

/**
* Returns the item metadata uuid for items created by the tool.
* \see setItemMetadataUuid()
* Returns the item metadata id for items created by the tool.
* \see setItemMetadataId()
*/
QString itemMetadataUuid() const;
int itemMetadataId() const;

/**
* Sets the item metadata \a uuid for items created by the tool.
* Sets the item metadata \a metadataId for items created by the tool.
*
* The \uuid associates the current tool behavior with a metadata entry
* The \a metadataId associates the current tool behavior with a metadata entry
* from QgsLayoutItemGuiRegistry.
*
* \see itemMetadataUuid()
* \see itemMetadataId()
*/
void setItemMetadataUuid( const QString &uuid );
void setItemMetadataId( int metadataId );

void layoutPressEvent( QgsLayoutViewMouseEvent *event ) override;
void layoutMoveEvent( QgsLayoutViewMouseEvent *event ) override;
Expand All @@ -68,7 +68,7 @@ class GUI_EXPORT QgsLayoutViewToolAddItem : public QgsLayoutViewTool

bool mDrawing = false;

QString mItemMetadataUuid;
int mItemMetadataId = -1;

//! Rubber band item
std::unique_ptr< QgsLayoutViewRubberBand > mRubberBand;
Expand Down

0 comments on commit b41fea9

Please sign in to comment.