QGIS API Documentation 3.29.0-Master (ade4f0cf0f)
qgslayoutitemcombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemcombobox.cpp
3 --------------------------------------
4 Date : October 2017
5 Copyright : (C) 201\7 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
17#include "qgslayoutmodel.h"
18
19//
20// QgsLayoutItemComboBox
21//
22
24 : QComboBox( parent )
25{
26 setCurrentLayout( layout );
27
28 setModelColumn( QgsLayoutModel::ItemId );
29 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutItemComboBox::indexChanged );
30}
31
33{
34 const bool prevAllowEmpty = mProxyModel && mProxyModel->allowEmptyItem();
35 const int itemType = mProxyModel ? mProxyModel->filterType() : -1;
36 mProxyModel = std::make_unique< QgsLayoutProxyModel >( layout, this );
37 connect( mProxyModel.get(), &QAbstractItemModel::rowsInserted, this, &QgsLayoutItemComboBox::rowsChanged );
38 connect( mProxyModel.get(), &QAbstractItemModel::rowsRemoved, this, &QgsLayoutItemComboBox::rowsChanged );
39 setModel( mProxyModel.get() );
40 setModelColumn( QgsLayoutModel::ItemId );
41 mProxyModel->sort( QgsLayoutModel::ItemId, Qt::AscendingOrder );
42 mProxyModel->setAllowEmptyItem( prevAllowEmpty );
43 if ( itemType >= 0 )
44 mProxyModel->setFilterType( static_cast< QgsLayoutItemRegistry::ItemType >( itemType ) );
45}
46
48{
49 return mProxyModel->layout();
50}
51
53{
54 if ( !mProxyModel->sourceLayerModel() )
55 return;
56
57 const QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem( const_cast< QgsLayoutItem * >( item ) );
58 if ( idx.isValid() )
59 {
60 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
61 if ( proxyIdx.isValid() )
62 {
63 setCurrentIndex( proxyIdx.row() );
64 return;
65 }
66 }
67 setCurrentIndex( mProxyModel->allowEmptyItem() ? 0 : -1 );
68}
69
71{
72 return item( currentIndex() );
73}
74
75void QgsLayoutItemComboBox::indexChanged( int i )
76{
77 Q_UNUSED( i )
78 emit itemChanged( currentItem() );
79}
80
81void QgsLayoutItemComboBox::rowsChanged()
82{
83 if ( count() == 1 )
84 {
85 //currently selected item has changed
86 emit itemChanged( currentItem() );
87 }
88 else if ( count() == 0 )
89 {
90 emit itemChanged( nullptr );
91 }
92}
93
95{
96 mProxyModel->setFilterType( itemType );
97}
98
100{
101 return mProxyModel->filterType();
102}
103
104void QgsLayoutItemComboBox::setExceptedItemList( const QList<QgsLayoutItem *> &exceptList )
105{
106 mProxyModel->setExceptedItemList( exceptList );
107}
108
109QList< QgsLayoutItem *> QgsLayoutItemComboBox::exceptedItemList() const
110{
111 return mProxyModel->exceptedItemList();
112}
113
115{
116 mProxyModel->setAllowEmptyItem( allowEmpty );
117}
118
120{
121 return mProxyModel->allowEmptyItem();
122}
123
124void QgsLayoutItemComboBox::setItemFlags( QgsLayoutItem::Flags flags )
125{
126 mProxyModel->setItemFlags( flags );
127}
128
129QgsLayoutItem::Flags QgsLayoutItemComboBox::itemFlags() const
130{
131 return mProxyModel->itemFlags();
132}
133
135{
136 const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
137 if ( !proxyIndex.isValid() )
138 {
139 return nullptr;
140 }
141
142 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
143 if ( !sourceIndex.isValid() )
144 {
145 return nullptr;
146 }
147
148 return mProxyModel->itemFromSourceIndex( sourceIndex );
149}
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.
@ ItemId
Item ID.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51