Skip to content

Commit

Permalink
Catch another place where illegal field names could be used
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 30, 2022
1 parent 772033d commit eab3edb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -1300,13 +1300,15 @@ void QgsFieldsItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *me
{
QAction *addColumnAction = new QAction( tr( "Add New Field…" ), menu );
QPointer<QgsDataItem>itemPtr { item };
const QSet< QString > illegalFieldNames = conn->illegalFieldNames();

connect( addColumnAction, &QAction::triggered, fieldsItem, [ md, fieldsItem, context, itemPtr, menu ]
connect( addColumnAction, &QAction::triggered, fieldsItem, [ md, fieldsItem, context, itemPtr, menu, illegalFieldNames ]
{
std::unique_ptr<QgsVectorLayer> layer { fieldsItem->layer() };
if ( layer )
{
QgsAddAttrDialog dialog( layer.get(), menu );
dialog.setIllegalFieldNames( illegalFieldNames );
if ( dialog.exec() == QDialog::Accepted )
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
Expand Down
20 changes: 19 additions & 1 deletion src/gui/qgsaddattrdialog.cpp
Expand Up @@ -68,6 +68,11 @@ QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt:
mNameEdit->setFocus();
}

void QgsAddAttrDialog::setIllegalFieldNames( const QSet<QString> &names )
{
mIllegalFieldNames = names;
}

void QgsAddAttrDialog::mTypeBox_currentIndexChanged( int idx )
{
mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
Expand Down Expand Up @@ -110,12 +115,25 @@ void QgsAddAttrDialog::setPrecisionMinMax()

void QgsAddAttrDialog::accept()
{
if ( mIsShapeFile && mNameEdit->text().compare( QLatin1String( "shape" ), Qt::CaseInsensitive ) == 0 )
const QString newName = mNameEdit->text().trimmed();
if ( mIsShapeFile && newName.compare( QLatin1String( "shape" ), Qt::CaseInsensitive ) == 0 )
{
QMessageBox::warning( this, tr( "Add Field" ),
tr( "Invalid field name. This field name is reserved and cannot be used." ) );
return;
}


for ( const QString &illegalName : std::as_const( mIllegalFieldNames ) )
{
if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
{
QMessageBox::warning( this, tr( "Add Field" ),
tr( "%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
return;
}
}

if ( mNameEdit->text().isEmpty() )
{
QMessageBox::warning( this, tr( "Add Field" ),
Expand Down
14 changes: 12 additions & 2 deletions src/gui/qgsaddattrdialog.h
Expand Up @@ -23,10 +23,11 @@

#include "ui_qgsaddattrdialogbase.h"
#include "qgsguiutils.h"
#include "qgsfields.h"
#include "qgis_gui.h"
#include <QSet>

class QgsVectorLayer;
class QgsField;

/**
* \ingroup gui
Expand All @@ -46,6 +47,14 @@ class GUI_EXPORT QgsAddAttrDialog: public QDialog, private Ui::QgsAddAttrDialogB
QgsAddAttrDialog( const std::list<QString> &typelist,
QWidget *parent = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );

/**
* Sets a list of field \a names which are considered illegal and
* should not be accepted by the dialog.
*
* \since QGIS 3.30
*/
void setIllegalFieldNames( const QSet< QString> &names );

//! Returns a field for the configured attribute
QgsField field() const;

Expand All @@ -55,7 +64,8 @@ class GUI_EXPORT QgsAddAttrDialog: public QDialog, private Ui::QgsAddAttrDialogB
void accept() override;

private:
bool mIsShapeFile;
bool mIsShapeFile = false;
QSet< QString > mIllegalFieldNames;

void setPrecisionMinMax();
};
Expand Down

0 comments on commit eab3edb

Please sign in to comment.