QGIS API Documentation  2.99.0-Master (b8fd1fd)
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 "qgsproject.h"
25 #include "qgsvectorlayer.h"
26 
27 #include <QTableWidgetItem>
28 #include <QLineEdit>
29 #include <QComboBox>
30 #include <QLabel>
31 #include <QFrame>
32 #include <QCompleter>
33 #include <QSpinBox>
34 #include <QPushButton>
35 #include <QHBoxLayout>
36 #include <QFileDialog>
37 
39  : QDialog()
40  , mLayer( vl )
41 {
42  setupUi( this );
43 
44  connect( layerComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( fillComboBoxes( int ) ) );
45  connect( keyComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( createPreview( int ) ) );
46  connect( valueComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( createPreview( int ) ) );
47  connect( previewButton, SIGNAL( pressed() ), this, SLOT( previewButtonPushed() ) );
48 
49  fillLayerList();
50 
51  keyComboBox->setDisabled( true );
52  valueComboBox->setDisabled( true );
53 }
54 
56 {
57  mLayer = layer;
58 }
59 
60 void QgsAttributeTypeLoadDialog::previewButtonPushed()
61 {
62  createPreview( valueComboBox->currentIndex(), true );
63 }
64 
65 void QgsAttributeTypeLoadDialog::fillLayerList()
66 {
67  layerComboBox->blockSignals( true );
68  layerComboBox->clear();
69  Q_FOREACH ( QgsMapLayer *l, QgsProject::instance()->mapLayers() )
70  {
71  QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
72  if ( vl )
73  layerComboBox->addItem( vl->name(), vl->id() );
74  }
75  layerComboBox->setCurrentIndex( -1 );
76  layerComboBox->blockSignals( false );
77 }
78 
79 void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
80 {
81  keyComboBox->blockSignals( true );
82  valueComboBox->blockSignals( true );
83 
84  //clear comboboxes first
85  keyComboBox->clear();
86  valueComboBox->clear();
87 
88  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layerIndex < 0 ? nullptr : QgsProject::instance()->mapLayer( layerComboBox->itemData( layerIndex ).toString() ) );
89  if ( vLayer )
90  {
91  QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
92  QMap<QString, int>::iterator it = fieldMap.begin();
93  for ( ; it != fieldMap.end(); ++it )
94  {
95  keyComboBox->addItem( it.key(), it.value() );
96  valueComboBox->addItem( it.key(), it.value() );
97  }
98  }
99 
100  keyComboBox->setEnabled( nullptr != vLayer );
101  valueComboBox->setEnabled( nullptr != vLayer );
102 
103  keyComboBox->setCurrentIndex( -1 );
104  valueComboBox->setCurrentIndex( -1 );
105 
106  keyComboBox->blockSignals( false );
107  valueComboBox->blockSignals( false );
108 }
109 
110 void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
111 {
112  previewTableWidget->clearContents();
113 
114  for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
115  {
116  previewTableWidget->removeRow( i );
117  }
118  if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
119  {
120  //when nothing is selected there is no reason for preview
121  return;
122  }
123  int idx = keyComboBox->currentData().toInt();
124  int idx2 = valueComboBox->currentData().toInt();
125  QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
126  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
127  if ( !vLayer )
128  return;
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 QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
160 {
161  return mValueMap;
162 }
163 
165 {
166  return nullCheckBox->isChecked();
167 }
168 
169 void QgsAttributeTypeLoadDialog::loadDataToValueMap()
170 {
171  mValueMap.clear();
172  int idx = keyComboBox->currentData().toInt();
173  int idx2 = valueComboBox->currentData().toInt();
174  QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
175  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
176  if ( !vLayer )
177  return;
178 
179  QgsAttributeList attributeList = QgsAttributeList();
180  attributeList.append( idx );
181  attributeList.append( idx2 );
182 
183  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
184 
185  QgsFeature f;
186  while ( fit.nextFeature( f ) )
187  {
188  QVariant val = f.attribute( idx );
189  if ( val.isValid() && !val.isNull() && !val.toString().isEmpty() )
190  {
191  mValueMap.insert( f.attribute( idx2 ).toString(), val );
192  }
193  }
194 }
195 
197 {
198  //store data to output variable
199  loadDataToValueMap();
200  QDialog::accept();
201 }
Wrapper for iterator of features from vector data provider or vector layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:52
QMap< QString, int > fieldNameMap() const
Return a map where the key is the name of the field and the value is its index.
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:136
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.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
QgsAttributeTypeLoadDialog(QgsVectorLayer *vl)
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const
Query the layer for features specified in request.
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
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:359
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:56
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.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:267