QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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#include "moc_qgsattributetypeloaddialog.cpp"
20
21#include "qgsmaplayer.h"
22#include "qgsfeatureiterator.h"
24#include "qgsproject.h"
25#include "qgsvectorlayer.h"
26#include "qgsgui.h"
27#include "qgshelp.h"
28
29#include <QTableWidgetItem>
30#include <QLineEdit>
31#include <QComboBox>
32#include <QLabel>
33#include <QFrame>
34#include <QCompleter>
35#include <QSpinBox>
36#include <QPushButton>
37#include <QHBoxLayout>
38#include <QFileDialog>
39
41 : mLayer( vl )
42{
43 setupUi( this );
45
46 layerComboBox->setFilters( Qgis::LayerFilter::VectorLayer );
47 layerComboBox->setCurrentIndex( -1 );
48
49 connect( layerComboBox, &QgsMapLayerComboBox::layerChanged, keyComboBox, &QgsFieldComboBox::setLayer );
50 connect( layerComboBox, &QgsMapLayerComboBox::layerChanged, valueComboBox, &QgsFieldComboBox::setLayer );
51 connect( keyComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [=]( int index ) { createPreview( index ); } );
52 connect( valueComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [=]( int index ) { createPreview( index ); } );
53 connect( previewButton, &QAbstractButton::pressed, this, &QgsAttributeTypeLoadDialog::previewButtonPushed );
54 connect( buttonBox, &QDialogButtonBox::helpRequested, this, [=] {
55 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#edit-widgets" ) );
56 } );
57}
58
60{
61 mLayer = layer;
62}
63
64void QgsAttributeTypeLoadDialog::previewButtonPushed()
65{
66 createPreview( valueComboBox->currentIndex(), true );
67}
68
69void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
70{
71 previewTableWidget->clearContents();
72
73 for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
74 {
75 previewTableWidget->removeRow( i );
76 }
77 if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
78 {
79 //when nothing is selected there is no reason for preview
80 return;
81 }
82 const int idx = keyComboBox->currentIndex();
83 const int idx2 = valueComboBox->currentIndex();
84 QgsMapLayer *dataLayer = layerComboBox->currentLayer();
85 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
86 if ( !vLayer )
87 return;
88
89 QgsAttributeList attributeList = QgsAttributeList();
90 attributeList.append( idx );
91 attributeList.append( idx2 );
92
93 QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( attributeList ) );
94
95 QgsFeature f;
96 QMap<QString, QVariant> valueMap;
97 while ( fit.nextFeature( f ) )
98 {
99 const QVariant val1 = f.attribute( idx );
100 const QVariant val2 = f.attribute( idx2 );
101 if ( val1.isValid() && !QgsVariantUtils::isNull( val1 ) && !val1.toString().isEmpty()
102 && val2.isValid() && !QgsVariantUtils::isNull( val2 ) && !val2.toString().isEmpty() )
103 {
104 valueMap.insert( val1.toString(), val2.toString() );
105 }
106 if ( !full && valueMap.size() > 8 )
107 break; //just first entries all on button
108 }
109 int row = 0;
110 for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
111 {
112 previewTableWidget->insertRow( row );
113 previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
114 previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
115 }
116}
117
118QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
119{
120 return mValueMap;
121}
122
124{
125 return nullCheckBox->isChecked();
126}
127
128void QgsAttributeTypeLoadDialog::loadDataToValueMap()
129{
130 mValueMap.clear();
131 const int idx = keyComboBox->currentIndex();
132 const int idx2 = valueComboBox->currentIndex();
133 QgsMapLayer *dataLayer = layerComboBox->currentLayer();
134 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
135 if ( !vLayer )
136 return;
137
138 QgsAttributeList attributeList = QgsAttributeList();
139 attributeList.append( idx );
140 attributeList.append( idx2 );
141
142 QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( attributeList ) );
143
144 QgsFeature f;
145 while ( fit.nextFeature( f ) )
146 {
147 const QVariant val = f.attribute( idx );
148 if ( val.isValid() && !QgsVariantUtils::isNull( val ) && !val.toString().isEmpty() )
149 {
150 mValueMap.insert( f.attribute( idx2 ).toString(), val );
151 }
152 }
153}
154
156{
157 //store data to output variable
158 loadDataToValueMap();
159 QDialog::accept();
160}
@ 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:58
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
Base class for all map layer types.
Definition qgsmaplayer.h:76
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
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.
QList< int > QgsAttributeList
Definition qgsfield.h:27