Commit 856d5fe0 authored by Francois Cartegnie's avatar Francois Cartegnie

Qt: PLModel: Use Model Indexes instead of PLItem references for external

methods.

The model/view is broken as the internal PLItem elements are accessed
from outside the model.
We need to change calls to make use of Model Indexes, and then can move
away some methods that belongs to views.
parent c6a5ad0b
......@@ -280,17 +280,7 @@ void LocationBar::setIndex( const QModelIndex &index )
while( true )
{
PLItem *item = model->getItem( i );
QString text;
char *fb_name = input_item_GetTitle( item->inputItem() );
if( EMPTY_STR( fb_name ) )
{
free( fb_name );
fb_name = input_item_GetName( item->inputItem() );
}
text = qfu(fb_name);
free(fb_name);
QString text = model->getTitle( i );
QAbstractButton *btn = new LocationButton( text, first, !first, this );
btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
......@@ -300,7 +290,7 @@ void LocationBar::setIndex( const QModelIndex &index )
actions.append( action );
CONNECT( btn, clicked(), action, trigger() );
mapper->setMapping( action, item->id() );
mapper->setMapping( action, model->itemId( i ) );
CONNECT( action, triggered(), mapper, map() );
first = false;
......
......@@ -34,8 +34,11 @@ class PLItem
{
friend class PLModel;
public:
PLItem( playlist_item_t *, PLItem *parent );
~PLItem();
bool hasSameParent( PLItem *other ) { return parent() == other->parent(); }
bool operator< ( PLItem& );
protected:
PLItem( playlist_item_t *, PLItem *parent );
int row() const;
......@@ -51,9 +54,7 @@ public:
PLItem *parent() { return parentItem; }
input_item_t *inputItem() const { return p_input; }
int id() { return i_id; }
bool operator< ( PLItem& );
protected:
QList<PLItem*> children;
PLItem *parentItem;
int i_id;
......
......@@ -71,7 +71,7 @@ PLModel::PLModel( playlist_t *_p_playlist, /* THEPL */
p_playlist = _p_playlist;
i_cached_id = -1;
i_cached_input_id = -1;
i_popup_item = i_popup_parent = -1;
popupLauncherIndex = QModelIndex();
sortingMenu = NULL;
rootItem = NULL; /* PLItem rootItem, will be set in rebuild( ) */
......@@ -151,7 +151,7 @@ bool modelIndexLessThen( const QModelIndex &i1, const QModelIndex &i2 )
if( !i1.isValid() || !i2.isValid() ) return false;
PLItem *item1 = static_cast<PLItem*>( i1.internalPointer() );
PLItem *item2 = static_cast<PLItem*>( i2.internalPointer() );
if( item1->parent() == item2->parent() ) return i1.row() < i2.row();
if( item1->hasSameParent( item2 ) ) return i1.row() < i2.row();
else return *item1 < *item2;
}
......@@ -473,6 +473,46 @@ int PLModel::itemId( const QModelIndex &index ) const
return getItem( index )->id();
}
QString PLModel::getURI( const QModelIndex &index ) const
{
QString uri;
input_item_t *p_item = getItem( index )->inputItem();
/* no PL lock as item gets refcount +1 from PLItem, which only depends of events */
vlc_mutex_lock( &p_item->lock );
uri = QString( p_item->psz_uri );
vlc_mutex_unlock( &p_item->lock );
return uri;
}
QString PLModel::getTitle( const QModelIndex &index ) const
{
QString title;
input_item_t *p_item = getItem( index )->inputItem();
char *fb_name = input_item_GetTitle( p_item );
if( EMPTY_STR( fb_name ) )
{
free( fb_name );
fb_name = input_item_GetName( p_item );
}
title = qfu(fb_name);
free(fb_name);
return title;
}
bool PLModel::isCurrentItem( const QModelIndex &index, playLocation where ) const
{
if ( where == IN_PLAYLIST )
{
return itemId( index ) == THEPL->p_playing->i_id;
}
else if ( where == IN_MEDIALIBRARY )
{
return THEPL->p_media_library &&
itemId( index ) == THEPL->p_media_library->i_id;
}
return false;
}
QVariant PLModel::headerData( int section, Qt::Orientation orientation,
int role ) const
{
......@@ -503,6 +543,17 @@ QModelIndex PLModel::index( const int i_id, const int c )
return index( findById( rootItem, i_id ), c );
}
QModelIndex PLModel::rootIndex() const
{
return index( findById( rootItem, rootItem->id() ), 0 );
}
bool PLModel::isTree() const
{
return ( ( rootItem && rootItem->id() != p_playlist->p_playing->i_id )
|| var_InheritBool( p_intf, "playlist-tree" ) );
}
/* Return the index of a given item */
QModelIndex PLModel::index( PLItem *item, int column ) const
{
......@@ -927,19 +978,22 @@ void PLModel::recurseDelete( QList<PLItem*> children, QModelIndexList *fullList
/******* Volume III: Sorting and searching ********/
void PLModel::sort( const int column, Qt::SortOrder order )
{
sort( rootItem->id(), column, order );
sort( index( rootItem->id(), 0 ) , column, order );
}
void PLModel::sort( const int i_root_id, const int column, Qt::SortOrder order )
void PLModel::sort( QModelIndex rootIndex, const int column, Qt::SortOrder order )
{
msg_Dbg( p_intf, "Sorting by column %i, order %i", column, order );
int meta = columnToMeta( column );
if( meta == COLUMN_END ) return;
PLItem *item = findById( rootItem, i_root_id );
PLItem *item = ( rootIndex.isValid() ) ? getItem( rootIndex )
: rootItem;
if( !item ) return;
int i_root_id = item->id();
commitBufferedRowInserts();
QModelIndex qIndex = index( item, 0 );
......@@ -974,12 +1028,11 @@ void PLModel::sort( const int i_root_id, const int column, Qt::SortOrder order )
}
PL_UNLOCK;
/* if we have popup item, try to make sure that you keep that item visible */
if( i_popup_item > -1 )
if( popupLauncherIndex.isValid() )
{
PLItem *popupitem = findById( rootItem, i_popup_item );
if( popupitem ) emit currentIndexChanged( index( popupitem, 0 ) );
emit currentIndexChanged( popupLauncherIndex );
/* reset i_popup_item as we don't show it as selected anymore anyway */
i_popup_item = -1;
popupLauncherIndex = QModelIndex();
}
else if( currentIndex().isValid() ) emit currentIndexChanged( currentIndex() );
}
......@@ -1051,33 +1104,13 @@ void PLModel::ensureArtRequested( const QModelIndex &index )
/*********** Popup *********/
bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list )
{
int i_id = index.isValid() ? itemId( index ) : rootItem->id();
PL_LOCK;
playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
if( !p_item )
{
PL_UNLOCK;
return false;
}
i_popup_item = index.isValid() ? p_item->i_id : -1;
i_popup_parent = index.isValid() ?
( p_item->p_parent ? p_item->p_parent->i_id : -1 ) :
( rootItem->id() );
bool tree = ( rootItem && rootItem->id() != p_playlist->p_playing->i_id ) ||
var_InheritBool( p_intf, "playlist-tree" );
input_item_t *p_input = p_item->p_input;
vlc_gc_incref( p_input );
PL_UNLOCK;
popupLauncherIndex = index.isValid() ? index : QModelIndex();
/* */
QMenu menu;
/* Play/Stream/Info static actions */
if( i_popup_item > -1 )
if( index.isValid() )
{
menu.addAction( QIcon( ":/menu/play" ), qtr(I_POP_PLAY), this, SLOT( popupPlay() ) );
menu.addAction( QIcon( ":/menu/stream" ),
......@@ -1085,28 +1118,27 @@ bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QMode
menu.addAction( qtr(I_POP_SAVE), this, SLOT( popupSave() ) );
menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO), this, SLOT( popupInfo() ) );
menu.addSeparator();
if( p_input->psz_uri && !strncasecmp( p_input->psz_uri, "file://", 7 ) )
if( getURI( index ).startsWith( "file://" ) )
{
menu.addAction( QIcon( ":/type/folder-grey" ),
qtr( I_POP_EXPLORE ), this, SLOT( popupExplore() ) );
}
}
vlc_gc_decref( p_input );
/* In PL or ML, allow to add a file/folder */
if( canEdit() )
{
QIcon addIcon( ":/buttons/playlist/playlist_add" );
if( tree ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
if( isTree() ) menu.addAction( addIcon, qtr(I_POP_NEWFOLDER), this, SLOT( popupAddNode() ) );
menu.addSeparator();
if( rootItem->id() == THEPL->p_playing->i_id )
if( isCurrentItem( rootIndex(), PLModel::IN_PLAYLIST ) )
{
menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
menu.addAction( addIcon, qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
}
else if( THEPL->p_media_library &&
rootItem->id() == THEPL->p_media_library->i_id )
else if( isCurrentItem( rootIndex(), PLModel::IN_MEDIALIBRARY ) )
{
menu.addAction( addIcon, qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
menu.addAction( addIcon, qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
......@@ -1114,16 +1146,16 @@ bool PLModel::popup( const QModelIndex & index, const QPoint &point, const QMode
}
}
if( i_popup_item > -1 )
if( index.isValid() )
{
if( rootItem->id() != THEPL->p_playing->i_id )
if( !isCurrentItem( rootIndex(), PLModel::IN_PLAYLIST ) )
menu.addAction( qtr( "Add to playlist"), this, SLOT( popupAddToPlaylist() ) );
}
menu.addSeparator();
/* Item removal */
if( i_popup_item > -1 )
if( index.isValid() )
{
menu.addAction( QIcon( ":/buttons/playlist/playlist_remove" ),
qtr(I_POP_DEL), this, SLOT( popupDel() ) );
......@@ -1183,9 +1215,12 @@ void PLModel::popupPlay()
{
PL_LOCK;
{
playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
i_popup_item );
activateItem( p_item );
if ( popupLauncherIndex.isValid() )
{
playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
itemId( popupLauncherIndex ) );
activateItem( p_item );
}
}
PL_UNLOCK;
}
......@@ -1209,11 +1244,9 @@ void PLModel::popupAddToPlaylist()
void PLModel::popupInfo()
{
PL_LOCK;
playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
i_popup_item );
if( p_item )
if( popupLauncherIndex.isValid() )
{
input_item_t* p_input = p_item->p_input;
input_item_t* p_input = getItem( popupLauncherIndex )->inputItem();
vlc_gc_incref( p_input );
PL_UNLOCK;
MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
......@@ -1244,10 +1277,9 @@ void PLModel::popupExplore()
char *uri = NULL, *path = NULL;
PL_LOCK;
playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_popup_item );
if( p_item )
if( popupLauncherIndex.isValid() )
{
input_item_t *p_input = p_item->p_input;
input_item_t *p_input = getItem( popupLauncherIndex )->inputItem();
uri = input_item_GetURI( p_input );
}
PL_UNLOCK;
......@@ -1272,11 +1304,13 @@ void PLModel::popupAddNode()
QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
QLineEdit::Normal, QString(), &ok);
if( !ok || name.isEmpty() ) return;
if( !ok || name.isEmpty() || !popupLauncherIndex.isValid() ) return;
PL_LOCK;
QModelIndex index = popupLauncherIndex.parent();
if ( !index.isValid() ) index = rootIndex();
playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
i_popup_parent );
itemId( index ) );
if( p_item )
playlist_NodeCreate( p_playlist, qtu( name ), p_item, PLAYLIST_END, 0, NULL );
PL_UNLOCK;
......@@ -1284,7 +1318,9 @@ void PLModel::popupAddNode()
void PLModel::popupSort( int column )
{
sort( i_popup_parent,
QModelIndex index = popupLauncherIndex.parent();
if( !index.isValid() ) index = rootIndex();
sort( index,
column > 0 ? column - 1 : -column - 1,
column > 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
}
......
......@@ -98,8 +98,19 @@ public:
/* Lookups */
QModelIndex index( const int i_id, const int c );
QModelIndex rootIndex() const;
bool isTree() const;
bool canEdit() const;
virtual QModelIndex currentIndex() const;
int itemId( const QModelIndex &index ) const;
QString getURI( const QModelIndex &index ) const;
QString getTitle( const QModelIndex &index ) const;
enum playLocation
{
IN_PLAYLIST,
IN_MEDIALIBRARY
};
bool isCurrentItem( const QModelIndex &index, playLocation where ) const;
/* */
void search( const QString& search_text, const QModelIndex & root, bool b_recursive );
......@@ -109,13 +120,6 @@ public:
virtual bool popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list );
virtual void doDelete( QModelIndexList selected );
PLItem *getItem( const QModelIndex & index ) const
{
if( index.isValid() )
return static_cast<PLItem*>( index.internalPointer() );
else return rootItem;
}
signals:
void currentIndexChanged( const QModelIndex& );
void rootIndexChanged();
......@@ -144,6 +148,12 @@ private:
/* Custom model private methods */
/* Lookups */
PLItem *getItem( const QModelIndex & index ) const
{
if( index.isValid() )
return static_cast<PLItem*>( index.internalPointer() );
else return rootItem;
}
QStringList selectedURIs();
QModelIndex index( PLItem *, const int c ) const;
bool isCurrent( const QModelIndex &index ) const;
......@@ -165,10 +175,10 @@ private:
void dropMove( const PlMimeData * data, PLItem *target, int new_pos );
/* */
void sort( const int i_root_id, const int column, Qt::SortOrder order );
void sort( QModelIndex rootIndex, const int column, Qt::SortOrder order );
/* Popup */
int i_popup_item, i_popup_parent;
QModelIndex popupLauncherIndex;
QModelIndexList current_selection;
QMenu *sortingMenu;
QSignalMapper *sortingMapper;
......@@ -177,7 +187,6 @@ private:
PLItem *findById( PLItem *, int ) const;
PLItem *findByInput( PLItem *, int ) const;
PLItem *findInner(PLItem *, int , bool ) const;
bool canEdit() const;
PLItem *p_cached_item;
PLItem *p_cached_item_bi;
......
......@@ -502,9 +502,7 @@ QRect PictureFlowSoftwareRenderer::renderSlide(const SlideInfo &slide, int col1,
index = ((PLModel*)state->model)->index( slide.slideIndex, 0, state->model->currentIndex().parent() );
if( !index.isValid() )
return QRect();
PLItem *item = static_cast<PLItem*>( index.internalPointer() );
artURL = InputManager::decodeArtURL( item->inputItem() );
artURL = plm->data( index, COLUMN_COVER ).toString();
}
#ifdef MEDIA_LIBRARY
else if( mlm != 0 )
......
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