Skip to content

Commit

Permalink
Implement delete field action
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent dcec98d commit 4a8fce2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 2 deletions.
11 changes: 11 additions & 0 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -151,6 +151,17 @@ class QgsAuxiliaryLayer : QgsVectorLayer
:rtype: bool
%End

virtual bool deleteAttribute( int attr );
%Docstring
Remove attribute from the layer and commit changes. The layer remains
editable.

\param attr The index of the attribute to remove

:return: true if the attribute is well deleted, false otherwise
:rtype: bool
%End

};


Expand Down
10 changes: 10 additions & 0 deletions python/core/qgsproperty.sip
Expand Up @@ -104,12 +104,22 @@ class QgsPropertyDefinition
:rtype: str
%End

void setName( const QString &name );
%Docstring
Sets the name of the property
%End

Origin origin() const;
%Docstring
Returns the origin of the property
:rtype: Origin
%End

void setOrigin( Origin origin );
%Docstring
Sets origin of the property
%End

QString description() const;
%Docstring
Descriptive name of the property.
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -1302,7 +1302,7 @@ Returns a map of field name to attribute alias
A set of attributes that are not advertised in WFS requests with QGIS server.
%End

bool deleteAttribute( int attr );
virtual bool deleteAttribute( int attr );
%Docstring
Delete an attribute field (but does not commit it)
:rtype: bool
Expand Down
48 changes: 48 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -87,6 +87,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
, mAuxiliaryLayerActionClear( nullptr )
, mAuxiliaryLayerActionDelete( nullptr )
, mAuxiliaryLayerActionExport( nullptr )
, mAuxiliaryLayerActionDeleteField( nullptr )
{
setupUi( this );
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
Expand Down Expand Up @@ -378,6 +379,8 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(

mAuxiliaryStorageActions->setMenu( menu );

connect( mAuxiliaryStorageFieldsDeleteBtn, &QPushButton::clicked, this, &QgsVectorLayerProperties::onAuxiliaryLayerDeleteField );

updateAuxiliaryStoragePage();
}

Expand Down Expand Up @@ -1631,3 +1634,48 @@ void QgsVectorLayerProperties::onAuxiliaryLayerExport()

QgisApp::instance()->saveAsFile( clone.get() );
}

void QgsVectorLayerProperties::onAuxiliaryLayerDeleteField()
{
QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
if ( !alayer )
return;

QList<QTreeWidgetItem *> items = mAuxiliaryStorageFieldsTree->selectedItems();
if ( items.count() < 1 )
return;

// get auxiliary field name and index from item
const QTreeWidgetItem *item = items[0];
QgsPropertyDefinition def;

if ( item->text( 0 ).compare( "pal", Qt::CaseInsensitive ) == 0 )
def.setOrigin( QgsPropertyDefinition::Pal );
else
def.setOrigin( QgsPropertyDefinition::Diagram );

def.setName( item->text( 1 ) );

const QString fieldName = QgsAuxiliaryField::name( def );

const int index = mLayer->auxiliaryLayer()->fields().indexOf( fieldName );
if ( index < 0 )
return;

// should be only 1 field
const QString msg = tr( "Are you sure you want to delete auxiliary field %1 for %2" ).arg( item->text( 1 ), item->text( 0 ) );

QMessageBox::StandardButton reply;
reply = QMessageBox::question( this, "Delete auxiliary field", msg, QMessageBox::Yes | QMessageBox::No );

if ( reply == QMessageBox::Yes )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
mLayer->auxiliaryLayer()->deleteAttribute( index );
QApplication::restoreOverrideCursor();
mLayer->updateFields();
updateAuxiliaryStoragePage( true );
mFieldsPropertiesDialog->init();
mLayer->triggerRepaint();
}
}
3 changes: 3 additions & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -162,6 +162,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private

void onAuxiliaryLayerDelete();

void onAuxiliaryLayerDeleteField();

void onAuxiliaryLayerExport();

private:
Expand Down Expand Up @@ -231,6 +233,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
QAction *mAuxiliaryLayerActionClear;
QAction *mAuxiliaryLayerActionDelete;
QAction *mAuxiliaryLayerActionExport;
QAction *mAuxiliaryLayerActionDeleteField;

private slots:
void openPanel( QgsPanelWidget *panel );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -233,6 +233,14 @@ QgsAuxiliaryFields QgsAuxiliaryLayer::auxiliaryFields() const
return afields;
}

bool QgsAuxiliaryLayer::deleteAttribute( int attr )
{
QgsVectorLayer::deleteAttribute( attr );
bool rc = commitChanges();
startEditing();
return rc;
}

bool QgsAuxiliaryLayer::save()
{
bool rc = false;
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -174,6 +174,16 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
*/
bool save();

/**
* Remove attribute from the layer and commit changes. The layer remains
* editable.
*
* \param attr The index of the attribute to remove
*
* \returns true if the attribute is well deleted, false otherwise
*/
virtual bool deleteAttribute( int attr ) override;

private:
QgsVectorLayerJoinInfo mJoinInfo;
const QgsVectorLayer *mLayer;
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsproperty.h
Expand Up @@ -141,11 +141,21 @@ class CORE_EXPORT QgsPropertyDefinition
*/
QString name() const { return mName; }

/**
* Sets the name of the property
*/
void setName( const QString &name ) { mName = name; }

/**
* Returns the origin of the property
*/
Origin origin() const { return mOrigin; }

/**
* Sets origin of the property
*/
void setOrigin( Origin origin ) { mOrigin = origin; }

/**
* Descriptive name of the property.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -1302,7 +1302,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
void setExcludeAttributesWfs( const QSet<QString> &att ) { mExcludeAttributesWFS = att; }

//! Delete an attribute field (but does not commit it)
bool deleteAttribute( int attr );
virtual bool deleteAttribute( int attr );

/**
* Deletes a list of attribute fields (but does not commit it)
Expand Down

0 comments on commit 4a8fce2

Please sign in to comment.