[vlc-commits] commit: Qt: use input_item_t* for drag-and-drop data (Jakob Leben )
git at videolan.org
git at videolan.org
Mon Mar 8 19:27:50 CET 2010
vlc | branch: master | Jakob Leben <jleben at videolan.org> | Mon Mar 8 19:16:59 2010 +0100| [61e1f02d91b431e23d379f0c62f1956d8e3a8637] | committer: Jakob Leben
Qt: use input_item_t* for drag-and-drop data
A helper object keeps reference count until drag-and-drop is finished.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=61e1f02d91b431e23d379f0c62f1956d8e3a8637
---
.../gui/qt4/components/playlist/playlist_model.cpp | 93 ++++++++++++++-----
.../gui/qt4/components/playlist/playlist_model.hpp | 18 ++++-
modules/gui/qt4/components/playlist/selector.cpp | 31 +++----
3 files changed, 98 insertions(+), 44 deletions(-)
diff --git a/modules/gui/qt4/components/playlist/playlist_model.cpp b/modules/gui/qt4/components/playlist/playlist_model.cpp
index d983b9a..33d3e0f 100644
--- a/modules/gui/qt4/components/playlist/playlist_model.cpp
+++ b/modules/gui/qt4/components/playlist/playlist_model.cpp
@@ -137,7 +137,7 @@ Qt::ItemFlags PLModel::flags( const QModelIndex &index ) const
QStringList PLModel::mimeTypes() const
{
QStringList types;
- types << "vlc/qt-playlist-item";
+ types << "vlc/qt-input-items";
return types;
}
@@ -152,9 +152,7 @@ bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
{
- QMimeData *mimeData = new QMimeData();
- QByteArray encodedData;
- QDataStream stream( &encodedData, QIODevice::WriteOnly );
+ PlMimeData *plMimeData = new PlMimeData();
QModelIndexList list;
foreach( const QModelIndex &index, indexes ) {
@@ -181,17 +179,17 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
else
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 */
bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
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 )
return true;
@@ -217,36 +215,34 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
copy = true;
PL_UNLOCK;
- QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
if( copy )
- dropAppendCopy( encodedData, getItem( parent ) );
+ dropAppendCopy( data, getItem( parent ) );
else
- dropMove( encodedData, getItem( parent ), row );
+ dropMove( data, getItem( parent ), row );
}
return true;
}
-void PLModel::dropAppendCopy( QByteArray& data, PLItem *target )
+void PLModel::dropAppendCopy( const QMimeData *data, PLItem *target )
{
- QDataStream stream( &data, QIODevice::ReadOnly );
-
PL_LOCK;
+
playlist_item_t *p_parent =
- playlist_ItemGetById( p_playlist, target->i_id );
+ playlist_ItemGetByInput( p_playlist, target->p_input );
if( !p_parent ) return;
bool b_flat = p_parent == p_playlist->p_playing &&
!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;
- stream >> i_id;
- playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
+ playlist_item_t *p_item = playlist_ItemGetByInput( p_playlist, p_input );
if( !p_item ) continue;
recursiveAppendCopy( p_playlist, p_item, p_parent, b_flat );
}
+
PL_UNLOCK;
}
@@ -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 );
}
-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;
int new_pos = row == -1 ? target->children.size() : row;
int model_pos = new_pos;
- while( !stream.atEnd() )
+ foreach( input_item_t *p_input, inputItems )
{
- int i_id;
- stream >> i_id;
- PLItem *item = findById( rootItem, i_id );
+ PLItem *item = findByInput( rootItem, p_input->i_id );
if( !item ) continue;
/* Better not try to move a node into itself.
@@ -1138,3 +1132,52 @@ void PLModel::popupSort( int column )
column > 0 ? column - 1 : -column - 1,
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;
+}
diff --git a/modules/gui/qt4/components/playlist/playlist_model.hpp b/modules/gui/qt4/components/playlist/playlist_model.hpp
index 4db07d6..d8556f3 100644
--- a/modules/gui/qt4/components/playlist/playlist_model.hpp
+++ b/modules/gui/qt4/components/playlist/playlist_model.hpp
@@ -142,8 +142,8 @@ private:
/* Deep actions (affect core playlist) */
static void recursiveAppendCopy( playlist_t *, playlist_item_t *, playlist_item_t *, bool );
- void dropAppendCopy( QByteArray& data, PLItem *target );
- void dropMove( QByteArray& data, PLItem *target, int new_pos );
+ void dropAppendCopy( const QMimeData * data, PLItem *target );
+ void dropMove( const QMimeData * data, PLItem *target, int new_pos );
/* Popup */
int i_popup_item, i_popup_parent, i_popup_column;
@@ -177,4 +177,18 @@ private slots:
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
diff --git a/modules/gui/qt4/components/playlist/selector.cpp b/modules/gui/qt4/components/playlist/selector.cpp
index 9c52565..c23c0c3 100644
--- a/modules/gui/qt4/components/playlist/selector.cpp
+++ b/modules/gui/qt4/components/playlist/selector.cpp
@@ -334,7 +334,7 @@ void PLSelector::createItems()
QStringList PLSelector::mimeTypes() const
{
QStringList types;
- types << "vlc/qt-playlist-item";
+ types << "vlc/qt-input-items";
return types;
}
@@ -350,25 +350,22 @@ bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
if( i_truth != IS_PL && i_truth != IS_ML ) return false;
bool to_pl = ( i_truth == IS_PL );
- if( data->hasFormat( "vlc/qt-playlist-item" ) )
- {
- 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;
+ QList<input_item_t*> inputItems = PlMimeData::inputItems( data );
- playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_id );
- if( !p_item ) continue;
+ playlist_Lock( 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 );
+ foreach( input_item_t *p_input, inputItems )
+ {
+ playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
+ if( !p_item ) continue;
+
+ 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;
}
More information about the vlc-commits
mailing list