Skip to content

Commit

Permalink
Field calculator: read available types from the provider (similar to …
Browse files Browse the repository at this point in the history
…QgsAddAttrDialog)

git-svn-id: http://svn.osgeo.org/qgis/trunk@11758 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Oct 6, 2009
1 parent 3c1dbe4 commit 7a1091a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
82 changes: 53 additions & 29 deletions src/app/qgsfieldcalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
#include "qgsfieldcalculator.h"
#include "qgssearchtreenode.h"
#include "qgssearchstring.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include <QMessageBox>

QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ): QDialog(), mVectorLayer( vl )
{
setupUi( this );
mOutputFieldTypeComboBox->addItem( tr( "Whole number (integer)" ), "Integer" );
mOutputFieldTypeComboBox->addItem( tr( "Decimal number (double)" ), "Double" );
mOutputFieldTypeComboBox->addItem( tr( "Text (string)" ), "String" );

populateFields();
populateOutputFieldTypes();

//default values for field width and precision
mOuputFieldWidthSpinBox->setValue( 10 );
Expand Down Expand Up @@ -82,23 +81,11 @@ void QgsFieldCalculator::accept()
else
{
//create new field
QgsField newField( mOutputFieldNameLineEdit->text() );
int index = mOutputFieldTypeComboBox->currentIndex();
if ( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole ) == "Double" )
{
newField.setType( QVariant::Double );
}
else if ( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole ) == "Integer" )
{
newField.setType( QVariant::Int );
}
else if ( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole ) == "String" )
{
newField.setType( QVariant::String );
}

newField.setLength( mOuputFieldWidthSpinBox->value() );
newField.setPrecision( mOutputFieldPrecisionSpinBox->value() );
QgsField newField( mOutputFieldNameLineEdit->text(),
( QVariant::Type ) mOutputFieldTypeComboBox->itemData( mOutputFieldTypeComboBox->currentIndex(), Qt::UserRole ).toInt(),
mOutputFieldTypeComboBox->itemData( mOutputFieldTypeComboBox->currentIndex(), Qt::UserRole + 1 ).toString(),
mOuputFieldWidthSpinBox->value(),
mOutputFieldPrecisionSpinBox->value() );

if ( !mVectorLayer->addAttribute( newField ) )
{
Expand Down Expand Up @@ -200,6 +187,36 @@ void QgsFieldCalculator::populateFields()
}
}

void QgsFieldCalculator::populateOutputFieldTypes()
{
if ( !mVectorLayer )
{
return;
}

QgsVectorDataProvider* provider = mVectorLayer->dataProvider();
if ( !provider )
{
return;
}

mOutputFieldTypeComboBox->blockSignals( true );
const QList< QgsVectorDataProvider::NativeType > &typelist = provider->nativeTypes();
for ( int i = 0; i < typelist.size(); i++ )
{
mOutputFieldTypeComboBox->addItem( typelist[i].mTypeDesc );
mOutputFieldTypeComboBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
mOutputFieldTypeComboBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
mOutputFieldTypeComboBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
mOutputFieldTypeComboBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
mOutputFieldTypeComboBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
mOutputFieldTypeComboBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
}
mOutputFieldTypeComboBox->blockSignals( false );
mOutputFieldTypeComboBox->setCurrentIndex( 0 );
on_mOutputFieldTypeComboBox_activated( 0 );
}

void QgsFieldCalculator::on_mUpdateExistingFieldCheckBox_stateChanged( int state )
{
if ( state == Qt::Checked )
Expand Down Expand Up @@ -321,16 +338,23 @@ void QgsFieldCalculator::on_mExpressionTextEdit_textChanged()
setOkButtonState();
}

void QgsFieldCalculator::on_mOutputFieldTypeComboBox_currentIndexChanged( const QString& text )
void QgsFieldCalculator::on_mOutputFieldTypeComboBox_activated( int index )
{
if ( text == tr( "Double" ) )
{
mOutputFieldPrecisionSpinBox->setEnabled( true );
}
else
{
mOutputFieldPrecisionSpinBox->setEnabled( false );
}
mOuputFieldWidthSpinBox->setMinimum( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole + 2 ).toInt() );
mOuputFieldWidthSpinBox->setMaximum( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole + 3 ).toInt() );
mOuputFieldWidthSpinBox->setEnabled( mOuputFieldWidthSpinBox->minimum() < mOuputFieldWidthSpinBox->maximum() );
if ( mOuputFieldWidthSpinBox->value() < mOuputFieldWidthSpinBox->minimum() )
mOuputFieldWidthSpinBox->setValue( mOuputFieldWidthSpinBox->minimum() );
if ( mOuputFieldWidthSpinBox->value() > mOuputFieldWidthSpinBox->maximum() )
mOuputFieldWidthSpinBox->setValue( mOuputFieldWidthSpinBox->maximum() );

mOutputFieldPrecisionSpinBox->setMinimum( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole + 4 ).toInt() );
mOutputFieldPrecisionSpinBox->setMaximum( mOutputFieldTypeComboBox->itemData( index, Qt::UserRole + 5 ).toInt() );
mOutputFieldPrecisionSpinBox->setEnabled( mOutputFieldPrecisionSpinBox->minimum() < mOutputFieldPrecisionSpinBox->maximum() );
if ( mOutputFieldPrecisionSpinBox->value() < mOutputFieldPrecisionSpinBox->minimum() )
mOutputFieldPrecisionSpinBox->setValue( mOutputFieldPrecisionSpinBox->minimum() );
if ( mOutputFieldPrecisionSpinBox->value() > mOutputFieldPrecisionSpinBox->maximum() )
mOutputFieldPrecisionSpinBox->setValue( mOutputFieldPrecisionSpinBox->maximum() );
}

void QgsFieldCalculator::getFieldValues( int limit )
Expand Down
5 changes: 4 additions & 1 deletion src/app/qgsfieldcalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
void on_mAllPushButton_clicked();
void on_mOutputFieldNameLineEdit_textChanged( const QString& text );
void on_mExpressionTextEdit_textChanged();
void on_mOutputFieldTypeComboBox_currentIndexChanged( const QString& text );
void on_mOutputFieldTypeComboBox_activated( int index );

private:
//default constructor forbidden
QgsFieldCalculator();
/**Inserts existing fields into the combo box*/
void populateFields();
/**Inserts the types supported by the provider into the combo box*/
void populateOutputFieldTypes();
/**Gets field values and inserts them into mValueListWidget
@limit 0: get all feature, != 0 stop after getting limit values*/
void getFieldValues( int limit );
Expand Down

0 comments on commit 7a1091a

Please sign in to comment.