1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include "dialogs_provider.hpp"
30 #include "components/playlist/playlist_model.hpp"
31 #include "components/playlist/panels.hpp"
32 #include "util/customwidgets.hpp"
34 #include <vlc_intf_strings.h>
37 #include <QPushButton>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
40 #include <QHeaderView>
42 #include <QModelIndexList>
45 #include <QSpacerItem>
47 #include <QSignalMapper>
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54 intf_thread_t *_p_intf,
55 playlist_t *p_playlist,
56 playlist_item_t *p_root ):
57 PLPanel( _parent, _p_intf )
59 model = new PLModel( p_playlist, p_intf, p_root, -1, this );
61 QVBoxLayout *layout = new QVBoxLayout();
62 layout->setSpacing( 0 ); layout->setMargin( 0 );
64 /* Create and configure the QTreeView */
65 view = new QVLCTreeView( 0 );
66 view->setSortingEnabled( true );
67 view->sortByColumn( 0 , Qt::AscendingOrder );
68 view->setModel( model );
69 view->setIconSize( QSize( 20, 20 ) );
70 view->setAlternatingRowColors( true );
71 view->setAnimated( true );
72 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
73 view->setDragEnabled( true );
74 view->setAcceptDrops( true );
75 view->setDropIndicatorShown( true );
76 view->setAutoScroll( true );
79 getSettings()->beginGroup("Playlist");
81 if( getSettings()->contains( "headerState" ) )
83 view->header()->restoreState(
84 getSettings()->value( "headerState" ).toByteArray() );
89 /* Configure the size of the header */
90 view->header()->resizeSection( 0, 200 );
91 view->header()->resizeSection( 1, 80 );
93 view->header()->setSortIndicatorShown( true );
94 view->header()->setClickable( true );
95 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
96 getSettings()->endGroup();
98 /* Connections for the TreeView */
99 CONNECT( view, activated( const QModelIndex& ) ,
100 model,activateItem( const QModelIndex& ) );
101 CONNECT( view, rightClicked( QModelIndex , QPoint ),
102 this, doPopup( QModelIndex, QPoint ) );
103 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
104 this, handleExpansion( const QModelIndex& ) );
105 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
106 this, popupSelectColumn( QPoint ) );
109 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
111 /* Buttons configuration */
112 QHBoxLayout *buttons = new QHBoxLayout;
114 /* Add item to the playlist button */
115 addButton = new QPushButton;
116 addButton->setIcon( QIcon( ":/playlist_add" ) );
117 addButton->setMaximumWidth( 30 );
118 BUTTONACT( addButton, popupAdd() );
119 buttons->addWidget( addButton );
121 /* Random 2-state button */
122 randomButton = new QPushButton( this );
123 if( model->hasRandom() )
125 randomButton->setIcon( QIcon( ":/shuffle_on" ));
126 randomButton->setToolTip( qtr( I_PL_RANDOM ));
130 randomButton->setIcon( QIcon( ":/shuffle_off" ) );
131 randomButton->setToolTip( qtr( I_PL_NORANDOM ));
133 BUTTONACT( randomButton, toggleRandom() );
134 buttons->addWidget( randomButton );
136 /* Repeat 3-state button */
137 repeatButton = new QPushButton( this );
138 if( model->hasRepeat() )
140 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
141 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
143 else if( model->hasLoop() )
145 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
146 repeatButton->setToolTip( qtr( I_PL_LOOP ));
150 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
151 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
153 BUTTONACT( repeatButton, toggleRepeat() );
154 buttons->addWidget( repeatButton );
157 gotoPlayingButton = new QPushButton;
158 BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
159 qtr( "Show the current item" ), gotoPlayingItem() );
160 buttons->addWidget( gotoPlayingButton );
162 /* A Spacer and the search possibilities */
163 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
164 buttons->addItem( spacer );
166 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
167 buttons->addWidget( filter );
169 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
170 searchLine->setMinimumWidth( 80 );
171 CONNECT( searchLine, textChanged(QString), this, search(QString));
172 buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
174 QPushButton *clear = new QPushButton;
175 clear->setMaximumWidth( 30 );
176 BUTTON_SET_ACT_I( clear, "", clear, qtr( "Clear" ), clearFilter() );
177 buttons->addWidget( clear );
179 /* Finish the layout */
180 layout->addWidget( view );
181 layout->addLayout( buttons );
182 // layout->addWidget( bar );
186 /* Function to toggle between the Repeat states */
187 void StandardPLPanel::toggleRepeat()
189 if( model->hasRepeat() )
191 model->setRepeat( false ); model->setLoop( true );
192 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
193 repeatButton->setToolTip( qtr( I_PL_LOOP ));
195 else if( model->hasLoop() )
197 model->setRepeat( false ) ; model->setLoop( false );
198 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
199 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
203 model->setRepeat( true );
204 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
205 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
209 /* Function to toggle between the Random states */
210 void StandardPLPanel::toggleRandom()
212 bool prev = model->hasRandom();
213 model->setRandom( !prev );
214 randomButton->setIcon( prev ?
215 QIcon( ":/shuffle_off" ) :
216 QIcon( ":/shuffle_on" ) );
217 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
220 void StandardPLPanel::gotoPlayingItem()
222 view->scrollTo( view->currentIndex() );
225 void StandardPLPanel::handleExpansion( const QModelIndex &index )
227 if( model->isCurrent( index ) )
228 view->scrollTo( index, QAbstractItemView::EnsureVisible );
231 void StandardPLPanel::setCurrentRootId( int _new )
233 currentRootId = _new;
234 if( currentRootId == THEPL->p_local_category->i_id ||
235 currentRootId == THEPL->p_local_onelevel->i_id )
237 addButton->setEnabled( true );
238 addButton->setToolTip( qtr(I_PL_ADDPL) );
240 else if( ( THEPL->p_ml_category &&
241 currentRootId == THEPL->p_ml_category->i_id ) ||
242 ( THEPL->p_ml_onelevel &&
243 currentRootId == THEPL->p_ml_onelevel->i_id ) )
245 addButton->setEnabled( true );
246 addButton->setToolTip( qtr(I_PL_ADDML) );
249 addButton->setEnabled( false );
252 /* PopupAdd Menu for the Add Menu */
253 void StandardPLPanel::popupAdd()
256 if( currentRootId == THEPL->p_local_category->i_id ||
257 currentRootId == THEPL->p_local_onelevel->i_id )
259 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
260 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
262 else if( ( THEPL->p_ml_category &&
263 currentRootId == THEPL->p_ml_category->i_id ) ||
264 ( THEPL->p_ml_onelevel &&
265 currentRootId == THEPL->p_ml_onelevel->i_id ) )
267 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
268 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
270 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
271 + QPoint( 0, addButton->height() ) );
274 void StandardPLPanel::popupSelectColumn( QPoint pos )
276 ContextUpdateMapper = new QSignalMapper(this);
280 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
283 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
285 QAction* option = selectColMenu.addAction(
286 qfu( psz_column_title( i_column ) ) );
287 option->setCheckable( true );
288 option->setChecked( model->shownFlags() & i_column );
289 ContextUpdateMapper->setMapping( option, i_column );
290 CONNECT( option, triggered(), ContextUpdateMapper, map() );
293 selectColMenu.exec( QCursor::pos() );
296 /* ClearFilter LineEdit */
297 void StandardPLPanel::clearFilter()
299 searchLine->setText( "" );
302 /* Search in the playlist */
303 void StandardPLPanel::search( QString searchText )
305 model->search( searchText );
308 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
310 if( !index.isValid() ) return;
311 QItemSelectionModel *selection = view->selectionModel();
312 QModelIndexList list = selection->selectedIndexes();
313 model->popup( index, point, list );
316 /* Set the root of the new Playlist */
317 /* This activated by the selector selection */
318 void StandardPLPanel::setRoot( int i_root_id )
321 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
324 p_item = playlist_GetPreferredNode( THEPL, p_item );
328 model->rebuild( p_item );
331 void StandardPLPanel::removeItem( int i_id )
333 model->removeItem( i_id );
336 /* Delete and Suppr key remove the selection
337 FilterKey function and code function */
338 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
349 void StandardPLPanel::deleteSelection()
351 QItemSelectionModel *selection = view->selectionModel();
352 QModelIndexList list = selection->selectedIndexes();
353 model->doDelete( list );
356 StandardPLPanel::~StandardPLPanel()
359 getSettings()->beginGroup("Playlist");
360 getSettings()->setValue( "headerState", view->header()->saveState() );
361 getSettings()->endGroup();