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 *****************************************************************************/
30 #include "dialogs_provider.hpp"
32 #include "components/playlist/playlist_model.hpp"
33 #include "components/playlist/standardpanel.hpp"
34 #include "components/playlist/icon_view.hpp"
35 #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>
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53 intf_thread_t *_p_intf,
54 playlist_t *p_playlist,
55 playlist_item_t *p_root ):
56 QWidget( _parent ), p_intf( _p_intf )
58 layout = new QGridLayout( this );
59 layout->setSpacing( 0 ); layout->setMargin( 0 );
60 setMinimumWidth( 300 );
62 model = new PLModel( p_playlist, p_intf, p_root, this );
64 /* Create and configure the QTreeView */
65 treeView = new QTreeView;
66 treeView->setModel( model );
69 treeView->setIconSize( QSize( 20, 20 ) );
70 treeView->setAlternatingRowColors( true );
71 treeView->setAnimated( true );
72 treeView->setUniformRowHeights( true );
73 treeView->setSortingEnabled( true );
74 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
75 treeView->header()->setSortIndicatorShown( true );
76 treeView->header()->setClickable( true );
77 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
79 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
80 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
81 treeView->setDragEnabled( true );
82 treeView->setAcceptDrops( true );
83 treeView->setDropIndicatorShown( true );
84 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
87 getSettings()->beginGroup("Playlist");
88 if( getSettings()->contains( "headerStateV2" ) )
90 treeView->header()->restoreState(
91 getSettings()->value( "headerStateV2" ).toByteArray() );
95 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
97 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
98 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
99 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
102 getSettings()->endGroup();
104 /* Connections for the TreeView */
105 CONNECT( treeView, activated( const QModelIndex& ),
106 model,activateItem( const QModelIndex& ) );
107 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
108 this, popupSelectColumn( QPoint ) );
109 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
110 this, popupPlView( const QPoint & ) );
111 CONNECT( model, currentChanged( const QModelIndex& ),
112 this, handleExpansion( const QModelIndex& ) );
119 titleFont.setPointSize( titleFont.pointSize() + 6 );
120 titleFont.setFamily( "Verdana" );
121 title->setFont( titleFont );
122 layout->addWidget( title, 0, 0 );
124 /* A Spacer and the search possibilities */
125 layout->setColumnStretch( 1, 10 );
127 SearchLineEdit *search = new SearchLineEdit( this );
128 search->setMaximumWidth( 200 );
129 layout->addWidget( search, 0, 4 );
130 CONNECT( search, textChanged( const QString& ),
131 this, search( const QString& ) );
132 layout->setColumnStretch( 4, 1 );
134 /* Add item to the playlist button */
135 addButton = new QPushButton;
136 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
137 addButton->setMaximumWidth( 30 );
138 BUTTONACT( addButton, popupAdd() );
139 layout->addWidget( addButton, 0, 3 );
141 QPushButton *viewButton = new QPushButton( this );
142 viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
143 layout->addWidget( viewButton, 0, 2 );
144 BUTTONACT( viewButton, toggleView() );
146 /* Finish the layout */
147 layout->addWidget( treeView, 1, 0, 1, -1 );
149 selectColumnsSigMapper = new QSignalMapper( this );
150 CONNECT( selectColumnsSigMapper, mapped( int ),
151 this, toggleColumnShown( int ) );
154 StandardPLPanel::~StandardPLPanel()
156 getSettings()->beginGroup("Playlist");
157 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
158 getSettings()->endGroup();
161 /* Unused anymore, but might be useful, like in right-click menu */
162 void StandardPLPanel::gotoPlayingItem()
164 treeView->scrollTo( model->currentIndex() );
167 void StandardPLPanel::handleExpansion( const QModelIndex& index )
169 treeView->scrollTo( index );
172 /* PopupAdd Menu for the Add Menu */
173 void StandardPLPanel::popupAdd()
176 if( currentRootId == THEPL->p_local_category->i_id ||
177 currentRootId == THEPL->p_local_onelevel->i_id )
179 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
180 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
181 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
183 else if( ( THEPL->p_ml_category &&
184 currentRootId == THEPL->p_ml_category->i_id ) ||
185 ( THEPL->p_ml_onelevel &&
186 currentRootId == THEPL->p_ml_onelevel->i_id ) )
188 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
189 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
190 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
193 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
194 + QPoint( 0, addButton->height() ) );
197 void StandardPLPanel::popupSelectColumn( QPoint pos )
201 /* We do not offer the option to hide index 0 column, or
202 * QTreeView will behave weird */
204 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
206 QAction* option = menu.addAction(
207 qfu( psz_column_title( i ) ) );
208 option->setCheckable( true );
209 option->setChecked( !treeView->isColumnHidden( j ) );
210 selectColumnsSigMapper->setMapping( option, j );
211 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
213 menu.exec( QCursor::pos() );
216 void StandardPLPanel::popupPlView( const QPoint &point )
218 QAbstractItemView *aView;
219 if ( treeView->isVisible() ) aView = treeView;
220 else aView = iconView;
222 QModelIndex index = aView->indexAt( point );
223 QPoint globalPoint = aView->viewport()->mapToGlobal( point );
224 QItemSelectionModel *selection = aView->selectionModel();
225 QModelIndexList list = selection->selectedIndexes();
226 model->popup( index, globalPoint, list );
229 void StandardPLPanel::toggleColumnShown( int i )
231 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
234 /* Search in the playlist */
235 void StandardPLPanel::search( const QString& searchText )
237 model->search( searchText );
240 /* Set the root of the new Playlist */
241 /* This activated by the selector selection */
242 void StandardPLPanel::setRoot( playlist_item_t *p_item )
247 playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
248 if( p_pref_item ) p_item = p_pref_item;
250 /* needed for popupAdd() */
251 currentRootId = p_item->i_id;
253 /* cosmetics, ..still need playlist locking.. */
254 char *psz_title = input_item_GetName( p_item->p_input );
255 title->setText( qfu(psz_title) );
261 model->rebuild( p_item );
263 /* enable/disable adding */
264 if( p_item == THEPL->p_local_category ||
265 p_item == THEPL->p_local_onelevel )
267 addButton->setEnabled( true );
268 addButton->setToolTip( qtr(I_PL_ADDPL) );
270 else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
271 ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
273 addButton->setEnabled( true );
274 addButton->setToolTip( qtr(I_PL_ADDML) );
277 addButton->setEnabled( false );
280 void StandardPLPanel::removeItem( int i_id )
282 model->removeItem( i_id );
285 /* Delete and Suppr key remove the selection
286 FilterKey function and code function */
287 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
298 void StandardPLPanel::deleteSelection()
300 QItemSelectionModel *selection = treeView->selectionModel();
301 QModelIndexList list = selection->selectedIndexes();
302 model->doDelete( list );
305 void StandardPLPanel::toggleView()
307 if( treeView && treeView->isVisible() )
309 if( iconView == NULL )
311 iconView = new PlIconView( model, this );
312 layout->addWidget( iconView, 1, 0, 1, -1 );
313 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
314 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
315 this, popupPlView( const QPoint & ) );
327 void StandardPLPanel::wheelEvent( QWheelEvent *e )
329 // Accept this event in order to prevent unwanted volume up/down changes