]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: return playlist model and views to "now playing" root if current root is deleted
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
index 7241cf1895e62ebff481257b22e59e46bd310276..4d073a439e866bf1c2a3104df37552528ea281bd 100644 (file)
@@ -32,6 +32,7 @@
 #include "components/playlist/standardpanel.hpp"
 #include "components/playlist/icon_view.hpp"
 #include "util/customwidgets.hpp"
+#include "menus.hpp"
 
 #include <vlc_intf_strings.h>
 
@@ -45,6 +46,7 @@
 #include <QWheelEvent>
 #include <QToolButton>
 #include <QFontMetrics>
+#include <QPainter>
 
 #include <assert.h>
 
@@ -77,6 +79,7 @@ StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
 
     locationBar = new LocationBar( model );
     layout->addWidget( locationBar, 0, 0 );
+    CONNECT( model, rootChanged(), locationBar, setRootIndex() );
 
     /* A Spacer and the search possibilities */
     layout->setColumnStretch( 1, 10 );
@@ -86,10 +89,10 @@ StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
     layout->addWidget( search, 0, 4 );
     CONNECT( search, textChanged( const QString& ),
              this, search( const QString& ) );
-    layout->setColumnStretch( 4, 2 );
+    layout->setColumnStretch( 4, 10 );
 
     /* Add item to the playlist button */
-    addButton = new QPushButton;
+    addButton = new QToolButton;
     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
     addButton->setMaximumWidth( 30 );
     BUTTONACT( addButton, popupAdd() );
@@ -97,7 +100,7 @@ StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
 
     /* Button to switch views */
     QToolButton *viewButton = new QToolButton( this );
-    viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogContentsView ) );
+    viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
     layout->addWidget( viewButton, 0, 2 );
 
     /* View selection menu */
@@ -162,17 +165,14 @@ void StandardPLPanel::handleExpansion( const QModelIndex& index )
 void StandardPLPanel::popupAdd()
 {
     QMenu popup;
-    if( currentRootId == THEPL->p_local_category->i_id ||
-        currentRootId == THEPL->p_local_onelevel->i_id )
+    if( currentRootId == THEPL->p_playing->i_id )
     {
         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
     }
-    else if( ( THEPL->p_ml_category &&
-                currentRootId == THEPL->p_ml_category->i_id ) ||
-             ( THEPL->p_ml_onelevel &&
-                currentRootId == THEPL->p_ml_onelevel->i_id ) )
+    else if( THEPL->p_media_library &&
+                currentRootId == THEPL->p_media_library->i_id )
     {
         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
@@ -187,9 +187,15 @@ 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 );
+    if( !index.isValid() ){
+        QVLCMenu::PopupMenu( p_intf, true );
+    }
+    else
+    {
+        QItemSelectionModel *selection = currentView->selectionModel();
+        QModelIndexList list = selection->selectedIndexes();
+        model->popup( index, globalPoint, list );
+    }
 }
 
 void StandardPLPanel::popupSelectColumn( QPoint pos )
@@ -230,9 +236,6 @@ void StandardPLPanel::setRoot( playlist_item_t *p_item )
     QPL_LOCK;
     assert( p_item );
 
-    playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
-    if( p_pref_item ) p_item = p_pref_item;
-
     /* needed for popupAdd() */
     currentRootId = p_item->i_id;
 
@@ -249,14 +252,12 @@ void StandardPLPanel::setRoot( playlist_item_t *p_item )
     locationBar->setIndex( QModelIndex() );
 
     /* enable/disable adding */
-    if( p_item == THEPL->p_local_category ||
-        p_item == THEPL->p_local_onelevel )
+    if( p_item == THEPL->p_playing )
     {
         addButton->setEnabled( true );
         addButton->setToolTip( qtr(I_PL_ADDPL) );
     }
-    else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
-              ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
+    else if( THEPL->p_media_library && p_item == THEPL->p_media_library )
     {
         addButton->setEnabled( true );
         addButton->setToolTip( qtr(I_PL_ADDML) );
@@ -437,9 +438,16 @@ void StandardPLPanel::browseInto( input_item_t *p_input )
     playlist_Lock( THEPL );
 
     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
-    assert( p_item != NULL );
+    if( !p_item )
+    {
+        playlist_Unlock( THEPL );
+        return;
+    }
 
     QModelIndex index = model->index( p_item->i_id, 0 );
+
+    playlist_Unlock( THEPL );
+
     if( currentView == iconView ) {
         iconView->setRootIndex( index );
         locationBar->setIndex( index );
@@ -449,7 +457,7 @@ void StandardPLPanel::browseInto( input_item_t *p_input )
 
     last_activated_id = -1;
 
-    playlist_Unlock( THEPL );
+
 }
 
 LocationBar::LocationBar( PLModel *m )
@@ -457,42 +465,99 @@ LocationBar::LocationBar( PLModel *m )
     model = m;
     mapper = new QSignalMapper( this );
     CONNECT( mapper, mapped( int ), this, invoke( int ) );
+
+    box = new QHBoxLayout;
+    box->setSpacing( 0 );
+    box->setContentsMargins( 0, 0, 0, 0 );
+    setLayout( box );
 }
 
 void LocationBar::setIndex( const QModelIndex &index )
 {
-    clear();
-    QAction *prev = NULL;
+    qDeleteAll( buttons );
+    buttons.clear();
     QModelIndex i = index;
-    QFont font;
-    QFontMetrics metrics( font );
-    font.setBold( true );
+    bool bold = 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 );
+        QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
+        if( bold ) btn->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
+        box->insertWidget( 0, btn, bold ? 1 : 0 );
+        buttons.append( btn );
 
         mapper->setMapping( btn, item->id() );
         CONNECT( btn, clicked( ), mapper, map( ) );
 
-        font = QFont();
+        bold = false;
 
         if( i.isValid() ) i = i.parent();
         else break;
     }
 }
 
+void LocationBar::setRootIndex()
+{
+    setIndex( QModelIndex() );
+}
+
 void LocationBar::invoke( int i_id )
 {
     QModelIndex index = model->index( i_id, 0 );
     setIndex( index );
     emit invoked ( index );
 }
+
+LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
+  : b_arrow( arrow )
+{
+    QFont font;
+    font.setBold( bold );
+    setFont( font );
+    setText( text );
+}
+
+#define PADDING 4
+
+void LocationButton::paintEvent ( QPaintEvent * event )
+{
+    QStyleOptionButton option;
+    option.initFrom( this );
+    //option.rect = rect();
+    //option.features = QStyleOptionButton::Flat;
+    option.state |= QStyle::State_Enabled;
+    //option.state |= isChecked() ? QStyle::State_On : QStyle::State_Off;
+    //if( isDown() ) option.state |= QStyle::State_Sunken;
+    QPainter p( this );
+
+    if( underMouse() )
+        style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
+
+    int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
+
+    QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
+    p.drawText( rect, Qt::AlignVCenter,
+                fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
+
+    if( b_arrow )
+    {
+        option.rect.setX( margin );
+        option.rect.setWidth( 8 );
+        style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
+    }
+}
+
+QSize LocationButton::sizeHint() const
+{
+    int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
+    QSize s( fontMetrics().boundingRect( text() ).size() );
+    s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
+    s.setHeight( QPushButton::sizeHint().height() );
+    return s;
+}
+
+#undef PADDING