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