QGIS API Documentation  2.5.0-Master
 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 : 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 "qgsfieldcombobox.h"
17 #include "qgsfieldproxymodel.h"
18 #include "qgsmaplayer.h"
19 #include "qgsvectorlayer.h"
20 
22  QComboBox( parent )
23 {
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 {
47 }
48 
50 {
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 }
Base class for all map layer types.
Definition: qgsmaplayer.h:48
QgsFieldModel * sourceFieldModel()
sourceFieldModel returns the QgsFieldModel used in this QSortFilterProxyModel
void indexChanged(int i)
QgsFieldComboBox(QWidget *parent=0)
QgsFieldComboBox creates a combo box to display the fields of a layer.
void setLayer(QgsVectorLayer *layer)
set the layer of which the fields are listed
QString currentField()
return the currently selected field
void fieldChanged(QString fieldName)
the signal is emitted when the currently selected field changes
QgsFieldProxyModel * mFieldProxyModel
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows fitering according to the type of field
QgsVectorLayer * layer()
returns the currently used layer
Definition: qgsfieldmodel.h:68
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer...
QgsFieldProxyModel * setFilters(Filters filters)
setFilters set flags that affect how fields are filtered
void setLayer(QgsVectorLayer *layer)
set the layer of whch fields are displayed
QgsVectorLayer * layer()
Returns the currently used layer.
void setField(QString fieldName)
setField sets the currently selected field
QModelIndex indexFromName(const QString &fieldName)
return the index corresponding to a given fieldName
Represents a vector layer which manages a vector based data sets.