QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgslayoutmodel.h"
19
20#include "moc_qgslayoutitemcombobox.cpp"
21
22//
23// QgsLayoutItemComboBox
24//
25
27 : QComboBox( parent )
28{
29 setCurrentLayout( layout );
30
31 setModelColumn( QgsLayoutModel::ItemId );
32 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutItemComboBox::indexChanged );
33}
34
36{
37 const bool prevAllowEmpty = mProxyModel && mProxyModel->allowEmptyItem();
38 const int itemType = mProxyModel ? mProxyModel->filterType() : -1;
39 mProxyModel = std::make_unique<QgsLayoutProxyModel>( layout, this );
40 connect( mProxyModel.get(), &QAbstractItemModel::rowsInserted, this, &QgsLayoutItemComboBox::rowsChanged );
41 connect( mProxyModel.get(), &QAbstractItemModel::rowsRemoved, this, &QgsLayoutItemComboBox::rowsChanged );
42 setModel( mProxyModel.get() );
43 setModelColumn( QgsLayoutModel::ItemId );
44 mProxyModel->sort( QgsLayoutModel::ItemId, Qt::AscendingOrder );
45 mProxyModel->setAllowEmptyItem( prevAllowEmpty );
46 if ( itemType >= 0 )
47 mProxyModel->setFilterType( static_cast<QgsLayoutItemRegistry::ItemType>( itemType ) );
48}
49
51{
52 return mProxyModel->layout();
53}
54
56{
57 if ( !mProxyModel->sourceLayerModel() )
58 return;
59
60 const QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem( const_cast<QgsLayoutItem *>( item ) );
61 if ( idx.isValid() )
62 {
63 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
64 if ( proxyIdx.isValid() )
65 {
66 setCurrentIndex( proxyIdx.row() );
67 return;
68 }
69 }
70 setCurrentIndex( mProxyModel->allowEmptyItem() ? 0 : -1 );
71}
72
74{
75 return item( currentIndex() );
76}
77
78void QgsLayoutItemComboBox::indexChanged( int i )
79{
80 Q_UNUSED( i )
81 emit itemChanged( currentItem() );
82}
83
84void QgsLayoutItemComboBox::rowsChanged()
85{
86 if ( count() == 1 )
87 {
88 //currently selected item has changed
89 emit itemChanged( currentItem() );
90 }
91 else if ( count() == 0 )
92 {
93 emit itemChanged( nullptr );
94 }
95}
96
101
103{
104 return mProxyModel->filterType();
105}
106
107void QgsLayoutItemComboBox::setExceptedItemList( const QList<QgsLayoutItem *> &exceptList )
108{
109 mProxyModel->setExceptedItemList( exceptList );
110}
111
112QList<QgsLayoutItem *> QgsLayoutItemComboBox::exceptedItemList() const
113{
114 return mProxyModel->exceptedItemList();
115}
116
118{
119 mProxyModel->setAllowEmptyItem( allowEmpty );
120}
121
123{
124 return mProxyModel->allowEmptyItem();
125}
126
128{
129 mProxyModel->setItemFlags( flags );
130}
131
133{
134 return mProxyModel->itemFlags();
135}
136
138{
139 const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
140 if ( !proxyIndex.isValid() )
141 {
142 return nullptr;
143 }
144
145 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
146 if ( !sourceIndex.isValid() )
147 {
148 return nullptr;
149 }
150
151 return mProxyModel->itemFromSourceIndex( sourceIndex );
152}
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:50