Skip to content

Commit

Permalink
add field icon in DnD tree (#39400)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Oct 16, 2020
1 parent 9808897 commit 3ffbed3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
5 changes: 4 additions & 1 deletion python/core/auto_generated/qgsfields.sip.in
Expand Up @@ -291,10 +291,13 @@ Utility function to return a list of QgsField instances
bool operator==( const QgsFields &other ) const;
bool operator!=( const QgsFields &other ) const;

QIcon iconForField( int fieldIdx ) const /Factory/;
QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const /Factory/;
%Docstring
Returns an icon corresponding to a field index, based on the field's type and source

:param fieldIdx: the field index
:param considerOrigin: if ``True`` the icon will the origin of the field

.. versionadded:: 2.14
%End
%MethodCode
Expand Down
19 changes: 17 additions & 2 deletions src/core/qgsfields.cpp
Expand Up @@ -272,9 +272,24 @@ QgsFields::iterator QgsFields::end()
return iterator( &d->fields.last() + 1 );
}

QIcon QgsFields::iconForField( int fieldIdx ) const
QIcon QgsFields::iconForField( int fieldIdx, bool considerOrigin ) const
{
return QgsFields::iconForFieldType( d->fields.at( fieldIdx ).field.type() );
if ( considerOrigin )
{
switch ( fieldOrigin( fieldIdx ) )
{
case QgsFields::OriginExpression:
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) );
break;

case QgsFields::OriginJoin:
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/join.svg" ) );

default:
return iconForFieldType( d->fields.at( fieldIdx ).field.type() );
}
}
return iconForFieldType( d->fields.at( fieldIdx ).field.type() );
}

QIcon QgsFields::iconForFieldType( const QVariant::Type &type )
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsfields.h
Expand Up @@ -321,9 +321,11 @@ class CORE_EXPORT QgsFields

/**
* Returns an icon corresponding to a field index, based on the field's type and source
* \param fieldIdx the field index
* \param considerOrigin if TRUE the icon will the origin of the field
* \since QGIS 2.14
*/
QIcon iconForField( int fieldIdx ) const SIP_FACTORY;
QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
#ifdef SIP_RUN
% MethodCode
if ( a0 < 0 || a0 >= sipCpp->count() )
Expand Down
6 changes: 3 additions & 3 deletions src/gui/vector/qgsattributesformproperties.cpp
Expand Up @@ -102,7 +102,7 @@ void QgsAttributesFormProperties::initAvailableWidgetsTree()

FieldConfig cfg( mLayer, i );

QTreeWidgetItem *item = mAvailableWidgetsTree->addItem( catitem, itemData );
QTreeWidgetItem *item = mAvailableWidgetsTree->addItem( catitem, itemData, -1, fields.iconForField( i, true ) );

item->setData( 0, FieldConfigRole, cfg );
item->setData( 0, FieldNameRole, field.name() );
Expand Down Expand Up @@ -937,7 +937,7 @@ QgsAttributesDnDTree::QgsAttributesDnDTree( QgsVectorLayer *layer, QWidget *pare
connect( this, &QTreeWidget::itemDoubleClicked, this, &QgsAttributesDnDTree::onItemDoubleClicked );
}

QTreeWidgetItem *QgsAttributesDnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data, int index )
QTreeWidgetItem *QgsAttributesDnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data, int index, const QIcon &icon )
{
QTreeWidgetItem *newItem = new QTreeWidgetItem( QStringList() << data.name() );

Expand All @@ -961,6 +961,7 @@ QTreeWidgetItem *QgsAttributesDnDTree::addItem( QTreeWidgetItem *parent, QgsAttr

newItem->setData( 0, QgsAttributesFormProperties::DnDTreeRole, data );
newItem->setText( 0, data.displayName() );
newItem->setIcon( 0, icon );

if ( index < 0 )
parent->addChild( newItem );
Expand Down Expand Up @@ -1500,4 +1501,3 @@ void QgsAttributesFormProperties::DnDTreeItemData::setBackgroundColor( const QCo
{
mBackgroundColor = backgroundColor;
}

2 changes: 1 addition & 1 deletion src/gui/vector/qgsattributesformproperties.h
Expand Up @@ -273,7 +273,7 @@ class GUI_EXPORT QgsAttributesDnDTree : public QTreeWidget
* Adds a new item to a \a parent. If \a index is -1, the item is added to the end of the parent's existing children.
* Otherwise it is inserted at the specified \a index.
*/
QTreeWidgetItem *addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data, int index = -1 );
QTreeWidgetItem *addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data, int index = -1, const QIcon &icon = QIcon() );
QTreeWidgetItem *addContainer( QTreeWidgetItem *parent, const QString &title, int columnCount );

enum Type
Expand Down
16 changes: 1 addition & 15 deletions src/gui/vector/qgssourcefieldsproperties.cpp
Expand Up @@ -198,21 +198,7 @@ void QgsSourceFieldsProperties::setRow( int row, int idx, const QgsField &field
{
QTableWidgetItem *dataItem = new QTableWidgetItem();
dataItem->setData( Qt::DisplayRole, idx );

switch ( mLayer->fields().fieldOrigin( idx ) )
{
case QgsFields::OriginExpression:
dataItem->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
break;

case QgsFields::OriginJoin:
dataItem->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/join.svg" ) ) );
break;

default:
dataItem->setIcon( mLayer->fields().iconForField( idx ) );
break;
}
dataItem->setIcon( mLayer->fields().iconForField( idx, true ) );
mFieldsList->setItem( row, AttrIdCol, dataItem );

// in case we insert and not append reindex remaining widgets by 1
Expand Down

0 comments on commit 3ffbed3

Please sign in to comment.