QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsaddattrdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsaddattrdialog.h - description
3 -------------------
4 begin : January 2005
5 copyright : (C) 2005 by Marco Hugentobler
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
18#include "qgsaddattrdialog.h"
19
20#include "qgsfields.h"
21#include "qgslogger.h"
23#include "qgsvectorlayer.h"
24
25#include <QMessageBox>
26#include <QString>
27
28#include "moc_qgsaddattrdialog.cpp"
29
30using namespace Qt::StringLiterals;
31
32QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt::WindowFlags fl )
33 : QDialog( parent, fl )
34 , mIsShapeFile( vlayer && vlayer->providerType() == "ogr"_L1 && vlayer->storageType() == "ESRI Shapefile"_L1 )
35{
36 setupUi( this );
37 connect( mTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
38 connect( mLength, &QSpinBox::editingFinished, this, &QgsAddAttrDialog::mLength_editingFinished );
39
40 if ( !vlayer || !vlayer->dataProvider() )
41 return;
42
44
45 //fill data types into the combo box
46 const QList<QgsVectorDataProvider::NativeType> &typelist = vlayer->dataProvider()->nativeTypes();
47 for ( int i = 0; i < typelist.size(); i++ )
48 {
49 QgsDebugMsgLevel( u"name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7"_s.arg( typelist[i].mTypeDesc ).arg( typelist[i].mType ).arg( typelist[i].mTypeName ).arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen ).arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ), 2 );
50
51 whileBlocking( mTypeBox )->addItem( QgsFields::iconForFieldType( typelist[i].mType, typelist[i].mSubType, typelist[i].mTypeName ), typelist[i].mTypeDesc );
52 mTypeBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
53 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
54 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
55 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
56 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
57 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
58 }
59
60 //default values for field width and precision
61 mLength->setValue( 10 );
62 mLength->setClearValue( 10 );
63 mPrec->setValue( 3 );
64 mPrec->setClearValue( 3 );
65 mTypeBox_currentIndexChanged( 0 );
66
67 if ( mIsShapeFile )
68 mNameEdit->setMaxLength( 10 );
69
70 mNameEdit->setFocus();
71
72 if ( !( attributeEditCapabilities & Qgis::VectorDataProviderAttributeEditCapability::EditAlias ) )
73 {
74 mLabelAlias->hide();
75 mAliasEdit->hide();
76 }
77 if ( !( attributeEditCapabilities & Qgis::VectorDataProviderAttributeEditCapability::EditComment ) )
78 {
79 mLabelComment->hide();
80 mCommentEdit->hide();
81 }
82}
83
84void QgsAddAttrDialog::setIllegalFieldNames( const QSet<QString> &names )
85{
86 mIllegalFieldNames = names;
87}
88
89void QgsAddAttrDialog::mTypeBox_currentIndexChanged( int idx )
90{
91 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
92
93 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
94 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
95 mLength->setVisible( mLength->minimum() < mLength->maximum() );
96 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
97 if ( mLength->value() < mLength->minimum() )
98 mLength->setValue( mLength->minimum() );
99 if ( mLength->value() > mLength->maximum() )
100 mLength->setValue( mLength->maximum() );
101 setPrecisionMinMax();
102}
103
104void QgsAddAttrDialog::mLength_editingFinished()
105{
106 setPrecisionMinMax();
107}
108
109void QgsAddAttrDialog::setPrecisionMinMax()
110{
111 const int idx = mTypeBox->currentIndex();
112 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
113 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
114 const bool precisionIsEnabled = minPrecType < maxPrecType;
115 mPrec->setEnabled( precisionIsEnabled );
116 mPrec->setVisible( precisionIsEnabled );
117 mPrecLabel->setVisible( precisionIsEnabled );
118
119 // Do not set min/max if it's disabled or we'll loose the default value,
120 // see https://github.com/qgis/QGIS/issues/26880 - QGIS saves integer field when
121 // I create a new real field through field calculator (Update field works as intended)
122 if ( precisionIsEnabled )
123 {
124 mPrec->setMinimum( minPrecType );
125 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
126 }
127}
128
129void QgsAddAttrDialog::accept()
130{
131 const QString newName = mNameEdit->text().trimmed();
132 if ( mIsShapeFile && newName.compare( "shape"_L1, Qt::CaseInsensitive ) == 0 )
133 {
134 QMessageBox::warning( this, tr( "Add Field" ), tr( "Invalid field name. This field name is reserved and cannot be used." ) );
135 return;
136 }
137
138
139 for ( const QString &illegalName : std::as_const( mIllegalFieldNames ) )
140 {
141 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
142 {
143 QMessageBox::warning( this, tr( "Add Field" ), tr( "%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
144 return;
145 }
146 }
147
148 if ( mNameEdit->text().isEmpty() )
149 {
150 QMessageBox::warning( this, tr( "Add Field" ), tr( "No name specified. Please specify a name to create a new field." ) );
151 return;
152 }
153
154 QDialog::accept();
155}
156
158{
159 QgsDebugMsgLevel( u"idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7"_s.arg( mTypeBox->currentIndex() ).arg( mNameEdit->text() ).arg( mTypeBox->currentData( Qt::UserRole ).toInt() ).arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() ).arg( mLength->value() ).arg( mPrec->value() ).arg( mCommentEdit->text() ), 2 );
160
161 QgsField res = QgsField(
162 mNameEdit->text(),
163 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
164 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
165 mLength->value(),
166 mPrec->isEnabled() ? mPrec->value() : 0,
167 mCommentEdit->text(),
168 static_cast<QMetaType::Type>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QMetaType::Type::QVariantMap ? QMetaType::Type::QString : QMetaType::Type::UnknownType
169 );
170
171 if ( !mAliasEdit->text().isEmpty() )
172 res.setAlias( mAliasEdit->text() );
173
174 return res;
175}
@ EditComment
Allows editing comments.
Definition qgis.h:611
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:621
QgsField field() const
Returns a field for the configured attribute.
void setIllegalFieldNames(const QSet< QString > &names)
Sets a list of field names which are considered illegal and should not be accepted by the dialog.
QgsAddAttrDialog(QgsVectorLayer *vlayer, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
constructor
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:302
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
QList< QgsVectorDataProvider::NativeType > nativeTypes() const
Returns the names of the supported types.
virtual Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const
Returns the provider's supported attribute editing capabilities.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6828
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63