]> git.sesse.net Git - vlc/commitdiff
Playlist model
authorClément Stenac <zorglub@videolan.org>
Sun, 25 Jun 2006 16:29:49 +0000 (16:29 +0000)
committerClément Stenac <zorglub@videolan.org>
Sun, 25 Jun 2006 16:29:49 +0000 (16:29 +0000)
modules/gui/qt4/Modules.am
modules/gui/qt4/components/open.hpp
modules/gui/qt4/playlist_model.cpp [new file with mode: 0644]
modules/gui/qt4/playlist_model.hpp [new file with mode: 0644]

index cdd5e7fe071446440e72c1325540ccbc5902c415..3ed6b1dd401ddf80d7a41780670582179b2c05c1 100644 (file)
@@ -17,6 +17,7 @@ UIH = $(TOUI:%=%.h)
 TOMOC = main_interface \
        dialogs_provider \
        input_manager \
+    playlist_model
        dialogs/playlist \
        dialogs/prefs_dialog \
        dialogs/streaminfo \
@@ -31,6 +32,7 @@ nodist_SOURCES_qt4 = \
                main_interface.moc.cpp \
                dialogs_provider.moc.cpp \
                input_manager.moc.cpp \
+        playlist_model.moc.cpp \
                dialogs/playlist.moc.cpp \
                dialogs/streaminfo.moc.cpp \
                dialogs/prefs_dialog.moc.cpp \
@@ -61,6 +63,7 @@ SOURCES_qt4 =         qt4.cpp \
                main_interface.cpp \
                dialogs_provider.cpp \
                input_manager.cpp \
+        playlist_model.cpp \
                dialogs/playlist.cpp \
                dialogs/prefs_dialog.cpp \
                dialogs/streaminfo.cpp \
@@ -76,6 +79,7 @@ EXTRA_DIST += \
        main_interface.hpp \
        dialogs_provider.hpp \
        input_manager.hpp \
+    playlist_model.hpp \
        dialogs/playlist.hpp \
        dialogs/streaminfo.hpp \
        dialogs/prefs_dialog.hpp \
index 2de569ed89819924f157ac389239187d7e070920..4d3137d83ba995194ac5e9923ab1c80c0a0219e6 100644 (file)
@@ -43,8 +43,6 @@ private:
     intf_thread_t *p_intf;
 public slots:
     virtual void sendUpdate() = 0;
-signals:
-    virtual void dataUpdated( QString, QString ) = 0;
 };
 
 class FileOpenPanel: public OpenPanel
@@ -59,7 +57,7 @@ private:
 public slots:
     virtual void sendUpdate() ;
 signals:
-    virtual void dataUpdated( QString, QString ) ;
+    void dataUpdated( QString, QString ) ;
 
 };
 
diff --git a/modules/gui/qt4/playlist_model.cpp b/modules/gui/qt4/playlist_model.cpp
new file mode 100644 (file)
index 0000000..f6f8f57
--- /dev/null
@@ -0,0 +1,364 @@
+/*****************************************************************************
+ * input_manager.cpp : Manage an input and interact with its GUI elements
+ ****************************************************************************
+ * Copyright (C) 2000-2005 the VideoLAN team
+ * $Id: wxwidgets.cpp 15731 2006-05-25 14:43:53Z zorglub $
+ *
+ * Authors: Clément Stenac <zorglub@videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include <QApplication>
+#include "playlist_model.hpp"
+#include <assert.h>
+
+
+static int PlaylistChanged( vlc_object_t *, const char *,
+                            vlc_value_t, vlc_value_t, void * );
+static int PlaylistNext( vlc_object_t *, const char *,
+                         vlc_value_t, vlc_value_t, void * );
+static int ItemChanged( vlc_object_t *, const char *,
+                        vlc_value_t, vlc_value_t, void * );
+static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
+                         vlc_value_t oval, vlc_value_t nval, void *param );
+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
+ *************************************************************************/
+
+PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
+{
+    parentItem = parent;
+    i_id = _i_id; i_input_id = _i_input_id;
+    model = m;
+}
+
+PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
+{
+    i_id = p_item->i_id;
+    i_input_id = p_item->p_input->i_id;
+    parentItem = parent;
+    model = m;
+}
+
+PLItem::~PLItem()
+{
+    qDeleteAll(children);
+}
+
+void PLItem::insertChild( PLItem *item, int i_pos )
+{
+    assert( model );
+    model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
+    children.append( item );
+    model->endInsertRows();
+}
+
+int PLItem::row() const
+{
+    if (parentItem)
+        return parentItem->children.indexOf(const_cast<PLItem*>(this));
+    return 0;
+}
+
+
+/*************************************************************************
+ * Playlist model implementation
+ *************************************************************************/
+
+PLModel::PLModel( playlist_item_t * p_root, int i_depth, QObject *parent)
+                                    : QAbstractItemModel(parent)
+{
+    rootItem = new PLItem( p_root, NULL, this );
+
+    i_items_to_append = 0;
+    b_need_update     = false;
+    i_cached_id       = -1;
+    i_cached_input_id = -1;
+
+    /* Some global changes happened -> Rebuild all */
+    var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
+    /* We went to the next item */
+    var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
+    /* One item has been updated */
+    var_AddCallback( p_playlist, "item-change", ItemChanged, this );
+    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
+    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
+}
+
+PLModel::~PLModel()
+{
+    var_DelCallback( p_playlist, "item-change", ItemChanged, this );
+    var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
+    var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
+    var_DelCallback( p_playlist, "item-append", ItemAppended, this );
+    var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
+    delete rootItem;
+}
+
+/****************** Base model mandatory implementations *****************/
+QVariant PLModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid())
+        return QVariant();
+    if (role != Qt::DisplayRole)
+        return QVariant();
+
+    PLItem *item = static_cast<PLItem*>(index.internalPointer());
+    return QVariant( item->columnString( index.column() ) );
+}
+
+Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
+{
+    if (!index.isValid())
+        return Qt::ItemIsEnabled;
+
+    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+QVariant PLModel::headerData( int section, Qt::Orientation orientation,
+                              int role) const
+{
+    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+            return QVariant( rootItem->columnString( section ) );
+    return QVariant();
+}
+
+QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
+                  const
+{
+    PLItem *parentItem;
+    if (!parent.isValid())
+        parentItem = rootItem;
+    else
+        parentItem = static_cast<PLItem*>(parent.internalPointer());
+
+    PLItem *childItem = parentItem->child(row);
+    if (childItem)
+        return createIndex(row, column, childItem);
+    else
+        return QModelIndex();
+}
+
+/* Return the index of a given item */
+QModelIndex PLModel::index( PLItem *item, int column ) const 
+{
+    if( !item ) return QModelIndex();
+    const PLItem *parent = item->parent();
+    if( parent )
+        return createIndex( parent->children.lastIndexOf( item ), column, item );
+    return QModelIndex();
+}
+
+QModelIndex PLModel::parent(const QModelIndex &index) const
+{
+    if (!index.isValid())
+        return QModelIndex();
+
+    PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
+    PLItem *parentItem = childItem->parent();
+
+    if (parentItem == rootItem)
+        return QModelIndex();
+
+    return createIndex(parentItem->row(), 0, parentItem);
+}
+
+int PLModel::childrenCount(const QModelIndex &parent) const
+{
+    PLItem *parentItem;
+
+    if (!parent.isValid())
+        parentItem = rootItem;
+    else
+        parentItem = static_cast<PLItem*>(parent.internalPointer());
+
+    return parentItem->childCount();
+}
+
+/************************* Lookups *****************************/
+
+PLItem *PLModel::FindById( PLItem *root, int i_id )
+{
+    return FindInner( root, i_id, false );
+}
+
+PLItem *PLModel::FindByInput( PLItem *root, int i_id )
+{
+    return FindInner( root, i_id, true );
+}
+
+#define CACHE( i, p ) i_cached_id = i; p_cached_item = p;
+#define ICACHE( i, p ) i_cached_input_id = i; p_cached_item_bi = p;
+
+PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
+{
+    if( ( !b_input && i_cached_id == i_id) || 
+        ( b_input && i_cached_input_id ==i_id ) )
+        return b_input ? p_cached_item_bi : p_cached_item;
+
+    if( !b_input && root->i_id == i_id )
+    {
+        CACHE( i_id, root );
+        return root;
+    }
+    else if( b_input && root->i_input_id == i_id )
+    {
+        ICACHE( i_id, root );
+        return root;
+    }
+
+    QList<PLItem *>::iterator it = root->children.begin();
+    while ( it != root->children.end() )
+    {
+        if( !b_input && (*it)->i_id == i_id )
+        {
+            CACHE( i_id, (*it) );
+            return p_cached_item;
+        }
+        else if( b_input && (*it)->i_input_id == i_id )
+        {
+            ICACHE( i_id, (*it) );
+        }
+        if( (*it)->children.size() )
+        {
+            PLItem *childFound = FindInner( (*it), i_id, b_input );
+            if( childFound ) 
+            {
+                if( b_input )
+                {
+                    ICACHE( i_id, childFound );
+                }
+                else
+                {
+                    CACHE( i_id, childFound );
+                }
+                return childFound;
+            } 
+        }
+    }
+    return NULL;
+}
+#undef CACHE
+#undef ICACHE
+
+
+/************************* Updates handling *****************************/
+void PLModel::customEvent( QEvent *event )
+{
+    PLEvent *ple = dynamic_cast<PLEvent *>(event);
+
+    if( event->type() == ItemUpdate_Type )
+        ProcessInputItemUpdate( ple->i_id );
+    else if( event->type() == ItemAppend_Type )
+        ProcessItemAppend( ple->p_add );
+    else
+        ProcessItemRemoval( ple->i_id );
+}
+
+void PLModel::ProcessInputItemUpdate( int i_input_id )
+{
+    assert( i_input_id >= 0 );
+    /// \todo
+}
+
+void PLModel::ProcessItemRemoval( int i_id )
+{
+    assert( i_id >= 0 );
+    if( i_id == i_cached_id ) i_cached_id = -1;
+    i_cached_input_id = -1;
+
+    /// \todo
+}
+
+void PLModel::ProcessItemAppend( playlist_add_t *p_add )
+{
+    playlist_item_t *p_item = NULL;
+    i_items_to_append--;
+    if( b_need_update ) return;
+
+    PLItem *nodeItem = FindById( rootItem, p_add->i_node );
+    if( !nodeItem ) goto end;
+
+    p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
+    if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
+
+    nodeItem->appendChild( new PLItem( p_item, nodeItem, this ) );
+
+
+end:
+    return;
+}
+        
+/**********************************************************************
+ * Playlist callbacks 
+ **********************************************************************/
+static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
+                            vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    PLModel *p_model = (PLModel *) param;
+    p_model->b_need_update = VLC_TRUE;
+    return VLC_SUCCESS;
+}
+
+static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
+                         vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    PLModel *p_model = (PLModel *) param;
+    PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
+    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
+    event = new PLEvent( ItemUpdate_Type, nval.i_int );
+    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
+    return VLC_SUCCESS;
+}
+
+static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
+                        vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    PLModel *p_model = (PLModel *) param;
+    PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int ); 
+    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
+    return VLC_SUCCESS;
+}
+
+static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
+                        vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    PLModel *p_model = (PLModel *) param;
+    PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
+    QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
+    return VLC_SUCCESS;
+}
+
+static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
+                         vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    PLModel *p_model = (PLModel *) param;
+    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;
+}
diff --git a/modules/gui/qt4/playlist_model.hpp b/modules/gui/qt4/playlist_model.hpp
new file mode 100644 (file)
index 0000000..8bea465
--- /dev/null
@@ -0,0 +1,130 @@
+/*****************************************************************************
+ * playlist_model.hpp : Model for a playlist tree
+ ****************************************************************************
+ * Copyright (C) 2000-2005 the VideoLAN team
+ * $Id: wxwidgets.cpp 15731 2006-05-25 14:43:53Z zorglub $
+ *
+ * Authors: Clément Stenac <zorglub@videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef _PLAYLIST_MODEL_H_
+#define _PLAYLIST_MODEL_H_
+
+#include <QObject>
+#include <QEvent>
+#include <vlc/vlc.h>
+#include <vlc/input.h>
+#include <vlc_playlist.h>
+
+class PLModel;
+
+class PLItem
+{
+public:
+    PLItem( int, int, PLItem *parent , PLModel *);
+    PLItem( playlist_item_t *, PLItem *parent, PLModel *);
+    ~PLItem();
+
+    int row() const;
+    void insertChild( PLItem *, int );
+
+    void appendChild( PLItem *item ) { insertChild( item, children.count() ); };
+    PLItem *child( int row ) { return children.value( row ); };
+    int childCount() const { return children.count(); };
+    QString columnString( int col ) { return strings.value( col ); };
+    PLItem *parent() { return parentItem; };
+protected:
+    QList<PLItem*> children;
+    int i_id;
+    int i_input_id;
+    friend class PLModel;
+private:
+    QList<QString> strings;
+    PLItem *parentItem;
+
+    PLModel *model;
+};
+
+static int ItemUpdate_Type = QEvent::User + 2;
+static int ItemDelete_Type = QEvent::User + 3;
+static int ItemAppend_Type = QEvent::User + 4;
+
+class PLEvent : public QEvent
+{
+public:
+    PLEvent( int type, int id ) : QEvent( (QEvent::Type)(type) )
+    { i_id = id; p_add = NULL; };
+    PLEvent(  playlist_add_t  *a ) : QEvent( (QEvent::Type)(ItemAppend_Type) )
+    { p_add = a; };
+    virtual ~PLEvent() {};
+                          
+    int i_id;
+    playlist_add_t *p_add;
+};   
+                    
+
+#include <QAbstractItemModel>
+#include <QModelIndex>
+#include <QVariant>
+
+class PLModel : public QAbstractItemModel
+{
+    Q_OBJECT
+
+public:
+    PLModel( playlist_item_t *, int, QObject *parent = 0);
+    ~PLModel();
+
+    void customEvent( QEvent * );
+
+    /* QModel stuff */
+    QVariant data( const QModelIndex &index, int role) const;
+    Qt::ItemFlags flags( const QModelIndex &index) const;
+    QVariant headerData( int section, Qt::Orientation orientation,
+                         int role = Qt::DisplayRole) const;
+    
+    QModelIndex index( int r, int c, const QModelIndex &parent ) const;
+    QModelIndex index( PLItem *, int c ) const;
+                    
+    QModelIndex parent( const QModelIndex &index) const;
+    int childrenCount( const QModelIndex &parent = QModelIndex() ) const;
+
+    bool b_need_update;
+    int i_items_to_append;
+private:
+    PLItem *rootItem;
+
+    playlist_t *p_playlist;
+
+    /* Update processing */
+    void ProcessInputItemUpdate( int i_input_id );
+    void ProcessItemRemoval( int i_id );
+    void ProcessItemAppend( playlist_add_t *p_add );
+            
+    /* Lookups */
+    PLItem *FindById( PLItem *, int );
+    PLItem *FindByInput( PLItem *, int );
+    PLItem *FindInner( PLItem *, int , bool );
+    PLItem *p_cached_item;
+    PLItem *p_cached_item_bi;
+    int i_cached_id;
+    int i_cached_input_id;
+
+friend class PLItem;
+};
+
+#endif