Skip to content
Permalink
Browse files

Ensure data defined button displayed fields are always up to date

Fix #14809
  • Loading branch information
nyalldawson committed Jun 8, 2016
1 parent f8f3b21 commit f0c8fe689a1bcdef593f130ede43084b342dd514
Showing with 46 additions and 35 deletions.
  1. +45 −35 src/gui/qgsdatadefinedbutton.cpp
  2. +1 −0 src/gui/qgsdatadefinedbutton.h
@@ -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,
@@ -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 )
@@ -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();
@@ -338,6 +338,7 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
void showExpressionDialog();
void showAssistant();
void updateGui();
void updateFieldLists();

const QgsVectorLayer* mVectorLayer;
QStringList mFieldNameList;

0 comments on commit f0c8fe6

Please sign in to comment.
You can’t perform that action at this time.