]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/selector.cpp
Revert "qt4: use longname to search activated input-item"
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
index 87ed50ede24b07b11248576823950c37f251a64c..42076f936359d971aacdc4eb15d3ba1aff7a4604 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * selector.cpp : Playlist source selector
  ****************************************************************************
- * Copyright (C) 2000-2005 the VideoLAN team
- * $Id: standardpanel.cpp 16024 2006-07-13 13:51:05Z xtophe $
+ * Copyright (C) 2006-2009 the VideoLAN team
+ * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
+ *          Jean-Baptiste Kempf
  *
  * 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
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <assert.h>
+
 #include "components/playlist/selector.hpp"
+#include "playlist_item.hpp"
 #include "qt4.hpp"
+
 #include <QVBoxLayout>
 #include <QHeaderView>
-#include <QTreeView>
+#include <QMimeData>
+
+#include <vlc_playlist.h>
+#include <vlc_services_discovery.h>
+
+PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
+           : QTreeWidget( p ), p_intf(_p_intf)
+{
+    setIconSize( QSize( 24,24 ) );
+//    view->setAlternatingRowColors( true );
+    setIndentation( 10 );
+    header()->hide();
+    setRootIsDecorated( false );
+//    model = new PLModel( THEPL, p_intf, THEPL->p_root_category, 1, this );
+//    view->setModel( model );
+    viewport()->setAcceptDrops(true);
+    setDropIndicatorShown(true);
+    invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
+
+    createItems();
+    CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
+             this, setSource( QTreeWidgetItem *) );
+    /* I believe this is unnecessary, seeing
+       QStyle::SH_ItemView_ActivateItemOnSingleClick
+        CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
+             this, setSource( QTreeWidgetItem *) ); */
 
-PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf,
-                        playlist_t *p_playlist ) : QWidget( p ), p_intf(_p_intf)
+    /* select the first item */
+//  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
+}
+
+void PLSelector::setSource( QTreeWidgetItem *item )
 {
-    model = new PLModel( THEPL, THEPL->p_root_category, 1, this );
-    model->Rebuild();
-    view = new QTreeView( 0 );
-    view->setIndentation( 0 );
-    view->header()->hide();
-    view->setModel( model );
-
-    connect( view, SIGNAL( activated( const QModelIndex& ) ),
-             this, SLOT( setSource( const QModelIndex& ) ) );
-    connect( view, SIGNAL( clicked( const QModelIndex& ) ),
-             this, SLOT( setSource( const QModelIndex& ) ) );
-
-    QVBoxLayout *layout = new QVBoxLayout();
-    layout->setSpacing( 0 ); layout->setMargin( 0 );
-    layout->addWidget( view );
-    setLayout( layout );
+    if( !item )
+        return;
+
+    bool b_ok;
+    int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
+    if( !b_ok )
+        return;
+
+    assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
+    if( i_type == SD_TYPE )
+    {
+        QString qs = item->data( 0, NAME_ROLE ).toString();
+        if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
+        {
+            playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
+#warning FIXME
+            playlist_item_t *pl_item =
+                    THEPL->p_root_category->pp_children[THEPL->p_root_category->i_children-1];
+            item->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( pl_item ) );
+
+            emit activated( pl_item );
+            return;
+        }
+    }
+
+    if( i_type == SD_TYPE )
+        msg_Dbg( p_intf, "SD already loaded, reloading" );
+
+    playlist_item_t *pl_item =
+            item->data( 0, PPL_ITEM_ROLE ).value<playlist_item_t *>();
+    if( pl_item )
+            emit activated( pl_item );
 }
 
-void PLSelector::setSource( const QModelIndex &index )
+void PLSelector::createItems()
 {
-    if( model )
-        emit activated( model->itemId( index ) );
+    QTreeWidgetItem *pl = new QTreeWidgetItem( this );
+    pl->setText( 0, qtr( "Playlist" ) );
+    pl->setData( 0, TYPE_ROLE, PL_TYPE );
+    pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
+/*  QTreeWidgetItem *empty = new QTreeWidgetItem( view );
+    empty->setFlags(Qt::NoItemFlags); */
+
+    QTreeWidgetItem *lib = new QTreeWidgetItem( this );
+    lib->setText( 0, qtr( "Library" ) );
+    lib->setData( 0, TYPE_ROLE, ML_TYPE );
+    lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
+
+/*  QTreeWidgetItem *empty2 = new QTreeWidgetItem( view );
+    empty2->setFlags(Qt::NoItemFlags);*/
+
+    QTreeWidgetItem *sds = new QTreeWidgetItem( this );
+    sds->setExpanded( true );
+    sds->setText( 0, qtr( "Libraries" ) );
+    sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
+
+    char **ppsz_longnames;
+    char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
+    if( !ppsz_names )
+        return;
+
+    char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
+    QTreeWidgetItem *sd_item;
+    for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
+    {
+        sd_item = new QTreeWidgetItem( QStringList( qfu(*ppsz_longname) ) );
+        sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
+        sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
+        sd_item->setFlags( sd_item->flags() & ~Qt::ItemIsDropEnabled );
+        sds->addChild( sd_item );
+        free( *ppsz_name );
+        free( *ppsz_longname );
+    }
+    free( ppsz_names );
+    free( ppsz_longnames );
+}
+
+QStringList PLSelector::mimeTypes() const
+{
+    QStringList types;
+    types << "vlc/qt-playlist-item";
+    return types;
+}
+
+bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
+  const QMimeData * data, Qt::DropAction action )
+{
+    if( !parent ) return false;
+
+    QVariant type = parent->data( 0, TYPE_ROLE );
+    if( type == QVariant() ) return false;
+    int i_type = type.toInt();
+    if( i_type != PL_TYPE && i_type != ML_TYPE ) return false;
+    bool to_pl = i_type == PL_TYPE;
+
+    if( data->hasFormat( "vlc/qt-playlist-item" ) )
+    {
+        QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
+        QDataStream stream( &encodedData, QIODevice::ReadOnly );
+        playlist_Lock( THEPL );
+        while( !stream.atEnd() )
+        {
+            PLItem *item;
+            stream.readRawData( (char*)&item, sizeof(PLItem*) );
+            input_item_t *pl_input =item->inputItem();
+            playlist_AddExt ( THEPL,
+                pl_input->psz_uri, pl_input->psz_name,
+                PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
+                pl_input->i_duration,
+                pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
+                to_pl, true );
+        }
+        playlist_Unlock( THEPL );
+    }
+    return true;
 }
 
 PLSelector::~PLSelector()