QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
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
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"
23#include "qgsproject.h"
24#include "qgsvectorlayer.h"
25
26#include <QTableWidgetItem>
27#include <QLineEdit>
28#include <QComboBox>
29#include <QLabel>
30#include <QFrame>
31#include <QCompleter>
32#include <QSpinBox>
33#include <QPushButton>
34#include <QHBoxLayout>
35#include <QFileDialog>
36
38 : mLayer( vl )
39{
40 setupUi( this );
41
42 connect( layerComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsAttributeTypeLoadDialog::fillComboBoxes );
43 connect( keyComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index ) { createPreview( index ); } );
44 connect( valueComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index ) { createPreview( index ); } );
45 connect( previewButton, &QAbstractButton::pressed, this, &QgsAttributeTypeLoadDialog::previewButtonPushed );
46
47 fillLayerList();
48
49 keyComboBox->setDisabled( true );
50 valueComboBox->setDisabled( true );
51}
52
54{
55 mLayer = layer;
56}
57
58void QgsAttributeTypeLoadDialog::previewButtonPushed()
59{
60 createPreview( valueComboBox->currentIndex(), true );
61}
62
63void QgsAttributeTypeLoadDialog::fillLayerList()
64{
65 layerComboBox->blockSignals( true );
66 layerComboBox->clear();
67 const auto constMapLayers = QgsProject::instance()->mapLayers();
68 for ( QgsMapLayer *l : constMapLayers )
69 {
70 QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
71 if ( vl )
72 layerComboBox->addItem( vl->name(), vl->id() );
73 }
74 layerComboBox->setCurrentIndex( -1 );
75 layerComboBox->blockSignals( false );
76}
77
78void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
79{
80 keyComboBox->blockSignals( true );
81 valueComboBox->blockSignals( true );
82
83 //clear comboboxes first
84 keyComboBox->clear();
85 valueComboBox->clear();
86
87 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layerIndex < 0 ? nullptr : QgsProject::instance()->mapLayer( layerComboBox->itemData( layerIndex ).toString() ) );
88 if ( vLayer )
89 {
90 QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
91 QMap<QString, int>::iterator it = fieldMap.begin();
92 for ( ; it != fieldMap.end(); ++it )
93 {
94 keyComboBox->addItem( it.key(), it.value() );
95 valueComboBox->addItem( it.key(), it.value() );
96 }
97 }
98
99 keyComboBox->setEnabled( nullptr != vLayer );
100 valueComboBox->setEnabled( nullptr != vLayer );
101
102 keyComboBox->setCurrentIndex( -1 );
103 valueComboBox->setCurrentIndex( -1 );
104
105 keyComboBox->blockSignals( false );
106 valueComboBox->blockSignals( false );
107}
108
109void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
110{
111 previewTableWidget->clearContents();
112
113 for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
114 {
115 previewTableWidget->removeRow( i );
116 }
117 if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
118 {
119 //when nothing is selected there is no reason for preview
120 return;
121 }
122 const int idx = keyComboBox->currentData().toInt();
123 const int idx2 = valueComboBox->currentData().toInt();
124 QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
125 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
126 if ( !vLayer )
127 return;
128
129 QgsAttributeList attributeList = QgsAttributeList();
130 attributeList.append( idx );
131 attributeList.append( idx2 );
132
133 QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( attributeList ) );
134
135 QgsFeature f;
136 QMap<QString, QVariant> valueMap;
137 while ( fit.nextFeature( f ) )
138 {
139 const QVariant val1 = f.attribute( idx );
140 const QVariant val2 = f.attribute( idx2 );
141 if ( val1.isValid() && !QgsVariantUtils::isNull( val1 ) && !val1.toString().isEmpty()
142 && val2.isValid() && !QgsVariantUtils::isNull( val2 ) && !val2.toString().isEmpty() )
143 {
144 valueMap.insert( val1.toString(), val2.toString() );
145 }
146 if ( !full && valueMap.size() > 8 )
147 break; //just first entries all on button
148 }
149 int row = 0;
150 for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
151 {
152 previewTableWidget->insertRow( row );
153 previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
154 previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
155 }
156}
157
158QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
159{
160 return mValueMap;
161}
162
164{
165 return nullCheckBox->isChecked();
166}
167
168void QgsAttributeTypeLoadDialog::loadDataToValueMap()
169{
170 mValueMap.clear();
171 const int idx = keyComboBox->currentData().toInt();
172 const int idx2 = valueComboBox->currentData().toInt();
173 QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
174 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
175 if ( !vLayer )
176 return;
177
178 QgsAttributeList attributeList = QgsAttributeList();
179 attributeList.append( idx );
180 attributeList.append( idx2 );
181
182 QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( attributeList ) );
183
184 QgsFeature f;
185 while ( fit.nextFeature( f ) )
186 {
187 const QVariant val = f.attribute( idx );
188 if ( val.isValid() && !QgsVariantUtils::isNull( val ) && !val.toString().isEmpty() )
189 {
190 mValueMap.insert( f.attribute( idx2 ).toString(), val );
191 }
192 }
193}
194
196{
197 //store data to output variable
198 loadDataToValueMap();
199 QDialog::accept();
200}
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
void accept() override
Overloaded accept method which will write the feature field values, then delegate to QDialog::accept(...
bool insertNull()
Returns true if the "Add NULL value" checkbox has been checked.
QMap< QString, QVariant > & valueMap()
Returns the value map which is currently active.
void setVectorLayer(QgsVectorLayer *layer)
Sets predefined vector layer for selection of data.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Base class for all map layer types.
Definition qgsmaplayer.h:75
QString name
Definition qgsmaplayer.h:78
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
QMap< QString, int > fieldNameMap() const
Returns a map where the key is the name of the field and the value is its index.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
QList< int > QgsAttributeList
Definition qgsfield.h:27