Skip to content
Permalink
Browse files

add field icon in DnD tree (#39400)

  • Loading branch information
3nids committed Oct 16, 2020
1 parent 9808897 commit 3ffbed314eaa6a66a8191fe47f270fe5f75b41fb
@@ -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
@@ -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 )
@@ -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() )
@@ -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() );
@@ -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() );

@@ -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 );
@@ -1500,4 +1501,3 @@ void QgsAttributesFormProperties::DnDTreeItemData::setBackgroundColor( const QCo
{
mBackgroundColor = backgroundColor;
}

@@ -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
@@ -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

0 comments on commit 3ffbed3

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