]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/standardpanel.cpp
VLC supports .vro (DVD-VR) files
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
index d406a22c035aaf65db91fefe6fd72cb4a294818d..998e9d052084bce96b77b3c8caa306c5f2cd31f8 100644 (file)
@@ -26,7 +26,6 @@
 # include "config.h"
 #endif
 
-#include "qt4.hpp"
 #include "dialogs_provider.hpp"
 
 #include "components/playlist/playlist_model.hpp"
@@ -44,6 +43,8 @@
 #include <QMenu>
 #include <QSignalMapper>
 #include <QWheelEvent>
+#include <QToolButton>
+#include <QFontMetrics>
 
 #include <assert.h>
 
@@ -59,77 +60,33 @@ StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
     layout->setSpacing( 0 ); layout->setMargin( 0 );
     setMinimumWidth( 300 );
 
-    model = new PLModel( p_playlist, p_intf, p_root, this );
-
-    /* Create and configure the QTreeView */
-    treeView = new QTreeView;
-    treeView->setModel( model );
     iconView = NULL;
+    treeView = NULL;
 
-    treeView->setIconSize( QSize( 20, 20 ) );
-    treeView->setAlternatingRowColors( true );
-    treeView->setAnimated( true );
-    treeView->setUniformRowHeights( true );
-    treeView->setSortingEnabled( true );
-    treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
-    treeView->header()->setSortIndicatorShown( true );
-    treeView->header()->setClickable( true );
-    treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
-
-    treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
-    treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
-    treeView->setDragEnabled( true );
-    treeView->setAcceptDrops( true );
-    treeView->setDropIndicatorShown( true );
-    treeView->setContextMenuPolicy( Qt::CustomContextMenu );
-
-    /* Saved Settings */
-    getSettings()->beginGroup("Playlist");
-    if( getSettings()->contains( "headerStateV2" ) )
-    {
-        treeView->header()->restoreState(
-                getSettings()->value( "headerStateV2" ).toByteArray() );
-    }
-    else
-    {
-        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
-        {
-            treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
-            if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
-            else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
-        }
-    }
-    getSettings()->endGroup();
-
-    /* Connections for the TreeView */
-    CONNECT( treeView, activated( const QModelIndex& ),
-             model,activateItem( const QModelIndex& ) );
-    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
-             this, popupSelectColumn( QPoint ) );
-    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
-             this, playlistPopup( const QPoint & ) );
-    CONNECT( model, currentChanged( const QModelIndex& ),
-             this, handleExpansion( const QModelIndex& ) );
-
+    model = new PLModel( p_playlist, p_intf, p_root, this );
     currentRootId = -1;
+    last_activated_id = -1;
 
     /* Title label */
-    title = new QLabel;
+    /*title = new QLabel;
     QFont titleFont;
     titleFont.setPointSize( titleFont.pointSize() + 6 );
     titleFont.setFamily( "Verdana" );
     title->setFont( titleFont );
-    layout->addWidget( title, 0, 0 );
+    layout->addWidget( title, 0, 0 );*/
+
+    locationBar = new LocationBar( model );
+    layout->addWidget( locationBar, 0, 0 );
 
     /* A Spacer and the search possibilities */
     layout->setColumnStretch( 1, 10 );
 
     SearchLineEdit *search = new SearchLineEdit( this );
-    search->setMaximumWidth( 200 );
+    search->setMaximumWidth( 300 );
     layout->addWidget( search, 0, 4 );
     CONNECT( search, textChanged( const QString& ),
              this, search( const QString& ) );
-    layout->setColumnStretch( 4, 1 );
+    layout->setColumnStretch( 4, 2 );
 
     /* Add item to the playlist button */
     addButton = new QPushButton;
@@ -138,35 +95,67 @@ StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
     BUTTONACT( addButton, popupAdd() );
     layout->addWidget( addButton, 0, 3 );
 
-    QPushButton *viewButton = new QPushButton( this );
-    viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
+    /* Button to switch views */
+    QToolButton *viewButton = new QToolButton( this );
+    viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogContentsView ) );
     layout->addWidget( viewButton, 0, 2 );
-    BUTTONACT( viewButton, toggleView() );
 
-    /* Finish the layout */
-    layout->addWidget( treeView, 1, 0, 1, -1 );
+    /* View selection menu */
+    viewSelectionMapper = new QSignalMapper;
+    CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
 
-    selectColumnsSigMapper = new QSignalMapper( this );
-    CONNECT( selectColumnsSigMapper, mapped( int ),
-             this, toggleColumnShown( int ) );
+    QActionGroup *actionGroup = new QActionGroup( this );
+
+    treeViewAction = actionGroup->addAction( "Detailed view" );
+    treeViewAction->setCheckable( true );
+    viewSelectionMapper->setMapping( treeViewAction, TREE_VIEW );
+    CONNECT( treeViewAction, triggered(), viewSelectionMapper, map() );
+
+    iconViewAction = actionGroup->addAction( "Icon view" );
+    iconViewAction->setCheckable( true );
+    viewSelectionMapper->setMapping( iconViewAction, ICON_VIEW );
+    CONNECT( iconViewAction, triggered(), viewSelectionMapper, map() );
+
+    BUTTONACT( viewButton, cycleViews() );
+    QMenu *viewMenu = new QMenu( this );
+    viewMenu->addActions( actionGroup->actions() );
+
+    viewButton->setMenu( viewMenu );
+
+    /* Saved Settings */
+    getSettings()->beginGroup("Playlist");
+
+    int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
+    showView( i_viewMode );
+
+    getSettings()->endGroup();
+
+    CONNECT( THEMIM, inputChanged( input_thread_t * ),
+             this, handleInputChange( input_thread_t * ) );
+
+    CONNECT( model, currentChanged( const QModelIndex& ),
+             this, handleExpansion( const QModelIndex& ) );
 }
 
 StandardPLPanel::~StandardPLPanel()
 {
     getSettings()->beginGroup("Playlist");
-    getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
+    if( treeView )
+        getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
+    getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
     getSettings()->endGroup();
 }
 
 /* Unused anymore, but might be useful, like in right-click menu */
 void StandardPLPanel::gotoPlayingItem()
 {
-    treeView->scrollTo( model->currentIndex() );
+    currentView->scrollTo( model->currentIndex() );
 }
 
 void StandardPLPanel::handleExpansion( const QModelIndex& index )
 {
-    treeView->scrollTo( index );
+    assert( currentView );
+    currentView->scrollTo( index );
 }
 
 /* PopupAdd Menu for the Add Menu */
@@ -194,9 +183,19 @@ void StandardPLPanel::popupAdd()
                         + QPoint( 0, addButton->height() ) );
 }
 
+void StandardPLPanel::popupPlView( const QPoint &point )
+{
+    QModelIndex index = currentView->indexAt( point );
+    QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
+    QItemSelectionModel *selection = currentView->selectionModel();
+    QModelIndexList list = selection->selectedIndexes();
+    model->popup( index, globalPoint, list );
+}
+
 void StandardPLPanel::popupSelectColumn( QPoint pos )
 {
     QMenu menu;
+    assert( treeView );
 
     /* We do not offer the option to hide index 0 column, or
     * QTreeView will behave weird */
@@ -213,19 +212,6 @@ void StandardPLPanel::popupSelectColumn( QPoint pos )
     menu.exec( QCursor::pos() );
 }
 
-void StandardPLPanel::playlistPopup( const QPoint &point )
-{
-    QAbstractItemView *aView;
-    if ( treeView->isVisible() ) aView = treeView;
-    else aView = iconView;
-
-    QModelIndex index = aView->indexAt( point );
-    QPoint globalPoint = aView->viewport()->mapToGlobal( point );
-    QItemSelectionModel *selection = aView->selectionModel();
-    QModelIndexList list = selection->selectedIndexes();
-    model->popup( index, globalPoint, list );
-}
-
 void StandardPLPanel::toggleColumnShown( int i )
 {
     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
@@ -251,15 +237,17 @@ void StandardPLPanel::setRoot( playlist_item_t *p_item )
     currentRootId = p_item->i_id;
 
     /* cosmetics, ..still need playlist locking.. */
-    char *psz_title = input_item_GetName( p_item->p_input );
+    /*char *psz_title = input_item_GetName( p_item->p_input );
     title->setText( qfu(psz_title) );
-    free( psz_title );
+    free( psz_title );*/
 
     QPL_UNLOCK;
 
     /* do THE job */
     model->rebuild( p_item );
 
+    locationBar->setIndex( QModelIndex() );
+
     /* enable/disable adding */
     if( p_item == THEPL->p_local_category ||
         p_item == THEPL->p_local_onelevel )
@@ -297,31 +285,121 @@ void StandardPLPanel::keyPressEvent( QKeyEvent *e )
 
 void StandardPLPanel::deleteSelection()
 {
-    QItemSelectionModel *selection = treeView->selectionModel();
+    QItemSelectionModel *selection = currentView->selectionModel();
     QModelIndexList list = selection->selectedIndexes();
     model->doDelete( list );
 }
 
-void StandardPLPanel::toggleView()
+void StandardPLPanel::createIconView()
 {
-    if( treeView && treeView->isVisible() )
+    iconView = new PlIconView( model, this );
+    iconView->setContextMenuPolicy( Qt::CustomContextMenu );
+    CONNECT( iconView, customContextMenuRequested( const QPoint & ),
+             this, popupPlView( const QPoint & ) );
+    CONNECT( iconView, activated( const QModelIndex & ),
+             this, activate( const QModelIndex & ) );
+    CONNECT( locationBar, invoked( const QModelIndex & ),
+             iconView, setRootIndex( const QModelIndex & ) );
+
+    layout->addWidget( iconView, 1, 0, 1, -1 );
+}
+
+void StandardPLPanel::createTreeView()
+{
+    /* Create and configure the QTreeView */
+    treeView = new QTreeView;
+
+    treeView->setIconSize( QSize( 20, 20 ) );
+    treeView->setAlternatingRowColors( true );
+    treeView->setAnimated( true );
+    treeView->setUniformRowHeights( true );
+    treeView->setSortingEnabled( true );
+    treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
+    treeView->header()->setSortIndicatorShown( true );
+    treeView->header()->setClickable( true );
+    treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
+
+    treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
+    treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
+    treeView->setDragEnabled( true );
+    treeView->setAcceptDrops( true );
+    treeView->setDropIndicatorShown( true );
+    treeView->setContextMenuPolicy( Qt::CustomContextMenu );
+
+    /* setModel after setSortingEnabled(true), or the model will sort immediately! */
+    treeView->setModel( model );
+
+    if( getSettings()->contains( "headerStateV2" ) )
     {
-        if( iconView == NULL )
+        treeView->header()->restoreState(
+                getSettings()->value( "headerStateV2" ).toByteArray() );
+    }
+    else
+    {
+        for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
         {
-            iconView = new PlIconView( model, this );
-            layout->addWidget( iconView, 1, 0, 1, -1 );
-            iconView->setContextMenuPolicy( Qt::CustomContextMenu );
-            CONNECT( iconView, customContextMenuRequested( const QPoint & ),
-                     this, playlistPopup( const QPoint & ) );
+            treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
+            if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
+            else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
         }
-        treeView->hide();
-        iconView->show();
     }
-    else
+
+    /* Connections for the TreeView */
+    CONNECT( treeView, activated( const QModelIndex& ),
+             this, activate( const QModelIndex& ) );
+    CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
+             this, popupSelectColumn( QPoint ) );
+    CONNECT( treeView, customContextMenuRequested( const QPoint & ),
+             this, popupPlView( const QPoint & ) );
+
+    /* SignalMapper for columns */
+    selectColumnsSigMapper = new QSignalMapper( this );
+    CONNECT( selectColumnsSigMapper, mapped( int ),
+             this, toggleColumnShown( int ) );
+
+    /* Finish the layout */
+    layout->addWidget( treeView, 1, 0, 1, -1 );
+}
+
+void StandardPLPanel::showView( int i_view )
+{
+    switch( i_view )
     {
-        iconView->hide();
+    case TREE_VIEW:
+    {
+        if( treeView == NULL )
+            createTreeView();
+        locationBar->setIndex( treeView->rootIndex() );
+        if( iconView ) iconView->hide();
         treeView->show();
+        currentView = treeView;
+        treeViewAction->setChecked( true );
+        break;
+    }
+    case ICON_VIEW:
+    {
+        if( iconView == NULL )
+            createIconView();
+
+        locationBar->setIndex( iconView->rootIndex() );
+        if( treeView ) treeView->hide();
+        iconView->show();
+        currentView = iconView;
+        iconViewAction->setChecked( true );
+        break;
     }
+    default:;
+    }
+}
+
+void StandardPLPanel::cycleViews()
+{
+    if( currentView == iconView )
+        showView( TREE_VIEW );
+    else if( currentView == treeView )
+        showView( ICON_VIEW );
+    else
+        assert( 0 );
 }
 
 void StandardPLPanel::wheelEvent( QWheelEvent *e )
@@ -329,3 +407,89 @@ void StandardPLPanel::wheelEvent( QWheelEvent *e )
     // Accept this event in order to prevent unwanted volume up/down changes
     e->accept();
 }
+
+void StandardPLPanel::activate( const QModelIndex &index )
+{
+    last_activated_id = model->itemId( index );
+    if( model->hasChildren( index ) )
+    {
+        if( currentView == iconView ) {
+            iconView->setRootIndex( index );
+            //title->setText( index.data().toString() );
+            locationBar->setIndex( index );
+        }
+    }
+    else
+    {
+        model->activateItem( index );
+    }
+}
+
+void StandardPLPanel::handleInputChange( input_thread_t *p_input_thread )
+{
+    if( currentView != iconView ) return;
+
+    input_item_t *p_input_item = input_GetItem( p_input_thread );
+    if( !p_input_item ) return;
+
+    playlist_Lock( THEPL );
+
+    playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input_item );
+
+    if( p_item  && p_item->p_parent &&
+        p_item->p_parent->i_id == last_activated_id )
+    {
+        QModelIndex index = model->index( p_item->p_parent->i_id, 0 );
+        iconView->setRootIndex( index );
+        //title->setText( index.data().toString() );
+        locationBar->setIndex( index );
+        last_activated_id = p_item->i_id;
+    }
+
+    playlist_Unlock( THEPL );
+}
+
+LocationBar::LocationBar( PLModel *m )
+{
+  model = m;
+  mapper = new QSignalMapper;
+  CONNECT( mapper, mapped( int ), this, invoke( int ) );
+}
+
+void LocationBar::setIndex( const QModelIndex &index )
+{
+  clear();
+  QAction *prev = NULL;
+  QModelIndex i = index;
+  QFont font;
+  QFontMetrics metrics( font );
+  font.setBold( true );
+  while( true )
+  {
+      PLItem *item = model->getItem( i );
+
+      QToolButton *btn = new QToolButton;
+      char *fb_name = input_item_GetTitleFbName( item->inputItem() );
+      QString text = qfu(fb_name);
+      free(fb_name);
+      text = QString("/ ") + metrics.elidedText( text, Qt::ElideRight, 150 );
+      btn->setText( text );
+      btn->setFont( font );
+      prev = insertWidget( prev, btn );
+
+      mapper->setMapping( btn, item->id() );
+      CONNECT( btn, clicked( ), mapper, map( ) );
+
+      font = QFont();
+
+      if( i.isValid() ) i = i.parent();
+      else break;
+  }
+}
+
+void LocationBar::invoke( int i_id )
+{
+  QModelIndex index = model->index( i_id, 0 );
+  setIndex( index );
+  emit invoked ( index );
+}