QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsattributetypeloaddialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsattributetypeloaddialog.cpp
3  -------------------
4  begin : June 2009
5  copyright : (C) 2000 by Richard Kostecky
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 
20 #include "qgsmaplayer.h"
21 #include "qgsvectordataprovider.h"
22 #include "qgslogger.h"
23 #include "qgsmaplayerregistry.h"
24 
25 #include <QTableWidgetItem>
26 #include <QLineEdit>
27 #include <QComboBox>
28 #include <QLabel>
29 #include <QFrame>
30 #include <QScrollArea>
31 #include <QCompleter>
32 #include <QSpinBox>
33 #include <QPushButton>
34 #include <QHBoxLayout>
35 #include <QFileDialog>
36 
38  : QDialog(),
39  mLayer( vl )
40 {
41  setupUi( this );
42 
43  connect( layerComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( fillComboBoxes( int ) ) );
44  connect( keyComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( createPreview( int ) ) );
45  connect( valueComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( createPreview( int ) ) );
46  connect( previewButton, SIGNAL( pressed( ) ), this, SLOT( previewButtonPushed( ) ) );
47 
48  fillLayerList();
49 }
50 
51 
53 {
54 
55 }
56 
57 
59 {
60  mLayer = layer;
61 }
62 
63 
64 
66 {
67  createPreview( valueComboBox->currentIndex(), true );
68 }
69 
71 {
72  layerComboBox->clear();
73  foreach ( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
74  {
75  QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
76  if ( vl )
77  layerComboBox->addItem( vl->name(), vl->id() );
78  }
79 }
80 
82 {
83  //clear comboboxes first
84  keyComboBox->clear();
85  valueComboBox->clear();
86 
87  if ( layerIndex < 0 )
88  {
89  return;
90  }
91 
92  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
93  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
94  if ( vLayer == NULL )
95  {
96  return;
97  }
98  QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
99  QMap<QString, int>::iterator it = fieldMap.begin();
100  for ( ; it != fieldMap.end(); ++it )
101  {
102  keyComboBox->addItem( it.key(), it.value() );
103  valueComboBox->addItem( it.key(), it.value() );
104  }
105 
106 }
107 
108 void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
109 {
110  previewTableWidget->clearContents();
111 
112  for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
113  {
114  previewTableWidget->removeRow( i );
115  }
116  if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
117  {
118  //when nothing is selected there is no reason for preview
119  return;
120  }
121  int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
122  int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
123  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
124  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
125  if ( vLayer == NULL )
126  {
127  return;
128  }
129 
130  QgsAttributeList attributeList = QgsAttributeList();
131  attributeList.append( idx );
132  attributeList.append( idx2 );
133 
134  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
135 
136  QgsFeature f;
137  QMap<QString, QVariant> valueMap;
138  while ( fit.nextFeature( f ) )
139  {
140  QVariant val1 = f.attribute( idx );
141  QVariant val2 = f.attribute( idx2 );
142  if ( val1.isValid() && !val1.isNull() && !val1.toString().isEmpty()
143  && val2.isValid() && !val2.isNull() && !val2.toString().isEmpty() )
144  {
145  valueMap.insert( val1.toString(), val2.toString() );
146  }
147  if ( !full && valueMap.size() > 8 )
148  break; //just first entries all on button
149  }
150  int row = 0;
151  for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
152  {
153  previewTableWidget->insertRow( row );
154  previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
155  previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
156  }
157 
158 }
159 
160 QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
161 {
162  return mValueMap;
163 }
164 
166 {
167  return nullCheckBox->isChecked();
168 }
169 
171 {
172  mValueMap.clear();
173  int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
174  int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
175  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
176  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
177  if ( vLayer == NULL )
178  {
179  return;
180  }
181 
182  QgsAttributeList attributeList = QgsAttributeList();
183  attributeList.append( idx );
184  attributeList.append( idx2 );
185 
186  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
187 
188  QgsFeature f;
189  while ( fit.nextFeature( f ) )
190  {
191  QVariant val = f.attribute( idx );
192  if ( val.isValid() && !val.isNull() && !val.toString().isEmpty() )
193  {
194  mValueMap.insert( f.attribute( idx2 ).toString(), val );
195  }
196  }
197 }
198 
199 
200 
202 {
203  //store data to output variable
205  QDialog::accept();
206 }
207 
Wrapper for iterator of features from vector data provider or vector layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:47
void fillComboBoxes(int layerIndex)
Slot which reacts to change of selected layer to fill other two comboboxes with correct data...
void setVectorLayer(QgsVectorLayer *layer)
Sets predefined vector layer for selection of data.
QMap< QString, QVariant > mValueMap
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
QMap< QString, QVariant > & valueMap()
Getter to value map which is currently active.
const QString & name() const
Get the display name of the layer.
void fillLayerList()
Internal function to fill the list of layers.
QgsAttributeTypeLoadDialog(QgsVectorLayer *vl)
void accept()
Overloaded accept method which will write the feature field values, then delegate to QDialog::accept(...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool insertNull()
Returns true if the "Add NULL value" checkbox has been checked.
QList< int > QgsAttributeList
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:92
QMap< QString, int > fieldNameMap() const
Return a map where the key is the name of the field and the value is its index.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:230
void loadDataToValueMap()
Function to transfer data from layer to value map used in editing.
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
void previewButtonPushed()
Slot to react to value Preview button pushed.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
void createPreview(int fieldIndex, bool full=false)
Slot to react to button push or change of selected column for display of preview. ...
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.