17#include "moc_qgslayoutitemcombobox.cpp"
30 connect(
this,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsLayoutItemComboBox::indexChanged );
35 const bool prevAllowEmpty = mProxyModel && mProxyModel->allowEmptyItem();
36 const int itemType = mProxyModel ? mProxyModel->filterType() : -1;
37 mProxyModel = std::make_unique<QgsLayoutProxyModel>( layout,
this );
38 connect( mProxyModel.get(), &QAbstractItemModel::rowsInserted,
this, &QgsLayoutItemComboBox::rowsChanged );
39 connect( mProxyModel.get(), &QAbstractItemModel::rowsRemoved,
this, &QgsLayoutItemComboBox::rowsChanged );
40 setModel( mProxyModel.get() );
43 mProxyModel->setAllowEmptyItem( prevAllowEmpty );
50 return mProxyModel->layout();
55 if ( !mProxyModel->sourceLayerModel() )
58 const QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem(
const_cast<QgsLayoutItem *
>(
item ) );
61 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
62 if ( proxyIdx.isValid() )
64 setCurrentIndex( proxyIdx.row() );
68 setCurrentIndex( mProxyModel->allowEmptyItem() ? 0 : -1 );
73 return item( currentIndex() );
76void QgsLayoutItemComboBox::indexChanged(
int i )
82void QgsLayoutItemComboBox::rowsChanged()
89 else if ( count() == 0 )
97 mProxyModel->setFilterType(
itemType );
102 return mProxyModel->filterType();
107 mProxyModel->setExceptedItemList( exceptList );
112 return mProxyModel->exceptedItemList();
117 mProxyModel->setAllowEmptyItem( allowEmpty );
122 return mProxyModel->allowEmptyItem();
127 mProxyModel->setItemFlags( flags );
132 return mProxyModel->itemFlags();
137 const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
138 if ( !proxyIndex.isValid() )
143 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
144 if ( !sourceIndex.isValid() )
149 return mProxyModel->itemFromSourceIndex( sourceIndex );
QgsLayoutItem * item(int index) const
Returns the item currently shown at the specified index within the combo box.
void setCurrentLayout(QgsLayout *layout)
Sets the layout containing the items to list in the combo box.
QgsLayout * currentLayout()
Returns the current layout containing the items shown in the combo box.
void setExceptedItemList(const QList< QgsLayoutItem * > &exceptList)
Sets a list of specific items to exclude from the combo box.
QgsLayoutItem * currentItem() const
Returns the item currently selected in the combo box.
QgsLayoutItemRegistry::ItemType itemType() const
Returns the filter for the item types to show in the combo box.
void setItemType(QgsLayoutItemRegistry::ItemType itemType)
Sets a filter for the item type to show in the combo box.
QgsLayoutItemComboBox(QWidget *parent=nullptr, QgsLayout *layout=nullptr)
QgsLayoutItemComboBox creates a combo box to display a list of items in a layout.
void setAllowEmptyItem(bool allowEmpty)
Sets whether an optional empty layout item is present in the combobox.
void setItem(const QgsLayoutItem *item)
Sets the currently selected item in the combo box.
QgsLayoutItem::Flags itemFlags() const
Returns the layout item flags used for filtering the available items.
bool allowEmptyItem() const
Returns true if the model includes the empty item choice.
void setItemFlags(QgsLayoutItem::Flags flags)
Sets layout item flags to use for filtering the available items.
QList< QgsLayoutItem * > exceptedItemList() const
Returns the list of specific items excluded from the combo box.
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
Base class for graphical items within a QgsLayout.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.