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;
66 view->header()->setSortIndicator( 0 , Qt::AscendingOrder );
67 view->setSortingEnabled( true );
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");
80 if( getSettings()->contains( "headerState" ) )
82 view->header()->restoreState(
83 getSettings()->value( "headerState" ).toByteArray() );
87 /* Configure the size of the header */
88 view->header()->resizeSection( 0, 200 );
89 view->header()->resizeSection( 1, 80 );
91 view->header()->setSortIndicatorShown( true );
92 view->header()->setClickable( true );
93 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
94 getSettings()->endGroup();
96 /* Connections for the TreeView */
97 CONNECT( view, activated( const QModelIndex& ) ,
98 model,activateItem( const QModelIndex& ) );
99 CONNECT( view, rightClicked( QModelIndex , QPoint ),
100 this, doPopup( QModelIndex, QPoint ) );
101 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
102 this, handleExpansion( const QModelIndex& ) );
103 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
104 this, popupSelectColumn( QPoint ) );
107 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
109 /* Buttons configuration */
110 QHBoxLayout *buttons = new QHBoxLayout;
112 /* Add item to the playlist button */
113 addButton = new QPushButton;
114 addButton->setIcon( QIcon( ":/playlist_add" ) );
115 addButton->setMaximumWidth( 30 );
116 BUTTONACT( addButton, popupAdd() );
117 buttons->addWidget( addButton );
119 /* Random 2-state button */
120 randomButton = new QPushButton( this );
121 if( model->hasRandom() )
123 randomButton->setIcon( QIcon( ":/shuffle_on" ));
124 randomButton->setToolTip( qtr( I_PL_RANDOM ));
128 randomButton->setIcon( QIcon( ":/shuffle_off" ) );
129 randomButton->setToolTip( qtr( I_PL_NORANDOM ));
131 BUTTONACT( randomButton, toggleRandom() );
132 buttons->addWidget( randomButton );
134 /* Repeat 3-state button */
135 repeatButton = new QPushButton( this );
136 if( model->hasRepeat() )
138 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
139 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
141 else if( model->hasLoop() )
143 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
144 repeatButton->setToolTip( qtr( I_PL_LOOP ));
148 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
149 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
151 BUTTONACT( repeatButton, toggleRepeat() );
152 buttons->addWidget( repeatButton );
155 gotoPlayingButton = new QPushButton;
156 BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
157 qtr( "Show the current item" ), gotoPlayingItem() );
158 buttons->addWidget( gotoPlayingButton );
160 /* A Spacer and the search possibilities */
161 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
162 buttons->addItem( spacer );
164 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
165 buttons->addWidget( filter );
167 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
168 searchLine->setMinimumWidth( 80 );
169 CONNECT( searchLine, textChanged(QString), this, search(QString));
170 buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
172 QPushButton *clear = new QPushButton;
173 clear->setMaximumWidth( 30 );
174 BUTTON_SET_ACT_I( clear, "", clear, qtr( "Clear" ), clearFilter() );
175 buttons->addWidget( clear );
177 /* Finish the layout */
178 layout->addWidget( view );
179 layout->addLayout( buttons );
180 // layout->addWidget( bar );
184 /* Function to toggle between the Repeat states */
185 void StandardPLPanel::toggleRepeat()
187 if( model->hasRepeat() )
189 model->setRepeat( false ); model->setLoop( true );
190 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
191 repeatButton->setToolTip( qtr( I_PL_LOOP ));
193 else if( model->hasLoop() )
195 model->setRepeat( false ) ; model->setLoop( false );
196 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
197 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
201 model->setRepeat( true );
202 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
203 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
207 /* Function to toggle between the Random states */
208 void StandardPLPanel::toggleRandom()
210 bool prev = model->hasRandom();
211 model->setRandom( !prev );
212 randomButton->setIcon( prev ?
213 QIcon( ":/shuffle_off" ) :
214 QIcon( ":/shuffle_on" ) );
215 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
218 void StandardPLPanel::gotoPlayingItem()
220 view->scrollTo( view->currentIndex() );
223 void StandardPLPanel::handleExpansion( const QModelIndex &index )
225 if( model->isCurrent( index ) )
226 view->scrollTo( index, QAbstractItemView::EnsureVisible );
229 void StandardPLPanel::setCurrentRootId( int _new )
231 currentRootId = _new;
232 if( currentRootId == THEPL->p_local_category->i_id ||
233 currentRootId == THEPL->p_local_onelevel->i_id )
235 addButton->setEnabled( true );
236 addButton->setToolTip( qtr(I_PL_ADDPL) );
238 else if( ( THEPL->p_ml_category &&
239 currentRootId == THEPL->p_ml_category->i_id ) ||
240 ( THEPL->p_ml_onelevel &&
241 currentRootId == THEPL->p_ml_onelevel->i_id ) )
243 addButton->setEnabled( true );
244 addButton->setToolTip( qtr(I_PL_ADDML) );
247 addButton->setEnabled( false );
250 /* PopupAdd Menu for the Add Menu */
251 void StandardPLPanel::popupAdd()
254 if( currentRootId == THEPL->p_local_category->i_id ||
255 currentRootId == THEPL->p_local_onelevel->i_id )
257 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
258 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
260 else if( ( THEPL->p_ml_category &&
261 currentRootId == THEPL->p_ml_category->i_id ) ||
262 ( THEPL->p_ml_onelevel &&
263 currentRootId == THEPL->p_ml_onelevel->i_id ) )
265 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
266 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
268 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
269 + QPoint( 0, addButton->height() ) );
272 void StandardPLPanel::popupSelectColumn( QPoint pos )
274 ContextUpdateMapper = new QSignalMapper(this);
278 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
281 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
283 QAction* option = selectColMenu.addAction(
284 qfu( psz_column_title( i_column ) ) );
285 option->setCheckable( true );
286 option->setChecked( model->shownFlags() & i_column );
287 ContextUpdateMapper->setMapping( option, i_column );
288 CONNECT( option, triggered(), ContextUpdateMapper, map() );
291 selectColMenu.exec( QCursor::pos() );
294 /* ClearFilter LineEdit */
295 void StandardPLPanel::clearFilter()
297 searchLine->setText( "" );
300 /* Search in the playlist */
301 void StandardPLPanel::search( QString searchText )
303 model->search( searchText );
306 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
308 if( !index.isValid() ) return;
309 QItemSelectionModel *selection = view->selectionModel();
310 QModelIndexList list = selection->selectedIndexes();
311 model->popup( index, point, list );
314 /* Set the root of the new Playlist */
315 /* This activated by the selector selection */
316 void StandardPLPanel::setRoot( int i_root_id )
319 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
322 p_item = playlist_GetPreferredNode( THEPL, p_item );
326 model->rebuild( p_item );
329 void StandardPLPanel::removeItem( int i_id )
331 model->removeItem( i_id );
334 /* Delete and Suppr key remove the selection
335 FilterKey function and code function */
336 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
347 void StandardPLPanel::deleteSelection()
349 QItemSelectionModel *selection = view->selectionModel();
350 QModelIndexList list = selection->selectedIndexes();
351 model->doDelete( list );
354 StandardPLPanel::~StandardPLPanel()
356 getSettings()->beginGroup("Playlist");
357 getSettings()->setValue( "headerState", view->header()->saveState() );
358 getSettings()->endGroup();