QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "moc_qgslayoutitemcombobox.cpp"
18#include "qgslayoutmodel.h"
19
20//
21// QgsLayoutItemComboBox
22//
23
25 : QComboBox( parent )
26{
27 setCurrentLayout( layout );
28
29 setModelColumn( QgsLayoutModel::ItemId );
30 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutItemComboBox::indexChanged );
31}
32
34{
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() );
41 setModelColumn( QgsLayoutModel::ItemId );
42 mProxyModel->sort( QgsLayoutModel::ItemId, Qt::AscendingOrder );
43 mProxyModel->setAllowEmptyItem( prevAllowEmpty );
44 if ( itemType >= 0 )
45 mProxyModel->setFilterType( static_cast< QgsLayoutItemRegistry::ItemType >( itemType ) );
46}
47
49{
50 return mProxyModel->layout();
51}
52
54{
55 if ( !mProxyModel->sourceLayerModel() )
56 return;
57
58 const QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem( const_cast< QgsLayoutItem * >( item ) );
59 if ( idx.isValid() )
60 {
61 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
62 if ( proxyIdx.isValid() )
63 {
64 setCurrentIndex( proxyIdx.row() );
65 return;
66 }
67 }
68 setCurrentIndex( mProxyModel->allowEmptyItem() ? 0 : -1 );
69}
70
72{
73 return item( currentIndex() );
74}
75
76void QgsLayoutItemComboBox::indexChanged( int i )
77{
78 Q_UNUSED( i )
79 emit itemChanged( currentItem() );
80}
81
82void QgsLayoutItemComboBox::rowsChanged()
83{
84 if ( count() == 1 )
85 {
86 //currently selected item has changed
87 emit itemChanged( currentItem() );
88 }
89 else if ( count() == 0 )
90 {
91 emit itemChanged( nullptr );
92 }
93}
94
96{
97 mProxyModel->setFilterType( itemType );
98}
99
101{
102 return mProxyModel->filterType();
103}
104
105void QgsLayoutItemComboBox::setExceptedItemList( const QList<QgsLayoutItem *> &exceptList )
106{
107 mProxyModel->setExceptedItemList( exceptList );
108}
109
110QList< QgsLayoutItem *> QgsLayoutItemComboBox::exceptedItemList() const
111{
112 return mProxyModel->exceptedItemList();
113}
114
116{
117 mProxyModel->setAllowEmptyItem( allowEmpty );
118}
119
121{
122 return mProxyModel->allowEmptyItem();
123}
124
126{
127 mProxyModel->setItemFlags( flags );
128}
129
131{
132 return mProxyModel->itemFlags();
133}
134
136{
137 const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
138 if ( !proxyIndex.isValid() )
139 {
140 return nullptr;
141 }
142
143 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
144 if ( !sourceIndex.isValid() )
145 {
146 return nullptr;
147 }
148
149 return mProxyModel->itemFromSourceIndex( sourceIndex );
150}
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.
QFlags< Flag > Flags
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49