Skip to content
Permalink
Browse files

add support to set fields to NULL in field calculator and fix followi…

…ng update of attribute table

git-svn-id: http://svn.osgeo.org/qgis/trunk@15179 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Feb 16, 2011
1 parent 98d204b commit 31f3800d5dcd24bfd7ab82bc9a26bdf41cf6969a
@@ -674,7 +674,7 @@ void QgsAttributeTableDialog::on_mAdvancedSearchButton_clicked()
QgsSearchQueryBuilder dlg( mLayer, this );
dlg.setSearchString( mQuery->displayText() );

if ( dlg.exec() )
if ( dlg.exec() == QDialog::Accepted )
doSearch( dlg.searchString() );
}

@@ -796,9 +796,19 @@ void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
{
QgsFieldCalculator calc( mLayer );
calc.exec();
if ( calc.exec() == QDialog::Accepted )
{
int col = mModel->fieldCol( calc.changedAttributeId() );

if ( col >= 0 )
{
mModel->reload( mModel->index( 0, col ),
mModel->index( mModel->rowCount(), col ) );
}
}
}


void QgsAttributeTableDialog::addFeature()
{
if ( !mLayer->isEditable() )
@@ -149,7 +149,7 @@ void QgsAttributeTableModel::layerDeleted()

void QgsAttributeTableModel::attributeValueChanged( int fid, int idx, const QVariant &value )
{
setData( index( idToRow( fid ), mAttributes.indexOf( idx ) ), value, Qt::EditRole );
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
}

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

int QgsAttributeTableModel::fieldCol( int idx ) const
{
return mAttributes.indexOf( idx );
}

int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
{
return mRowIdMap.size();
@@ -106,6 +106,10 @@ class QgsAttributeTableModel: public QAbstractTableModel
* get field index from column
*/
int fieldIdx( int col ) const;
/**
* get column from field index
*/
int fieldCol( int idx ) const;
/**
* Maps row to feature id
* @param row row number
@@ -20,7 +20,10 @@
#include "qgsvectorlayer.h"
#include <QMessageBox>

QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ): QDialog(), mVectorLayer( vl )
QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl )
: QDialog()
, mVectorLayer( vl )
, mAttributeId( -1 )
{
setupUi( this );

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

mVectorLayer->beginEditCommand( "Field calculator" );

int attributeId = -1; //id of the field (can be existing field or newly created one

//update existing field
if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked )
{
QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() );
if ( fieldIt != mFieldMap.end() )
{
attributeId = fieldIt.value();
mAttributeId = fieldIt.value();
}
}
//create new field
@@ -128,14 +129,13 @@ void QgsFieldCalculator::accept()
{
if ( it.value().name() == mOutputFieldNameLineEdit->text() )
{
attributeId = it.key();
mAttributeId = it.key();
break;
}
}
}


if ( attributeId == -1 )
if ( mAttributeId == -1 )
{
mVectorLayer->destroyEditCommand();
return;
@@ -172,9 +172,9 @@ void QgsFieldCalculator::accept()
if ( value.isError() )
{
//insert NULL value for this feature and continue the calculation
if( searchTree->errorMsg() == QObject::tr( "Division by zero." ) )
if ( searchTree->errorMsg() == QObject::tr( "Division by zero." ) )
{
mVectorLayer->changeAttributeValue( feature.id(), attributeId, QVariant(), false );
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, QVariant(), false );
}
else
{
@@ -184,11 +184,15 @@ void QgsFieldCalculator::accept()
}
else if ( value.isNumeric() )
{
mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.number(), false );
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value.number(), false );
}
else if ( value.isNull() )
{
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, QVariant(), false );
}
else
{
mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.string(), false );
mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value.string(), false );
}

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


void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous )
void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged( QListWidgetItem * current, QListWidgetItem * previous )
{
getFieldValues( 25 );
}
@@ -29,6 +29,8 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
QgsFieldCalculator( QgsVectorLayer* vl );
~QgsFieldCalculator();

int changedAttributeId() const { return mAttributeId; }

public slots:
void accept();

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

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

@@ -80,6 +82,9 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
QgsVectorLayer* mVectorLayer;
/**Key: field name, Value: field index*/
QMap<QString, int> mFieldMap;

/**idx of changed attribute*/
int mAttributeId;
};

#endif // QGSFIELDCALCULATOR_H
@@ -174,6 +174,7 @@ scalar_exp:
| ID { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opID, 0, 0); addToTmpNodes($$); }
| NUMBER { $$ = new QgsSearchTreeNode($1); addToTmpNodes($$); }
| STRING { $$ = new QgsSearchTreeNode(QString::fromUtf8(yytext), 0); addToTmpNodes($$); }
| NULLVALUE { $$ = new QgsSearchTreeNode(QString::null, 0); addToTmpNodes($$); }
| COLUMN_REF { $$ = new QgsSearchTreeNode(QString::fromUtf8(yytext), 1); addToTmpNodes($$); }
| scalar_exp IN '(' scalar_exp_list ')' { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opIN, $1, $4); joinTmpNodes($$,$1,$4); }
| scalar_exp NOT IN '(' scalar_exp_list ')' { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opNOTIN, $1, $5); joinTmpNodes($$,$1,$5); }

0 comments on commit 31f3800

Please sign in to comment.