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