@@ -42,28 +42,42 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl )
42
42
mOutputFieldWidthSpinBox ->setValue ( 10 );
43
43
mOutputFieldPrecisionSpinBox ->setValue ( 3 );
44
44
45
- // disable creation of new fields if not supported by data provider
46
- if ( !( vl->dataProvider ()->capabilities () & QgsVectorDataProvider::AddAttributes ) )
45
+ mUpdateExistingGroupBox ->setEnabled ( vl->dataProvider ()->capabilities () & QgsVectorDataProvider::ChangeAttributeValues );
46
+ mNewFieldGroupBox ->setEnabled ( vl->dataProvider ()->capabilities () & QgsVectorDataProvider::AddAttributes );
47
+
48
+ Q_ASSERT ( mNewFieldGroupBox ->isEnabled () || mUpdateExistingGroupBox ->isEnabled () );
49
+
50
+ if ( mNewFieldGroupBox ->isEnabled () )
51
+ {
52
+ mNewFieldGroupBox ->setChecked ( true );
53
+ }
54
+ else
47
55
{
56
+ mNewFieldGroupBox ->setToolTip ( tr ( " Not available for layer" ) );
57
+ mUpdateExistingGroupBox ->setChecked ( true );
48
58
mUpdateExistingGroupBox ->setCheckable ( false );
49
- mNewFieldGroupBox ->setChecked ( false );
50
- mNewFieldGroupBox ->setTitle ( mNewFieldGroupBox ->title () + tr ( " (not supported by provider)" ) );
51
59
}
52
60
53
- if ( vl-> selectedFeaturesIds (). size () > 0 )
61
+ if ( mUpdateExistingGroupBox -> isEnabled () )
54
62
{
55
- mOnlyUpdateSelectedCheckBox ->setChecked ( true );
63
+ mUpdateExistingGroupBox ->setChecked ( ! mNewFieldGroupBox -> isEnabled () );
56
64
}
65
+ else
66
+ {
67
+ mUpdateExistingGroupBox ->setToolTip ( tr ( " Not available for layer" ) );
68
+ mNewFieldGroupBox ->setChecked ( true );
69
+ mNewFieldGroupBox ->setCheckable ( false );
70
+ }
71
+
72
+ mOnlyUpdateSelectedCheckBox ->setChecked ( vl->selectedFeaturesIds ().size () > 0 );
57
73
}
58
74
59
75
QgsFieldCalculator::~QgsFieldCalculator ()
60
76
{
61
-
62
77
}
63
78
64
79
void QgsFieldCalculator::accept ()
65
80
{
66
-
67
81
QString calcString = builder->expressionText ();
68
82
QgsExpression exp ( calcString );
69
83
@@ -79,7 +93,7 @@ void QgsFieldCalculator::accept()
79
93
mVectorLayer ->beginEditCommand ( " Field calculator" );
80
94
81
95
// update existing field
82
- if ( mUpdateExistingGroupBox ->isChecked () )
96
+ if ( mUpdateExistingGroupBox ->isChecked () || ! mNewFieldGroupBox -> isEnabled () )
83
97
{
84
98
QMap<QString, int >::const_iterator fieldIt = mFieldMap .find ( mExistingFieldComboBox ->currentText () );
85
99
if ( fieldIt != mFieldMap .end () )
@@ -128,7 +142,7 @@ void QgsFieldCalculator::accept()
128
142
bool calculationSuccess = true ;
129
143
QString error;
130
144
131
- bool onlySelected = ( mOnlyUpdateSelectedCheckBox ->isChecked () );
145
+ bool onlySelected = mOnlyUpdateSelectedCheckBox ->isChecked ();
132
146
QgsFeatureIds selectedIds = mVectorLayer ->selectedFeaturesIds ();
133
147
134
148
// block layerModified signals (that would trigger table update)
@@ -269,18 +283,22 @@ void QgsFieldCalculator::populateFields()
269
283
void QgsFieldCalculator::setOkButtonState ()
270
284
{
271
285
QPushButton* okButton = mButtonBox ->button ( QDialogButtonBox::Ok );
272
- okButton->setToolTip ( " " );
273
-
274
- bool emptyFieldName = mOutputFieldNameLineEdit ->text ().isEmpty ();
275
- bool expressionValid = builder->isExpressionValid ();
276
286
277
- if ( emptyFieldName )
287
+ if (( mNewFieldGroupBox ->isChecked () || !mUpdateExistingGroupBox ->isEnabled () )
288
+ && mOutputFieldNameLineEdit ->text ().isEmpty () )
289
+ {
278
290
okButton->setToolTip ( tr ( " Please enter a field name" ) );
291
+ okButton->setEnabled ( false );
292
+ return ;
293
+ }
279
294
280
- if ( !expressionValid )
295
+ if ( !builder->isExpressionValid () )
296
+ {
281
297
okButton->setToolTip ( okButton->toolTip () + tr ( " \n The expression is invalid see (more info) for details" ) );
298
+ okButton->setEnabled ( false );
299
+ return ;
300
+ }
282
301
283
- bool okEnabled = ( !emptyFieldName || mUpdateExistingGroupBox ->isChecked () ) && expressionValid;
284
-
285
- okButton->setEnabled ( okEnabled );
302
+ okButton->setToolTip ( " " );
303
+ okButton->setEnabled ( true );
286
304
}
0 commit comments