QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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, SIGNAL( activated( int ) ), this, SLOT( indexChanged( int ) ) );
27  connect( mProxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
28  connect( mProxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
29 }
30 
32 {
33  if ( !layer )
34  {
35  setCurrentIndex( -1 );
36  return;
37  }
38 
39  QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
40  if ( idx.isValid() )
41  {
42  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
43  if ( proxyIdx.isValid() )
44  {
45  setCurrentIndex( proxyIdx.row() );
46  emit layerChanged( currentLayer() );
47  return;
48  }
49  }
50  setCurrentIndex( -1 );
51  emit layerChanged( currentLayer() );
52 }
53 
55 {
56  return layer( currentIndex() );
57 }
58 
59 QgsMapLayer *QgsMapLayerComboBox::layer( int layerIndex ) const
60 {
61  const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 );
62  if ( !proxyIndex.isValid() )
63  {
64  return nullptr;
65  }
66 
67  const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
68  if ( !index.isValid() )
69  {
70  return nullptr;
71  }
72 
73  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
74  if ( layer )
75  {
76  return layer;
77  }
78  return nullptr;
79 }
80 
82 {
83  Q_UNUSED( i );
85  emit layerChanged( layer );
86 }
87 
89 {
90  if ( count() == 1 )
91  {
92  //currently selected layer item has changed
93  emit layerChanged( currentLayer() );
94  }
95  else if ( count() == 0 )
96  {
97  emit layerChanged( nullptr );
98  }
99 }
100 
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void layerChanged(QgsMapLayer *layer)
layerChanged this signal is emitted whenever the currently selected layer changes ...
bool isValid() const
int count() const
int row() const
void activated(int index)
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
QgsMapLayerModel * sourceLayerModel() const
layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
void * internalPointer() const
QgsMapLayerComboBox(QWidget *parent=nullptr)
QgsMapLayerComboBox creates a combo box to dislpay the list of layers (currently in the registry)...
void setLayer(QgsMapLayer *layer)
setLayer set the current layer selected in the combo
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
void setModel(QAbstractItemModel *model)
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
void setCurrentIndex(int index)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QgsMapLayer * layer(int layerIndex) const
Return the layer currently shown at the specified index within the combo box.
QgsMapLayer * currentLayer() const
Returns the current layer selected in the combo box.