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"
36 #include <vlc_intf_strings.h>
38 #include <QPushButton>
39 #include <QHeaderView>
41 #include <QModelIndexList>
44 #include <QSignalMapper>
45 #include <QWheelEvent>
51 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
52 intf_thread_t *_p_intf,
53 playlist_t *p_playlist,
54 playlist_item_t *p_root ):
55 QWidget( _parent ), p_intf( _p_intf )
57 layout = new QGridLayout( this );
58 layout->setSpacing( 0 ); layout->setMargin( 0 );
59 setMinimumWidth( 300 );
61 model = new PLModel( p_playlist, p_intf, p_root, this );
62 CONNECT( model, currentChanged( const QModelIndex& ),
63 this, handleExpansion( const QModelIndex& ) );
69 getSettings()->beginGroup("Playlist");
71 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
72 if( i_viewMode == ICON_VIEW )
75 currentView = iconView;
80 currentView = treeView;
83 getSettings()->endGroup();
90 titleFont.setPointSize( titleFont.pointSize() + 6 );
91 titleFont.setFamily( "Verdana" );
92 title->setFont( titleFont );
93 layout->addWidget( title, 0, 0 );
95 /* A Spacer and the search possibilities */
96 layout->setColumnStretch( 1, 10 );
98 SearchLineEdit *search = new SearchLineEdit( this );
99 search->setMaximumWidth( 300 );
100 layout->addWidget( search, 0, 4 );
101 CONNECT( search, textChanged( const QString& ),
102 this, search( const QString& ) );
103 layout->setColumnStretch( 4, 2 );
105 /* Add item to the playlist button */
106 addButton = new QPushButton;
107 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
108 addButton->setMaximumWidth( 30 );
109 BUTTONACT( addButton, popupAdd() );
110 layout->addWidget( addButton, 0, 3 );
112 QPushButton *viewButton = new QPushButton( this );
113 viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
114 layout->addWidget( viewButton, 0, 2 );
115 BUTTONACT( viewButton, toggleView() );
118 StandardPLPanel::~StandardPLPanel()
120 getSettings()->beginGroup("Playlist");
122 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
123 getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
124 getSettings()->endGroup();
127 /* Unused anymore, but might be useful, like in right-click menu */
128 void StandardPLPanel::gotoPlayingItem()
130 currentView->scrollTo( model->currentIndex() );
133 void StandardPLPanel::handleExpansion( const QModelIndex& index )
135 assert( currentView );
136 currentView->scrollTo( index );
139 /* PopupAdd Menu for the Add Menu */
140 void StandardPLPanel::popupAdd()
143 if( currentRootId == THEPL->p_local_category->i_id ||
144 currentRootId == THEPL->p_local_onelevel->i_id )
146 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
147 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
148 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
150 else if( ( THEPL->p_ml_category &&
151 currentRootId == THEPL->p_ml_category->i_id ) ||
152 ( THEPL->p_ml_onelevel &&
153 currentRootId == THEPL->p_ml_onelevel->i_id ) )
155 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
156 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
157 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
160 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
161 + QPoint( 0, addButton->height() ) );
164 void StandardPLPanel::popupPlView( const QPoint &point )
166 QModelIndex index = currentView->indexAt( point );
167 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
168 QItemSelectionModel *selection = currentView->selectionModel();
169 QModelIndexList list = selection->selectedIndexes();
170 model->popup( index, globalPoint, list );
173 void StandardPLPanel::popupSelectColumn( QPoint pos )
178 /* We do not offer the option to hide index 0 column, or
179 * QTreeView will behave weird */
181 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
183 QAction* option = menu.addAction(
184 qfu( psz_column_title( i ) ) );
185 option->setCheckable( true );
186 option->setChecked( !treeView->isColumnHidden( j ) );
187 selectColumnsSigMapper->setMapping( option, j );
188 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
190 menu.exec( QCursor::pos() );
193 void StandardPLPanel::toggleColumnShown( int i )
195 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
198 /* Search in the playlist */
199 void StandardPLPanel::search( const QString& searchText )
201 model->search( searchText );
204 /* Set the root of the new Playlist */
205 /* This activated by the selector selection */
206 void StandardPLPanel::setRoot( playlist_item_t *p_item )
211 playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
212 if( p_pref_item ) p_item = p_pref_item;
214 /* needed for popupAdd() */
215 currentRootId = p_item->i_id;
217 /* cosmetics, ..still need playlist locking.. */
218 char *psz_title = input_item_GetName( p_item->p_input );
219 title->setText( qfu(psz_title) );
225 model->rebuild( p_item );
227 /* enable/disable adding */
228 if( p_item == THEPL->p_local_category ||
229 p_item == THEPL->p_local_onelevel )
231 addButton->setEnabled( true );
232 addButton->setToolTip( qtr(I_PL_ADDPL) );
234 else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
235 ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
237 addButton->setEnabled( true );
238 addButton->setToolTip( qtr(I_PL_ADDML) );
241 addButton->setEnabled( false );
244 void StandardPLPanel::removeItem( int i_id )
246 model->removeItem( i_id );
249 /* Delete and Suppr key remove the selection
250 FilterKey function and code function */
251 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
262 void StandardPLPanel::deleteSelection()
264 QItemSelectionModel *selection = currentView->selectionModel();
265 QModelIndexList list = selection->selectedIndexes();
266 model->doDelete( list );
269 void StandardPLPanel::createIconView()
271 iconView = new PlIconView( model, this );
272 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
273 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
274 this, popupPlView( const QPoint & ) );
276 layout->addWidget( iconView, 1, 0, 1, -1 );
279 void StandardPLPanel::createTreeView()
281 /* Create and configure the QTreeView */
282 treeView = new QTreeView;
283 treeView->setModel( model );
285 treeView->setIconSize( QSize( 20, 20 ) );
286 treeView->setAlternatingRowColors( true );
287 treeView->setAnimated( true );
288 treeView->setUniformRowHeights( true );
289 treeView->setSortingEnabled( true );
290 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
291 treeView->header()->setSortIndicatorShown( true );
292 treeView->header()->setClickable( true );
293 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
295 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
296 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
297 treeView->setDragEnabled( true );
298 treeView->setAcceptDrops( true );
299 treeView->setDropIndicatorShown( true );
300 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
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 /* Connections for the TreeView */
318 CONNECT( treeView, activated( const QModelIndex& ),
319 model,activateItem( const QModelIndex& ) );
320 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
321 this, popupSelectColumn( QPoint ) );
322 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
323 this, popupPlView( const QPoint & ) );
325 /* SignalMapper for columns */
326 selectColumnsSigMapper = new QSignalMapper( this );
327 CONNECT( selectColumnsSigMapper, mapped( int ),
328 this, toggleColumnShown( int ) );
330 /* Finish the layout */
331 layout->addWidget( treeView, 1, 0, 1, -1 );
334 void StandardPLPanel::toggleView()
336 if( treeView && treeView->isVisible() )
338 if( iconView == NULL )
343 currentView = iconView;
347 if( treeView == NULL )
352 currentView = treeView;
356 void StandardPLPanel::wheelEvent( QWheelEvent *e )
358 // Accept this event in order to prevent unwanted volume up/down changes