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& ) );
141 CONNECT( model, rootChanged(), this, handleRootChange() );
144 StandardPLPanel::~StandardPLPanel()
146 getSettings()->beginGroup("Playlist");
148 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
149 getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
150 getSettings()->endGroup();
153 /* Unused anymore, but might be useful, like in right-click menu */
154 void StandardPLPanel::gotoPlayingItem()
156 currentView->scrollTo( model->currentIndex() );
159 void StandardPLPanel::handleExpansion( const QModelIndex& index )
161 assert( currentView );
162 currentView->scrollTo( index );
165 void StandardPLPanel::handleRootChange()
167 /* needed for popupAdd() */
168 PLItem *root = model->getItem( QModelIndex() );
169 currentRootId = root->id();
171 locationBar->setIndex( QModelIndex() );
173 /* enable/disable adding */
174 if( currentRootId == THEPL->p_playing->i_id )
176 addButton->setEnabled( true );
177 addButton->setToolTip( qtr(I_PL_ADDPL) );
179 else if( THEPL->p_media_library &&
180 currentRootId == THEPL->p_media_library->i_id )
182 addButton->setEnabled( true );
183 addButton->setToolTip( qtr(I_PL_ADDML) );
186 addButton->setEnabled( false );
189 /* PopupAdd Menu for the Add Menu */
190 void StandardPLPanel::popupAdd()
193 if( currentRootId == THEPL->p_playing->i_id )
195 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
196 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
197 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
199 else if( THEPL->p_media_library &&
200 currentRootId == THEPL->p_media_library->i_id )
202 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
203 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
204 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
207 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
208 + QPoint( 0, addButton->height() ) );
211 void StandardPLPanel::popupPlView( const QPoint &point )
213 QModelIndex index = currentView->indexAt( point );
214 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
215 if( !index.isValid() ){
216 QVLCMenu::PopupMenu( p_intf, true );
220 QItemSelectionModel *selection = currentView->selectionModel();
221 QModelIndexList list = selection->selectedIndexes();
222 model->popup( index, globalPoint, list );
226 void StandardPLPanel::popupSelectColumn( QPoint pos )
231 /* We do not offer the option to hide index 0 column, or
232 * QTreeView will behave weird */
234 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
236 QAction* option = menu.addAction(
237 qfu( psz_column_title( i ) ) );
238 option->setCheckable( true );
239 option->setChecked( !treeView->isColumnHidden( j ) );
240 selectColumnsSigMapper->setMapping( option, j );
241 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
243 menu.exec( QCursor::pos() );
246 void StandardPLPanel::toggleColumnShown( int i )
248 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
251 /* Search in the playlist */
252 void StandardPLPanel::search( const QString& searchText )
254 model->search( searchText );
257 /* Set the root of the new Playlist */
258 /* This activated by the selector selection */
259 void StandardPLPanel::setRoot( playlist_item_t *p_item )
261 model->rebuild( p_item );
264 /* Delete and Suppr key remove the selection
265 FilterKey function and code function */
266 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
277 void StandardPLPanel::deleteSelection()
279 QItemSelectionModel *selection = currentView->selectionModel();
280 QModelIndexList list = selection->selectedIndexes();
281 model->doDelete( list );
284 void StandardPLPanel::createIconView()
286 iconView = new PlIconView( model, this );
287 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
288 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
289 this, popupPlView( const QPoint & ) );
290 CONNECT( iconView, activated( const QModelIndex & ),
291 this, activate( const QModelIndex & ) );
292 CONNECT( locationBar, invoked( const QModelIndex & ),
293 iconView, setRootIndex( const QModelIndex & ) );
295 layout->addWidget( iconView, 1, 0, 1, -1 );
298 void StandardPLPanel::createTreeView()
300 /* Create and configure the QTreeView */
301 treeView = new QTreeView;
303 treeView->setIconSize( QSize( 20, 20 ) );
304 treeView->setAlternatingRowColors( true );
305 treeView->setAnimated( true );
306 treeView->setUniformRowHeights( true );
307 treeView->setSortingEnabled( true );
308 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
309 treeView->header()->setSortIndicatorShown( true );
310 treeView->header()->setClickable( true );
311 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
313 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
314 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
315 treeView->setDragEnabled( true );
316 treeView->setAcceptDrops( true );
317 treeView->setDropIndicatorShown( true );
318 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
320 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
321 treeView->setModel( model );
323 if( getSettings()->contains( "headerStateV2" ) )
325 treeView->header()->restoreState(
326 getSettings()->value( "headerStateV2" ).toByteArray() );
330 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
332 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
333 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
334 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
338 /* Connections for the TreeView */
339 CONNECT( treeView, activated( const QModelIndex& ),
340 this, activate( const QModelIndex& ) );
341 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
342 this, popupSelectColumn( QPoint ) );
343 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
344 this, popupPlView( const QPoint & ) );
346 /* SignalMapper for columns */
347 selectColumnsSigMapper = new QSignalMapper( this );
348 CONNECT( selectColumnsSigMapper, mapped( int ),
349 this, toggleColumnShown( int ) );
351 /* Finish the layout */
352 layout->addWidget( treeView, 1, 0, 1, -1 );
355 void StandardPLPanel::showView( int i_view )
361 if( treeView == NULL )
363 locationBar->setIndex( treeView->rootIndex() );
364 if( iconView ) iconView->hide();
366 currentView = treeView;
367 treeViewAction->setChecked( true );
372 if( iconView == NULL )
375 locationBar->setIndex( iconView->rootIndex() );
376 if( treeView ) treeView->hide();
378 currentView = iconView;
379 iconViewAction->setChecked( true );
386 void StandardPLPanel::cycleViews()
388 if( currentView == iconView )
389 showView( TREE_VIEW );
390 else if( currentView == treeView )
391 showView( ICON_VIEW );
396 void StandardPLPanel::wheelEvent( QWheelEvent *e )
398 // Accept this event in order to prevent unwanted volume up/down changes
402 void StandardPLPanel::activate( const QModelIndex &index )
404 if( model->hasChildren( index ) )
406 if( currentView == iconView ) {
407 iconView->setRootIndex( index );
408 //title->setText( index.data().toString() );
409 locationBar->setIndex( index );
414 playlist_Lock( THEPL );
415 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
416 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
417 last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
418 playlist_Unlock( THEPL );
419 model->activateItem( index );
423 void StandardPLPanel::browseInto( input_item_t *p_input )
426 if( p_input->i_id != last_activated_id ) return;
428 playlist_Lock( THEPL );
430 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
433 playlist_Unlock( THEPL );
437 QModelIndex index = model->index( p_item->i_id, 0 );
439 playlist_Unlock( THEPL );
441 if( currentView == iconView ) {
442 iconView->setRootIndex( index );
443 locationBar->setIndex( index );
446 treeView->setExpanded( index, true );
448 last_activated_id = -1;
453 LocationBar::LocationBar( PLModel *m )
456 mapper = new QSignalMapper( this );
457 CONNECT( mapper, mapped( int ), this, invoke( int ) );
459 box = new QHBoxLayout;
460 box->setSpacing( 0 );
461 box->setContentsMargins( 0, 0, 0, 0 );
465 void LocationBar::setIndex( const QModelIndex &index )
467 qDeleteAll( buttons );
469 QModelIndex i = index;
473 PLItem *item = model->getItem( i );
475 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
476 QString text = qfu(fb_name);
478 QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
479 if( bold ) btn->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
480 box->insertWidget( 0, btn, bold ? 1 : 0 );
481 buttons.append( btn );
483 mapper->setMapping( btn, item->id() );
484 CONNECT( btn, clicked( ), mapper, map( ) );
488 if( i.isValid() ) i = i.parent();
493 void LocationBar::setRootIndex()
495 setIndex( QModelIndex() );
498 void LocationBar::invoke( int i_id )
500 QModelIndex index = model->index( i_id, 0 );
502 emit invoked ( index );
505 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
509 font.setBold( bold );
516 void LocationButton::paintEvent ( QPaintEvent * event )
518 QStyleOptionButton option;
519 option.initFrom( this );
520 //option.rect = rect();
521 //option.features = QStyleOptionButton::Flat;
522 option.state |= QStyle::State_Enabled;
523 //option.state |= isChecked() ? QStyle::State_On : QStyle::State_Off;
524 //if( isDown() ) option.state |= QStyle::State_Sunken;
528 style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
530 int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
532 QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
533 p.drawText( rect, Qt::AlignVCenter,
534 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
538 option.rect.setX( margin );
539 option.rect.setWidth( 8 );
540 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
544 QSize LocationButton::sizeHint() const
546 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
547 QSize s( fontMetrics().boundingRect( text() ).size() );
548 s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
549 s.setHeight( QPushButton::sizeHint().height() );