Skip to content
Permalink
Browse files

Virtual fields definition moved to field calculator

This place is more appropriate than the add attributes dialog.
See also http://osgeo-org.1560.x6.nabble.com/Virtual-Fields-Allow-adding-fields-out-of-edit-mode-td5166223.html for a related discussion.

Fix #11342
  • Loading branch information
m-kuhn committed Oct 13, 2014
1 parent 1770ec1 commit 68de043ecbcf0203b6a3667b488be71f479e5d91
@@ -8994,7 +8994,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )

bool canChangeAttributes = dprovider->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
bool canDeleteFeatures = dprovider->capabilities() & QgsVectorDataProvider::DeleteFeatures;
bool canAddAttributes = dprovider->capabilities() & QgsVectorDataProvider::AddAttributes;
bool canAddFeatures = dprovider->capabilities() & QgsVectorDataProvider::AddFeatures;
bool canSupportEditing = dprovider->capabilities() & QgsVectorDataProvider::EditingCapabilities;
bool canChangeGeometry = dprovider->capabilities() & QgsVectorDataProvider::ChangeGeometries;
@@ -9126,7 +9125,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionAddFeature->setIcon( QgsApplication::getThemeIcon( "/mActionNewTableRow.png" ) );
}

mActionOpenFieldCalc->setEnabled( isEditable && ( canChangeAttributes || canAddAttributes ) );
mActionOpenFieldCalc->setEnabled( true );

return;
}
@@ -19,15 +19,12 @@
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgslogger.h"
#include "qgsexpressionbuilderdialog.h"

#include <QMessageBox>
#include <QLabel>

QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mIsShapeFile( vlayer && vlayer->providerType() == "ogr" && vlayer->storageType() == "ESRI Shapefile" )
, mLayer( vlayer )
{
setupUi( this );

@@ -53,18 +50,9 @@ QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt:
}

on_mTypeBox_currentIndexChanged( 0 );
on_mFieldModeButtonGroup_buttonClicked( mButtonProviderField );

if ( mIsShapeFile )
mNameEdit->setMaxLength( 10 );

mExpressionWidget->setLayer( vlayer );

int cap = mLayer->dataProvider()->capabilities();

mButtonProviderField->setEnabled( cap & QgsVectorDataProvider::AddAttributes );

mInfoIcon->setPixmap( style()->standardPixmap( QStyle::SP_MessageBoxInformation ) );
}

void QgsAddAttrDialog::on_mTypeBox_currentIndexChanged( int idx )
@@ -82,22 +70,6 @@ void QgsAddAttrDialog::on_mTypeBox_currentIndexChanged( int idx )
setPrecisionMinMax();
}

void QgsAddAttrDialog::on_mFieldModeButtonGroup_buttonClicked( QAbstractButton* button )
{
if ( button == mButtonProviderField )
{
mExpressionWidget->hide();
mExpressionLabel->hide();
}
else
{
mExpressionWidget->show();
mExpressionLabel->show();
}

mLayerEditableInfo->setVisible( !mLayer->isEditable() && button == mButtonProviderField );
}

void QgsAddAttrDialog::on_mLength_editingFinished()
{
setPrecisionMinMax();
@@ -128,17 +100,6 @@ void QgsAddAttrDialog::accept()
tr( "No name specified. Please specify a name to create a new field." ) );
return;
}
if ( mButtonVirtualField->isChecked() && mExpressionWidget->currentField().isEmpty() )
{
QMessageBox::warning( this, tr( "Warning" ),
tr( "No expression specified. Please enter an expression that will be used to calculate the field values." ) );
return;
}

if ( !mLayer->isEditable() && mode() == ProviderField )
{
mLayer->startEditing();
}

QDialog::accept();
}
@@ -162,16 +123,3 @@ QgsField QgsAddAttrDialog::field() const
mPrec->value(),
mCommentEdit->text() );
}

const QString QgsAddAttrDialog::expression() const
{
return mExpressionWidget->currentField();
}

QgsAddAttrDialog::AttributeMode QgsAddAttrDialog::mode() const
{
if ( mButtonVirtualField->isChecked() )
return VirtualField;
else
return ProviderField;
}
@@ -28,28 +28,20 @@ class APP_EXPORT QgsAddAttrDialog: public QDialog, private Ui::QgsAddAttrDialogB
{
Q_OBJECT
public:
enum AttributeMode
{
ProviderField,
VirtualField
};

QgsAddAttrDialog( QgsVectorLayer *vlayer,
QWidget *parent = 0, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
QgsAddAttrDialog( const std::list<QString>& typelist,
QWidget *parent = 0, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );

QgsField field() const;
const QString expression() const;
AttributeMode mode() const;

private slots:
public slots:
void on_mTypeBox_currentIndexChanged( int idx );
void on_mFieldModeButtonGroup_buttonClicked( QAbstractButton* button );
void on_mLength_editingFinished();
void accept();

private:
bool mIsShapeFile;
QgsVectorLayer* mLayer;

void setPrecisionMinMax();
};
@@ -172,7 +172,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
mToggleEditingButton->blockSignals( false );

mSaveEditsButton->setEnabled( mToggleEditingButton->isEnabled() && mLayer->isEditable() );
mOpenFieldCalculator->setEnabled(( canChangeAttributes || canAddAttributes ) && mLayer->isEditable() );
mAddAttribute->setEnabled(( canChangeAttributes || canAddAttributes ) && mLayer->isEditable() );
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );
mAddFeature->setHidden( !canAddFeatures || mLayer->geometryType() != QGis::NoGeometry );
@@ -558,7 +558,7 @@ void QgsAttributeTableDialog::editingToggled()
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
mOpenFieldCalculator->setEnabled(( canChangeAttributes || canAddAttributes ) && mLayer->isEditable() );
mAddAttribute->setEnabled(( canChangeAttributes || canAddAttributes ) && mLayer->isEditable() );
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );

@@ -579,24 +579,18 @@ void QgsAttributeTableDialog::on_mAddAttribute_clicked()
QgsAddAttrDialog dialog( mLayer, this );
if ( dialog.exec() == QDialog::Accepted )
{
if ( dialog.mode() == QgsAddAttrDialog::VirtualField )
mLayer->beginEditCommand( tr( "Attribute added" ) );
if ( mLayer->addAttribute( dialog.field() ) )
{
mLayer->addExpressionField( dialog.expression(), dialog.field() );
mLayer->endEditCommand();
}
else
{
mLayer->beginEditCommand( tr( "Attribute added" ) );
if ( mLayer->addAttribute( dialog.field() ) )
{
mLayer->endEditCommand();
}
else
{
mLayer->destroyEditCommand();
QMessageBox::critical( this, tr( "Failed to add field" ), tr( "Failed to add field '%1' of type '%2'. Is the field name unique?" ).arg( dialog.field().name() ).arg( dialog.field().typeName() ) );
}
mLayer->destroyEditCommand();
QMessageBox::critical( this, tr( "Failed to add field" ), tr( "Failed to add field '%1' of type '%2'. Is the field name unique?" ).arg( dialog.field().name() ).arg( dialog.field().typeName() ) );
}


// update model - a field has been added or updated
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
columnBoxInit();

0 comments on commit 68de043

Please sign in to comment.
You can’t perform that action at this time.