1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright (C) 2000-2009 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * JB Kempf <jb@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include "dialogs_provider.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/standardpanel.hpp"
33 #include "components/playlist/icon_view.hpp"
34 #include "util/customwidgets.hpp"
37 #include <vlc_intf_strings.h>
39 #include <QPushButton>
40 #include <QHeaderView>
42 #include <QModelIndexList>
45 #include <QSignalMapper>
46 #include <QWheelEvent>
47 #include <QToolButton>
48 #include <QFontMetrics>
55 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
56 intf_thread_t *_p_intf,
57 playlist_t *p_playlist,
58 playlist_item_t *p_root ):
59 QWidget( _parent ), p_intf( _p_intf )
61 layout = new QGridLayout( this );
62 layout->setSpacing( 0 ); layout->setMargin( 0 );
63 setMinimumWidth( 300 );
68 model = new PLModel( p_playlist, p_intf, p_root, this );
70 last_activated_id = -1;
75 titleFont.setPointSize( titleFont.pointSize() + 6 );
76 titleFont.setFamily( "Verdana" );
77 title->setFont( titleFont );
78 layout->addWidget( title, 0, 0 );*/
80 locationBar = new LocationBar( model );
81 layout->addWidget( locationBar, 0, 0 );
82 CONNECT( model, rootChanged(), locationBar, setRootIndex() );
84 /* A Spacer and the search possibilities */
85 layout->setColumnStretch( 1, 10 );
87 SearchLineEdit *search = new SearchLineEdit( this );
88 search->setMaximumWidth( 300 );
89 layout->addWidget( search, 0, 4 );
90 CONNECT( search, textChanged( const QString& ),
91 this, search( const QString& ) );
92 layout->setColumnStretch( 4, 10 );
94 /* Add item to the playlist button */
95 addButton = new QToolButton;
96 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
97 addButton->setMaximumWidth( 30 );
98 BUTTONACT( addButton, popupAdd() );
99 layout->addWidget( addButton, 0, 3 );
101 /* Button to switch views */
102 QToolButton *viewButton = new QToolButton( this );
103 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
104 layout->addWidget( viewButton, 0, 2 );
106 /* View selection menu */
107 viewSelectionMapper = new QSignalMapper( this );
108 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
110 QActionGroup *actionGroup = new QActionGroup( this );
112 treeViewAction = actionGroup->addAction( "Detailed view" );
113 treeViewAction->setCheckable( true );
114 viewSelectionMapper->setMapping( treeViewAction, TREE_VIEW );
115 CONNECT( treeViewAction, triggered(), viewSelectionMapper, map() );
117 iconViewAction = actionGroup->addAction( "Icon view" );
118 iconViewAction->setCheckable( true );
119 viewSelectionMapper->setMapping( iconViewAction, ICON_VIEW );
120 CONNECT( iconViewAction, triggered(), viewSelectionMapper, map() );
122 BUTTONACT( viewButton, cycleViews() );
123 QMenu *viewMenu = new QMenu( this );
124 viewMenu->addActions( actionGroup->actions() );
126 viewButton->setMenu( viewMenu );
129 getSettings()->beginGroup("Playlist");
131 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
132 showView( i_viewMode );
134 getSettings()->endGroup();
136 CONNECT( THEMIM, leafBecameParent( input_item_t *),
137 this, browseInto( input_item_t * ) );
139 CONNECT( model, currentChanged( const QModelIndex& ),
140 this, handleExpansion( const QModelIndex& ) );
143 StandardPLPanel::~StandardPLPanel()
145 getSettings()->beginGroup("Playlist");
147 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
148 getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
149 getSettings()->endGroup();
152 /* Unused anymore, but might be useful, like in right-click menu */
153 void StandardPLPanel::gotoPlayingItem()
155 currentView->scrollTo( model->currentIndex() );
158 void StandardPLPanel::handleExpansion( const QModelIndex& index )
160 assert( currentView );
161 currentView->scrollTo( index );
164 /* PopupAdd Menu for the Add Menu */
165 void StandardPLPanel::popupAdd()
168 if( currentRootId == THEPL->p_playing->i_id )
170 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
171 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
172 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
174 else if( THEPL->p_media_library &&
175 currentRootId == THEPL->p_media_library->i_id )
177 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
178 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
179 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
182 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
183 + QPoint( 0, addButton->height() ) );
186 void StandardPLPanel::popupPlView( const QPoint &point )
188 QModelIndex index = currentView->indexAt( point );
189 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
190 if( !index.isValid() ){
191 QVLCMenu::PopupMenu( p_intf, true );
195 QItemSelectionModel *selection = currentView->selectionModel();
196 QModelIndexList list = selection->selectedIndexes();
197 model->popup( index, globalPoint, list );
201 void StandardPLPanel::popupSelectColumn( QPoint pos )
206 /* We do not offer the option to hide index 0 column, or
207 * QTreeView will behave weird */
209 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
211 QAction* option = menu.addAction(
212 qfu( psz_column_title( i ) ) );
213 option->setCheckable( true );
214 option->setChecked( !treeView->isColumnHidden( j ) );
215 selectColumnsSigMapper->setMapping( option, j );
216 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
218 menu.exec( QCursor::pos() );
221 void StandardPLPanel::toggleColumnShown( int i )
223 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
226 /* Search in the playlist */
227 void StandardPLPanel::search( const QString& searchText )
229 model->search( searchText );
232 /* Set the root of the new Playlist */
233 /* This activated by the selector selection */
234 void StandardPLPanel::setRoot( playlist_item_t *p_item )
239 /* needed for popupAdd() */
240 currentRootId = p_item->i_id;
242 /* cosmetics, ..still need playlist locking.. */
243 /*char *psz_title = input_item_GetName( p_item->p_input );
244 title->setText( qfu(psz_title) );
250 model->rebuild( p_item );
252 locationBar->setIndex( QModelIndex() );
254 /* enable/disable adding */
255 if( p_item == THEPL->p_playing )
257 addButton->setEnabled( true );
258 addButton->setToolTip( qtr(I_PL_ADDPL) );
260 else if( THEPL->p_media_library && p_item == THEPL->p_media_library )
262 addButton->setEnabled( true );
263 addButton->setToolTip( qtr(I_PL_ADDML) );
266 addButton->setEnabled( false );
269 void StandardPLPanel::removeItem( int i_id )
271 model->removeItem( i_id );
274 /* Delete and Suppr key remove the selection
275 FilterKey function and code function */
276 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
287 void StandardPLPanel::deleteSelection()
289 QItemSelectionModel *selection = currentView->selectionModel();
290 QModelIndexList list = selection->selectedIndexes();
291 model->doDelete( list );
294 void StandardPLPanel::createIconView()
296 iconView = new PlIconView( model, this );
297 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
298 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
299 this, popupPlView( const QPoint & ) );
300 CONNECT( iconView, activated( const QModelIndex & ),
301 this, activate( const QModelIndex & ) );
302 CONNECT( locationBar, invoked( const QModelIndex & ),
303 iconView, setRootIndex( const QModelIndex & ) );
305 layout->addWidget( iconView, 1, 0, 1, -1 );
308 void StandardPLPanel::createTreeView()
310 /* Create and configure the QTreeView */
311 treeView = new QTreeView;
313 treeView->setIconSize( QSize( 20, 20 ) );
314 treeView->setAlternatingRowColors( true );
315 treeView->setAnimated( true );
316 treeView->setUniformRowHeights( true );
317 treeView->setSortingEnabled( true );
318 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
319 treeView->header()->setSortIndicatorShown( true );
320 treeView->header()->setClickable( true );
321 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
323 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
324 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
325 treeView->setDragEnabled( true );
326 treeView->setAcceptDrops( true );
327 treeView->setDropIndicatorShown( true );
328 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
330 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
331 treeView->setModel( model );
333 if( getSettings()->contains( "headerStateV2" ) )
335 treeView->header()->restoreState(
336 getSettings()->value( "headerStateV2" ).toByteArray() );
340 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
342 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
343 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
344 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
348 /* Connections for the TreeView */
349 CONNECT( treeView, activated( const QModelIndex& ),
350 this, activate( const QModelIndex& ) );
351 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
352 this, popupSelectColumn( QPoint ) );
353 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
354 this, popupPlView( const QPoint & ) );
356 /* SignalMapper for columns */
357 selectColumnsSigMapper = new QSignalMapper( this );
358 CONNECT( selectColumnsSigMapper, mapped( int ),
359 this, toggleColumnShown( int ) );
361 /* Finish the layout */
362 layout->addWidget( treeView, 1, 0, 1, -1 );
365 void StandardPLPanel::showView( int i_view )
371 if( treeView == NULL )
373 locationBar->setIndex( treeView->rootIndex() );
374 if( iconView ) iconView->hide();
376 currentView = treeView;
377 treeViewAction->setChecked( true );
382 if( iconView == NULL )
385 locationBar->setIndex( iconView->rootIndex() );
386 if( treeView ) treeView->hide();
388 currentView = iconView;
389 iconViewAction->setChecked( true );
396 void StandardPLPanel::cycleViews()
398 if( currentView == iconView )
399 showView( TREE_VIEW );
400 else if( currentView == treeView )
401 showView( ICON_VIEW );
406 void StandardPLPanel::wheelEvent( QWheelEvent *e )
408 // Accept this event in order to prevent unwanted volume up/down changes
412 void StandardPLPanel::activate( const QModelIndex &index )
414 if( model->hasChildren( index ) )
416 if( currentView == iconView ) {
417 iconView->setRootIndex( index );
418 //title->setText( index.data().toString() );
419 locationBar->setIndex( index );
424 playlist_Lock( THEPL );
425 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
426 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
427 last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
428 playlist_Unlock( THEPL );
429 model->activateItem( index );
433 void StandardPLPanel::browseInto( input_item_t *p_input )
436 if( p_input->i_id != last_activated_id ) return;
438 playlist_Lock( THEPL );
440 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
443 playlist_Unlock( THEPL );
447 QModelIndex index = model->index( p_item->i_id, 0 );
449 playlist_Unlock( THEPL );
451 if( currentView == iconView ) {
452 iconView->setRootIndex( index );
453 locationBar->setIndex( index );
456 treeView->setExpanded( index, true );
458 last_activated_id = -1;
463 LocationBar::LocationBar( PLModel *m )
466 mapper = new QSignalMapper( this );
467 CONNECT( mapper, mapped( int ), this, invoke( int ) );
469 box = new QHBoxLayout;
470 box->setSpacing( 0 );
471 box->setContentsMargins( 0, 0, 0, 0 );
475 void LocationBar::setIndex( const QModelIndex &index )
477 qDeleteAll( buttons );
479 QModelIndex i = index;
483 PLItem *item = model->getItem( i );
485 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
486 QString text = qfu(fb_name);
488 QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
489 if( bold ) btn->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
490 box->insertWidget( 0, btn, bold ? 1 : 0 );
491 buttons.append( btn );
493 mapper->setMapping( btn, item->id() );
494 CONNECT( btn, clicked( ), mapper, map( ) );
498 if( i.isValid() ) i = i.parent();
503 void LocationBar::setRootIndex()
505 setIndex( QModelIndex() );
508 void LocationBar::invoke( int i_id )
510 QModelIndex index = model->index( i_id, 0 );
512 emit invoked ( index );
515 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
519 font.setBold( bold );
526 void LocationButton::paintEvent ( QPaintEvent * event )
528 QStyleOptionButton option;
529 option.initFrom( this );
530 //option.rect = rect();
531 //option.features = QStyleOptionButton::Flat;
532 option.state |= QStyle::State_Enabled;
533 //option.state |= isChecked() ? QStyle::State_On : QStyle::State_Off;
534 //if( isDown() ) option.state |= QStyle::State_Sunken;
538 style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
540 int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
542 QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
543 p.drawText( rect, Qt::AlignVCenter,
544 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
548 option.rect.setX( margin );
549 option.rect.setWidth( 8 );
550 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
554 QSize LocationButton::sizeHint() const
556 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
557 QSize s( fontMetrics().boundingRect( text() ).size() );
558 s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
559 s.setHeight( QPushButton::sizeHint().height() );