QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfieldcombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldcombobox.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 "qgsfieldcombobox.h"
17 #include "qgsfieldproxymodel.h"
18 #include "qgsmaplayer.h"
19 #include "qgsvectorlayer.h"
20 
22  QComboBox( parent )
23 {
24  mFieldProxyModel = new QgsFieldProxyModel( this );
25  setModel( mFieldProxyModel );
26 
27  connect( this, SIGNAL( activated( int ) ), this, SLOT( indexChanged( int ) ) );
28 }
29 
30 void QgsFieldComboBox::setFilters( QgsFieldProxyModel::Filters filters )
31 {
32  mFieldProxyModel->setFilters( filters );
33 }
34 
36 {
37  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
38  if ( vl )
39  {
40  setLayer( vl );
41  }
42 }
43 
45 {
46  mFieldProxyModel->sourceFieldModel()->setLayer( layer );
47 }
48 
50 {
51  return mFieldProxyModel->sourceFieldModel()->layer();
52 }
53 
54 void QgsFieldComboBox::setField( QString fieldName )
55 {
56  QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
57  if ( idx.isValid() )
58  {
59  QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
60  if ( proxyIdx.isValid() )
61  {
62  setCurrentIndex( idx.row() );
63  emit fieldChanged( currentField() );
64  return;
65  }
66  }
67  setCurrentIndex( -1 );
68 }
69 
71 {
72  int i = currentIndex();
73 
74  const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
75  if ( !proxyIndex.isValid() )
76  {
77  return "";
78  }
79 
80  QString name = mFieldProxyModel->data( proxyIndex, QgsFieldModel::FieldNameRole ).toString();
81  return name;
82 }
83 
85 {
86  Q_UNUSED( i );
87  QString name = currentField();
88  emit fieldChanged( name );
89 }