Skip to content

Commit

Permalink
Virtual fields definition moved to field calculator
Browse files Browse the repository at this point in the history
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 68de043
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 386 deletions.
3 changes: 1 addition & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -9126,7 +9125,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionAddFeature->setIcon( QgsApplication::getThemeIcon( "/mActionNewTableRow.png" ) );
}

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

return;
}
Expand Down
52 changes: 0 additions & 52 deletions src/app/qgsaddattrdialog.cpp
Expand Up @@ -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 );

Expand All @@ -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 )
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
}
14 changes: 3 additions & 11 deletions src/app/qgsaddattrdialog.h
Expand Up @@ -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();
};
Expand Down
22 changes: 8 additions & 14 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );

Expand All @@ -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();
Expand Down

0 comments on commit 68de043

Please sign in to comment.