Commit 61e1f02d authored by Jakob Leben's avatar Jakob Leben

Qt: use input_item_t* for drag-and-drop data

A helper object keeps reference count until drag-and-drop is finished.
parent c518343c
...@@ -137,7 +137,7 @@ Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const ...@@ -137,7 +137,7 @@ Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
QStringList PLModel::mimeTypes() const QStringList PLModel::mimeTypes() const
{ {
QStringList types; QStringList types;
types << "vlc/qt-playlist-item"; types << "vlc/qt-input-items";
return types; return types;
} }
...@@ -152,9 +152,7 @@ bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 ) ...@@ -152,9 +152,7 @@ bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
{ {
QMimeData *mimeData = new QMimeData(); PlMimeData *plMimeData = new PlMimeData();
QByteArray encodedData;
QDataStream stream( &encodedData, QIODevice::WriteOnly );
QModelIndexList list; QModelIndexList list;
foreach( const QModelIndex &index, indexes ) { foreach( const QModelIndex &index, indexes ) {
...@@ -181,17 +179,17 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const ...@@ -181,17 +179,17 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
else else
item = getItem( index ); item = getItem( index );
stream << item->id(); plMimeData->appendItem( item->p_input );
} }
mimeData->setData( "vlc/qt-playlist-item", encodedData );
return mimeData; return plMimeData->mimeData();
} }
/* Drop operation */ /* Drop operation */
bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action, bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent ) int row, int column, const QModelIndex &parent )
{ {
if( data->hasFormat( "vlc/qt-playlist-item" ) ) if( data->hasFormat( "vlc/qt-input-items" ) )
{ {
if( action == Qt::IgnoreAction ) if( action == Qt::IgnoreAction )
return true; return true;
...@@ -217,36 +215,34 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action, ...@@ -217,36 +215,34 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
copy = true; copy = true;
PL_UNLOCK; PL_UNLOCK;
QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
if( copy ) if( copy )
dropAppendCopy( encodedData, getItem( parent ) ); dropAppendCopy( data, getItem( parent ) );
else else
dropMove( encodedData, getItem( parent ), row ); dropMove( data, getItem( parent ), row );
} }
return true; return true;
} }
void PLModel::dropAppendCopy( QByteArray& data, PLItem *target ) void PLModel::dropAppendCopy( const QMimeData *data, PLItem *target )
{ {
QDataStream stream( &data, QIODevice::ReadOnly );
PL_LOCK; PL_LOCK;
playlist_item_t *p_parent = playlist_item_t *p_parent =
playlist_ItemGetById( p_playlist, target->i_id ); playlist_ItemGetByInput( p_playlist, target->p_input );
if( !p_parent ) return; if( !p_parent ) return;
bool b_flat = p_parent == p_playlist->p_playing && bool b_flat = p_parent == p_playlist->p_playing &&
!var_InheritBool( p_intf, "playlist-tree" ); !var_InheritBool( p_intf, "playlist-tree" );
while( !stream.atEnd() ) QList<input_item_t*> inputItems = PlMimeData::inputItems( data );
foreach( input_item_t* p_input, inputItems )
{ {
int i_id; playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
stream >> i_id;
playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
if( !p_item ) continue; if( !p_item ) continue;
recursiveAppendCopy( p_playlist, p_item, p_parent, b_flat ); recursiveAppendCopy( p_playlist, p_item, p_parent, b_flat );
} }
PL_UNLOCK; PL_UNLOCK;
} }
...@@ -278,18 +274,16 @@ void PLModel::recursiveAppendCopy( playlist_t *p_playlist, playlist_item_t *sour ...@@ -278,18 +274,16 @@ void PLModel::recursiveAppendCopy( playlist_t *p_playlist, playlist_item_t *sour
recursiveAppendCopy( p_playlist, source->pp_children[i], target, b_flat ); recursiveAppendCopy( p_playlist, source->pp_children[i], target, b_flat );
} }
void PLModel::dropMove( QByteArray& data, PLItem *target, int row ) void PLModel::dropMove( const QMimeData * mimeData, PLItem *target, int row )
{ {
QDataStream stream( &data, QIODevice::ReadOnly ); QList<input_item_t*> inputItems = PlMimeData::inputItems( mimeData );
QList<PLItem*> model_items; QList<PLItem*> model_items;
int new_pos = row == -1 ? target->children.size() : row; int new_pos = row == -1 ? target->children.size() : row;
int model_pos = new_pos; int model_pos = new_pos;
while( !stream.atEnd() ) foreach( input_item_t *p_input, inputItems )
{ {
int i_id; PLItem *item = findByInput( rootItem, p_input->i_id );
stream >> i_id;
PLItem *item = findById( rootItem, i_id );
if( !item ) continue; if( !item ) continue;
/* Better not try to move a node into itself. /* Better not try to move a node into itself.
...@@ -1138,3 +1132,52 @@ void PLModel::popupSort( int column ) ...@@ -1138,3 +1132,52 @@ void PLModel::popupSort( int column )
column > 0 ? column - 1 : -column - 1, column > 0 ? column - 1 : -column - 1,
column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder ); column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
} }
/******************* Drag and Drop helper class ******************/
PlMimeData::PlMimeData( )
{
_mimeData = new QMimeData;
setParent( _mimeData );
}
PlMimeData::~PlMimeData()
{
foreach( input_item_t *p_item, _inputItems )
vlc_gc_decref( p_item );
}
void PlMimeData::appendItem( input_item_t *p_item )
{
vlc_gc_incref( p_item );
_inputItems.append( p_item );
}
QMimeData * PlMimeData::mimeData()
{
QByteArray encodedData;
input_item_t *pp_items[_inputItems.size()];
for( int i = 0; i < _inputItems.size() ; i++ )
pp_items[i] = _inputItems[i];
_mimeData->setData( "vlc/qt-input-items",
QByteArray( (char*) pp_items, _inputItems.size() * sizeof( input_item_t*) ) );
return _mimeData;
}
QList<input_item_t*> PlMimeData::inputItems( const QMimeData * mimeData )
{
QList<input_item_t*> list;
if( !mimeData->hasFormat( "vlc/qt-input-items" ) ) return list;
QByteArray encodedData = mimeData->data( "vlc/qt-input-items" );
input_item_t **pp_items = (input_item_t **) encodedData.data();
int i_items = encodedData.size() / sizeof( input_item_t* );
for( int i = 0; i < i_items; i++ )
list.append( pp_items[i] );
return list;
}
...@@ -142,8 +142,8 @@ private: ...@@ -142,8 +142,8 @@ private:
/* Deep actions (affect core playlist) */ /* Deep actions (affect core playlist) */
static void recursiveAppendCopy( playlist_t *, playlist_item_t *, playlist_item_t *, bool ); static void recursiveAppendCopy( playlist_t *, playlist_item_t *, playlist_item_t *, bool );
void dropAppendCopy( QByteArray& data, PLItem *target ); void dropAppendCopy( const QMimeData * data, PLItem *target );
void dropMove( QByteArray& data, PLItem *target, int new_pos ); void dropMove( const QMimeData * data, PLItem *target, int new_pos );
/* Popup */ /* Popup */
int i_popup_item, i_popup_parent, i_popup_column; int i_popup_item, i_popup_parent, i_popup_column;
...@@ -177,4 +177,18 @@ private slots: ...@@ -177,4 +177,18 @@ private slots:
void processItemAppend( int item, int parent ); void processItemAppend( int item, int parent );
}; };
class PlMimeData : public QObject
{
public:
PlMimeData();
~PlMimeData();
void appendItem( input_item_t *p_item );
QMimeData *mimeData();
static QList<input_item_t*> inputItems( const QMimeData * mimeData );
private:
QList<input_item_t*> _inputItems;
QMimeData *_mimeData;
};
#endif #endif
...@@ -334,7 +334,7 @@ void PLSelector::createItems() ...@@ -334,7 +334,7 @@ void PLSelector::createItems()
QStringList PLSelector::mimeTypes() const QStringList PLSelector::mimeTypes() const
{ {
QStringList types; QStringList types;
types << "vlc/qt-playlist-item"; types << "vlc/qt-input-items";
return types; return types;
} }
...@@ -350,25 +350,22 @@ bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index, ...@@ -350,25 +350,22 @@ bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
if( i_truth != IS_PL && i_truth != IS_ML ) return false; if( i_truth != IS_PL && i_truth != IS_ML ) return false;
bool to_pl = ( i_truth == IS_PL ); bool to_pl = ( i_truth == IS_PL );
if( data->hasFormat( "vlc/qt-playlist-item" ) ) QList<input_item_t*> inputItems = PlMimeData::inputItems( data );
{
QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
QDataStream stream( &encodedData, QIODevice::ReadOnly );
playlist_Lock( THEPL );
while( !stream.atEnd() )
{
int i_id;
stream >> i_id;
playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_id ); playlist_Lock( THEPL );
if( !p_item ) continue;
PLModel::recursiveAppendCopy( THEPL, p_item, foreach( input_item_t *p_input, inputItems )
to_pl ? THEPL->p_playing : THEPL->p_media_library, {
to_pl && !var_InheritBool( p_intf, "playlist-tree" ) ); playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
} if( !p_item ) continue;
playlist_Unlock( THEPL );
PLModel::recursiveAppendCopy( THEPL, p_item,
to_pl ? THEPL->p_playing : THEPL->p_media_library,
to_pl && !var_InheritBool( p_intf, "playlist-tree" ) );
} }
playlist_Unlock( THEPL );
return true; return true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment