QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 {
56 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#edit-widgets" ) );
57 } );
58
59
60}
61
63{
64 mLayer = layer;
65}
66
67void QgsAttributeTypeLoadDialog::previewButtonPushed()
68{
69 createPreview( valueComboBox->currentIndex(), true );
70}
71
72void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
73{
74 previewTableWidget->clearContents();
75
76 for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
77 {
78 previewTableWidget->removeRow( i );
79 }
80 if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
81 {
82 //when nothing is selected there is no reason for preview
83 return;
84 }
85 const int idx = keyComboBox->currentIndex();
86 const int idx2 = valueComboBox->currentIndex();
87 QgsMapLayer *dataLayer = layerComboBox->currentLayer();
88 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
89 if ( !vLayer )
90 return;
91
92 QgsAttributeList attributeList = QgsAttributeList();
93 attributeList.append( idx );
94 attributeList.append( idx2 );
95
96 QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( attributeList ) );
97
98 QgsFeature f;
99 QMap<QString, QVariant> valueMap;
100 while ( fit.nextFeature( f ) )
101 {
102 const QVariant val1 = f.attribute( idx );
103 const QVariant val2 = f.attribute( idx2 );
104 if ( val1.isValid() && !QgsVariantUtils::isNull( val1 ) && !val1.toString().isEmpty()
105 && val2.isValid() && !QgsVariantUtils::isNull( val2 ) && !val2.toString().isEmpty() )
106 {
107 valueMap.insert( val1.toString(), val2.toString() );
108 }
109 if ( !full && valueMap.size() > 8 )
110 break; //just first entries all on button
111 }
112 int row = 0;
113 for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); ++mit, row++ )
114 {
115 previewTableWidget->insertRow( row );
116 previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
117 previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
118 }
119}
120
121QMap<QString, QVariant> &QgsAttributeTypeLoadDialog::valueMap()
122{
123 return mValueMap;
124}
125
127{
128 return nullCheckBox->isChecked();
129}
130
131void QgsAttributeTypeLoadDialog::loadDataToValueMap()
132{
133 mValueMap.clear();
134 const int idx = keyComboBox->currentIndex();
135 const int idx2 = valueComboBox->currentIndex();
136 QgsMapLayer *dataLayer = layerComboBox->currentLayer();
137 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
138 if ( !vLayer )
139 return;
140
141 QgsAttributeList attributeList = QgsAttributeList();
142 attributeList.append( idx );
143 attributeList.append( idx2 );
144
145 QgsFeatureIterator fit = vLayer->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( attributeList ) );
146
147 QgsFeature f;
148 while ( fit.nextFeature( f ) )
149 {
150 const QVariant val = f.attribute( idx );
151 if ( val.isValid() && !QgsVariantUtils::isNull( val ) && !val.toString().isEmpty() )
152 {
153 mValueMap.insert( f.attribute( idx2 ).toString(), val );
154 }
155 }
156}
157
159{
160 //store data to output variable
161 loadDataToValueMap();
162 QDialog::accept();
163}
@ 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:209
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