QGIS API Documentation  3.6.0-Noosa (5873452)
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 
20  : QComboBox( parent )
21 {
22  setCurrentLayout( layout );
23 
24  setModelColumn( QgsLayoutModel::ItemId );
25  connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutItemComboBox::indexChanged );
26 }
27 
29 {
30  mProxyModel = qgis::make_unique< QgsLayoutProxyModel >( layout, this );
31  connect( mProxyModel.get(), &QAbstractItemModel::rowsInserted, this, &QgsLayoutItemComboBox::rowsChanged );
32  connect( mProxyModel.get(), &QAbstractItemModel::rowsRemoved, this, &QgsLayoutItemComboBox::rowsChanged );
33  setModel( mProxyModel.get() );
34  setModelColumn( QgsLayoutModel::ItemId );
35  mProxyModel->sort( 0, Qt::AscendingOrder );
36 }
37 
39 {
40  if ( !mProxyModel->sourceLayerModel() )
41  return;
42 
43  QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem( const_cast< QgsLayoutItem * >( item ) );
44  if ( idx.isValid() )
45  {
46  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
47  if ( proxyIdx.isValid() )
48  {
49  setCurrentIndex( proxyIdx.row() );
50  return;
51  }
52  }
53  setCurrentIndex( -1 );
54 }
55 
57 {
58  return item( currentIndex() );
59 }
60 
61 void QgsLayoutItemComboBox::indexChanged( int i )
62 {
63  Q_UNUSED( i );
64  emit itemChanged( currentItem() );
65 }
66 
67 void QgsLayoutItemComboBox::rowsChanged()
68 {
69  if ( count() == 1 )
70  {
71  //currently selected item has changed
72  emit itemChanged( currentItem() );
73  }
74  else if ( count() == 0 )
75  {
76  emit itemChanged( nullptr );
77  }
78 }
79 
81 {
82  mProxyModel->setFilterType( itemType );
83 }
84 
86 {
87  return mProxyModel->filterType();
88 }
89 
90 void QgsLayoutItemComboBox::setExceptedItemList( const QList<QgsLayoutItem *> &exceptList )
91 {
92  mProxyModel->setExceptedItemList( exceptList );
93 }
94 
95 QList< QgsLayoutItem *> QgsLayoutItemComboBox::exceptedItemList() const
96 {
97  return mProxyModel->exceptedItemList();
98 }
99 
101 {
102  const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
103  if ( !proxyIndex.isValid() )
104  {
105  return nullptr;
106  }
107 
108  QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
109  if ( !sourceIndex.isValid() )
110  {
111  return nullptr;
112  }
113 
114  return mProxyModel->itemFromSourceIndex( sourceIndex );
115 }
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.
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.
void setCurrentLayout(QgsLayout *layout)
Sets the layout containing the items to list in the combo box.
QgsLayoutItem * currentItem() const
Returns the item currently selected in the combo box.