QGIS API Documentation  2.14.0-Essen
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  QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
34  if ( idx.isValid() )
35  {
36  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
37  if ( proxyIdx.isValid() )
38  {
39  setCurrentIndex( proxyIdx.row() );
40  emit layerChanged( currentLayer() );
41  return;
42  }
43  }
44  setCurrentIndex( -1 );
45  emit layerChanged( currentLayer() );
46 }
47 
49 {
50  return layer( currentIndex() );
51 }
52 
53 QgsMapLayer *QgsMapLayerComboBox::layer( int layerIndex ) const
54 {
55  const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 );
56  if ( !proxyIndex.isValid() )
57  {
58  return nullptr;
59  }
60 
61  const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
62  if ( !index.isValid() )
63  {
64  return nullptr;
65  }
66 
67  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
68  if ( layer )
69  {
70  return layer;
71  }
72  return nullptr;
73 }
74 
76 {
77  Q_UNUSED( i );
79  emit layerChanged( layer );
80 }
81 
83 {
84  if ( count() == 1 )
85  {
86  //currently selected layer item has changed
87  emit layerChanged( currentLayer() );
88  }
89  else if ( count() == 0 )
90  {
91  emit layerChanged( nullptr );
92  }
93 }
94 
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer * currentLayer() const
Returns the current layer selected in the combo box.
QgsMapLayer * layer(int layerIndex) const
Return 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 ...
QgsMapLayerModel * sourceLayerModel() const
layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
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...
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)