QGIS API Documentation  2.10.1-Pisa
 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 
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 );
88  emit fieldChanged( name );
89 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsFieldModel * sourceFieldModel()
sourceFieldModel returns the QgsFieldModel used in this QSortFilterProxyModel
QString currentField() const
return the currently selected field
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
void fieldChanged(QString fieldName)
the signal is emitted when the currently selected field changes
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
const char * name() const
bool isValid() const
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
int row() const
void activated(int index)
void setField(QString fieldName)
setField sets the currently selected field
void setModel(QAbstractItemModel *model)
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
void setCurrentIndex(int index)
QModelIndex indexFromName(const QString &fieldName)
return the index corresponding to a given fieldName
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QgsVectorLayer * layer() const
Returns the currently used layer.
Represents a vector layer which manages a vector based data sets.
QString toString() const
virtual QVariant data(const QModelIndex &index, int role) const