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 );
67 getSettings()->beginGroup("Playlist");
69 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
70 if( i_viewMode == ICON_VIEW )
71 iconView = new PlIconView( model, this );
75 getSettings()->endGroup();
82 titleFont.setPointSize( titleFont.pointSize() + 6 );
83 titleFont.setFamily( "Verdana" );
84 title->setFont( titleFont );
85 layout->addWidget( title, 0, 0 );
87 /* A Spacer and the search possibilities */
88 layout->setColumnStretch( 1, 10 );
90 SearchLineEdit *search = new SearchLineEdit( this );
91 search->setMaximumWidth( 300 );
92 layout->addWidget( search, 0, 4 );
93 CONNECT( search, textChanged( const QString& ),
94 this, search( const QString& ) );
95 layout->setColumnStretch( 4, 2 );
97 /* Add item to the playlist button */
98 addButton = new QPushButton;
99 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
100 addButton->setMaximumWidth( 30 );
101 BUTTONACT( addButton, popupAdd() );
102 layout->addWidget( addButton, 0, 3 );
104 QPushButton *viewButton = new QPushButton( this );
105 viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
106 layout->addWidget( viewButton, 0, 2 );
107 BUTTONACT( viewButton, toggleView() );
110 StandardPLPanel::~StandardPLPanel()
112 getSettings()->beginGroup("Playlist");
114 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
115 getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
116 getSettings()->endGroup();
119 /* Unused anymore, but might be useful, like in right-click menu */
120 void StandardPLPanel::gotoPlayingItem()
122 currentView->scrollTo( model->currentIndex() );
125 void StandardPLPanel::handleExpansion( const QModelIndex& index )
128 treeView->scrollTo( index );
131 /* PopupAdd Menu for the Add Menu */
132 void StandardPLPanel::popupAdd()
135 if( currentRootId == THEPL->p_local_category->i_id ||
136 currentRootId == THEPL->p_local_onelevel->i_id )
138 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
139 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
140 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
142 else if( ( THEPL->p_ml_category &&
143 currentRootId == THEPL->p_ml_category->i_id ) ||
144 ( THEPL->p_ml_onelevel &&
145 currentRootId == THEPL->p_ml_onelevel->i_id ) )
147 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
148 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
149 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
152 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
153 + QPoint( 0, addButton->height() ) );
156 void StandardPLPanel::popupPlView( const QPoint &point )
158 QModelIndex index = currentView->indexAt( point );
159 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
160 QItemSelectionModel *selection = currentView->selectionModel();
161 QModelIndexList list = selection->selectedIndexes();
162 model->popup( index, globalPoint, list );
165 void StandardPLPanel::popupSelectColumn( QPoint pos )
170 /* We do not offer the option to hide index 0 column, or
171 * QTreeView will behave weird */
173 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
175 QAction* option = menu.addAction(
176 qfu( psz_column_title( i ) ) );
177 option->setCheckable( true );
178 option->setChecked( !treeView->isColumnHidden( j ) );
179 selectColumnsSigMapper->setMapping( option, j );
180 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
182 menu.exec( QCursor::pos() );
185 void StandardPLPanel::toggleColumnShown( int i )
187 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
190 /* Search in the playlist */
191 void StandardPLPanel::search( const QString& searchText )
193 model->search( searchText );
196 /* Set the root of the new Playlist */
197 /* This activated by the selector selection */
198 void StandardPLPanel::setRoot( playlist_item_t *p_item )
203 playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
204 if( p_pref_item ) p_item = p_pref_item;
206 /* needed for popupAdd() */
207 currentRootId = p_item->i_id;
209 /* cosmetics, ..still need playlist locking.. */
210 char *psz_title = input_item_GetName( p_item->p_input );
211 title->setText( qfu(psz_title) );
217 model->rebuild( p_item );
219 /* enable/disable adding */
220 if( p_item == THEPL->p_local_category ||
221 p_item == THEPL->p_local_onelevel )
223 addButton->setEnabled( true );
224 addButton->setToolTip( qtr(I_PL_ADDPL) );
226 else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
227 ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
229 addButton->setEnabled( true );
230 addButton->setToolTip( qtr(I_PL_ADDML) );
233 addButton->setEnabled( false );
236 void StandardPLPanel::removeItem( int i_id )
238 model->removeItem( i_id );
241 /* Delete and Suppr key remove the selection
242 FilterKey function and code function */
243 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
254 void StandardPLPanel::deleteSelection()
256 QItemSelectionModel *selection = currentView->selectionModel();
257 QModelIndexList list = selection->selectedIndexes();
258 model->doDelete( list );
261 void StandardPLPanel::createIconView()
263 iconView = new PlIconView( model, this );
264 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
265 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
266 this, popupPlView( const QPoint & ) );
268 layout->addWidget( iconView, 1, 0, 1, -1 );
271 void StandardPLPanel::createTreeView()
273 /* Create and configure the QTreeView */
274 treeView = new QTreeView;
275 treeView->setModel( model );
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 if( getSettings()->contains( "headerStateV2" ) )
296 treeView->header()->restoreState(
297 getSettings()->value( "headerStateV2" ).toByteArray() );
301 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
303 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
304 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
305 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
309 /* Connections for the TreeView */
310 CONNECT( treeView, activated( const QModelIndex& ),
311 model,activateItem( const QModelIndex& ) );
312 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
313 this, popupSelectColumn( QPoint ) );
314 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
315 this, popupPlView( const QPoint & ) );
316 CONNECT( model, currentChanged( const QModelIndex& ),
317 this, handleExpansion( const QModelIndex& ) );
319 /* SignalMapper for columns */
320 selectColumnsSigMapper = new QSignalMapper( this );
321 CONNECT( selectColumnsSigMapper, mapped( int ),
322 this, toggleColumnShown( int ) );
324 /* Finish the layout */
325 layout->addWidget( treeView, 1, 0, 1, -1 );
328 void StandardPLPanel::toggleView()
330 if( treeView && treeView->isVisible() )
332 if( iconView == NULL )
337 currentView = iconView;
341 if( treeView == NULL )
346 currentView = treeView;
350 void StandardPLPanel::wheelEvent( QWheelEvent *e )
352 // Accept this event in order to prevent unwanted volume up/down changes