QGIS API Documentation  2.99.0-Master (08ee180)
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 "qgsfeatureiterator.h"
22 #include "qgsvectordataprovider.h"
23 #include "qgslogger.h"
24 #include "qgsmaplayerregistry.h"
25 #include "qgsvectorlayer.h"
26 
27 #include <QTableWidgetItem>
28 #include <QLineEdit>
29 #include <QComboBox>
30 #include <QLabel>
31 #include <QFrame>
32 #include <QScrollArea>
33 #include <QCompleter>
34 #include <QSpinBox>
35 #include <QPushButton>
36 #include <QHBoxLayout>
37 #include <QFileDialog>
38 
40  : QDialog()
41  , mLayer( vl )
42 {
43  setupUi( this );
44 
45  connect( layerComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( fillComboBoxes( int ) ) );
46  connect( keyComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( createPreview( int ) ) );
47  connect( valueComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( createPreview( int ) ) );
48  connect( previewButton, SIGNAL( pressed() ), this, SLOT( previewButtonPushed() ) );
49 
50  fillLayerList();
51 
52  keyComboBox->setDisabled( true );
53  valueComboBox->setDisabled( true );
54 }
55 
57 {
58 
59 }
60 
62 {
63  mLayer = layer;
64 }
65 
66 void QgsAttributeTypeLoadDialog::previewButtonPushed()
67 {
68  createPreview( valueComboBox->currentIndex(), true );
69 }
70 
71 void QgsAttributeTypeLoadDialog::fillLayerList()
72 {
73  layerComboBox->blockSignals( true );
74  layerComboBox->clear();
75  Q_FOREACH ( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
76  {
77  QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
78  if ( vl )
79  layerComboBox->addItem( vl->name(), vl->id() );
80  }
81  layerComboBox->setCurrentIndex( -1 );
82  layerComboBox->blockSignals( false );
83 }
84 
85 void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
86 {
87  keyComboBox->blockSignals( true );
88  valueComboBox->blockSignals( true );
89 
90  //clear comboboxes first
91  keyComboBox->clear();
92  valueComboBox->clear();
93 
94  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layerIndex < 0 ? nullptr : QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerIndex ).toString() ) );
95  if ( vLayer )
96  {
97  QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
98  QMap<QString, int>::iterator it = fieldMap.begin();
99  for ( ; it != fieldMap.end(); ++it )
100  {
101  keyComboBox->addItem( it.key(), it.value() );
102  valueComboBox->addItem( it.key(), it.value() );
103  }
104  }
105 
106  keyComboBox->setEnabled( nullptr != vLayer );
107  valueComboBox->setEnabled( nullptr != vLayer );
108 
109  keyComboBox->setCurrentIndex( -1 );
110  valueComboBox->setCurrentIndex( -1 );
111 
112  keyComboBox->blockSignals( false );
113  valueComboBox->blockSignals( false );
114 }
115 
116 void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
117 {
118  previewTableWidget->clearContents();
119 
120  for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
121  {
122  previewTableWidget->removeRow( i );
123  }
124  if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
125  {
126  //when nothing is selected there is no reason for preview
127  return;
128  }
129  int idx = keyComboBox->currentData().toInt();
130  int idx2 = valueComboBox->currentData().toInt();
131  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentData().toString() );
132  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
133  if ( !vLayer )
134  return;
135 
136  QgsAttributeList attributeList = QgsAttributeList();
137  attributeList.append( idx );
138  attributeList.append( idx2 );
139 
140  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
141 
142  QgsFeature f;
143  QMap<QString, QVariant> valueMap;
144  while ( fit.nextFeature( f ) )
145  {
146  QVariant val1 = f.attribute( idx );
147  QVariant val2 = f.attribute( idx2 );
148  if ( val1.isValid() && !val1.isNull() && !val1.toString().isEmpty()
149  && val2.isValid() && !val2.isNull() && !val2.toString().isEmpty() )
150  {
151  valueMap.insert( val1.toString(), val2.toString() );
152  }
153  if ( !full && valueMap.size() > 8 )
154  break; //just first entries all on button
155  }
156  int row = 0;
157  for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
158  {
159  previewTableWidget->insertRow( row );
160  previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
161  previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
162  }
163 }
164 
165 QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
166 {
167  return mValueMap;
168 }
169 
171 {
172  return nullCheckBox->isChecked();
173 }
174 
175 void QgsAttributeTypeLoadDialog::loadDataToValueMap()
176 {
177  mValueMap.clear();
178  int idx = keyComboBox->currentData().toInt();
179  int idx2 = valueComboBox->currentData().toInt();
180  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentData().toString() );
181  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
182  if ( !vLayer )
183  return;
184 
185  QgsAttributeList attributeList = QgsAttributeList();
186  attributeList.append( idx );
187  attributeList.append( idx2 );
188 
189  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
190 
191  QgsFeature f;
192  while ( fit.nextFeature( f ) )
193  {
194  QVariant val = f.attribute( idx );
195  if ( val.isValid() && !val.isNull() && !val.toString().isEmpty() )
196  {
197  mValueMap.insert( f.attribute( idx2 ).toString(), val );
198  }
199  }
200 }
201 
203 {
204  //store data to output variable
205  loadDataToValueMap();
206  QDialog::accept();
207 }
Wrapper for iterator of features from vector data provider or vector layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
void setVectorLayer(QgsVectorLayer *layer)
Sets predefined vector layer for selection of data.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
void accept() override
Overloaded accept method which will write the feature field values, then delegate to QDialog::accept(...
QMap< QString, QVariant > & valueMap()
Getter to value map which is currently active.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const
Query the layer for features specified in request.
QgsAttributeTypeLoadDialog(QgsVectorLayer *vl)
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
Returns the layer&#39;s unique ID, which is used to access this layer from QgsMapLayerRegistry.
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:277
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
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.