QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 : cSf.Kostej@gmail.com
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  keyComboBox->setDisabled( true );
51  valueComboBox->setDisabled( true );
52 }
53 
55 {
56 
57 }
58 
60 {
61  mLayer = layer;
62 }
63 
64 void QgsAttributeTypeLoadDialog::previewButtonPushed()
65 {
66  createPreview( valueComboBox->currentIndex(), true );
67 }
68 
69 void QgsAttributeTypeLoadDialog::fillLayerList()
70 {
71  layerComboBox->blockSignals( true );
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  layerComboBox->setCurrentIndex( -1 );
80  layerComboBox->blockSignals( false );
81 }
82 
83 void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
84 {
85  keyComboBox->blockSignals( true );
86  valueComboBox->blockSignals( true );
87 
88  //clear comboboxes first
89  keyComboBox->clear();
90  valueComboBox->clear();
91 
92  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layerIndex < 0 ? 0 : QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerIndex ).toString() ) );
93  if ( vLayer )
94  {
95  QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
96  QMap<QString, int>::iterator it = fieldMap.begin();
97  for ( ; it != fieldMap.end(); ++it )
98  {
99  keyComboBox->addItem( it.key(), it.value() );
100  valueComboBox->addItem( it.key(), it.value() );
101  }
102  }
103 
104  keyComboBox->setEnabled( vLayer != 0 );
105  valueComboBox->setEnabled( vLayer != 0 );
106 
107  keyComboBox->setCurrentIndex( -1 );
108  valueComboBox->setCurrentIndex( -1 );
109 
110  keyComboBox->blockSignals( false );
111  valueComboBox->blockSignals( false );
112 }
113 
114 void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
115 {
116  previewTableWidget->clearContents();
117 
118  for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
119  {
120  previewTableWidget->removeRow( i );
121  }
122  if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
123  {
124  //when nothing is selected there is no reason for preview
125  return;
126  }
127  int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
128  int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
129  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
130  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
131  if ( !vLayer )
132  return;
133 
134  QgsAttributeList attributeList = QgsAttributeList();
135  attributeList.append( idx );
136  attributeList.append( idx2 );
137 
138  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
139 
140  QgsFeature f;
141  QMap<QString, QVariant> valueMap;
142  while ( fit.nextFeature( f ) )
143  {
144  QVariant val1 = f.attribute( idx );
145  QVariant val2 = f.attribute( idx2 );
146  if ( val1.isValid() && !val1.isNull() && !val1.toString().isEmpty()
147  && val2.isValid() && !val2.isNull() && !val2.toString().isEmpty() )
148  {
149  valueMap.insert( val1.toString(), val2.toString() );
150  }
151  if ( !full && valueMap.size() > 8 )
152  break; //just first entries all on button
153  }
154  int row = 0;
155  for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
156  {
157  previewTableWidget->insertRow( row );
158  previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
159  previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
160  }
161 }
162 
163 QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
164 {
165  return mValueMap;
166 }
167 
169 {
170  return nullCheckBox->isChecked();
171 }
172 
173 void QgsAttributeTypeLoadDialog::loadDataToValueMap()
174 {
175  mValueMap.clear();
176  int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
177  int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
178  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
179  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
180  if ( !vLayer )
181  return;
182 
183  QgsAttributeList attributeList = QgsAttributeList();
184  attributeList.append( idx );
185  attributeList.append( idx2 );
186 
187  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
188 
189  QgsFeature f;
190  while ( fit.nextFeature( f ) )
191  {
192  QVariant val = f.attribute( idx );
193  if ( val.isValid() && !val.isNull() && !val.toString().isEmpty() )
194  {
195  mValueMap.insert( f.attribute( idx2 ).toString(), val );
196  }
197  }
198 }
199 
201 {
202  //store data to output variable
203  loadDataToValueMap();
204  QDialog::accept();
205 }
Wrapper for iterator of features from vector data provider or vector layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:48
void setVectorLayer(QgsVectorLayer *layer)
Sets predefined vector layer for selection of data.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
static QgsMapLayerRegistry * instance()
Definition: qgssingleton.h:23
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.
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:95
QMap< QString, int > fieldNameMap() const
Return a map where the key is the name of the field and the value is its index.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:230
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
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.