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 if( getSettings()->contains( "headerState" ) )
81 view->header()->restoreState( getSettings()->value( "headerState" ).toByteArray() );
82 msg_Dbg( p_intf, "exists" );
87 /* Configure the size of the header */
88 view->header()->resizeSection( 0, 200 );
89 view->header()->resizeSection( 1, 80 );
90 view->header()->setSortIndicatorShown( true );
91 view->header()->setClickable( true );
92 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
95 /* Connections for the TreeView */
96 CONNECT( view, activated( const QModelIndex& ) ,
97 model,activateItem( const QModelIndex& ) );
98 CONNECT( view, rightClicked( QModelIndex , QPoint ),
99 this, doPopup( QModelIndex, QPoint ) );
100 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
101 this, handleExpansion( const QModelIndex& ) );
102 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
103 this, popupSelectColumn( QPoint ) );
106 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
108 /* Buttons configuration */
109 QHBoxLayout *buttons = new QHBoxLayout;
111 /* Add item to the playlist button */
112 addButton = new QPushButton;
113 addButton->setIcon( QIcon( ":/playlist_add" ) );
114 addButton->setMaximumWidth( 30 );
115 BUTTONACT( addButton, popupAdd() );
116 buttons->addWidget( addButton );
118 /* Random 2-state button */
119 randomButton = new QPushButton( this );
120 if( model->hasRandom() )
122 randomButton->setIcon( QIcon( ":/shuffle_on" ));
123 randomButton->setToolTip( qtr( I_PL_RANDOM ));
127 randomButton->setIcon( QIcon( ":/shuffle_off" ) );
128 randomButton->setToolTip( qtr( I_PL_NORANDOM ));
130 BUTTONACT( randomButton, toggleRandom() );
131 buttons->addWidget( randomButton );
133 /* Repeat 3-state button */
134 repeatButton = new QPushButton( this );
135 if( model->hasRepeat() )
137 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
138 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
140 else if( model->hasLoop() )
142 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
143 repeatButton->setToolTip( qtr( I_PL_LOOP ));
147 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
148 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
150 BUTTONACT( repeatButton, toggleRepeat() );
151 buttons->addWidget( repeatButton );
154 gotoPlayingButton = new QPushButton;
155 BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
156 qtr( "Show the current item" ), gotoPlayingItem() );
157 buttons->addWidget( gotoPlayingButton );
159 /* A Spacer and the search possibilities */
160 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
161 buttons->addItem( spacer );
163 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
164 buttons->addWidget( filter );
166 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
167 searchLine->setMinimumWidth( 80 );
168 CONNECT( searchLine, textChanged(QString), this, search(QString));
169 buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
171 QPushButton *clear = new QPushButton;
172 clear->setMaximumWidth( 30 );
173 BUTTON_SET_ACT_I( clear, "", clear, qtr( "Clear" ), clearFilter() );
174 buttons->addWidget( clear );
176 /* Finish the layout */
177 layout->addWidget( view );
178 layout->addLayout( buttons );
179 // layout->addWidget( bar );
183 /* Function to toggle between the Repeat states */
184 void StandardPLPanel::toggleRepeat()
186 if( model->hasRepeat() )
188 model->setRepeat( false ); model->setLoop( true );
189 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
190 repeatButton->setToolTip( qtr( I_PL_LOOP ));
192 else if( model->hasLoop() )
194 model->setRepeat( false ) ; model->setLoop( false );
195 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
196 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
200 model->setRepeat( true );
201 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
202 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
206 /* Function to toggle between the Random states */
207 void StandardPLPanel::toggleRandom()
209 bool prev = model->hasRandom();
210 model->setRandom( !prev );
211 randomButton->setIcon( prev ?
212 QIcon( ":/shuffle_off" ) :
213 QIcon( ":/shuffle_on" ) );
214 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
217 void StandardPLPanel::gotoPlayingItem()
219 view->scrollTo( view->currentIndex() );
222 void StandardPLPanel::handleExpansion( const QModelIndex &index )
224 if( model->isCurrent( index ) )
225 view->scrollTo( index, QAbstractItemView::EnsureVisible );
228 void StandardPLPanel::setCurrentRootId( int _new )
230 currentRootId = _new;
231 if( currentRootId == THEPL->p_local_category->i_id ||
232 currentRootId == THEPL->p_local_onelevel->i_id )
234 addButton->setEnabled( true );
235 addButton->setToolTip( qtr(I_PL_ADDPL) );
237 else if( ( THEPL->p_ml_category &&
238 currentRootId == THEPL->p_ml_category->i_id ) ||
239 ( THEPL->p_ml_onelevel &&
240 currentRootId == THEPL->p_ml_onelevel->i_id ) )
242 addButton->setEnabled( true );
243 addButton->setToolTip( qtr(I_PL_ADDML) );
246 addButton->setEnabled( false );
249 /* PopupAdd Menu for the Add Menu */
250 void StandardPLPanel::popupAdd()
253 if( currentRootId == THEPL->p_local_category->i_id ||
254 currentRootId == THEPL->p_local_onelevel->i_id )
256 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
257 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
259 else if( ( THEPL->p_ml_category &&
260 currentRootId == THEPL->p_ml_category->i_id ) ||
261 ( THEPL->p_ml_onelevel &&
262 currentRootId == THEPL->p_ml_onelevel->i_id ) )
264 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
265 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
267 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
268 + QPoint( 0, addButton->height() ) );
271 void StandardPLPanel::popupSelectColumn( QPoint pos )
273 ContextUpdateMapper = new QSignalMapper(this);
277 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
280 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
282 QAction* option = selectColMenu.addAction(
283 qfu( psz_column_title( i_column ) ) );
284 option->setCheckable( true );
285 option->setChecked( model->shownFlags() & i_column );
286 ContextUpdateMapper->setMapping( option, i_column );
287 CONNECT( option, triggered(), ContextUpdateMapper, map() );
290 selectColMenu.exec( QCursor::pos() );
293 /* ClearFilter LineEdit */
294 void StandardPLPanel::clearFilter()
296 searchLine->setText( "" );
299 /* Search in the playlist */
300 void StandardPLPanel::search( QString searchText )
302 model->search( searchText );
305 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
307 if( !index.isValid() ) return;
308 QItemSelectionModel *selection = view->selectionModel();
309 QModelIndexList list = selection->selectedIndexes();
310 model->popup( index, point, list );
313 /* Set the root of the new Playlist */
314 /* This activated by the selector selection */
315 void StandardPLPanel::setRoot( int i_root_id )
318 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
321 p_item = playlist_GetPreferredNode( THEPL, p_item );
325 model->rebuild( p_item );
328 void StandardPLPanel::removeItem( int i_id )
330 model->removeItem( i_id );
333 /* Delete and Suppr key remove the selection
334 FilterKey function and code function */
335 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
346 void StandardPLPanel::deleteSelection()
348 QItemSelectionModel *selection = view->selectionModel();
349 QModelIndexList list = selection->selectedIndexes();
350 model->doDelete( list );
353 StandardPLPanel::~StandardPLPanel()
356 getSettings()->beginGroup("playlistdialog");
357 getSettings()->setValue( "headerState", view->header()->saveState() );
358 getSettings()->endGroup();