QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsmaplayercombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayercombobox.cpp
3  --------------------------------------
4  Date : 01.04.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
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 "qgsmaplayercombobox.h"
17 #include "qgsmaplayermodel.h"
18 
19 
21  : QComboBox( parent )
22 {
23  mProxyModel = new QgsMapLayerProxyModel( this );
24  setModel( mProxyModel );
25 
26  connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsMapLayerComboBox::indexChanged );
27  connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsMapLayerComboBox::rowsChanged );
28  connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsMapLayerComboBox::rowsChanged );
29 }
30 
31 void QgsMapLayerComboBox::setExcludedProviders( const QStringList &providers )
32 {
33  mProxyModel->setExcludedProviders( providers );
34 }
35 
37 {
38  return mProxyModel->excludedProviders();
39 }
40 
42 {
43  mProxyModel->sourceLayerModel()->setAllowEmptyLayer( allowEmpty );
44 }
45 
47 {
48  return mProxyModel->sourceLayerModel()->allowEmptyLayer();
49 }
50 
52 {
53  mProxyModel->sourceLayerModel()->setShowCrs( showCrs );
54 }
55 
57 {
58  return mProxyModel->sourceLayerModel()->showCrs();
59 }
60 
61 void QgsMapLayerComboBox::setAdditionalItems( const QStringList &items )
62 {
63  mProxyModel->sourceLayerModel()->setAdditionalItems( items );
64 }
65 
67 {
68  return mProxyModel->sourceLayerModel()->additionalItems();
69 }
70 
72 {
73  if ( !layer )
74  {
75  setCurrentIndex( -1 );
76  return;
77  }
78 
79  QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
80  if ( idx.isValid() )
81  {
82  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
83  if ( proxyIdx.isValid() )
84  {
85  setCurrentIndex( proxyIdx.row() );
86  emit layerChanged( currentLayer() );
87  return;
88  }
89  }
90  setCurrentIndex( -1 );
91  emit layerChanged( currentLayer() );
92 }
93 
95 {
96  return layer( currentIndex() );
97 }
98 
99 QgsMapLayer *QgsMapLayerComboBox::layer( int layerIndex ) const
100 {
101  const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 );
102  if ( !proxyIndex.isValid() )
103  {
104  return nullptr;
105  }
106 
107  const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
108  if ( !index.isValid() )
109  {
110  return nullptr;
111  }
112 
113  QgsMapLayer *layer = static_cast<QgsMapLayer *>( index.internalPointer() );
114  if ( layer )
115  {
116  return layer;
117  }
118  return nullptr;
119 }
120 
122 {
123  Q_UNUSED( i );
125  emit layerChanged( layer );
126 }
127 
129 {
130  if ( count() == 1 )
131  {
132  //currently selected layer item has changed
133  emit layerChanged( currentLayer() );
134  }
135  else if ( count() == 0 )
136  {
137  emit layerChanged( nullptr );
138  }
139 }
140 
bool showCrs() const
Returns true if the combo box shows the layer&#39;s CRS.
Base class for all map layer types.
Definition: qgsmaplayer.h:63
QStringList additionalItems
QgsMapLayer * currentLayer() const
Returns the current layer selected in the combo box.
void setExcludedProviders(const QStringList &providers)
Sets a list of data providers which should be excluded from the combobox.
QgsMapLayer * layer(int layerIndex) const
Returns the layer currently shown at the specified index within the combo box.
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
void layerChanged(QgsMapLayer *layer)
layerChanged this signal is emitted whenever the currently selected layer changes ...
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the combobox.
QgsMapLayerModel * sourceLayerModel() const
layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the model&#39;s display role.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
void setAllowEmptyLayer(bool allowEmpty)
Sets whether an optional empty layer ("not set") option is present in the model.
QgsMapLayerComboBox(QWidget *parent=nullptr)
QgsMapLayerComboBox creates a combo box to display the list of layers (currently in the registry)...
void setAllowEmptyLayer(bool allowEmpty)
Sets whether an optional empty layer ("not set") option is shown in the combo box.
bool allowEmptyLayer() const
Returns true if the combo box allows the empty layer ("not set") choice.
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the combo box text.
QStringList excludedProviders() const
Returns the list of data providers which are excluded from the combobox.
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the combo box...
void setLayer(QgsMapLayer *layer)
setLayer set the current layer selected in the combo
void setExcludedProviders(const QStringList &providers)
Sets a blacklist of data providers which should be excluded from the model.
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the model.
QStringList excludedProviders() const
Returns the blacklist of data providers which are excluded from the model.