QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 : denis.rouzaud@gmail.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 "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 }
28 
29 void QgsMapLayerComboBox::setFilters( QgsMapLayerProxyModel::Filters filters )
30 {
31  mProxyModel->setFilters( filters );
32 }
33 
35 {
36  QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
37  if ( idx.isValid() )
38  {
39  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
40  if ( proxyIdx.isValid() )
41  {
42  setCurrentIndex( proxyIdx.row() );
43  emit layerChanged( currentLayer() );
44  return;
45  }
46  }
47  setCurrentIndex( -1 );
48  emit layerChanged( currentLayer() );
49 }
50 
52 {
53  int i = currentIndex();
54 
55  const QModelIndex proxyIndex = mProxyModel->index( i, 0 );
56  if ( !proxyIndex.isValid() )
57  {
58  return 0;
59  }
60 
61  const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
62  if ( !index.isValid() )
63  {
64  return 0;
65  }
66 
67  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
68  if ( layer )
69  {
70  return layer;
71  }
72  return 0;
73 }
74 
76 {
77  Q_UNUSED( i );
78  QgsMapLayer* layer = currentLayer();
79  emit layerChanged( layer );
80 }
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:48
QgsMapLayer * currentLayer() const
currentLayer returns the current layer selected in 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
void setFilters(QgsMapLayerProxyModel::Filters filters)
setFilters allows fitering according to layer type and/or geometry type.
The QgsMapLayerProxModel class provides an easy to use model to display the list of layers in widgets...
QgsMapLayerProxyModel * mProxyModel
void setLayer(QgsMapLayer *layer)
setLayer set the current layer selected in the combo
QgsMapLayerProxyModel * setFilters(Filters filters)
setFilters set flags that affect how layers are filtered
QgsMapLayerComboBox(QWidget *parent=0)
QgsMapLayerComboBox creates a combo box to dislpay the list of layers (currently in the registry)...