@@ -31,8 +31,6 @@ QgsPropertyOverrideButton::QgsPropertyOverrideButton( QWidget* parent,
31
31
const QgsVectorLayer* layer )
32
32
: QToolButton( parent )
33
33
, mVectorLayer( layer )
34
- , mActive( false )
35
- , mUseExpression( false )
36
34
, mExpressionContextGenerator( nullptr )
37
35
{
38
36
setFocusPolicy ( Qt::StrongFocus );
@@ -183,16 +181,7 @@ void QgsPropertyOverrideButton::updateFieldLists()
183
181
184
182
QgsProperty QgsPropertyOverrideButton::toProperty () const
185
183
{
186
- QgsProperty p;
187
- if ( mUseExpression )
188
- {
189
- p = QgsProperty::fromExpression ( mExpressionString , mActive );
190
- }
191
- else if ( !mFieldName .isEmpty () )
192
- {
193
- p = QgsProperty::fromField ( mFieldName , mActive );
194
- }
195
- return p;
184
+ return mProperty ;
196
185
}
197
186
198
187
void QgsPropertyOverrideButton::setVectorLayer ( const QgsVectorLayer* layer )
@@ -216,7 +205,7 @@ void QgsPropertyOverrideButton::mouseReleaseEvent( QMouseEvent *event )
216
205
if (( event->modifiers () & ( Qt::ControlModifier ) )
217
206
|| event->button () == Qt::RightButton )
218
207
{
219
- setActivePrivate ( !mActive );
208
+ setActivePrivate ( !mProperty . isActive () );
220
209
updateGui ();
221
210
emit changed ();
222
211
event->ignore ();
@@ -239,24 +228,22 @@ void QgsPropertyOverrideButton::setToProperty( const QgsProperty& property )
239
228
case QgsProperty::FieldBasedProperty:
240
229
{
241
230
mFieldName = property.field ();
242
- mUseExpression = false ;
243
231
break ;
244
232
}
245
233
case QgsProperty::ExpressionBasedProperty:
246
234
{
247
235
mExpressionString = property.expressionString ();
248
- mUseExpression = true ;
249
236
break ;
250
237
}
251
238
}
252
239
}
253
240
else
254
241
{
255
242
mFieldName .clear ();
256
- mUseExpression = false ;
257
243
mExpressionString .clear ();
258
244
}
259
- setActive ( property && property.isActive () );
245
+ mProperty = property;
246
+ setActive ( mProperty && mProperty .isActive () );
260
247
updateGui ();
261
248
}
262
249
@@ -267,7 +254,6 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
267
254
updateFieldLists ();
268
255
269
256
bool hasExp = !mExpressionString .isEmpty ();
270
- bool hasField = !mFieldName .isEmpty ();
271
257
QString ddTitle = tr ( " Data defined override" );
272
258
273
259
QAction* ddTitleAct = mDefineMenu ->addAction ( ddTitle );
@@ -277,24 +263,24 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
277
263
ddTitleAct->setEnabled ( false );
278
264
279
265
bool addActiveAction = false ;
280
- if ( mUseExpression && hasExp )
266
+ if ( mProperty . propertyType () == QgsProperty::ExpressionBasedProperty && hasExp )
281
267
{
282
268
QgsExpression exp ( mExpressionString );
283
269
// whether expression is parse-able
284
270
addActiveAction = !exp .hasParserError ();
285
271
}
286
- else if ( ! mUseExpression && hasField )
272
+ else if ( mProperty . propertyType () == QgsProperty::FieldBasedProperty )
287
273
{
288
274
// whether field exists
289
275
addActiveAction = mFieldNameList .contains ( mFieldName );
290
276
}
291
277
292
278
if ( addActiveAction )
293
279
{
294
- ddTitleAct->setText ( ddTitle + " (" + ( mUseExpression ? tr ( " expression" ) : tr ( " field" ) ) + ' )' );
280
+ ddTitleAct->setText ( ddTitle + " (" + ( mProperty . propertyType () == QgsProperty::ExpressionBasedProperty ? tr ( " expression" ) : tr ( " field" ) ) + ' )' );
295
281
mDefineMenu ->addAction ( mActionActive );
296
- mActionActive ->setText ( mActive ? tr ( " Deactivate" ) : tr ( " Activate" ) );
297
- mActionActive ->setData ( QVariant ( mActive ? false : true ) );
282
+ mActionActive ->setText ( mProperty . isActive () ? tr ( " Deactivate" ) : tr ( " Activate" ) );
283
+ mActionActive ->setData ( QVariant ( mProperty . isActive () ? false : true ) );
298
284
}
299
285
300
286
if ( !mFullDescription .isEmpty () )
@@ -326,8 +312,8 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
326
312
if ( mFieldName == fldname )
327
313
{
328
314
act->setCheckable ( true );
329
- act->setChecked ( ! mUseExpression );
330
- fieldActive = ! mUseExpression ;
315
+ act->setChecked ( mProperty . propertyType () != QgsProperty::ExpressionBasedProperty );
316
+ fieldActive = mProperty . propertyType () != QgsProperty::ExpressionBasedProperty ;
331
317
}
332
318
}
333
319
}
@@ -363,7 +349,7 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
363
349
QAction* act = mVariablesMenu ->addAction ( variable );
364
350
act->setData ( QVariant ( variable ) );
365
351
366
- if ( mUseExpression && hasExp && mExpressionString == ' @' + variable )
352
+ if ( mProperty . propertyType () == QgsProperty::ExpressionBasedProperty && hasExp && mExpressionString == ' @' + variable )
367
353
{
368
354
act->setCheckable ( true );
369
355
act->setChecked ( true );
@@ -403,7 +389,7 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
403
389
mActionExpression ->setText ( expString );
404
390
}
405
391
mDefineMenu ->addAction ( mActionExpression );
406
- mActionExpression ->setChecked ( mUseExpression && !variableActive );
392
+ mActionExpression ->setChecked ( mProperty . propertyType () == QgsProperty::ExpressionBasedProperty && !variableActive );
407
393
408
394
mDefineMenu ->addAction ( mActionExpDialog );
409
395
mDefineMenu ->addAction ( mActionCopyExpr );
@@ -435,7 +421,7 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
435
421
}
436
422
else if ( action == mActionExpression )
437
423
{
438
- mUseExpression = true ;
424
+ mProperty . setExpressionString ( mExpressionString ) ;
439
425
setActivePrivate ( true );
440
426
updateGui ();
441
427
emit changed ();
@@ -450,18 +436,18 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
450
436
if ( !exprString.isEmpty () )
451
437
{
452
438
mExpressionString = exprString;
453
- mUseExpression = true ;
454
- setActivePrivate ( mActive );
439
+ mProperty . setExpressionString ( mExpressionString ) ;
440
+ setActivePrivate ( mProperty . isActive () );
455
441
updateGui ();
456
442
emit changed ();
457
443
}
458
444
}
459
445
else if ( action == mActionClearExpr )
460
446
{
461
447
// only deactivate if defined expression is being used
462
- if ( mActive && mUseExpression )
448
+ if ( mProperty . isActive () && mProperty . propertyType () == QgsProperty::ExpressionBasedProperty )
463
449
{
464
- mUseExpression = false ;
450
+ mProperty . setStaticValue ( QVariant () ) ;
465
451
setActivePrivate ( false );
466
452
}
467
453
mExpressionString .clear ();
@@ -480,7 +466,7 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
480
466
{
481
467
mFieldName = action->data ().toString ();
482
468
}
483
- mUseExpression = false ;
469
+ mProperty . setField ( mFieldName ) ;
484
470
setActivePrivate ( true );
485
471
updateGui ();
486
472
emit changed ();
@@ -492,7 +478,7 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
492
478
{
493
479
mExpressionString = action->data ().toString ().prepend ( " @" );
494
480
}
495
- mUseExpression = true ;
481
+ mProperty . setExpressionString ( mExpressionString ) ;
496
482
setActivePrivate ( true );
497
483
updateGui ();
498
484
emit changed ();
@@ -515,12 +501,9 @@ void QgsPropertyOverrideButton::showExpressionDialog()
515
501
QgsExpressionBuilderDialog d ( const_cast <QgsVectorLayer*>( mVectorLayer ), mExpressionString , this , QStringLiteral ( " generic" ), context );
516
502
if ( d.exec () == QDialog::Accepted )
517
503
{
518
- QString newExp = d.expressionText ();
519
504
mExpressionString = d.expressionText ().trimmed ();
520
- bool hasExp = !newExp.isEmpty ();
521
-
522
- mUseExpression = hasExp;
523
- setActivePrivate ( hasExp );
505
+ mProperty .setExpressionString ( mExpressionString );
506
+ setActivePrivate ( !mExpressionString .isEmpty () );
524
507
updateGui ();
525
508
emit changed ();
526
509
}
@@ -532,37 +515,25 @@ void QgsPropertyOverrideButton::updateGui()
532
515
bool hasExp = !mExpressionString .isEmpty ();
533
516
bool hasField = !mFieldName .isEmpty ();
534
517
535
- if ( mUseExpression && !hasExp )
536
- {
537
- setActive ( false );
538
- mUseExpression = false ;
539
- }
540
- else if ( !mUseExpression && !hasField )
541
- {
542
- setActive ( false );
543
- }
544
-
545
518
QIcon icon = QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefine.svg" ) );
546
519
QString deftip = tr ( " undefined" );
547
- if ( mUseExpression && hasExp )
520
+ if ( mProperty . propertyType () == QgsProperty::ExpressionBasedProperty && hasExp )
548
521
{
549
- icon = mActive ? QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineExpressionOn.svg" ) ) : QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineExpression.svg" ) );
522
+ icon = mProperty . isActive () ? QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineExpressionOn.svg" ) ) : QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineExpression.svg" ) );
550
523
551
524
QgsExpression exp ( mExpressionString );
552
525
if ( exp .hasParserError () )
553
526
{
554
- setActive ( false );
555
527
icon = QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineExpressionError.svg" ) );
556
528
deftip = tr ( " Parse error: %1" ).arg ( exp .parserErrorString () );
557
529
}
558
530
}
559
- else if ( ! mUseExpression && hasField )
531
+ else if ( mProperty . propertyType () != QgsProperty::ExpressionBasedProperty && hasField )
560
532
{
561
- icon = mActive ? QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineOn.svg" ) ) : QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefine.svg" ) );
533
+ icon = mProperty . isActive () ? QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineOn.svg" ) ) : QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefine.svg" ) );
562
534
563
535
if ( !mFieldNameList .contains ( mFieldName ) )
564
536
{
565
- setActive ( false );
566
537
icon = QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconDataDefineError.svg" ) );
567
538
deftip = tr ( " '%1' field missing" ).arg ( mFieldName );
568
539
}
@@ -573,7 +544,7 @@ void QgsPropertyOverrideButton::updateGui()
573
544
// build full description for tool tip and popup dialog
574
545
mFullDescription = tr ( " <b><u>Data defined override</u></b><br>" );
575
546
576
- mFullDescription += tr ( " <b>Active: </b>%1 <i>(ctrl|right-click toggles)</i><br>" ).arg ( mActive ? tr ( " yes" ) : tr ( " no" ) );
547
+ mFullDescription += tr ( " <b>Active: </b>%1 <i>(ctrl|right-click toggles)</i><br>" ).arg ( mProperty . isActive () ? tr ( " yes" ) : tr ( " no" ) );
577
548
578
549
if ( !mUsageInfo .isEmpty () )
579
550
{
@@ -593,7 +564,7 @@ void QgsPropertyOverrideButton::updateGui()
593
564
QString deftype ( " " );
594
565
if ( deftip != tr ( " undefined" ) )
595
566
{
596
- deftype = QString ( " (%1)" ).arg ( mUseExpression ? tr ( " expression" ) : tr ( " field" ) );
567
+ deftype = QString ( " (%1)" ).arg ( mProperty . propertyType () == QgsProperty::ExpressionBasedProperty ? tr ( " expression" ) : tr ( " field" ) );
597
568
}
598
569
599
570
// truncate long expressions, or tool tip may be too wide for screen
@@ -611,10 +582,10 @@ void QgsPropertyOverrideButton::updateGui()
611
582
612
583
void QgsPropertyOverrideButton::setActivePrivate ( bool active )
613
584
{
614
- if ( mActive != active )
585
+ if ( mProperty . isActive () != active )
615
586
{
616
- mActive = active;
617
- emit activated ( mActive );
587
+ mProperty . setActive ( active ) ;
588
+ emit activated ( mProperty . isActive () );
618
589
}
619
590
}
620
591
@@ -644,11 +615,11 @@ void QgsPropertyOverrideButton::checkCheckedWidgets( bool check )
644
615
645
616
void QgsPropertyOverrideButton::setActive ( bool active )
646
617
{
647
- if ( mActive != active )
618
+ if ( mProperty . isActive () != active )
648
619
{
649
- mActive = active;
620
+ mProperty . setActive ( active ) ;
650
621
emit changed ();
651
- emit activated ( mActive );
622
+ emit activated ( mProperty . isActive () );
652
623
}
653
624
}
654
625
0 commit comments