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