QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
125 {
126  const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
127  if ( !proxyIndex.isValid() )
128  {
129  return nullptr;
130  }
131 
132  QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
133  if ( !sourceIndex.isValid() )
134  {
135  return nullptr;
136  }
137 
138  return mProxyModel->itemFromSourceIndex( sourceIndex );
139 }
QgsLayoutItemComboBox(QWidget *parent=nullptr, QgsLayout *layout=nullptr)
QgsLayoutItemComboBox creates a combo box to display a list of items in a layout. ...
Base class for graphical items within a QgsLayout.
void setItemType(QgsLayoutItemRegistry::ItemType itemType)
Sets a filter for the item type to show in the combo box.
QgsLayoutItemRegistry::ItemType itemType() const
Returns the filter for the item types to show in the combo box.
void setItem(const QgsLayoutItem *item)
Sets the currently selected item in the combo box.
QgsLayoutItem * item(int index) const
Returns the item currently shown at the specified index within the combo box.
QgsLayout * currentLayout()
Returns the current layout containing the items shown in the combo box.
QList< QgsLayoutItem *> exceptedItemList() const
Returns the list of specific items excluded from the combo box.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
void setExceptedItemList(const QList< QgsLayoutItem * > &exceptList)
Sets a list of specific items to exclude from the combo box.
bool allowEmptyItem() const
Returns true if the model includes the empty item choice.
void setCurrentLayout(QgsLayout *layout)
Sets the layout containing the items to list in the combo box.
void setAllowEmptyItem(bool allowEmpty)
Sets whether an optional empty layout item is present in the combobox.
QgsLayoutItem * currentItem() const
Returns the item currently selected in the combo box.