Skip to content

Commit 4a8fce2

Browse files
committedOct 9, 2017
Implement delete field action
1 parent dcec98d commit 4a8fce2

9 files changed

+102
-2
lines changed
 

‎python/core/qgsauxiliarystorage.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ class QgsAuxiliaryLayer : QgsVectorLayer
151151
:rtype: bool
152152
%End
153153

154+
virtual bool deleteAttribute( int attr );
155+
%Docstring
156+
Remove attribute from the layer and commit changes. The layer remains
157+
editable.
158+
159+
\param attr The index of the attribute to remove
160+
161+
:return: true if the attribute is well deleted, false otherwise
162+
:rtype: bool
163+
%End
164+
154165
};
155166

156167

‎python/core/qgsproperty.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,22 @@ class QgsPropertyDefinition
104104
:rtype: str
105105
%End
106106

107+
void setName( const QString &name );
108+
%Docstring
109+
Sets the name of the property
110+
%End
111+
107112
Origin origin() const;
108113
%Docstring
109114
Returns the origin of the property
110115
:rtype: Origin
111116
%End
112117

118+
void setOrigin( Origin origin );
119+
%Docstring
120+
Sets origin of the property
121+
%End
122+
113123
QString description() const;
114124
%Docstring
115125
Descriptive name of the property.

‎python/core/qgsvectorlayer.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ Returns a map of field name to attribute alias
13021302
A set of attributes that are not advertised in WFS requests with QGIS server.
13031303
%End
13041304

1305-
bool deleteAttribute( int attr );
1305+
virtual bool deleteAttribute( int attr );
13061306
%Docstring
13071307
Delete an attribute field (but does not commit it)
13081308
:rtype: bool

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
8787
, mAuxiliaryLayerActionClear( nullptr )
8888
, mAuxiliaryLayerActionDelete( nullptr )
8989
, mAuxiliaryLayerActionExport( nullptr )
90+
, mAuxiliaryLayerActionDeleteField( nullptr )
9091
{
9192
setupUi( this );
9293
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
@@ -378,6 +379,8 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
378379

379380
mAuxiliaryStorageActions->setMenu( menu );
380381

382+
connect( mAuxiliaryStorageFieldsDeleteBtn, &QPushButton::clicked, this, &QgsVectorLayerProperties::onAuxiliaryLayerDeleteField );
383+
381384
updateAuxiliaryStoragePage();
382385
}
383386

@@ -1631,3 +1634,48 @@ void QgsVectorLayerProperties::onAuxiliaryLayerExport()
16311634

16321635
QgisApp::instance()->saveAsFile( clone.get() );
16331636
}
1637+
1638+
void QgsVectorLayerProperties::onAuxiliaryLayerDeleteField()
1639+
{
1640+
QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
1641+
if ( !alayer )
1642+
return;
1643+
1644+
QList<QTreeWidgetItem *> items = mAuxiliaryStorageFieldsTree->selectedItems();
1645+
if ( items.count() < 1 )
1646+
return;
1647+
1648+
// get auxiliary field name and index from item
1649+
const QTreeWidgetItem *item = items[0];
1650+
QgsPropertyDefinition def;
1651+
1652+
if ( item->text( 0 ).compare( "pal", Qt::CaseInsensitive ) == 0 )
1653+
def.setOrigin( QgsPropertyDefinition::Pal );
1654+
else
1655+
def.setOrigin( QgsPropertyDefinition::Diagram );
1656+
1657+
def.setName( item->text( 1 ) );
1658+
1659+
const QString fieldName = QgsAuxiliaryField::name( def );
1660+
1661+
const int index = mLayer->auxiliaryLayer()->fields().indexOf( fieldName );
1662+
if ( index < 0 )
1663+
return;
1664+
1665+
// should be only 1 field
1666+
const QString msg = tr( "Are you sure you want to delete auxiliary field %1 for %2" ).arg( item->text( 1 ), item->text( 0 ) );
1667+
1668+
QMessageBox::StandardButton reply;
1669+
reply = QMessageBox::question( this, "Delete auxiliary field", msg, QMessageBox::Yes | QMessageBox::No );
1670+
1671+
if ( reply == QMessageBox::Yes )
1672+
{
1673+
QApplication::setOverrideCursor( Qt::WaitCursor );
1674+
mLayer->auxiliaryLayer()->deleteAttribute( index );
1675+
QApplication::restoreOverrideCursor();
1676+
mLayer->updateFields();
1677+
updateAuxiliaryStoragePage( true );
1678+
mFieldsPropertiesDialog->init();
1679+
mLayer->triggerRepaint();
1680+
}
1681+
}

‎src/app/qgsvectorlayerproperties.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
162162

163163
void onAuxiliaryLayerDelete();
164164

165+
void onAuxiliaryLayerDeleteField();
166+
165167
void onAuxiliaryLayerExport();
166168

167169
private:
@@ -231,6 +233,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
231233
QAction *mAuxiliaryLayerActionClear;
232234
QAction *mAuxiliaryLayerActionDelete;
233235
QAction *mAuxiliaryLayerActionExport;
236+
QAction *mAuxiliaryLayerActionDeleteField;
234237

235238
private slots:
236239
void openPanel( QgsPanelWidget *panel );

‎src/core/qgsauxiliarystorage.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ QgsAuxiliaryFields QgsAuxiliaryLayer::auxiliaryFields() const
233233
return afields;
234234
}
235235

236+
bool QgsAuxiliaryLayer::deleteAttribute( int attr )
237+
{
238+
QgsVectorLayer::deleteAttribute( attr );
239+
bool rc = commitChanges();
240+
startEditing();
241+
return rc;
242+
}
243+
236244
bool QgsAuxiliaryLayer::save()
237245
{
238246
bool rc = false;

‎src/core/qgsauxiliarystorage.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
174174
*/
175175
bool save();
176176

177+
/**
178+
* Remove attribute from the layer and commit changes. The layer remains
179+
* editable.
180+
*
181+
* \param attr The index of the attribute to remove
182+
*
183+
* \returns true if the attribute is well deleted, false otherwise
184+
*/
185+
virtual bool deleteAttribute( int attr ) override;
186+
177187
private:
178188
QgsVectorLayerJoinInfo mJoinInfo;
179189
const QgsVectorLayer *mLayer;

‎src/core/qgsproperty.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,21 @@ class CORE_EXPORT QgsPropertyDefinition
141141
*/
142142
QString name() const { return mName; }
143143

144+
/**
145+
* Sets the name of the property
146+
*/
147+
void setName( const QString &name ) { mName = name; }
148+
144149
/**
145150
* Returns the origin of the property
146151
*/
147152
Origin origin() const { return mOrigin; }
148153

154+
/**
155+
* Sets origin of the property
156+
*/
157+
void setOrigin( Origin origin ) { mOrigin = origin; }
158+
149159
/**
150160
* Descriptive name of the property.
151161
*/

‎src/core/qgsvectorlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
13021302
void setExcludeAttributesWfs( const QSet<QString> &att ) { mExcludeAttributesWFS = att; }
13031303

13041304
//! Delete an attribute field (but does not commit it)
1305-
bool deleteAttribute( int attr );
1305+
virtual bool deleteAttribute( int attr );
13061306

13071307
/**
13081308
* Deletes a list of attribute fields (but does not commit it)

0 commit comments

Comments
 (0)
Please sign in to comment.