QGIS API Documentation  2.99.0-Master (0a63d1f)
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 <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  mLayer = layer;
59 }
60 
61 void QgsAttributeTypeLoadDialog::previewButtonPushed()
62 {
63  createPreview( valueComboBox->currentIndex(), true );
64 }
65 
66 void QgsAttributeTypeLoadDialog::fillLayerList()
67 {
68  layerComboBox->blockSignals( true );
69  layerComboBox->clear();
70  Q_FOREACH ( QgsMapLayer *l, QgsProject::instance()->mapLayers() )
71  {
72  QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
73  if ( vl )
74  layerComboBox->addItem( vl->name(), vl->id() );
75  }
76  layerComboBox->setCurrentIndex( -1 );
77  layerComboBox->blockSignals( false );
78 }
79 
80 void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
81 {
82  keyComboBox->blockSignals( true );
83  valueComboBox->blockSignals( true );
84 
85  //clear comboboxes first
86  keyComboBox->clear();
87  valueComboBox->clear();
88 
89  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layerIndex < 0 ? nullptr : QgsProject::instance()->mapLayer( layerComboBox->itemData( layerIndex ).toString() ) );
90  if ( vLayer )
91  {
92  QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
93  QMap<QString, int>::iterator it = fieldMap.begin();
94  for ( ; it != fieldMap.end(); ++it )
95  {
96  keyComboBox->addItem( it.key(), it.value() );
97  valueComboBox->addItem( it.key(), it.value() );
98  }
99  }
100 
101  keyComboBox->setEnabled( nullptr != vLayer );
102  valueComboBox->setEnabled( nullptr != vLayer );
103 
104  keyComboBox->setCurrentIndex( -1 );
105  valueComboBox->setCurrentIndex( -1 );
106 
107  keyComboBox->blockSignals( false );
108  valueComboBox->blockSignals( false );
109 }
110 
111 void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
112 {
113  previewTableWidget->clearContents();
114 
115  for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
116  {
117  previewTableWidget->removeRow( i );
118  }
119  if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
120  {
121  //when nothing is selected there is no reason for preview
122  return;
123  }
124  int idx = keyComboBox->currentData().toInt();
125  int idx2 = valueComboBox->currentData().toInt();
126  QgsMapLayer* dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
127  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
128  if ( !vLayer )
129  return;
130 
131  QgsAttributeList attributeList = QgsAttributeList();
132  attributeList.append( idx );
133  attributeList.append( idx2 );
134 
135  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
136 
137  QgsFeature f;
138  QMap<QString, QVariant> valueMap;
139  while ( fit.nextFeature( f ) )
140  {
141  QVariant val1 = f.attribute( idx );
142  QVariant val2 = f.attribute( idx2 );
143  if ( val1.isValid() && !val1.isNull() && !val1.toString().isEmpty()
144  && val2.isValid() && !val2.isNull() && !val2.toString().isEmpty() )
145  {
146  valueMap.insert( val1.toString(), val2.toString() );
147  }
148  if ( !full && valueMap.size() > 8 )
149  break; //just first entries all on button
150  }
151  int row = 0;
152  for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
153  {
154  previewTableWidget->insertRow( row );
155  previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
156  previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
157  }
158 }
159 
160 QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
161 {
162  return mValueMap;
163 }
164 
166 {
167  return nullCheckBox->isChecked();
168 }
169 
170 void QgsAttributeTypeLoadDialog::loadDataToValueMap()
171 {
172  mValueMap.clear();
173  int idx = keyComboBox->currentData().toInt();
174  int idx2 = valueComboBox->currentData().toInt();
175  QgsMapLayer* dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
176  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
177  if ( !vLayer )
178  return;
179 
180  QgsAttributeList attributeList = QgsAttributeList();
181  attributeList.append( idx );
182  attributeList.append( idx2 );
183 
184  QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributeList ) );
185 
186  QgsFeature f;
187  while ( fit.nextFeature( f ) )
188  {
189  QVariant val = f.attribute( idx );
190  if ( val.isValid() && !val.isNull() && !val.toString().isEmpty() )
191  {
192  mValueMap.insert( f.attribute( idx2 ).toString(), val );
193  }
194  }
195 }
196 
198 {
199  //store data to output variable
200  loadDataToValueMap();
201  QDialog::accept();
202 }
Wrapper for iterator of features from vector data provider or vector layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:50
QMap< QString, int > fieldNameMap() const
Return a map where the key is the name of the field and the value is its index.
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: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:350
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:54
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:262