mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
Update of existing fields in field calculator and possibility to apply only to selection
git-svn-id: http://svn.osgeo.org/qgis/trunk@11741 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
parent
77c03ad448
commit
0105afcfd2
@ -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" ) )
|
||||
{
|
||||
newField.setType( QVariant::Int );
|
||||
}
|
||||
else if ( mOutputFieldTypeComboBox->currentText() == tr( "String" ) )
|
||||
{
|
||||
newField.setType( QVariant::String );
|
||||
}
|
||||
int attributeId = -1; //id of the field (can be existing field or newly created one
|
||||
|
||||
newField.setLength( mOuputFieldWidthSpinBox->value() );
|
||||
newField.setPrecision( mOutputFieldPrecisionSpinBox->value() );
|
||||
|
||||
|
||||
if ( !mVectorLayer->addAttribute( newField ) )
|
||||
//update existing field
|
||||
if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked )
|
||||
{
|
||||
mVectorLayer->destroyEditCommand();
|
||||
return;
|
||||
}
|
||||
|
||||
//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() )
|
||||
QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() );
|
||||
if ( fieldIt != mFieldMap.end() )
|
||||
{
|
||||
attributeId = it.key();
|
||||
break;
|
||||
attributeId = fieldIt.value();
|
||||
}
|
||||
}
|
||||
//create new field
|
||||
else
|
||||
{
|
||||
//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() );
|
||||
|
||||
if ( !mVectorLayer->addAttribute( newField ) )
|
||||
{
|
||||
QMessageBox::critical( 0, tr( "Provider error" ), tr( "Could not add the new field to the provider." ) );
|
||||
mVectorLayer->destroyEditCommand();
|
||||
return;
|
||||
}
|
||||
|
||||
//get index of the new field
|
||||
const QgsFieldMap fieldList = mVectorLayer->pendingFields();
|
||||
|
||||
QgsFieldMap::const_iterator it = fieldList.constBegin();
|
||||
for ( ; it != fieldList.constEnd(); ++it )
|
||||
{
|
||||
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,12 +189,29 @@ 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 )
|
||||
{
|
||||
if ( !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();
|
||||
|
@ -1,95 +1,115 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<ui version="4.0" >
|
||||
<class>QgsFieldCalculatorBase</class>
|
||||
<widget class="QDialog" name="QgsFieldCalculatorBase">
|
||||
<property name="geometry">
|
||||
<widget class="QDialog" name="QgsFieldCalculatorBase" >
|
||||
<property name="geometry" >
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>383</width>
|
||||
<height>496</height>
|
||||
<width>615</width>
|
||||
<height>628</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<property name="windowTitle" >
|
||||
<string>Field calculator</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="mFieldNameLabel">
|
||||
<property name="text">
|
||||
<string>Output field name:</string>
|
||||
<layout class="QGridLayout" name="gridLayout_5" >
|
||||
<item row="0" column="0" >
|
||||
<widget class="QCheckBox" name="mUpdateExistingFieldCheckBox" >
|
||||
<property name="text" >
|
||||
<string>Update existing field</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QLineEdit" name="mOutputFieldNameLineEdit"/>
|
||||
<item row="0" column="1" colspan="2" >
|
||||
<widget class="QComboBox" name="mExistingFieldComboBox" />
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="mOutputFieldTypeLabel">
|
||||
<property name="text">
|
||||
<string>Output field type:</string>
|
||||
<item row="1" column="0" colspan="3" >
|
||||
<widget class="QCheckBox" name="mOnlyUpdateSelectedCheckBox" >
|
||||
<property name="text" >
|
||||
<string>Only update selected features</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QComboBox" name="mOutputFieldTypeComboBox"/>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="mOutputFieldWidthLabel">
|
||||
<property name="text">
|
||||
<string>Output field width:</string>
|
||||
<item row="2" column="0" colspan="3" >
|
||||
<widget class="QGroupBox" name="mNewFieldGroupBox" >
|
||||
<property name="title" >
|
||||
<string>New field</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3" >
|
||||
<item row="0" column="0" >
|
||||
<widget class="QLabel" name="mFieldNameLabel" >
|
||||
<property name="text" >
|
||||
<string>Output field name:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="3" >
|
||||
<widget class="QLineEdit" name="mOutputFieldNameLineEdit" />
|
||||
</item>
|
||||
<item row="1" column="0" >
|
||||
<widget class="QLabel" name="mOutputFieldTypeLabel" >
|
||||
<property name="text" >
|
||||
<string>Output field type:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1" colspan="3" >
|
||||
<widget class="QComboBox" name="mOutputFieldTypeComboBox" />
|
||||
</item>
|
||||
<item row="2" column="0" >
|
||||
<widget class="QLabel" name="mOutputFieldWidthLabel" >
|
||||
<property name="text" >
|
||||
<string>Output field width:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1" >
|
||||
<widget class="QSpinBox" name="mOuputFieldWidthSpinBox" />
|
||||
</item>
|
||||
<item row="2" column="2" >
|
||||
<widget class="QLabel" name="mOutputFieldPrecisionLabel" >
|
||||
<property name="text" >
|
||||
<string>Output field precision:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3" >
|
||||
<widget class="QSpinBox" name="mOutputFieldPrecisionSpinBox" />
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QSpinBox" name="mOuputFieldWidthSpinBox"/>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QLabel" name="mOutputFieldPrecisionLabel">
|
||||
<property name="text">
|
||||
<string>Output field precision:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3">
|
||||
<widget class="QSpinBox" name="mOutputFieldPrecisionSpinBox"/>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<widget class="QGroupBox" name="mFieldsGroupBox">
|
||||
<property name="title">
|
||||
<item row="3" column="0" colspan="2" >
|
||||
<widget class="QGroupBox" name="mFieldsGroupBox" >
|
||||
<property name="title" >
|
||||
<string>Fields</string>
|
||||
</property>
|
||||
<widget class="QListWidget" name="mFieldsListWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>20</y>
|
||||
<width>221</width>
|
||||
<height>151</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<layout class="QGridLayout" name="gridLayout_4" >
|
||||
<item row="0" column="0" >
|
||||
<widget class="QListWidget" name="mFieldsListWidget" />
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="2" colspan="2">
|
||||
<widget class="QGroupBox" name="mValuesGroupBox">
|
||||
<property name="title">
|
||||
<item row="3" column="2" >
|
||||
<widget class="QGroupBox" name="mValuesGroupBox" >
|
||||
<property name="title" >
|
||||
<string>Values</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0" colspan="2">
|
||||
<widget class="QListWidget" name="mValueListWidget"/>
|
||||
<layout class="QGridLayout" name="gridLayout_2" >
|
||||
<item row="0" column="0" colspan="2" >
|
||||
<widget class="QListWidget" name="mValueListWidget" />
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QPushButton" name="mSamplePushButton">
|
||||
<property name="text">
|
||||
<item row="1" column="0" >
|
||||
<widget class="QPushButton" name="mSamplePushButton" >
|
||||
<property name="text" >
|
||||
<string>Sample</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="mAllPushButton">
|
||||
<property name="text">
|
||||
<item row="1" column="1" >
|
||||
<widget class="QPushButton" name="mAllPushButton" >
|
||||
<property name="text" >
|
||||
<string>All</string>
|
||||
</property>
|
||||
</widget>
|
||||
@ -97,106 +117,106 @@
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="4">
|
||||
<widget class="QGroupBox" name="mOperatorsGroupBox">
|
||||
<property name="title">
|
||||
<item row="4" column="0" colspan="3" >
|
||||
<widget class="QGroupBox" name="mOperatorsGroupBox" >
|
||||
<property name="title" >
|
||||
<string>Operators</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="mPlusPushButton">
|
||||
<property name="text">
|
||||
<layout class="QGridLayout" name="gridLayout" >
|
||||
<item row="0" column="0" >
|
||||
<widget class="QPushButton" name="mPlusPushButton" >
|
||||
<property name="text" >
|
||||
<string>+</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="mMultiplyPushButton">
|
||||
<property name="text">
|
||||
<item row="0" column="1" >
|
||||
<widget class="QPushButton" name="mMultiplyPushButton" >
|
||||
<property name="text" >
|
||||
<string>*</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QPushButton" name="mSqrtButton">
|
||||
<property name="text">
|
||||
<item row="0" column="2" >
|
||||
<widget class="QPushButton" name="mSqrtButton" >
|
||||
<property name="text" >
|
||||
<string>sqrt</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="3">
|
||||
<widget class="QPushButton" name="mSinButton">
|
||||
<property name="text">
|
||||
<item row="0" column="3" >
|
||||
<widget class="QPushButton" name="mSinButton" >
|
||||
<property name="text" >
|
||||
<string>sin</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="4">
|
||||
<widget class="QPushButton" name="mTanButton">
|
||||
<property name="text">
|
||||
<item row="0" column="4" >
|
||||
<widget class="QPushButton" name="mTanButton" >
|
||||
<property name="text" >
|
||||
<string>tan</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="5">
|
||||
<widget class="QPushButton" name="mACosButton">
|
||||
<property name="text">
|
||||
<item row="0" column="5" >
|
||||
<widget class="QPushButton" name="mACosButton" >
|
||||
<property name="text" >
|
||||
<string>acos</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="6">
|
||||
<widget class="QPushButton" name="mOpenBracketPushButton">
|
||||
<property name="text">
|
||||
<item row="0" column="6" >
|
||||
<widget class="QPushButton" name="mOpenBracketPushButton" >
|
||||
<property name="text" >
|
||||
<string>(</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QPushButton" name="mMinusPushButton">
|
||||
<property name="text">
|
||||
<item row="1" column="0" >
|
||||
<widget class="QPushButton" name="mMinusPushButton" >
|
||||
<property name="text" >
|
||||
<string>-</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="mDividePushButton">
|
||||
<property name="text">
|
||||
<item row="1" column="1" >
|
||||
<widget class="QPushButton" name="mDividePushButton" >
|
||||
<property name="text" >
|
||||
<string>/</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QPushButton" name="mExpButton">
|
||||
<property name="text">
|
||||
<item row="1" column="2" >
|
||||
<widget class="QPushButton" name="mExpButton" >
|
||||
<property name="text" >
|
||||
<string>^</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<widget class="QPushButton" name="mCosButton">
|
||||
<property name="text">
|
||||
<item row="1" column="3" >
|
||||
<widget class="QPushButton" name="mCosButton" >
|
||||
<property name="text" >
|
||||
<string>cos</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="4">
|
||||
<widget class="QPushButton" name="mASinButton">
|
||||
<property name="text">
|
||||
<item row="1" column="4" >
|
||||
<widget class="QPushButton" name="mASinButton" >
|
||||
<property name="text" >
|
||||
<string>asin</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="5">
|
||||
<widget class="QPushButton" name="mATanButton">
|
||||
<property name="text">
|
||||
<item row="1" column="5" >
|
||||
<widget class="QPushButton" name="mATanButton" >
|
||||
<property name="text" >
|
||||
<string>atan</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="6">
|
||||
<widget class="QPushButton" name="mCloseBracketPushButton">
|
||||
<property name="text">
|
||||
<item row="1" column="6" >
|
||||
<widget class="QPushButton" name="mCloseBracketPushButton" >
|
||||
<property name="text" >
|
||||
<string>)</string>
|
||||
</property>
|
||||
</widget>
|
||||
@ -204,22 +224,22 @@
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0" colspan="2">
|
||||
<widget class="QLabel" name="mFieldCalculatorExpressionLabel">
|
||||
<property name="text">
|
||||
<item row="5" column="0" colspan="2" >
|
||||
<widget class="QLabel" name="mFieldCalculatorExpressionLabel" >
|
||||
<property name="text" >
|
||||
<string>Field calculator expression:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0" colspan="4">
|
||||
<widget class="QTextEdit" name="mExpressionTextEdit"/>
|
||||
<item row="6" column="0" colspan="3" >
|
||||
<widget class="QTextEdit" name="mExpressionTextEdit" />
|
||||
</item>
|
||||
<item row="7" column="0" colspan="2">
|
||||
<widget class="QDialogButtonBox" name="mButtonBox">
|
||||
<property name="orientation">
|
||||
<item row="7" column="0" colspan="2" >
|
||||
<widget class="QDialogButtonBox" name="mButtonBox" >
|
||||
<property name="orientation" >
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<property name="standardButtons" >
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
@ -234,11 +254,11 @@
|
||||
<receiver>QgsFieldCalculatorBase</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<hint type="sourcelabel" >
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<hint type="destinationlabel" >
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
@ -250,11 +270,11 @@
|
||||
<receiver>QgsFieldCalculatorBase</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<hint type="sourcelabel" >
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<hint type="destinationlabel" >
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
|
Loading…
x
Reference in New Issue
Block a user