QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
16 #include "qgslayoutitemcombobox.h"
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  int itemType = mProxyModel ? mProxyModel->filterType() : -1;
36  mProxyModel = qgis::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  QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem( const_cast< QgsLayoutItem * >( item ) );
58  if ( idx.isValid() )
59  {
60  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 
75 void QgsLayoutItemComboBox::indexChanged( int i )
76 {
77  Q_UNUSED( i )
78  emit itemChanged( currentItem() );
79 }
80 
81 void 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 
104 void QgsLayoutItemComboBox::setExceptedItemList( const QList<QgsLayoutItem *> &exceptList )
105 {
106  mProxyModel->setExceptedItemList( exceptList );
107 }
108 
109 QList< 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 
124 void QgsLayoutItemComboBox::setItemFlags( QgsLayoutItem::Flags flags )
125 {
126  mProxyModel->setItemFlags( flags );
127 }
128 
129 QgsLayoutItem::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  QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
143  if ( !sourceIndex.isValid() )
144  {
145  return nullptr;
146  }
147 
148  return mProxyModel->itemFromSourceIndex( sourceIndex );
149 }
QgsLayoutItemComboBox::QgsLayoutItemComboBox
QgsLayoutItemComboBox(QWidget *parent=nullptr, QgsLayout *layout=nullptr)
QgsLayoutItemComboBox creates a combo box to display a list of items in a layout.
Definition: qgslayoutitemcombobox.cpp:23
QgsLayoutItemRegistry::ItemType
ItemType
Item types.
Definition: qgslayoutitemregistry.h:307
QgsLayoutItemComboBox::currentLayout
QgsLayout * currentLayout()
Returns the current layout containing the items shown in the combo box.
Definition: qgslayoutitemcombobox.cpp:47
QgsLayoutItemComboBox::setItemFlags
void setItemFlags(QgsLayoutItem::Flags flags)
Sets layout item flags to use for filtering the available items.
Definition: qgslayoutitemcombobox.cpp:124
QgsLayoutItemComboBox::itemType
QgsLayoutItemRegistry::ItemType itemType() const
Returns the filter for the item types to show in the combo box.
Definition: qgslayoutitemcombobox.cpp:99
QgsLayoutItemComboBox::itemFlags
QgsLayoutItem::Flags itemFlags() const
Returns the layout item flags used for filtering the available items.
Definition: qgslayoutitemcombobox.cpp:129
QgsLayoutItemComboBox::setItemType
void setItemType(QgsLayoutItemRegistry::ItemType itemType)
Sets a filter for the item type to show in the combo box.
Definition: qgslayoutitemcombobox.cpp:94
QgsLayoutItemComboBox::exceptedItemList
QList< QgsLayoutItem * > exceptedItemList() const
Returns the list of specific items excluded from the combo box.
Definition: qgslayoutitemcombobox.cpp:109
QgsLayoutModel::ItemId
@ ItemId
Item ID.
Definition: qgslayoutmodel.h:63
QgsLayoutItemComboBox::item
QgsLayoutItem * item(int index) const
Returns the item currently shown at the specified index within the combo box.
Definition: qgslayoutitemcombobox.cpp:134
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsLayoutItemComboBox::currentItem
QgsLayoutItem * currentItem() const
Returns the item currently selected in the combo box.
Definition: qgslayoutitemcombobox.cpp:70
QgsLayoutItemComboBox::setCurrentLayout
void setCurrentLayout(QgsLayout *layout)
Sets the layout containing the items to list in the combo box.
Definition: qgslayoutitemcombobox.cpp:32
qgslayoutitemcombobox.h
QgsLayoutItemComboBox::setAllowEmptyItem
void setAllowEmptyItem(bool allowEmpty)
Sets whether an optional empty layout item is present in the combobox.
Definition: qgslayoutitemcombobox.cpp:114
QgsLayoutItemComboBox::setExceptedItemList
void setExceptedItemList(const QList< QgsLayoutItem * > &exceptList)
Sets a list of specific items to exclude from the combo box.
Definition: qgslayoutitemcombobox.cpp:104
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItemComboBox::itemChanged
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
QgsLayoutItemComboBox::setItem
void setItem(const QgsLayoutItem *item)
Sets the currently selected item in the combo box.
Definition: qgslayoutitemcombobox.cpp:52
qgslayoutmodel.h
QgsLayoutItemComboBox::allowEmptyItem
bool allowEmptyItem() const
Returns true if the model includes the empty item choice.
Definition: qgslayoutitemcombobox.cpp:119