Skip to content

Commit be9e93d

Browse files
committed
Refactor internals of QgsPropertyOverrideButton
1 parent 54089ff commit be9e93d

File tree

2 files changed

+38
-66
lines changed

2 files changed

+38
-66
lines changed

src/gui/qgspropertyoverridebutton.cpp

+34-63
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ QgsPropertyOverrideButton::QgsPropertyOverrideButton( QWidget* parent,
3131
const QgsVectorLayer* layer )
3232
: QToolButton( parent )
3333
, mVectorLayer( layer )
34-
, mActive( false )
35-
, mUseExpression( false )
3634
, mExpressionContextGenerator( nullptr )
3735
{
3836
setFocusPolicy( Qt::StrongFocus );
@@ -183,16 +181,7 @@ void QgsPropertyOverrideButton::updateFieldLists()
183181

184182
QgsProperty QgsPropertyOverrideButton::toProperty() const
185183
{
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;
196185
}
197186

198187
void QgsPropertyOverrideButton::setVectorLayer( const QgsVectorLayer* layer )
@@ -216,7 +205,7 @@ void QgsPropertyOverrideButton::mouseReleaseEvent( QMouseEvent *event )
216205
if (( event->modifiers() & ( Qt::ControlModifier ) )
217206
|| event->button() == Qt::RightButton )
218207
{
219-
setActivePrivate( !mActive );
208+
setActivePrivate( !mProperty.isActive() );
220209
updateGui();
221210
emit changed();
222211
event->ignore();
@@ -239,24 +228,22 @@ void QgsPropertyOverrideButton::setToProperty( const QgsProperty& property )
239228
case QgsProperty::FieldBasedProperty:
240229
{
241230
mFieldName = property.field();
242-
mUseExpression = false;
243231
break;
244232
}
245233
case QgsProperty::ExpressionBasedProperty:
246234
{
247235
mExpressionString = property.expressionString();
248-
mUseExpression = true;
249236
break;
250237
}
251238
}
252239
}
253240
else
254241
{
255242
mFieldName.clear();
256-
mUseExpression = false;
257243
mExpressionString.clear();
258244
}
259-
setActive( property && property.isActive() );
245+
mProperty = property;
246+
setActive( mProperty && mProperty.isActive() );
260247
updateGui();
261248
}
262249

@@ -267,7 +254,6 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
267254
updateFieldLists();
268255

269256
bool hasExp = !mExpressionString.isEmpty();
270-
bool hasField = !mFieldName.isEmpty();
271257
QString ddTitle = tr( "Data defined override" );
272258

273259
QAction* ddTitleAct = mDefineMenu->addAction( ddTitle );
@@ -277,24 +263,24 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
277263
ddTitleAct->setEnabled( false );
278264

279265
bool addActiveAction = false;
280-
if ( mUseExpression && hasExp )
266+
if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && hasExp )
281267
{
282268
QgsExpression exp( mExpressionString );
283269
// whether expression is parse-able
284270
addActiveAction = !exp.hasParserError();
285271
}
286-
else if ( !mUseExpression && hasField )
272+
else if ( mProperty.propertyType() == QgsProperty::FieldBasedProperty )
287273
{
288274
// whether field exists
289275
addActiveAction = mFieldNameList.contains( mFieldName );
290276
}
291277

292278
if ( addActiveAction )
293279
{
294-
ddTitleAct->setText( ddTitle + " (" + ( mUseExpression ? tr( "expression" ) : tr( "field" ) ) + ')' );
280+
ddTitleAct->setText( ddTitle + " (" + ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty ? tr( "expression" ) : tr( "field" ) ) + ')' );
295281
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 ) );
298284
}
299285

300286
if ( !mFullDescription.isEmpty() )
@@ -326,8 +312,8 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
326312
if ( mFieldName == fldname )
327313
{
328314
act->setCheckable( true );
329-
act->setChecked( !mUseExpression );
330-
fieldActive = !mUseExpression;
315+
act->setChecked( mProperty.propertyType() != QgsProperty::ExpressionBasedProperty );
316+
fieldActive = mProperty.propertyType() != QgsProperty::ExpressionBasedProperty;
331317
}
332318
}
333319
}
@@ -363,7 +349,7 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
363349
QAction* act = mVariablesMenu->addAction( variable );
364350
act->setData( QVariant( variable ) );
365351

366-
if ( mUseExpression && hasExp && mExpressionString == '@' + variable )
352+
if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && hasExp && mExpressionString == '@' + variable )
367353
{
368354
act->setCheckable( true );
369355
act->setChecked( true );
@@ -403,7 +389,7 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
403389
mActionExpression->setText( expString );
404390
}
405391
mDefineMenu->addAction( mActionExpression );
406-
mActionExpression->setChecked( mUseExpression && !variableActive );
392+
mActionExpression->setChecked( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && !variableActive );
407393

408394
mDefineMenu->addAction( mActionExpDialog );
409395
mDefineMenu->addAction( mActionCopyExpr );
@@ -435,7 +421,7 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
435421
}
436422
else if ( action == mActionExpression )
437423
{
438-
mUseExpression = true;
424+
mProperty.setExpressionString( mExpressionString );
439425
setActivePrivate( true );
440426
updateGui();
441427
emit changed();
@@ -450,18 +436,18 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
450436
if ( !exprString.isEmpty() )
451437
{
452438
mExpressionString = exprString;
453-
mUseExpression = true;
454-
setActivePrivate( mActive );
439+
mProperty.setExpressionString( mExpressionString );
440+
setActivePrivate( mProperty.isActive() );
455441
updateGui();
456442
emit changed();
457443
}
458444
}
459445
else if ( action == mActionClearExpr )
460446
{
461447
// only deactivate if defined expression is being used
462-
if ( mActive && mUseExpression )
448+
if ( mProperty.isActive() && mProperty.propertyType() == QgsProperty::ExpressionBasedProperty )
463449
{
464-
mUseExpression = false;
450+
mProperty.setStaticValue( QVariant() );
465451
setActivePrivate( false );
466452
}
467453
mExpressionString.clear();
@@ -480,7 +466,7 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
480466
{
481467
mFieldName = action->data().toString();
482468
}
483-
mUseExpression = false;
469+
mProperty.setField( mFieldName );
484470
setActivePrivate( true );
485471
updateGui();
486472
emit changed();
@@ -492,7 +478,7 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction* action )
492478
{
493479
mExpressionString = action->data().toString().prepend( "@" );
494480
}
495-
mUseExpression = true;
481+
mProperty.setExpressionString( mExpressionString );
496482
setActivePrivate( true );
497483
updateGui();
498484
emit changed();
@@ -515,12 +501,9 @@ void QgsPropertyOverrideButton::showExpressionDialog()
515501
QgsExpressionBuilderDialog d( const_cast<QgsVectorLayer*>( mVectorLayer ), mExpressionString, this, QStringLiteral( "generic" ), context );
516502
if ( d.exec() == QDialog::Accepted )
517503
{
518-
QString newExp = d.expressionText();
519504
mExpressionString = d.expressionText().trimmed();
520-
bool hasExp = !newExp.isEmpty();
521-
522-
mUseExpression = hasExp;
523-
setActivePrivate( hasExp );
505+
mProperty.setExpressionString( mExpressionString );
506+
setActivePrivate( !mExpressionString.isEmpty() );
524507
updateGui();
525508
emit changed();
526509
}
@@ -532,37 +515,25 @@ void QgsPropertyOverrideButton::updateGui()
532515
bool hasExp = !mExpressionString.isEmpty();
533516
bool hasField = !mFieldName.isEmpty();
534517

535-
if ( mUseExpression && !hasExp )
536-
{
537-
setActive( false );
538-
mUseExpression = false;
539-
}
540-
else if ( !mUseExpression && !hasField )
541-
{
542-
setActive( false );
543-
}
544-
545518
QIcon icon = QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefine.svg" ) );
546519
QString deftip = tr( "undefined" );
547-
if ( mUseExpression && hasExp )
520+
if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && hasExp )
548521
{
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" ) );
550523

551524
QgsExpression exp( mExpressionString );
552525
if ( exp.hasParserError() )
553526
{
554-
setActive( false );
555527
icon = QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefineExpressionError.svg" ) );
556528
deftip = tr( "Parse error: %1" ).arg( exp.parserErrorString() );
557529
}
558530
}
559-
else if ( !mUseExpression && hasField )
531+
else if ( mProperty.propertyType() != QgsProperty::ExpressionBasedProperty && hasField )
560532
{
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" ) );
562534

563535
if ( !mFieldNameList.contains( mFieldName ) )
564536
{
565-
setActive( false );
566537
icon = QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefineError.svg" ) );
567538
deftip = tr( "'%1' field missing" ).arg( mFieldName );
568539
}
@@ -573,7 +544,7 @@ void QgsPropertyOverrideButton::updateGui()
573544
// build full description for tool tip and popup dialog
574545
mFullDescription = tr( "<b><u>Data defined override</u></b><br>" );
575546

576-
mFullDescription += tr( "<b>Active: </b>%1&nbsp;&nbsp;&nbsp;<i>(ctrl|right-click toggles)</i><br>" ).arg( mActive ? tr( "yes" ) : tr( "no" ) );
547+
mFullDescription += tr( "<b>Active: </b>%1&nbsp;&nbsp;&nbsp;<i>(ctrl|right-click toggles)</i><br>" ).arg( mProperty.isActive() ? tr( "yes" ) : tr( "no" ) );
577548

578549
if ( !mUsageInfo.isEmpty() )
579550
{
@@ -593,7 +564,7 @@ void QgsPropertyOverrideButton::updateGui()
593564
QString deftype( "" );
594565
if ( deftip != tr( "undefined" ) )
595566
{
596-
deftype = QString( " (%1)" ).arg( mUseExpression ? tr( "expression" ) : tr( "field" ) );
567+
deftype = QString( " (%1)" ).arg( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty ? tr( "expression" ) : tr( "field" ) );
597568
}
598569

599570
// truncate long expressions, or tool tip may be too wide for screen
@@ -611,10 +582,10 @@ void QgsPropertyOverrideButton::updateGui()
611582

612583
void QgsPropertyOverrideButton::setActivePrivate( bool active )
613584
{
614-
if ( mActive != active )
585+
if ( mProperty.isActive() != active )
615586
{
616-
mActive = active;
617-
emit activated( mActive );
587+
mProperty.setActive( active );
588+
emit activated( mProperty.isActive() );
618589
}
619590
}
620591

@@ -644,11 +615,11 @@ void QgsPropertyOverrideButton::checkCheckedWidgets( bool check )
644615

645616
void QgsPropertyOverrideButton::setActive( bool active )
646617
{
647-
if ( mActive != active )
618+
if ( mProperty.isActive() != active )
648619
{
649-
mActive = active;
620+
mProperty.setActive( active );
650621
emit changed();
651-
emit activated( mActive );
622+
emit activated( mProperty.isActive() );
652623
}
653624
}
654625

src/gui/qgspropertyoverridebutton.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
105105
/**
106106
* Returns true if the button has an active property.
107107
*/
108-
bool isActive() const { return mActive; }
108+
bool isActive() const { return mProperty.isActive(); }
109109

110110
/**
111111
* Returns the data type which the widget will accept. This is used to filter
@@ -197,8 +197,6 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
197197
QStringList mFieldNameList;
198198
QStringList mFieldTypeList;
199199

200-
bool mActive;
201-
bool mUseExpression;
202200
QString mExpressionString;
203201
QString mFieldName;
204202

@@ -227,6 +225,9 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
227225

228226
QList< QPointer<QWidget> > mCheckedWidgets;
229227

228+
//! Internal property used for storing state of widget
229+
QgsProperty mProperty;
230+
230231
private slots:
231232
void aboutToShowMenu();
232233
void menuActionTriggered( QAction* action );

0 commit comments

Comments
 (0)