Skip to content

Commit

Permalink
Ensure data defined button displayed fields are always up to date
Browse files Browse the repository at this point in the history
Fix #14809
  • Loading branch information
nyalldawson committed Jun 8, 2016
1 parent f8f3b21 commit f0c8fe6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
80 changes: 45 additions & 35 deletions src/gui/qgsdatadefinedbutton.cpp
Expand Up @@ -101,6 +101,47 @@ QgsDataDefinedButton::~QgsDataDefinedButton()
mCheckedWidgets.clear();
}

void QgsDataDefinedButton::updateFieldLists()
{
mFieldNameList.clear();
mFieldTypeList.clear();

if ( mVectorLayer )
{
// store just a list of fields of unknown type or those that match the expected type
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
{
bool fieldMatch = false;
// NOTE: these are the only QVariant enums supported at this time (see QgsField)
QString fieldType;
switch ( f.type() )
{
case QVariant::String:
fieldMatch = mDataTypes.testFlag( String );
fieldType = tr( "string" );
break;
case QVariant::Int:
fieldMatch = mDataTypes.testFlag( Int ) || mDataTypes.testFlag( Double );
fieldType = tr( "integer" );
break;
case QVariant::Double:
fieldMatch = mDataTypes.testFlag( Double );
fieldType = tr( "double" );
break;
case QVariant::Invalid:
default:
fieldMatch = true; // field type is unknown
fieldType = tr( "unknown type" );
}
if ( fieldMatch || mDataTypes.testFlag( AnyType ) )
{
mFieldNameList << f.name();
mFieldTypeList << fieldType;
}
}
}
}

void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
const QgsDataDefined* datadefined,
const DataTypes& datatypes,
Expand Down Expand Up @@ -155,44 +196,11 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
mActionDataTypes->setText( tr( "Field type: " ) + mDataTypesString );
}

if ( mVectorLayer )
{
// store just a list of fields of unknown type or those that match the expected type
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
{
bool fieldMatch = false;
// NOTE: these are the only QVariant enums supported at this time (see QgsField)
QString fieldType;
switch ( f.type() )
{
case QVariant::String:
fieldMatch = mDataTypes.testFlag( String );
fieldType = tr( "string" );
break;
case QVariant::Int:
fieldMatch = mDataTypes.testFlag( Int ) || mDataTypes.testFlag( Double );
fieldType = tr( "integer" );
break;
case QVariant::Double:
fieldMatch = mDataTypes.testFlag( Double );
fieldType = tr( "double" );
break;
case QVariant::Invalid:
default:
fieldMatch = true; // field type is unknown
fieldType = tr( "unknown type" );
}
if ( fieldMatch || mDataTypes.testFlag( AnyType ) )
{
mFieldNameList << f.name();
mFieldTypeList << fieldType;
}
}
}

updateFieldLists();
updateGui();
}


void QgsDataDefinedButton::updateDataDefined( QgsDataDefined *dd ) const
{
if ( !dd )
Expand Down Expand Up @@ -230,6 +238,8 @@ void QgsDataDefinedButton::mouseReleaseEvent( QMouseEvent *event )
void QgsDataDefinedButton::aboutToShowMenu()
{
mDefineMenu->clear();
// update fields so that changes made to layer's fields are reflected
updateFieldLists();

bool hasExp = !getExpression().isEmpty();
bool hasField = !getField().isEmpty();
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsdatadefinedbutton.h
Expand Up @@ -338,6 +338,7 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
void showExpressionDialog();
void showAssistant();
void updateGui();
void updateFieldLists();

const QgsVectorLayer* mVectorLayer;
QStringList mFieldNameList;
Expand Down

0 comments on commit f0c8fe6

Please sign in to comment.