Skip to content

Commit 1162bce

Browse files
author
jef
committed
add support to set fields to NULL in field calculator and fix following update of attribute table
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15179 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ba79e61 commit 1162bce

6 files changed

+45
-16
lines changed

src/app/attributetable/qgsattributetabledialog.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ void QgsAttributeTableDialog::on_mAdvancedSearchButton_clicked()
674674
QgsSearchQueryBuilder dlg( mLayer, this );
675675
dlg.setSearchString( mQuery->displayText() );
676676

677-
if ( dlg.exec() )
677+
if ( dlg.exec() == QDialog::Accepted )
678678
doSearch( dlg.searchString() );
679679
}
680680

@@ -796,9 +796,19 @@ void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
796796
void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
797797
{
798798
QgsFieldCalculator calc( mLayer );
799-
calc.exec();
799+
if ( calc.exec() == QDialog::Accepted )
800+
{
801+
int col = mModel->fieldCol( calc.changedAttributeId() );
802+
803+
if ( col >= 0 )
804+
{
805+
mModel->reload( mModel->index( 0, col ),
806+
mModel->index( mModel->rowCount(), col ) );
807+
}
808+
}
800809
}
801810

811+
802812
void QgsAttributeTableDialog::addFeature()
803813
{
804814
if ( !mLayer->isEditable() )

src/app/attributetable/qgsattributetablemodel.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void QgsAttributeTableModel::layerDeleted()
149149

150150
void QgsAttributeTableModel::attributeValueChanged( int fid, int idx, const QVariant &value )
151151
{
152-
setData( index( idToRow( fid ), mAttributes.indexOf( idx ) ), value, Qt::EditRole );
152+
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
153153
}
154154

155155
void QgsAttributeTableModel::loadAttributes()
@@ -297,6 +297,11 @@ int QgsAttributeTableModel::fieldIdx( int col ) const
297297
return mAttributes[ col ];
298298
}
299299

300+
int QgsAttributeTableModel::fieldCol( int idx ) const
301+
{
302+
return mAttributes.indexOf( idx );
303+
}
304+
300305
int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
301306
{
302307
return mRowIdMap.size();

src/app/attributetable/qgsattributetablemodel.h

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class QgsAttributeTableModel: public QAbstractTableModel
106106
* get field index from column
107107
*/
108108
int fieldIdx( int col ) const;
109+
/**
110+
* get column from field index
111+
*/
112+
int fieldCol( int idx ) const;
109113
/**
110114
* Maps row to feature id
111115
* @param row row number

src/app/qgsfieldcalculator.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
#include "qgsvectorlayer.h"
2121
#include <QMessageBox>
2222

23-
QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ): QDialog(), mVectorLayer( vl )
23+
QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl )
24+
: QDialog()
25+
, mVectorLayer( vl )
26+
, mAttributeId( -1 )
2427
{
2528
setupUi( this );
2629

@@ -92,15 +95,13 @@ void QgsFieldCalculator::accept()
9295

9396
mVectorLayer->beginEditCommand( "Field calculator" );
9497

95-
int attributeId = -1; //id of the field (can be existing field or newly created one
96-
9798
//update existing field
9899
if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked )
99100
{
100101
QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() );
101102
if ( fieldIt != mFieldMap.end() )
102103
{
103-
attributeId = fieldIt.value();
104+
mAttributeId = fieldIt.value();
104105
}
105106
}
106107
//create new field
@@ -128,14 +129,13 @@ void QgsFieldCalculator::accept()
128129
{
129130
if ( it.value().name() == mOutputFieldNameLineEdit->text() )
130131
{
131-
attributeId = it.key();
132+
mAttributeId = it.key();
132133
break;
133134
}
134135
}
135136
}
136137

137-
138-
if ( attributeId == -1 )
138+
if ( mAttributeId == -1 )
139139
{
140140
mVectorLayer->destroyEditCommand();
141141
return;
@@ -172,9 +172,9 @@ void QgsFieldCalculator::accept()
172172
if ( value.isError() )
173173
{
174174
//insert NULL value for this feature and continue the calculation
175-
if( searchTree->errorMsg() == QObject::tr( "Division by zero." ) )
175+
if ( searchTree->errorMsg() == QObject::tr( "Division by zero." ) )
176176
{
177-
mVectorLayer->changeAttributeValue( feature.id(), attributeId, QVariant(), false );
177+
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, QVariant(), false );
178178
}
179179
else
180180
{
@@ -184,11 +184,15 @@ void QgsFieldCalculator::accept()
184184
}
185185
else if ( value.isNumeric() )
186186
{
187-
mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.number(), false );
187+
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value.number(), false );
188+
}
189+
else if ( value.isNull() )
190+
{
191+
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, QVariant(), false );
188192
}
189193
else
190194
{
191-
mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.string(), false );
195+
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value.string(), false );
192196
}
193197

194198
rownum++;
@@ -499,7 +503,7 @@ void QgsFieldCalculator::setOkButtonState()
499503
}
500504

501505

502-
void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous )
506+
void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged( QListWidgetItem * current, QListWidgetItem * previous )
503507
{
504508
getFieldValues( 25 );
505509
}

src/app/qgsfieldcalculator.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
2929
QgsFieldCalculator( QgsVectorLayer* vl );
3030
~QgsFieldCalculator();
3131

32+
int changedAttributeId() const { return mAttributeId; }
33+
3234
public slots:
3335
void accept();
3436

@@ -60,7 +62,7 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
6062
void on_mOutputFieldNameLineEdit_textChanged( const QString& text );
6163
void on_mExpressionTextEdit_textChanged();
6264
void on_mOutputFieldTypeComboBox_activated( int index );
63-
void on_mFieldsListWidget_currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous );
65+
void on_mFieldsListWidget_currentItemChanged( QListWidgetItem * current, QListWidgetItem * previous );
6466

6567
void on_mButtonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
6668

@@ -80,6 +82,9 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
8082
QgsVectorLayer* mVectorLayer;
8183
/**Key: field name, Value: field index*/
8284
QMap<QString, int> mFieldMap;
85+
86+
/**idx of changed attribute*/
87+
int mAttributeId;
8388
};
8489

8590
#endif // QGSFIELDCALCULATOR_H

src/core/qgssearchstringparser.yy

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ scalar_exp:
174174
| ID { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opID, 0, 0); addToTmpNodes($$); }
175175
| NUMBER { $$ = new QgsSearchTreeNode($1); addToTmpNodes($$); }
176176
| STRING { $$ = new QgsSearchTreeNode(QString::fromUtf8(yytext), 0); addToTmpNodes($$); }
177+
| NULLVALUE { $$ = new QgsSearchTreeNode(QString::null, 0); addToTmpNodes($$); }
177178
| COLUMN_REF { $$ = new QgsSearchTreeNode(QString::fromUtf8(yytext), 1); addToTmpNodes($$); }
178179
| scalar_exp IN '(' scalar_exp_list ')' { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opIN, $1, $4); joinTmpNodes($$,$1,$4); }
179180
| scalar_exp NOT IN '(' scalar_exp_list ')' { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opNOTIN, $1, $5); joinTmpNodes($$,$1,$5); }

0 commit comments

Comments
 (0)