]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/selector.cpp
Qt: PLSelector: Make effectless root entries not selectable
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
index 32bf8e659fba6dbd9389814365d3299a058207e4..64831a1a3537b7d24c5f1e0519951c72cf7c9dbc 100644 (file)
@@ -31,6 +31,7 @@
 #include "playlist_model.hpp"                /* plMimeData */
 #include "input_manager.hpp"                 /* MainInputManager, for podcast */
 
+#include <QApplication>
 #include <QInputDialog>
 #include <QMessageBox>
 #include <QMimeData>
@@ -39,6 +40,7 @@
 #include <QHBoxLayout>
 #include <QPainter>
 #include <QPalette>
+#include <QScrollBar>
 
 #include <vlc_playlist.h>
 #include <vlc_services_discovery.h>
@@ -83,6 +85,8 @@ void PLSelItem::addAction( ItemAction act, const QString& tooltip )
         icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
     case RM_ACTION:
         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
+    default:
+        return;
     }
 
     lblAction = new SelectorActionButton();
@@ -116,6 +120,14 @@ PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
     setDropIndicatorShown(true);
     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
 
+#ifdef Q_WS_MAC
+    setAutoFillBackground( true );
+    QPalette palette;
+    palette.setColor( QPalette::Window, QColor(209,215,226) );
+    setPalette( palette );
+#endif
+    setMinimumHeight( 120 );
+
     /* Podcasts */
     podcastsParent = NULL;
     podcastsParentId = -1;
@@ -129,25 +141,21 @@ PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
               this, inputItemUpdate( input_item_t * ) );
 
     createItems();
+
+    /* Expand at least to show level 2 */
+    for ( int i = 0; i < topLevelItemCount(); i++ )
+        expandItem( topLevelItem( i ) );
+
+    /***
+     * We need to react to both clicks and activation (enter-key) here.
+     * We use curItem to avoid rebuilding twice.
+     * See QStyle::SH_ItemView_ActivateItemOnSingleClick
+     ***/
+    curItem = NULL;
     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
              this, setSource( QTreeWidgetItem *) );
     CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
              this, setSource( QTreeWidgetItem *) );
-
-    /* I believe this is unnecessary, seeing
-       QStyle::SH_ItemView_ActivateItemOnSingleClick
-        CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
-             this, setSource( QTreeWidgetItem *) ); */
-
-    /* select the first item */
-//  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
-
-#ifdef Q_WS_MAC
-    setAutoFillBackground( true );
-    QPalette palette;
-    palette.setColor( QPalette::Window, QColor(209,215,226) );
-    setPalette( palette );
-#endif
 }
 
 PLSelector::~PLSelector()
@@ -185,18 +193,31 @@ void PLSelector::createItems()
     PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
                               THEPL->p_playing );
     pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
+    setCurrentItem( pl->treeItem() );
 
     /* ML */
     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
                               THEPL->p_media_library );
     ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
 
+#ifdef MEDIA_LIBRARY
+    /* SQL ML */
+    addItem( SQL_ML_TYPE, "SQL Media Library" )->treeItem();
+#endif
+
     /* SD nodes */
     QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, N_("My Computer") )->treeItem();
     QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, N_("Devices") )->treeItem();
     QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, N_("Local Network") )->treeItem();
     QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, N_("Internet") )->treeItem();
 
+#define NOT_SELECTABLE(w) w->setFlags( w->flags() ^ Qt::ItemIsSelectable );
+    NOT_SELECTABLE( mycomp );
+    NOT_SELECTABLE( devices );
+    NOT_SELECTABLE( lan );
+    NOT_SELECTABLE( internet );
+#undef NOT_SELECTABLE
+
     /* SD subnodes */
     char **ppsz_longnames;
     int *p_categories;
@@ -254,7 +275,7 @@ void PLSelector::createItems()
 
 void PLSelector::setSource( QTreeWidgetItem *item )
 {
-    if( !item )
+    if( !item || item == curItem )
         return;
 
     bool b_ok;
@@ -268,13 +289,32 @@ void PLSelector::setSource( QTreeWidgetItem *item )
         QString qs = item->data( 0, NAME_ROLE ).toString();
         sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
         if( !sd_loaded )
-            playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
+        {
+            if ( playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) ) != VLC_SUCCESS )
+                return ;
+
+            services_discovery_descriptor_t *p_test = new services_discovery_descriptor_t;
+            int i_ret = playlist_ServicesDiscoveryControl( THEPL, qtu( qs ), SD_CMD_DESCRIPTOR, p_test );
+            if( i_ret == VLC_SUCCESS && p_test->i_capabilities & SD_CAP_SEARCH )
+                item->setData( 0, CAP_SEARCH_ROLE, true );
+        }
+    }
+#ifdef MEDIA_LIBRARY
+    else if( i_type == SQL_ML_TYPE )
+    {
+        emit categoryActivated( NULL, true );
+        return;
     }
+#endif
+
+    curItem = item;
 
     /* */
     playlist_Lock( THEPL );
     playlist_item_t *pl_item = NULL;
 
+    /* Special case for podcast */
+    // FIXME: simplify
     if( i_type == SD_TYPE )
     {
         /* Find the right item for the SD */
@@ -300,7 +340,7 @@ void PLSelector::setSource( QTreeWidgetItem *item )
 
     /* */
     if( pl_item )
-        emit activated( pl_item );
+        emit categoryActivated( pl_item, false );
 }
 
 PLSelItem * PLSelector::addItem (
@@ -341,8 +381,8 @@ QStringList PLSelector::mimeTypes() const
     return types;
 }
 
-bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
-    const QMimeData * data, Qt::DropAction action )
+bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int,
+    const QMimeData * data, Qt::DropAction )
 {
     if( !parent ) return false;
 
@@ -449,7 +489,7 @@ void PLSelector::inputItemUpdate( input_item_t *arg )
     }
 }
 
-void PLSelector::podcastAdd( PLSelItem* item )
+void PLSelector::podcastAdd( PLSelItem * )
 {
     bool ok;
     QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
@@ -459,8 +499,7 @@ void PLSelector::podcastAdd( PLSelItem* item )
 
     setSource( podcastsParent ); //to load the SD in case it's not loaded
 
-    vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
-        p_intf->p_libvlc, "podcast", FIND_CHILD );
+    vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name( p_intf->p_libvlc, "podcast" );
     if( !p_obj ) return;
 
     QString request("ADD:");
@@ -483,7 +522,7 @@ void PLSelector::podcastRemove( PLSelItem* item )
     if( !input ) return;
 
     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
-        p_intf->p_libvlc, "podcast", FIND_CHILD );
+        p_intf->p_libvlc, "podcast" );
     if( !p_obj ) return;
 
     QString request("RM:");
@@ -510,8 +549,26 @@ void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QM
                             QStyle::PE_IndicatorArrowRight, &option, painter );
 }
 
-void PLSelector::getCurrentSelectedItem( int* type, QString *string)
+void PLSelector::getCurrentItemInfos( int* type, bool* can_delay_search, QString *string)
 {
     *type = currentItem()->data( 0, TYPE_ROLE ).toInt();
     *string = currentItem()->data( 0, NAME_ROLE ).toString();
+    *can_delay_search = currentItem()->data( 0, CAP_SEARCH_ROLE ).toBool();
+}
+
+int PLSelector::getCurrentItemCategory()
+{
+    return currentItem()->data( 0, SPECIAL_ROLE ).toInt();
+}
+
+void PLSelector::wheelEvent( QWheelEvent *e )
+{
+    if( verticalScrollBar()->isVisible() && (
+        (verticalScrollBar()->value() != verticalScrollBar()->minimum() && e->delta() >= 0 ) ||
+        (verticalScrollBar()->value() != verticalScrollBar()->maximum() && e->delta() < 0 )
+        ) )
+        QApplication::sendEvent(verticalScrollBar(), e);
+
+    // Accept this event in order to prevent unwanted volume up/down changes
+    e->accept();
 }