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>
41 #include <QPushButton>
42 #include <QHeaderView>
44 #include <QModelIndexList>
47 #include <QWheelEvent>
48 #include <QToolButton>
49 #include <QFontMetrics>
50 #include <QStackedLayout>
57 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
58 intf_thread_t *_p_intf,
59 playlist_t *p_playlist,
60 playlist_item_t *p_root,
61 PLSelector *_p_selector ):
62 QWidget( _parent ), p_intf( _p_intf ),
63 p_selector( _p_selector )
65 layout = new QGridLayout( this );
66 layout->setSpacing( 0 ); layout->setMargin( 0 );
67 setMinimumWidth( 300 );
72 viewStack = new QStackedLayout();
73 layout->addLayout( viewStack, 1, 0, 1, -1 );
75 model = new PLModel( p_playlist, p_intf, p_root, this );
77 currentRootIndexId = -1;
81 getSettings()->beginGroup("Playlist");
83 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
85 getSettings()->endGroup();
87 showView( i_viewMode );
89 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
90 this, browseInto( input_item_t * ) );
92 CONNECT( model, currentChanged( const QModelIndex& ),
93 this, handleExpansion( const QModelIndex& ) );
94 CONNECT( model, rootChanged(), this, handleRootChange() );
97 StandardPLPanel::~StandardPLPanel()
99 getSettings()->beginGroup("Playlist");
101 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
102 if( currentView == treeView )
103 getSettings()->setValue( "view-mode", TREE_VIEW );
104 else if( currentView == listView )
105 getSettings()->setValue( "view-mode", LIST_VIEW );
106 else if( currentView == iconView )
107 getSettings()->setValue( "view-mode", ICON_VIEW );
108 getSettings()->endGroup();
111 /* Unused anymore, but might be useful, like in right-click menu */
112 void StandardPLPanel::gotoPlayingItem()
114 currentView->scrollTo( model->currentIndex() );
117 void StandardPLPanel::handleExpansion( const QModelIndex& index )
119 assert( currentView );
120 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
121 browseInto( index.parent() );
122 currentView->scrollTo( index );
125 void StandardPLPanel::handleRootChange()
130 void StandardPLPanel::popupPlView( const QPoint &point )
132 QModelIndex index = currentView->indexAt( point );
133 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
134 QItemSelectionModel *selection = currentView->selectionModel();
135 QModelIndexList list = selection->selectedIndexes();
137 if( !model->popup( index, globalPoint, list ) )
138 QVLCMenu::PopupMenu( p_intf, true );
141 void StandardPLPanel::popupSelectColumn( QPoint pos )
146 /* We do not offer the option to hide index 0 column, or
147 * QTreeView will behave weird */
149 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
151 QAction* option = menu.addAction(
152 qfu( psz_column_title( i ) ) );
153 option->setCheckable( true );
154 option->setChecked( !treeView->isColumnHidden( j ) );
155 selectColumnsSigMapper->setMapping( option, j );
156 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
158 menu.exec( QCursor::pos() );
161 void StandardPLPanel::toggleColumnShown( int i )
163 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
166 /* Search in the playlist */
167 void StandardPLPanel::search( const QString& searchText )
171 p_selector->getCurrentSelectedItem( &type, &name );
172 if( type != SD_TYPE )
174 bool flat = currentView == iconView || currentView == listView;
175 model->search( searchText,
176 flat ? currentView->rootIndex() : QModelIndex(),
181 void StandardPLPanel::searchDelayed( const QString& searchText )
185 p_selector->getCurrentSelectedItem( &type, &name );
187 if( type == SD_TYPE )
189 if( !name.isEmpty() && !searchText.isEmpty() )
190 playlist_QueryServicesDiscovery( THEPL, qtu( name ), qtu( searchText ) );
194 /* Set the root of the new Playlist */
195 /* This activated by the selector selection */
196 void StandardPLPanel::setRoot( playlist_item_t *p_item )
198 model->rebuild( p_item );
201 void StandardPLPanel::browseInto( const QModelIndex &index )
203 if( currentView == iconView || currentView == listView )
205 currentRootIndexId = model->itemId( index );
206 currentView->setRootIndex( index );
209 emit viewChanged( index );
212 void StandardPLPanel::browseInto( )
214 browseInto( currentRootIndexId != -1 && currentView != treeView ?
215 model->index( currentRootIndexId, 0 ) :
219 void StandardPLPanel::wheelEvent( QWheelEvent *e )
221 // Accept this event in order to prevent unwanted volume up/down changes
225 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
227 if (event->type() == QEvent::KeyPress)
229 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
230 if( keyEvent->key() == Qt::Key_Delete ||
231 keyEvent->key() == Qt::Key_Backspace )
240 void StandardPLPanel::deleteSelection()
242 QItemSelectionModel *selection = currentView->selectionModel();
243 QModelIndexList list = selection->selectedIndexes();
244 model->doDelete( list );
247 void StandardPLPanel::createIconView()
249 iconView = new PlIconView( model, this );
250 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
251 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
252 this, popupPlView( const QPoint & ) );
253 CONNECT( iconView, activated( const QModelIndex & ),
254 this, activate( const QModelIndex & ) );
255 iconView->installEventFilter( this );
256 viewStack->addWidget( iconView );
259 void StandardPLPanel::createListView()
261 listView = new PlListView( model, this );
262 listView->setContextMenuPolicy( Qt::CustomContextMenu );
263 CONNECT( listView, customContextMenuRequested( const QPoint & ),
264 this, popupPlView( const QPoint & ) );
265 CONNECT( listView, activated( const QModelIndex & ),
266 this, activate( const QModelIndex & ) );
267 listView->installEventFilter( this );
268 viewStack->addWidget( listView );
272 void StandardPLPanel::createTreeView()
274 /* Create and configure the QTreeView */
275 treeView = new PlTreeView;
277 treeView->setIconSize( QSize( 20, 20 ) );
278 treeView->setAlternatingRowColors( true );
279 treeView->setAnimated( true );
280 treeView->setUniformRowHeights( true );
281 treeView->setSortingEnabled( true );
282 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
283 treeView->header()->setSortIndicatorShown( true );
284 treeView->header()->setClickable( true );
285 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
287 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
288 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
289 treeView->setDragEnabled( true );
290 treeView->setAcceptDrops( true );
291 treeView->setDropIndicatorShown( true );
292 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
294 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
295 treeView->setModel( model );
297 getSettings()->beginGroup("Playlist");
299 if( getSettings()->contains( "headerStateV2" ) )
301 treeView->header()->restoreState(
302 getSettings()->value( "headerStateV2" ).toByteArray() );
306 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
308 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
309 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
310 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
314 getSettings()->endGroup();
316 /* Connections for the TreeView */
317 CONNECT( treeView, activated( const QModelIndex& ),
318 this, activate( const QModelIndex& ) );
319 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
320 this, popupSelectColumn( QPoint ) );
321 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
322 this, popupPlView( const QPoint & ) );
323 treeView->installEventFilter( this );
325 /* SignalMapper for columns */
326 selectColumnsSigMapper = new QSignalMapper( this );
327 CONNECT( selectColumnsSigMapper, mapped( int ),
328 this, toggleColumnShown( int ) );
330 /* Finish the layout */
331 viewStack->addWidget( treeView );
334 void StandardPLPanel::showView( int i_view )
340 if( treeView == NULL )
342 currentView = treeView;
347 if( iconView == NULL )
349 currentView = iconView;
354 if( listView == NULL )
356 currentView = listView;
362 viewStack->setCurrentWidget( currentView );
363 //viewActions[i_view]->setChecked( true );
368 void StandardPLPanel::cycleViews()
370 if( currentView == iconView )
371 showView( TREE_VIEW );
372 else if( currentView == treeView )
373 showView( LIST_VIEW );
374 else if( currentView == listView )
375 showView( ICON_VIEW );
380 void StandardPLPanel::activate( const QModelIndex &index )
382 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
384 if( currentView != treeView )
389 playlist_Lock( THEPL );
390 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
391 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
392 lastActivatedId = p_item->p_input->i_id;
393 playlist_Unlock( THEPL );
394 model->activateItem( index );
398 void StandardPLPanel::browseInto( input_item_t *p_input )
401 if( p_input->i_id != lastActivatedId ) return;
403 playlist_Lock( THEPL );
405 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
408 playlist_Unlock( THEPL );
412 QModelIndex index = model->index( p_item->i_id, 0 );
414 playlist_Unlock( THEPL );
416 if( currentView == treeView )
417 treeView->setExpanded( index, true );
421 lastActivatedId = -1;