Skip to content
Permalink
Browse files
Update of existing fields in field calculator and possibility to appl…
…y only to selection

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11741 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Oct 2, 2009
1 parent 83e2d1e commit da74bf8bbabc478cad1acad596173563125c2639
Showing with 220 additions and 150 deletions.
  1. +77 −28 src/app/qgsfieldcalculator.cpp
  2. +1 −0 src/app/qgsfieldcalculator.h
  3. +142 −122 src/ui/qgsfieldcalculatorbase.ui
@@ -32,6 +32,8 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ): QDialog(), mVector
mOuputFieldWidthSpinBox->setValue( 10 );
mOutputFieldPrecisionSpinBox->setValue( 3 );

mUpdateExistingFieldCheckBox->setCheckState( Qt::Checked );

//disable ok button until there is text for output field and expression
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
}
@@ -65,43 +67,60 @@ void QgsFieldCalculator::accept()

mVectorLayer->beginEditCommand( "Field calculator" );

//create new field
QgsField newField( mOutputFieldNameLineEdit->text() );
if ( mOutputFieldTypeComboBox->currentText() == tr( "Double" ) )
{
newField.setType( QVariant::Double );
}
else if ( mOutputFieldTypeComboBox->currentText() == tr( "Integer" ) )
int attributeId = -1; //id of the field (can be existing field or newly created one

//update existing field
if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked )
{
newField.setType( QVariant::Int );
QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() );
if ( fieldIt != mFieldMap.end() )
{
attributeId = fieldIt.value();
}
}
else if ( mOutputFieldTypeComboBox->currentText() == tr( "String" ) )
//create new field
else
{
newField.setType( QVariant::String );
}
//create new field
QgsField newField( mOutputFieldNameLineEdit->text() );
if ( mOutputFieldTypeComboBox->currentText() == tr( "Double" ) )
{
newField.setType( QVariant::Double );
}
else if ( mOutputFieldTypeComboBox->currentText() == tr( "Integer" ) )
{
newField.setType( QVariant::Int );
}
else if ( mOutputFieldTypeComboBox->currentText() == tr( "String" ) )
{
newField.setType( QVariant::String );
}

newField.setLength( mOuputFieldWidthSpinBox->value() );
newField.setPrecision( mOutputFieldPrecisionSpinBox->value() );
newField.setLength( mOuputFieldWidthSpinBox->value() );
newField.setPrecision( mOutputFieldPrecisionSpinBox->value() );

if ( !mVectorLayer->addAttribute( newField ) )
{
QMessageBox::critical( 0, tr( "Provider error" ), tr( "Could not add the new field to the provider." ) );
mVectorLayer->destroyEditCommand();
return;
}

if ( !mVectorLayer->addAttribute( newField ) )
{
mVectorLayer->destroyEditCommand();
return;
}
//get index of the new field
const QgsFieldMap fieldList = mVectorLayer->pendingFields();

//get index of the new field
const QgsFieldMap fieldList = mVectorLayer->pendingFields();
int attributeId = -1;
QgsFieldMap::const_iterator it = fieldList.constBegin();
for ( ; it != fieldList.constEnd(); ++it )
{
if ( it.value().name() == mOutputFieldNameLineEdit->text() )
QgsFieldMap::const_iterator it = fieldList.constBegin();
for ( ; it != fieldList.constEnd(); ++it )
{
attributeId = it.key();
break;
if ( it.value().name() == mOutputFieldNameLineEdit->text() )
{
attributeId = it.key();
break;
}
}
}


if ( attributeId == -1 )
{
mVectorLayer->destroyEditCommand();
@@ -112,9 +131,21 @@ void QgsFieldCalculator::accept()
QgsFeature feature;
bool calculationSuccess = true;

bool onlySelected = ( mOnlyUpdateSelectedCheckBox->checkState() == Qt::Checked );
QgsFeatureIds selectedIds = mVectorLayer->selectedFeaturesIds();


mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), QgsRectangle(), false, false );
while ( mVectorLayer->nextFeature( feature ) )
{
if ( onlySelected )
{
if ( !selectedIds.contains( feature.id() ) )
{
continue;
}
}

QgsSearchTreeValue value = searchTree->valueAgainst( mVectorLayer->pendingFields(), feature.attributeMap() );
if ( value.isError() )
{
@@ -158,10 +189,27 @@ void QgsFieldCalculator::populateFields()
QgsFieldMap::const_iterator fieldIt = fieldMap.constBegin();
for ( ; fieldIt != fieldMap.constEnd(); ++fieldIt )
{

QString fieldName = fieldIt.value().name();

//insert into field list and field combo box
mFieldMap.insert( fieldName, fieldIt.key() );
mFieldsListWidget->addItem( fieldName );
mExistingFieldComboBox->addItem( fieldName );
}
}

void QgsFieldCalculator::on_mUpdateExistingFieldCheckBox_stateChanged( int state )
{
if ( state == Qt::Checked )
{
mNewFieldGroupBox->setEnabled( false );
}
else
{
mNewFieldGroupBox->setEnabled( true );
}
setOkButtonState();
}

void QgsFieldCalculator::on_mFieldsListWidget_itemDoubleClicked( QListWidgetItem * item )
@@ -343,7 +391,8 @@ void QgsFieldCalculator::getFieldValues( int limit )
void QgsFieldCalculator::setOkButtonState()
{
bool okEnabled = true;
if ( mOutputFieldNameLineEdit->text().isEmpty() || mExpressionTextEdit->toPlainText().isEmpty() )
if (( mOutputFieldNameLineEdit->text().isEmpty() && mUpdateExistingFieldCheckBox->checkState() == Qt::Unchecked )\
|| mExpressionTextEdit->toPlainText().isEmpty() )
{
okEnabled = false;
}
@@ -31,6 +31,7 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
public slots:
void accept();

void on_mUpdateExistingFieldCheckBox_stateChanged( int state );
void on_mFieldsListWidget_itemDoubleClicked( QListWidgetItem * item );
void on_mValueListWidget_itemDoubleClicked( QListWidgetItem * item );
void on_mPlusPushButton_clicked();

0 comments on commit da74bf8

Please sign in to comment.