Commit 57cace39 authored by Francois Cartegnie's avatar Francois Cartegnie

Qt: ML/PLItem: Factorize and clean.

parent f5714b1e
...@@ -87,11 +87,12 @@ MLItem::MLItem( const MLModel *p_model, ...@@ -87,11 +87,12 @@ MLItem::MLItem( const MLModel *p_model,
intf_thread_t* _p_intf, intf_thread_t* _p_intf,
ml_media_t *p_media, ml_media_t *p_media,
MLItem *p_parent ) MLItem *p_parent )
: p_intf( _p_intf ), model( p_model ), children(), parentItem( p_parent ) : p_intf( _p_intf ), model( p_model )
{ {
parentItem = p_parent;
if( p_media ) if( p_media )
ml_gc_incref( p_media ); ml_gc_incref( p_media );
this->media = p_media; media = p_media;
p_ml = ml_Get( _p_intf ); p_ml = ml_Get( _p_intf );
} }
...@@ -104,53 +105,21 @@ MLItem::~MLItem() ...@@ -104,53 +105,21 @@ MLItem::~MLItem()
clearChildren(); clearChildren();
} }
/** AbstractPLItem* MLItem::child( int row ) const
* @brief recursively delete all children of this node
* @note must be entered after the appropriate beginRemoveRows()
*/
void MLItem::clearChildren()
{
// Recursively delete all children
qDeleteAll( children );
children.clear();
}
MLItem* MLItem::child( int row ) const
{ {
if( row < 0 || row >= childCount() ) return NULL; if( row < 0 || row >= childCount() ) return NULL;
else return children.at( row ); else return children.at( row );
} }
void MLItem::addChild( MLItem *child, int row )
{
assert( child );
children.insert( row==-1 ? children.count() : row, child );
}
void MLItem::delChild( int row ) void MLItem::delChild( int row )
{ {
if( !childCount() ) return; // assert ? if( !childCount() ) return; // assert ?
MLItem *item = AbstractPLItem *item =
children.takeAt( ( row!=-1 ) ? row : ( children.count()-1 ) ); children.takeAt( ( row!=-1 ) ? row : ( children.count()-1 ) );
assert( item ); assert( item );
delete item; delete item;
} }
int MLItem::rowOfChild( MLItem *item ) const
{
return children.indexOf( item );
}
int MLItem::childCount() const
{
return children.count();
}
MLItem* MLItem::parent() const
{
return parentItem;
}
input_item_t* MLItem::inputItem() input_item_t* MLItem::inputItem()
{ {
return ml_CreateInputItem( p_ml, id() ); return ml_CreateInputItem( p_ml, id() );
......
...@@ -38,49 +38,40 @@ ...@@ -38,49 +38,40 @@
#include <vlc_interface.h> #include <vlc_interface.h>
#include <vlc_media_library.h> #include <vlc_media_library.h>
#include "playlist_item.hpp"
#include "ml_model.hpp" #include "ml_model.hpp"
#include "qt4.hpp" #include "qt4.hpp"
class MLModel; class MLModel;
class MLItem class MLItem : public AbstractPLItem
{ {
friend class MLModel; friend class MLModel;
public: public:
MLItem( const MLModel *p_model, intf_thread_t *_p_intf, MLItem( const MLModel *p_model, intf_thread_t *_p_intf,
ml_media_t *p_media, MLItem *p_parent ); ml_media_t *p_media, MLItem *p_parent );
virtual ~MLItem(); virtual ~MLItem();
bool operator<( MLItem* item );
protected: private:
void addChild( MLItem *child, int row = -1 ); /* AbstractPLItem */
void delChild( int row ); int id() const;
void clearChildren();
MLItem* child( int row ) const;
int childCount() const;
MLItem* parent() const;
input_item_t *inputItem(); input_item_t *inputItem();
AbstractPLItem* child( int row ) const;
/* Local */
void delChild( int row );
QVariant data( int column ) const; QVariant data( int column ) const;
bool setData( ml_select_e meta, const QVariant &data ); bool setData( ml_select_e meta, const QVariant &data );
int rowOfChild( MLItem *item ) const;
// Media structure connections // Media structure connections
int id() const;
ml_media_t* getMedia() const; ml_media_t* getMedia() const;
QUrl getUri() const; QUrl getUri() const;
bool operator<( MLItem* item );
private:
ml_media_t* media; ml_media_t* media;
intf_thread_t* p_intf; intf_thread_t* p_intf;
const MLModel *model; const MLModel *model;
media_library_t* p_ml; media_library_t* p_ml;
QList< MLItem* > children;
MLItem *parentItem;
}; };
#endif #endif
......
...@@ -35,6 +35,12 @@ ...@@ -35,6 +35,12 @@
* Playlist item implementation * Playlist item implementation
*************************************************************************/ *************************************************************************/
void AbstractPLItem::clearChildren()
{
qDeleteAll( children );
children.clear();
}
/* /*
Playlist item is just a wrapper, an abstraction of the playlist_item Playlist item is just a wrapper, an abstraction of the playlist_item
in order to be managed by PLModel in order to be managed by PLModel
...@@ -71,57 +77,40 @@ PLItem::~PLItem() ...@@ -71,57 +77,40 @@ PLItem::~PLItem()
children.clear(); children.clear();
} }
void PLItem::insertChild( PLItem *item, int i_pos )
{
children.insert( i_pos, item );
}
void PLItem::appendChild( PLItem *item )
{
children.insert( children.count(), item );
}
void PLItem::removeChild( PLItem *item ) void PLItem::removeChild( PLItem *item )
{ {
children.removeOne( item ); children.removeOne( item );
delete item; delete item;
} }
void PLItem::removeChildren()
{
qDeleteAll( children );
children.clear();
}
void PLItem::takeChildAt( int index ) void PLItem::takeChildAt( int index )
{ {
PLItem *child = children[index]; AbstractPLItem *child = children[index];
child->parentItem = NULL; child->parentItem = NULL;
children.removeAt( index ); children.removeAt( index );
} }
/* This function is used to get one's parent's row number in the model */ /* This function is used to get one's parent's row number in the model */
int PLItem::row() const int PLItem::row()
{ {
if( parentItem ) if( parentItem )
return parentItem->children.indexOf( const_cast<PLItem*>(this) ); return parentItem->indexOf( this );
// We don't ever inherit PLItem, yet, but it might come :D
return 0; return 0;
} }
bool PLItem::operator< ( PLItem& other ) bool PLItem::operator< ( PLItem& other )
{ {
PLItem *item1 = this; AbstractPLItem *item1 = this;
while( item1->parentItem ) while( item1->parentItem )
{ {
PLItem *item2 = &other; AbstractPLItem *item2 = &other;
while( item2->parentItem ) while( item2->parentItem )
{ {
if( item1 == item2->parentItem ) return true; if( item1 == item2->parentItem ) return true;
if( item2 == item1->parentItem ) return false; if( item2 == item1->parentItem ) return false;
if( item1->parentItem == item2->parentItem ) if( item1->parentItem == item2->parentItem )
return item1->parentItem->children.indexOf( item1 ) < return item1->parentItem->indexOf( item1 ) <
item1->parentItem->children.indexOf( item2 ); item1->parentItem->indexOf( item2 );
item2 = item2->parentItem; item2 = item2->parentItem;
} }
item1 = item1->parentItem; item1 = item1->parentItem;
......
...@@ -30,39 +30,54 @@ ...@@ -30,39 +30,54 @@
#include <QList> #include <QList>
class PLItem class AbstractPLItem
{ {
friend class PLItem; /* super ugly glue stuff */
friend class MLItem;
friend class PLModel; friend class PLModel;
friend class MLModel;
protected:
virtual int id() const = 0;
int childCount() const { return children.count(); }
int indexOf( AbstractPLItem *item ) const { return children.indexOf( item ); };
int lastIndexOf( AbstractPLItem *item ) const { return children.lastIndexOf( item ); };
AbstractPLItem *parent() { return parentItem; }
virtual input_item_t *inputItem() = 0;
void insertChild( AbstractPLItem *item, int pos = -1 ) { children.insert( pos, item ); }
void appendChild( AbstractPLItem *item ) { insertChild( item, children.count() ); } ;
virtual AbstractPLItem *child( int id ) const = 0;
void clearChildren();
QList<AbstractPLItem *> children;
AbstractPLItem *parentItem;
};
class PLItem : public AbstractPLItem
{
friend class PLModel;
public: public:
~PLItem(); ~PLItem();
bool hasSameParent( PLItem *other ) { return parent() == other->parent(); } bool hasSameParent( PLItem *other ) { return parent() == other->parent(); }
bool operator< ( PLItem& ); bool operator< ( PLItem& );
protected:
PLItem( playlist_item_t *, PLItem *parent );
int row() const; private:
/* AbstractPLItem */
int id() const { return i_id; };
input_item_t *inputItem() { return p_input; }
AbstractPLItem *child( int id ) const { return children.value( id ); };
void insertChild( PLItem *, int pos ); /* Local */
void appendChild( PLItem *item ); PLItem( playlist_item_t *, PLItem *parent );
int row();
void removeChild( PLItem * ); void removeChild( PLItem * );
void removeChildren();
void takeChildAt( int ); void takeChildAt( int );
PLItem *child( int row ) const { return children.value( row ); }
int childCount() const { return children.count(); }
PLItem *parent() { return parentItem; }
input_item_t *inputItem() const { return p_input; }
int id() { return i_id; }
QList<PLItem*> children;
PLItem *parentItem;
int i_id;
input_item_t *p_input;
private:
PLItem( playlist_item_t * ); PLItem( playlist_item_t * );
void init( playlist_item_t *, PLItem * ); void init( playlist_item_t *, PLItem * );
int i_id;
input_item_t *p_input;
}; };
#endif #endif
......
...@@ -147,11 +147,11 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const ...@@ -147,11 +147,11 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
qSort(list.begin(), list.end(), modelIndexLessThen); qSort(list.begin(), list.end(), modelIndexLessThen);
PLItem *item = NULL; AbstractPLItem *item = NULL;
foreach( const QModelIndex &index, list ) { foreach( const QModelIndex &index, list ) {
if( item ) if( item )
{ {
PLItem *testee = getItem( index ); AbstractPLItem *testee = getItem( index );
while( testee->parent() ) while( testee->parent() )
{ {
if( testee->parent() == item || if( testee->parent() == item ||
...@@ -164,7 +164,7 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const ...@@ -164,7 +164,7 @@ QMimeData *PLModel::mimeData( const QModelIndexList &indexes ) const
else else
item = getItem( index ); item = getItem( index );
plMimeData->appendItem( item->inputItem() ); plMimeData->appendItem( static_cast<PLItem*>(item)->inputItem() );
} }
return plMimeData; return plMimeData;
...@@ -247,7 +247,7 @@ void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row ) ...@@ -247,7 +247,7 @@ void PLModel::dropMove( const PlMimeData * plMimeData, PLItem *target, int row )
/* Better not try to move a node into itself. /* Better not try to move a node into itself.
Abort the whole operation in that case, Abort the whole operation in that case,
because it is ambiguous. */ because it is ambiguous. */
PLItem *climber = target; AbstractPLItem *climber = target;
while( climber ) while( climber )
{ {
if( climber == item ) if( climber == item )
...@@ -324,7 +324,7 @@ void PLModel::activateItem( playlist_item_t *p_item ) ...@@ -324,7 +324,7 @@ void PLModel::activateItem( playlist_item_t *p_item )
QVariant PLModel::data( const QModelIndex &index, const int role ) const QVariant PLModel::data( const QModelIndex &index, const int role ) const
{ {
if( !index.isValid() ) return QVariant(); if( !index.isValid() ) return QVariant();
const PLItem *item = getItem( index ); PLItem *item = getItem( index );
if( role == Qt::DisplayRole ) if( role == Qt::DisplayRole )
{ {
int metadata = columnToMeta( index.column() ); int metadata = columnToMeta( index.column() );
...@@ -512,7 +512,7 @@ QModelIndex PLModel::index( const int row, const int column, const QModelIndex & ...@@ -512,7 +512,7 @@ QModelIndex PLModel::index( const int row, const int column, const QModelIndex &
{ {
PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem; PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
PLItem *childItem = parentItem->child( row ); PLItem *childItem = static_cast<PLItem*>(parentItem->child( row ));
if( childItem ) if( childItem )
return createIndex( row, column, childItem ); return createIndex( row, column, childItem );
else else
...@@ -539,9 +539,9 @@ bool PLModel::isTree() const ...@@ -539,9 +539,9 @@ bool PLModel::isTree() const
QModelIndex PLModel::index( PLItem *item, int column ) const QModelIndex PLModel::index( PLItem *item, int column ) const
{ {
if( !item ) return QModelIndex(); if( !item ) return QModelIndex();
const PLItem *parent = item->parent(); AbstractPLItem *parent = item->parent();
if( parent ) if( parent )
return createIndex( parent->children.lastIndexOf( item ), return createIndex( parent->lastIndexOf( item ),
column, item ); column, item );
return QModelIndex(); return QModelIndex();
} }
...@@ -565,7 +565,7 @@ QModelIndex PLModel::parent( const QModelIndex &index ) const ...@@ -565,7 +565,7 @@ QModelIndex PLModel::parent( const QModelIndex &index ) const
return QModelIndex(); return QModelIndex();
} }
PLItem *parentItem = childItem->parent(); PLItem *parentItem = static_cast<PLItem*>(childItem->parent());
if( !parentItem || parentItem == rootItem ) return QModelIndex(); if( !parentItem || parentItem == rootItem ) return QModelIndex();
if( !parentItem->parent() ) if( !parentItem->parent() )
{ {
...@@ -577,7 +577,7 @@ QModelIndex PLModel::parent( const QModelIndex &index ) const ...@@ -577,7 +577,7 @@ QModelIndex PLModel::parent( const QModelIndex &index ) const
int PLModel::rowCount( const QModelIndex &parent ) const int PLModel::rowCount( const QModelIndex &parent ) const
{ {
const PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem; PLItem *parentItem = parent.isValid() ? getItem( parent ) : rootItem;
return parentItem->childCount(); return parentItem->childCount();
} }
...@@ -603,18 +603,19 @@ PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input ) const ...@@ -603,18 +603,19 @@ PLItem * PLModel::findInner( PLItem *root, int i_id, bool b_input ) const
else if( b_input && root->inputItem()->i_id == i_id ) else if( b_input && root->inputItem()->i_id == i_id )
return root; return root;
QList<PLItem *>::iterator it = root->children.begin(); QList<AbstractPLItem *>::iterator it = root->children.begin();
while ( it != root->children.end() ) while ( it != root->children.end() )
{ {
if( !b_input && (*it)->id() == i_id ) PLItem *item = static_cast<PLItem *>(*it);
return (*it); if( !b_input && item->id() == i_id )
return item;
else if( b_input && (*it)->inputItem()->i_id == i_id ) else if( b_input && item->inputItem()->i_id == i_id )
return (*it); return item;
if( (*it)->childCount() ) if( item->childCount() )
{ {
PLItem *childFound = findInner( (*it), i_id, b_input ); PLItem *childFound = findInner( item, i_id, b_input );
if( childFound ) if( childFound )
return childFound; return childFound;
} }
...@@ -759,8 +760,8 @@ void PLModel::processItemAppend( int i_item, int i_parent ) ...@@ -759,8 +760,8 @@ void PLModel::processItemAppend( int i_item, int i_parent )
/* Search for an already matching children */ /* Search for an already matching children */
if ( isBufferedForInsert( nodeParentItem, i_item ) ) return; if ( isBufferedForInsert( nodeParentItem, i_item ) ) return;
foreach( const PLItem *existing, nodeParentItem->children ) foreach( const AbstractPLItem *existing, nodeParentItem->children )
if( existing->i_id == i_item ) return; if( existing->id() == i_item ) return;
/* Find the child */ /* Find the child */
PL_LOCK; PL_LOCK;
...@@ -789,7 +790,7 @@ void PLModel::rebuild( playlist_item_t *p_root ) ...@@ -789,7 +790,7 @@ void PLModel::rebuild( playlist_item_t *p_root )
/* Invalidate cache */ /* Invalidate cache */
i_cached_id = i_cached_input_id = -1; i_cached_id = i_cached_input_id = -1;
if( rootItem ) rootItem->removeChildren(); if( rootItem ) rootItem->clearChildren();
PL_LOCK; PL_LOCK;
if( p_root ) // Can be NULL if( p_root ) // Can be NULL
...@@ -811,9 +812,9 @@ void PLModel::takeItem( PLItem *item ) ...@@ -811,9 +812,9 @@ void PLModel::takeItem( PLItem *item )
{ {
commitBufferedRowInserts(); commitBufferedRowInserts();
assert( item ); assert( item );
PLItem *parent = item->parent(); PLItem *parent = static_cast<PLItem*>(item->parent());
assert( parent ); assert( parent );
int i_index = parent->children.indexOf( item ); int i_index = parent->indexOf( item );
beginRemoveRows( index( parent, 0 ), i_index, i_index ); beginRemoveRows( index( parent, 0 ), i_index, i_index );
parent->takeChildAt( i_index ); parent->takeChildAt( i_index );
...@@ -844,8 +845,8 @@ void PLModel::removeItem( PLItem *item ) ...@@ -844,8 +845,8 @@ void PLModel::removeItem( PLItem *item )
i_cached_input_id = -1; i_cached_input_id = -1;
if( item->parent() ) { if( item->parent() ) {
int i = item->parent()->children.indexOf( item ); int i = item->parent()->indexOf( item );
beginRemoveRows( index( item->parent(), 0), i, i ); beginRemoveRows( index( static_cast<PLItem*>(item->parent()), 0), i, i );
item->parent()->children.removeAt(i); item->parent()->children.removeAt(i);
delete item; delete item;
endRemoveRows(); endRemoveRows();
...@@ -915,11 +916,11 @@ void PLModel::doDelete( QModelIndexList selected ) ...@@ -915,11 +916,11 @@ void PLModel::doDelete( QModelIndexList selected )
} }
} }
void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList ) void PLModel::recurseDelete( QList<AbstractPLItem*> children, QModelIndexList *fullList )
{ {
for( int i = children.count() - 1; i >= 0 ; i-- ) for( int i = children.count() - 1; i >= 0 ; i-- )
{ {
PLItem *item = children[i]; PLItem *item = static_cast<PLItem *>(children[i]);
if( item->childCount() ) if( item->childCount() )
recurseDelete( item->children, fullList ); recurseDelete( item->children, fullList );
fullList->removeAll( index( item, 0 ) ); fullList->removeAll( index( item, 0 ) );
...@@ -952,7 +953,7 @@ void PLModel::sort( QModelIndex caller, QModelIndex rootIndex, const int column, ...@@ -952,7 +953,7 @@ void PLModel::sort( QModelIndex caller, QModelIndex rootIndex, const int column,
if( count ) if( count )
{ {
beginRemoveRows( qIndex, 0, count - 1 ); beginRemoveRows( qIndex, 0, count - 1 );
item->removeChildren(); item->clearChildren();
endRemoveRows( ); endRemoveRows( );
} }
...@@ -1003,7 +1004,7 @@ void PLModel::search( const QString& search_text, const QModelIndex & idx, bool ...@@ -1003,7 +1004,7 @@ void PLModel::search( const QString& search_text, const QModelIndex & idx, bool
PLItem *searchRoot = getItem( idx ); PLItem *searchRoot = getItem( idx );
beginRemoveRows( idx, 0, searchRoot->childCount() - 1 ); beginRemoveRows( idx, 0, searchRoot->childCount() - 1 );
searchRoot->removeChildren(); searchRoot->clearChildren();
endRemoveRows(); endRemoveRows();
beginInsertRows( idx, 0, searchRoot->childCount() - 1 ); beginInsertRows( idx, 0, searchRoot->childCount() - 1 );
......
...@@ -157,7 +157,7 @@ private: ...@@ -157,7 +157,7 @@ private:
void updateTreeItem( PLItem * ); void updateTreeItem( PLItem * );
void removeItem ( PLItem * ); void removeItem ( PLItem * );
void removeItem( int ); void removeItem( int );
void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList ); void recurseDelete( QList<AbstractPLItem*> children, QModelIndexList *fullList );
void takeItem( PLItem * ); //will not delete item void takeItem( PLItem * ); //will not delete item
void insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos ); void insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos );
/* ...of which the following will not update the views */ /* ...of which the following will not update the views */
......
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