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 "components/playlist/selector.hpp"
35 #include "util/customwidgets.hpp"
37 #include "input_manager.hpp"
39 #include <vlc_intf_strings.h>
40 #include <vlc_services_discovery.h>
42 #include <QPushButton>
43 #include <QHeaderView>
45 #include <QModelIndexList>
48 #include <QWheelEvent>
49 #include <QToolButton>
50 #include <QFontMetrics>
51 #include <QStackedLayout>
58 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
59 intf_thread_t *_p_intf,
60 playlist_t *p_playlist,
61 playlist_item_t *p_root,
62 PLSelector *_p_selector,
65 QWidget( _parent ), p_intf( _p_intf ),
66 p_selector( _p_selector )
68 layout = new QGridLayout( this );
69 layout->setSpacing( 0 ); layout->setMargin( 0 );
70 setMinimumWidth( 300 );
75 viewStack = new QStackedLayout();
76 layout->addLayout( viewStack, 1, 0, 1, -1 );
80 currentRootIndexId = -1;
84 getSettings()->beginGroup("Playlist");
86 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
88 getSettings()->endGroup();
90 showView( i_viewMode );
92 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
93 this, browseInto( input_item_t * ) );
95 CONNECT( model, currentChanged( const QModelIndex& ),
96 this, handleExpansion( const QModelIndex& ) );
97 CONNECT( model, rootChanged(), this, handleRootChange() );
100 StandardPLPanel::~StandardPLPanel()
102 getSettings()->beginGroup("Playlist");
104 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
105 if( currentView == treeView )
106 getSettings()->setValue( "view-mode", TREE_VIEW );
107 else if( currentView == listView )
108 getSettings()->setValue( "view-mode", LIST_VIEW );
109 else if( currentView == iconView )
110 getSettings()->setValue( "view-mode", ICON_VIEW );
111 getSettings()->endGroup();
114 /* Unused anymore, but might be useful, like in right-click menu */
115 void StandardPLPanel::gotoPlayingItem()
117 currentView->scrollTo( model->currentIndex() );
120 void StandardPLPanel::handleExpansion( const QModelIndex& index )
122 assert( currentView );
123 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
124 browseInto( index.parent() );
125 currentView->scrollTo( index );
128 void StandardPLPanel::handleRootChange()
133 void StandardPLPanel::popupPlView( const QPoint &point )
135 QModelIndex index = currentView->indexAt( point );
136 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
137 QItemSelectionModel *selection = currentView->selectionModel();
138 QModelIndexList list = selection->selectedIndexes();
140 if( !model->popup( index, globalPoint, list ) )
141 QVLCMenu::PopupMenu( p_intf, true );
144 void StandardPLPanel::popupSelectColumn( QPoint pos )
149 /* We do not offer the option to hide index 0 column, or
150 * QTreeView will behave weird */
152 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
154 QAction* option = menu.addAction(
155 qfu( psz_column_title( i ) ) );
156 option->setCheckable( true );
157 option->setChecked( !treeView->isColumnHidden( j ) );
158 selectColumnsSigMapper->setMapping( option, j );
159 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
161 menu.exec( QCursor::pos() );
164 void StandardPLPanel::toggleColumnShown( int i )
166 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
169 /* Search in the playlist */
170 void StandardPLPanel::search( const QString& searchText )
174 p_selector->getCurrentSelectedItem( &type, &name );
175 if( type != SD_TYPE )
177 bool flat = currentView == iconView || currentView == listView;
178 model->search( searchText,
179 flat ? currentView->rootIndex() : QModelIndex(),
184 void StandardPLPanel::searchDelayed( const QString& searchText )
188 p_selector->getCurrentSelectedItem( &type, &name );
190 if( type == SD_TYPE )
192 if( !name.isEmpty() && !searchText.isEmpty() )
193 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH, qtu( searchText ) );
197 /* Set the root of the new Playlist */
198 /* This activated by the selector selection */
199 void StandardPLPanel::setRoot( playlist_item_t *p_item )
201 model->rebuild( p_item );
204 void StandardPLPanel::browseInto( const QModelIndex &index )
206 if( currentView == iconView || currentView == listView )
208 currentRootIndexId = model->itemId( index );
209 currentView->setRootIndex( index );
212 emit viewChanged( index );
215 void StandardPLPanel::browseInto( )
217 browseInto( currentRootIndexId != -1 && currentView != treeView ?
218 model->index( currentRootIndexId, 0 ) :
222 void StandardPLPanel::wheelEvent( QWheelEvent *e )
224 // Accept this event in order to prevent unwanted volume up/down changes
228 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
230 if (event->type() == QEvent::KeyPress)
232 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
233 if( keyEvent->key() == Qt::Key_Delete ||
234 keyEvent->key() == Qt::Key_Backspace )
243 void StandardPLPanel::deleteSelection()
245 QItemSelectionModel *selection = currentView->selectionModel();
246 QModelIndexList list = selection->selectedIndexes();
247 model->doDelete( list );
250 void StandardPLPanel::createIconView()
252 iconView = new PlIconView( model, this );
253 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
254 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
255 this, popupPlView( const QPoint & ) );
256 CONNECT( iconView, activated( const QModelIndex & ),
257 this, activate( const QModelIndex & ) );
258 iconView->installEventFilter( this );
259 viewStack->addWidget( iconView );
262 void StandardPLPanel::createListView()
264 listView = new PlListView( model, this );
265 listView->setContextMenuPolicy( Qt::CustomContextMenu );
266 CONNECT( listView, customContextMenuRequested( const QPoint & ),
267 this, popupPlView( const QPoint & ) );
268 CONNECT( listView, activated( const QModelIndex & ),
269 this, activate( const QModelIndex & ) );
270 listView->installEventFilter( this );
271 viewStack->addWidget( listView );
275 void StandardPLPanel::createTreeView()
277 /* Create and configure the QTreeView */
278 treeView = new PlTreeView;
280 treeView->setIconSize( QSize( 20, 20 ) );
281 treeView->setAlternatingRowColors( true );
282 treeView->setAnimated( true );
283 treeView->setUniformRowHeights( true );
284 treeView->setSortingEnabled( true );
285 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
286 treeView->header()->setSortIndicatorShown( true );
287 treeView->header()->setClickable( true );
288 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
290 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
291 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
292 treeView->setDragEnabled( true );
293 treeView->setAcceptDrops( true );
294 treeView->setDropIndicatorShown( true );
295 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
297 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
298 treeView->setModel( model );
300 getSettings()->beginGroup("Playlist");
302 if( getSettings()->contains( "headerStateV2" ) )
304 treeView->header()->restoreState(
305 getSettings()->value( "headerStateV2" ).toByteArray() );
309 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
311 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
312 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
313 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
317 getSettings()->endGroup();
319 /* Connections for the TreeView */
320 CONNECT( treeView, activated( const QModelIndex& ),
321 this, activate( const QModelIndex& ) );
322 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
323 this, popupSelectColumn( QPoint ) );
324 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
325 this, popupPlView( const QPoint & ) );
326 treeView->installEventFilter( this );
328 /* SignalMapper for columns */
329 selectColumnsSigMapper = new QSignalMapper( this );
330 CONNECT( selectColumnsSigMapper, mapped( int ),
331 this, toggleColumnShown( int ) );
333 /* Finish the layout */
334 viewStack->addWidget( treeView );
337 void StandardPLPanel::showView( int i_view )
343 if( treeView == NULL )
345 currentView = treeView;
350 if( iconView == NULL )
352 currentView = iconView;
357 if( listView == NULL )
359 currentView = listView;
365 viewStack->setCurrentWidget( currentView );
366 //viewActions[i_view]->setChecked( true );
371 void StandardPLPanel::cycleViews()
373 if( currentView == iconView )
374 showView( TREE_VIEW );
375 else if( currentView == treeView )
376 showView( LIST_VIEW );
377 else if( currentView == listView )
378 showView( ICON_VIEW );
383 void StandardPLPanel::activate( const QModelIndex &index )
385 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
387 if( currentView != treeView )
392 playlist_Lock( THEPL );
393 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
394 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
395 lastActivatedId = p_item->p_input->i_id;
396 playlist_Unlock( THEPL );
397 model->activateItem( index );
401 void StandardPLPanel::browseInto( input_item_t *p_input )
404 if( p_input->i_id != lastActivatedId ) return;
406 playlist_Lock( THEPL );
408 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
411 playlist_Unlock( THEPL );
415 QModelIndex index = model->index( p_item->i_id, 0 );
417 playlist_Unlock( THEPL );
419 if( currentView == treeView )
420 treeView->setExpanded( index, true );
424 lastActivatedId = -1;