QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
30 {
31  QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
32  if ( idx.isValid() )
33  {
34  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
35  if ( proxyIdx.isValid() )
36  {
37  setCurrentIndex( proxyIdx.row() );
38  emit layerChanged( currentLayer() );
39  return;
40  }
41  }
42  setCurrentIndex( -1 );
43  emit layerChanged( currentLayer() );
44 }
45 
47 {
48  int i = currentIndex();
49 
50  const QModelIndex proxyIndex = mProxyModel->index( i, 0 );
51  if ( !proxyIndex.isValid() )
52  {
53  return 0;
54  }
55 
56  const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
57  if ( !index.isValid() )
58  {
59  return 0;
60  }
61 
62  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
63  if ( layer )
64  {
65  return layer;
66  }
67  return 0;
68 }
69 
71 {
72  Q_UNUSED( i );
73  QgsMapLayer* layer = currentLayer();
74  emit layerChanged( layer );
75 }
76 
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
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
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
void setLayer(QgsMapLayer *layer)
setLayer set the current layer selected in the combo
QgsMapLayerComboBox(QWidget *parent=0)
QgsMapLayerComboBox creates a combo box to dislpay the list of layers (currently in the registry)...