#8859 AppFwk : Enable TableView as optional drop target

A TableView can receive drag/drop data. The drop operations finds the PdmObject for a row, and delegates handling of the drop operation to this object.
This commit is contained in:
Magne Sjaastad
2022-04-30 15:18:52 +02:00
parent 4ef1f73a58
commit feb1c417c5
5 changed files with 88 additions and 1 deletions

View File

@@ -57,8 +57,9 @@ namespace caf
//--------------------------------------------------------------------------------------------------
PdmUiTableViewQModel::PdmUiTableViewQModel( QWidget* parent )
: QAbstractTableModel( parent )
, m_pdmList( nullptr )
, m_dropTargetEnabled( false )
{
m_pdmList = nullptr;
}
//--------------------------------------------------------------------------------------------------
@@ -141,6 +142,12 @@ Qt::ItemFlags PdmUiTableViewQModel::flags( const QModelIndex& index ) const
}
}
}
if ( m_dropTargetEnabled )
{
flagMask |= Qt::ItemIsDropEnabled;
}
return flagMask;
}
@@ -661,6 +668,63 @@ void PdmUiTableViewQModel::createPersistentPushButtonWidgets( QTableView* tableV
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewQModel::enableDropTarget( bool enable )
{
m_dropTargetEnabled = enable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PdmUiTableViewQModel::dropMimeData( const QMimeData* data,
Qt::DropAction action,
int row,
int column,
const QModelIndex& parent )
{
// The receiving model index is specified by the parent. Find object and field from parent, and send QMimeData to
// this object
if ( !m_dropTargetEnabled ) return false;
auto pdmObject = pdmObjectForRow( parent.row() );
auto field = getField( parent );
if ( pdmObject )
{
pdmObject->handleDroppedMimeData( data, action, field );
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Qt::DropActions PdmUiTableViewQModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PdmUiTableViewQModel::canDropMimeData( const QMimeData* data,
Qt::DropAction action,
int row,
int column,
const QModelIndex& parent ) const
{
// This function can be extended to validate incoming QMimeData and see if this data is matching with the drop
// target. For now, all data is allowed to be dropped when drop target is enabled
return m_dropTargetEnabled;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------