Skip to content

Commit

Permalink
Drop fields in correct position in drag and drop designer
Browse files Browse the repository at this point in the history
Instead of always dropping fields at the end of the container,
insert them at the dropped location. Otherwise the drop indicator
line is misleading.
  • Loading branch information
nyalldawson committed Jan 16, 2018
1 parent fa120b5 commit a3a999e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/app/qgsattributesformproperties.cpp
Expand Up @@ -786,7 +786,7 @@ DnDTree::DnDTree( QgsVectorLayer *layer, QWidget *parent )
connect( this, &QTreeWidget::itemDoubleClicked, this, &DnDTree::onItemDoubleClicked );
}

QTreeWidgetItem *DnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data )
QTreeWidgetItem *DnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data, int index )
{
QTreeWidgetItem *newItem = new QTreeWidgetItem( QStringList() << data.name() );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
Expand All @@ -811,7 +811,10 @@ QTreeWidgetItem *DnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormPro
}
}
newItem->setData( 0, QgsAttributesFormProperties::DnDTreeRole, data );
parent->addChild( newItem );
if ( index < 0 )
parent->addChild( newItem );
else
parent->insertChild( index, newItem );

return newItem;
}
Expand Down Expand Up @@ -850,7 +853,6 @@ void DnDTree::dragMoveEvent( QDragMoveEvent *event )

bool DnDTree::dropMimeData( QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action )
{
Q_UNUSED( index )
bool bDropSuccessful = false;

if ( action == Qt::IgnoreAction )
Expand All @@ -869,12 +871,12 @@ bool DnDTree::dropMimeData( QTreeWidgetItem *parent, int index, const QMimeData

if ( parent )
{
addItem( parent, itemElement );
addItem( parent, itemElement, index );
bDropSuccessful = true;
}
else
{
addItem( invisibleRootItem(), itemElement );
addItem( invisibleRootItem(), itemElement, index );
bDropSuccessful = true;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/app/qgsattributesformproperties.h
Expand Up @@ -238,7 +238,12 @@ class DnDTree : public QTreeWidget

public:
explicit DnDTree( QgsVectorLayer *layer, QWidget *parent = nullptr );
QTreeWidgetItem *addItem( QTreeWidgetItem *parent, QgsAttributesFormProperties::DnDTreeItemData data );

/**
* 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 *addContainer( QTreeWidgetItem *parent, const QString &title, int columnCount );

enum Type
Expand Down

0 comments on commit a3a999e

Please sign in to comment.