]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/playlist_model.cpp
Qt Imageset update.
[vlc] / modules / gui / qt4 / components / playlist / playlist_model.cpp
index 20a8c2014b9e7886d906b6a3e9c93a996b95a754..74c1b50c8f6f9a4cd6e49a3b238bf55986ff7c1c 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <assert.h>
-#include <QIcon>
-#include <QFont>
-#include <QMenu>
-#include <QApplication>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include "qt4.hpp"
 #include "components/playlist/playlist_model.hpp"
 #include "dialogs/mediainfo.hpp"
 #include <vlc_intf_strings.h>
 
-#include "pixmaps/type_unknown.xpm"
+#include "pixmaps/types/type_unknown.xpm"
+
+#include <assert.h>
+#include <QIcon>
+#include <QFont>
+#include <QMenu>
+#include <QApplication>
+#include <QSettings>
+
+#include "sorting.h"
 
-#define DEPTH_PL -1
-#define DEPTH_SEL 1
 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
 
 static int PlaylistChanged( vlc_object_t *, const char *,
@@ -49,221 +54,6 @@ static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
                         vlc_value_t oval, vlc_value_t nval, void *param );
 
-/*************************************************************************
- * Playlist item implementation
- *************************************************************************/
-
-/*
-   Playlist item is just a wrapper, an abstraction of the playlist_item
-   in order to be managed by PLModel
-
-   PLItem have a parent, and id and a input Id
-*/
-
-
-void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
-{
-    parentItem = parent;          /* Can be NULL, but only for the rootItem */
-    i_id       = _i_id;           /* Playlist item specific id */
-    i_input_id = _i_input_id;     /* Identifier of the input */
-    model      = m;               /* PLModel (QAbsmodel) */
-    i_type     = -1;              /* Item type - Avoid segfault */
-    b_current  = false;
-
-    assert( model );
-
-    /* No parent, should be the main one */
-    if( parentItem == NULL )
-    {
-        i_showflags = config_GetInt( model->p_intf, "qt-pl-showflags" );
-        updateview();
-    }
-    else
-    {
-        i_showflags = parentItem->i_showflags;
-        //Add empty string and update() handles data appending
-        item_col_strings.append( "" );
-    }
-    msg_Dbg( model->p_intf, "PLItem created of type: %i", model->i_depth );
-}
-
-/*
-   Constructors
-   Call the above function init
-   So far the first constructor isn't used...
-   */
-PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
-{
-    init( _i_id, _i_input_id, parent, m );
-}
-
-PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
-{
-    init( p_item->i_id, p_item->p_input->i_id, parent, m );
-}
-
-PLItem::~PLItem()
-{
-    qDeleteAll( children );
-    children.clear();
-}
-
-/* Column manager */
-void PLItem::updateview()
-{
-    item_col_strings.clear();
-
-    if( model->i_depth == 1 )  /* Selector Panel */
-    {
-        item_col_strings.append( "" );
-        return;
-    }
-
-    for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
-    {
-        if( i_showflags & i_index )
-        {
-            switch( i_index )
-            {
-            case VLC_META_ENGINE_ARTIST:
-                item_col_strings.append( qtr( VLC_META_ARTIST ) );
-                break;
-            case VLC_META_ENGINE_TITLE:
-                item_col_strings.append( qtr( VLC_META_TITLE ) );
-                break;
-            case VLC_META_ENGINE_DESCRIPTION:
-                item_col_strings.append( qtr( VLC_META_DESCRIPTION ) );
-                break;
-            case VLC_META_ENGINE_DURATION:
-                item_col_strings.append( qtr( "Duration" ) );
-                break;
-            case VLC_META_ENGINE_GENRE:
-                item_col_strings.append( qtr( VLC_META_GENRE ) );
-                break;
-            case VLC_META_ENGINE_COLLECTION:
-                item_col_strings.append( qtr( VLC_META_COLLECTION ) );
-                break;
-            case VLC_META_ENGINE_SEQ_NUM:
-                item_col_strings.append( qtr( VLC_META_SEQ_NUM ) );
-                break;
-            case VLC_META_ENGINE_RATING:
-                item_col_strings.append( qtr( VLC_META_RATING ) );
-                break;
-            default:
-                break;
-            }
-        }
-    }
-}
-
-/* So far signal is always true.
-   Using signal false would not call PLModel... Why ?
- */
-void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
-{
-    if( signal )
-        model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
-    children.insert( i_pos, item );
-    if( signal )
-        model->endInsertRows();
-}
-
-void PLItem::remove( PLItem *removed )
-{
-    if( model->i_depth == DEPTH_SEL || parentItem )
-    {
-        int i_index = parentItem->children.indexOf( removed );
-        model->beginRemoveRows( model->index( parentItem, 0 ),
-                                i_index, i_index );
-        parentItem->children.removeAt( i_index );
-        model->endRemoveRows();
-    }
-}
-
-/* This function is used to get one's parent's row number in the model */
-int PLItem::row() const
-{
-    if( parentItem )
-        return parentItem->children.indexOf( const_cast<PLItem*>(this) ); // Why this ? I don't think we ever inherit PLItem
-    return 0;
-}
-
-/* update the PL Item, get the good names and so on */
-void PLItem::update( playlist_item_t *p_item, bool iscurrent )
-{
-    char psz_duration[MSTRTIME_MAX_SIZE];
-    char *psz_meta;
-
-    assert( p_item->p_input->i_id == i_input_id );
-
-    i_type = p_item->p_input->i_type;
-    b_current = iscurrent;
-
-    item_col_strings.clear();
-
-    if( model->i_depth == 1 )  /* Selector Panel */
-    {
-        item_col_strings.append( qfu( p_item->p_input->psz_name ) );
-        return;
-    }
-
-#define ADD_META( item, meta ) \
-    psz_meta = input_item_Get ## meta ( item->p_input ); \
-    item_col_strings.append( qfu( psz_meta ) ); \
-    free( psz_meta );
-
-    for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
-    {
-        if( parentItem->i_showflags & i_index )
-        {
-            switch( i_index )
-            {
-            case VLC_META_ENGINE_ARTIST:
-                ADD_META( p_item, Artist );
-                break;
-            case VLC_META_ENGINE_TITLE:
-                char *psz_title, *psz_name;
-                psz_title = input_item_GetTitle( p_item->p_input );
-                psz_name = input_item_GetName( p_item->p_input );
-                if( psz_title )
-                {
-                    ADD_META( p_item, Title );
-                }
-                else if( psz_name )
-                {
-                    item_col_strings.append( qfu( psz_name ) );
-                }
-                free( psz_title );
-                free( psz_name );
-                break;
-            case VLC_META_ENGINE_DESCRIPTION:
-                ADD_META( p_item, Description );
-                break;
-            case VLC_META_ENGINE_DURATION:
-                secstotimestr( psz_duration,
-                    input_item_GetDuration( p_item->p_input ) / 1000000 );
-                item_col_strings.append( QString( psz_duration ) );
-                break;
-            case VLC_META_ENGINE_GENRE:
-                ADD_META( p_item, Genre );
-                break;
-            case VLC_META_ENGINE_COLLECTION:
-                ADD_META( p_item, Album );
-                break;
-            case VLC_META_ENGINE_SEQ_NUM:
-                ADD_META( p_item, TrackNum );
-                break;
-            case VLC_META_ENGINE_RATING:
-                ADD_META( p_item, Rating );
-            default:
-                break;
-            }
-        }
-
-    }
-#undef ADD_META
-}
-
 /*************************************************************************
  * Playlist model implementation
  *************************************************************************/
@@ -284,8 +74,6 @@ PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
     assert( i_depth == DEPTH_SEL || i_depth == DEPTH_PL );
     p_intf            = _p_intf;
     p_playlist        = _p_playlist;
-    i_items_to_append = 0;
-    b_need_update     = false;
     i_cached_id       = -1;
     i_cached_input_id = -1;
     i_popup_item      = i_popup_parent = -1;
@@ -295,22 +83,23 @@ PLModel::PLModel( playlist_t *_p_playlist,  /* THEPL */
     /* Icons initialization */
 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( x ) )
     ADD_ICON( UNKNOWN , type_unknown_xpm );
-    ADD_ICON( FILE, ":/pixmaps/type_file.png" );
-    ADD_ICON( DIRECTORY, ":/pixmaps/type_directory.png" );
-    ADD_ICON( DISC, ":/pixmaps/disc_16px.png" );
-    ADD_ICON( CDDA, ":/pixmaps/cdda_16px.png" );
-    ADD_ICON( CARD, ":/pixmaps/capture-card_16px.png" );
-    ADD_ICON( NET, ":/pixmaps/type_net.png" );
-    ADD_ICON( PLAYLIST, ":/pixmaps/type_playlist.png" );
-    ADD_ICON( NODE, ":/pixmaps/type_node.png" );
+    ADD_ICON( FILE, ":/type_file" );
+    ADD_ICON( DIRECTORY, ":/type_directory" );
+    ADD_ICON( DISC, ":/disc" );
+    ADD_ICON( CDDA, ":/cdda" );
+    ADD_ICON( CARD, ":/capture-card" );
+    ADD_ICON( NET, ":/type_net" );
+    ADD_ICON( PLAYLIST, ":/type_playlist" );
+    ADD_ICON( NODE, ":/type_node" );
 #undef ADD_ICON
 
-    addCallbacks();
     rebuild( p_root );
 }
 
 PLModel::~PLModel()
 {
+    QSettings settings( "vlc", "vlc-qt-interface" );
+    settings.setValue( "qt-pl-showflags", rootItem->i_showflags );
     delCallbacks();
     delete rootItem;
 }
@@ -379,9 +168,9 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
             PL_LOCK;
             playlist_item_t *p_target =
                         playlist_ItemGetById( p_playlist, targetItem->i_id,
-                                              VLC_TRUE );
+                                              pl_Locked );
             playlist_item_t *p_src = playlist_ItemGetById( p_playlist, srcId,
-                                                           VLC_TRUE );
+                                                           pl_Locked );
 
             if( !p_target || !p_src )
             {
@@ -394,7 +183,7 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
                 assert( parentItem );
                 playlist_item_t *p_parent =
                          playlist_ItemGetById( p_playlist, parentItem->i_id,
-                                               VLC_TRUE );
+                                               pl_Locked );
                 if( !p_parent )
                 {
                     PL_UNLOCK;
@@ -471,7 +260,7 @@ void PLModel::activateItem( const QModelIndex &index )
     assert( item );
     PL_LOCK;
     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
-                                                    VLC_TRUE);
+                                                    pl_Locked );
     activateItem( p_item );
     PL_UNLOCK;
 }
@@ -487,7 +276,7 @@ void PLModel::activateItem( playlist_item_t *p_item )
         p_parent = p_parent->p_parent;
     }
     if( p_parent )
-        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
+        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
                           p_parent, p_item );
 }
 
@@ -627,17 +416,17 @@ bool PLModel::hasLoop()
 }
 void PLModel::setLoop( bool on )
 {
-    var_SetBool( p_playlist, "loop", on ? VLC_TRUE:VLC_FALSE );
+    var_SetBool( p_playlist, "loop", on ? true:false );
     config_PutInt( p_playlist, "loop", on ? 1: 0 );
 }
 void PLModel::setRepeat( bool on )
 {
-    var_SetBool( p_playlist, "repeat", on ? VLC_TRUE:VLC_FALSE );
+    var_SetBool( p_playlist, "repeat", on ? true:false );
     config_PutInt( p_playlist, "repeat", on ? 1: 0 );
 }
 void PLModel::setRandom( bool on )
 {
-    var_SetBool( p_playlist, "random", on ? VLC_TRUE:VLC_FALSE );
+    var_SetBool( p_playlist, "random", on ? true:false );
     config_PutInt( p_playlist, "random", on ? 1: 0 );
 }
 
@@ -734,7 +523,11 @@ void PLModel::ProcessInputItemUpdate( int i_input_id )
     if( i_input_id <= 0 ) return;
     PLItem *item = FindByInput( rootItem, i_input_id );
     if( item )
+    {
+        QPL_LOCK;
         UpdateTreeItem( item, true );
+        QPL_UNLOCK;
+    }
 }
 
 void PLModel::ProcessItemRemoval( int i_id )
@@ -750,14 +543,12 @@ void PLModel::ProcessItemAppend( playlist_add_t *p_add )
 {
     playlist_item_t *p_item = NULL;
     PLItem *newItem = NULL;
-    i_items_to_append--;
-    if( b_need_update ) return;
 
     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
     PL_LOCK;
     if( !nodeItem ) goto end;
 
-    p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
+    p_item = playlist_ItemGetById( p_playlist, p_add->i_item, pl_Locked );
     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
     if( i_depth == DEPTH_SEL && p_item->p_parent &&
                         p_item->p_parent->i_id != rootItem->i_id )
@@ -799,7 +590,7 @@ void PLModel::rebuild( playlist_item_t *p_root )
     }
     if( p_root )
     {
-        //if( rootItem ) delete rootItem;
+        delete rootItem;
         rootItem = new PLItem( p_root, NULL, this );
     }
     assert( rootItem );
@@ -826,7 +617,7 @@ void PLModel::rebuild( playlist_item_t *p_root )
 void PLModel::UpdateNodeChildren( PLItem *root )
 {
     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id,
-                                                    VLC_TRUE );
+                                                    pl_Locked );
     UpdateNodeChildren( p_node, root );
 }
 
@@ -848,7 +639,7 @@ void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
 {
     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
-                                                    VLC_TRUE );
+                                                    pl_Locked );
     UpdateTreeItem( p_item, item, signal, force );
 }
 
@@ -908,15 +699,15 @@ void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
 
     PL_LOCK;
     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
-                                                    VLC_TRUE );
+                                                    pl_Locked );
     if( !p_item )
     {
         PL_UNLOCK; return;
     }
     if( p_item->i_children == -1 )
-        playlist_DeleteFromInput( p_playlist, item->i_input_id, VLC_TRUE );
+        playlist_DeleteFromInput( p_playlist, item->i_input_id, pl_Locked );
     else
-        playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
+        playlist_NodeDelete( p_playlist, p_item, true, false );
     /* And finally, remove it from the tree */
     item->remove( item );
     PL_UNLOCK;
@@ -925,23 +716,55 @@ void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
 /******* Volume III: Sorting and searching ********/
 void PLModel::sort( int column, Qt::SortOrder order )
 {
+    int i_index = -1;
+    int i_flag = 0;
+
+    // FIXME: Disable sorting on startup by ignoring
+    // first call of sorting caused by showing dialog
+    // see: standardpanel.cpp:65
+    static bool b_first_time = true;
+    if( b_first_time )
+    {
+        b_first_time = false;
+        return;
+    }
+
+#define CHECK_COLUMN( meta )                        \
+{                                                   \
+    if( ( shownFlags() & meta ) )                   \
+        i_index++;                                  \
+    if( column == i_index )                         \
+    {                                               \
+        i_flag = meta;                              \
+        goto next;                                  \
+    }                                               \
+}
+
+    CHECK_COLUMN( COLUMN_NUMBER );
+    CHECK_COLUMN( COLUMN_TITLE );
+    CHECK_COLUMN( COLUMN_DURATION );
+    CHECK_COLUMN( COLUMN_ARTIST );
+    CHECK_COLUMN( COLUMN_GENRE );
+    CHECK_COLUMN( COLUMN_ALBUM );
+    CHECK_COLUMN( COLUMN_TRACK_NUMBER );
+    CHECK_COLUMN( COLUMN_DESCRIPTION );
+
+#undef CHECK_COLUMN
+
+next:
     PL_LOCK;
     {
         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
                                                         rootItem->i_id,
-                                                        VLC_TRUE );
-        int i_mode;
-        switch( column )
-        {
-        case 0: i_mode = SORT_TITLE_NODES_FIRST;break;
-        case 1: i_mode = SORT_ARTIST;break;
-        case 2: i_mode = SORT_DURATION; break;
-        default: i_mode = SORT_TITLE_NODES_FIRST; break;
-        }
+                                                        pl_Locked );
         if( p_root )
-            playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
+        {
+            playlist_RecursiveNodeSort( p_playlist, p_root,
+                                        i_column_sorting( i_flag ),
                                         order == Qt::AscendingOrder ?
                                             ORDER_NORMAL : ORDER_REVERSE );
+            p_playlist->b_reset_currently_playing = true;
+        }
     }
     PL_UNLOCK;
     rebuild();
@@ -954,7 +777,7 @@ void PLModel::search( QString search_text )
     {
         playlist_item_t *p_root = playlist_ItemGetById( p_playlist,
                                                         rootItem->i_id,
-                                                        VLC_TRUE );
+                                                        pl_Locked );
         assert( p_root );
         char *psz_name = search_text.toUtf8().data();
         playlist_LiveSearchUpdate( p_playlist , p_root, psz_name );
@@ -969,7 +792,7 @@ void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
     assert( index.isValid() );
     PL_LOCK;
     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
-                                                    itemId( index ), VLC_TRUE );
+                                                    itemId( index ), pl_Locked );
     if( p_item )
     {
         i_popup_item = p_item->i_id;
@@ -1003,54 +826,39 @@ void PLModel::popup( QModelIndex & index, QPoint &point, QModelIndexList list )
 
 void PLModel::viewchanged( int meta )
 {
-   if( rootItem )
-   {
-       int index=0;
-       switch( meta )
-       {
-       case VLC_META_ENGINE_TITLE:
-           index=0; break;
-       case VLC_META_ENGINE_DURATION:
-           index=1; break;
-       case VLC_META_ENGINE_ARTIST:
-           index=2; break;
-       case VLC_META_ENGINE_GENRE:
-           index=3; break;
-       case VLC_META_ENGINE_COPYRIGHT:
-           index=4; break;
-       case VLC_META_ENGINE_COLLECTION:
-           index=5; break;
-       case VLC_META_ENGINE_SEQ_NUM:
-           index=6; break;
-       case VLC_META_ENGINE_DESCRIPTION:
-           index=7; break;
-       default:
-           break;
-       }
-       /* UNUSED        emit layoutAboutToBeChanged(); */
-       index = __MIN( index , rootItem->item_col_strings.count() );
-       QModelIndex parent = createIndex( 0, 0, rootItem );
-
-       if( rootItem->i_showflags & meta )
-           /* Removing columns */
-       {
-           beginRemoveColumns( parent, index, index+1 );
-           rootItem->i_showflags &= ~( meta );
-           rootItem->updateview();
-           endRemoveColumns();
-       }
-       else
-       {
-           /* Adding columns */
-           beginInsertColumns( createIndex( 0, 0, rootItem), index, index+1 );
-           rootItem->i_showflags |= meta;
-           rootItem->updateview();
-           endInsertColumns();
-       }
-       rebuild();
-       config_PutInt( p_intf, "qt-pl-showflags", rootItem->i_showflags );
-       config_SaveConfigFile( p_intf, NULL );
-   }
+    assert( meta );
+    int _meta = meta;
+    if( rootItem )
+    {
+        int index=-1;
+        while( _meta )
+        {
+            index++;
+            _meta >>= 1;
+        }
+
+        /* UNUSED        emit layoutAboutToBeChanged(); */
+        index = __MIN( index, rootItem->item_col_strings.count() );
+        QModelIndex parent = createIndex( 0, 0, rootItem );
+
+        if( rootItem->i_showflags & meta )
+            /* Removing columns */
+        {
+            beginRemoveColumns( parent, index, index+1 );
+            rootItem->i_showflags &= ~( meta );
+            rootItem->updateColumnHeaders();
+            endRemoveColumns();
+        }
+        else
+        {
+            /* Adding columns */
+            beginInsertColumns( parent, index, index+1 );
+            rootItem->i_showflags |= meta;
+            rootItem->updateColumnHeaders();
+            endInsertColumns();
+        }
+        rebuild();
+    }
 }
 
 void PLModel::popupDel()
@@ -1062,7 +870,8 @@ void PLModel::popupPlay()
     PL_LOCK;
     {
         playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
-                                                        i_popup_item,VLC_TRUE );
+                                                        i_popup_item,
+                                                        pl_Locked );
         activateItem( p_item );
     }
     PL_UNLOCK;
@@ -1072,7 +881,7 @@ void PLModel::popupInfo()
 {
     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
                                                     i_popup_item,
-                                                    VLC_TRUE );
+                                                    pl_Unlocked );
     if( p_item )
     {
         MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_item->p_input );
@@ -1094,7 +903,7 @@ void PLModel::popupSave()
 #include <shellapi.h>
 void PLModel::popupExplore()
 {
-    ShellExecuteW( NULL, L"explore", L"C:\\", NULL, NULL, SW_SHOWNORMAL );
+    ShellExecute( NULL, "explore", "C:\\", NULL, NULL, SW_SHOWNORMAL );
 }
 #endif
 
@@ -1146,11 +955,6 @@ static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
 
-    if( ++p_model->i_items_to_append >= 50 )
-    {
-//        p_model->b_need_update = VLC_TRUE;
-//        return VLC_SUCCESS;
-    }
     PLEvent *event = new PLEvent(  p_add );
     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
     return VLC_SUCCESS;