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"
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/panels.hpp"
33 #include "util/customwidgets.hpp"
35 #include <vlc_intf_strings.h>
38 #include <QPushButton>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
41 #include <QHeaderView>
43 #include <QModelIndexList>
46 #include <QSpacerItem>
48 #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 SearchLineEdit *search = new SearchLineEdit( this );
168 buttons->addWidget( search );
169 filter->setBuddy( search );
170 CONNECT( search, textChanged( QString ), this, search( QString ) );
172 /* Finish the layout */
173 layout->addWidget( view );
174 layout->addLayout( buttons );
175 // layout->addWidget( bar );
179 /* Function to toggle between the Repeat states */
180 void StandardPLPanel::toggleRepeat()
182 if( model->hasRepeat() )
184 model->setRepeat( false ); model->setLoop( true );
185 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
186 repeatButton->setToolTip( qtr( I_PL_LOOP ));
188 else if( model->hasLoop() )
190 model->setRepeat( false ) ; model->setLoop( false );
191 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
192 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
196 model->setRepeat( true );
197 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
198 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
202 /* Function to toggle between the Random states */
203 void StandardPLPanel::toggleRandom()
205 bool prev = model->hasRandom();
206 model->setRandom( !prev );
207 randomButton->setIcon( prev ?
208 QIcon( ":/shuffle_off" ) :
209 QIcon( ":/shuffle_on" ) );
210 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
213 void StandardPLPanel::gotoPlayingItem()
215 view->scrollTo( view->currentIndex() );
218 void StandardPLPanel::handleExpansion( const QModelIndex &index )
220 if( model->isCurrent( index ) )
221 view->scrollTo( index, QAbstractItemView::EnsureVisible );
224 void StandardPLPanel::setCurrentRootId( int _new )
226 currentRootId = _new;
227 if( currentRootId == THEPL->p_local_category->i_id ||
228 currentRootId == THEPL->p_local_onelevel->i_id )
230 addButton->setEnabled( true );
231 addButton->setToolTip( qtr(I_PL_ADDPL) );
233 else if( ( THEPL->p_ml_category &&
234 currentRootId == THEPL->p_ml_category->i_id ) ||
235 ( THEPL->p_ml_onelevel &&
236 currentRootId == THEPL->p_ml_onelevel->i_id ) )
238 addButton->setEnabled( true );
239 addButton->setToolTip( qtr(I_PL_ADDML) );
242 addButton->setEnabled( false );
245 /* PopupAdd Menu for the Add Menu */
246 void StandardPLPanel::popupAdd()
249 if( currentRootId == THEPL->p_local_category->i_id ||
250 currentRootId == THEPL->p_local_onelevel->i_id )
252 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
253 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
255 else if( ( THEPL->p_ml_category &&
256 currentRootId == THEPL->p_ml_category->i_id ) ||
257 ( THEPL->p_ml_onelevel &&
258 currentRootId == THEPL->p_ml_onelevel->i_id ) )
260 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
261 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
263 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
264 + QPoint( 0, addButton->height() ) );
267 void StandardPLPanel::popupSelectColumn( QPoint pos )
269 ContextUpdateMapper = new QSignalMapper(this);
273 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
276 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
278 QAction* option = selectColMenu.addAction(
279 qfu( psz_column_title( i_column ) ) );
280 option->setCheckable( true );
281 option->setChecked( model->shownFlags() & i_column );
282 ContextUpdateMapper->setMapping( option, i_column );
283 CONNECT( option, triggered(), ContextUpdateMapper, map() );
286 selectColMenu.exec( QCursor::pos() );
289 /* Search in the playlist */
290 void StandardPLPanel::search( QString searchText )
292 model->search( searchText );
295 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
297 if( !index.isValid() ) return;
298 QItemSelectionModel *selection = view->selectionModel();
299 QModelIndexList list = selection->selectedIndexes();
300 model->popup( index, point, list );
303 /* Set the root of the new Playlist */
304 /* This activated by the selector selection */
305 void StandardPLPanel::setRoot( int i_root_id )
308 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
310 p_item = playlist_GetPreferredNode( THEPL, p_item );
314 model->rebuild( p_item );
317 void StandardPLPanel::removeItem( int i_id )
319 model->removeItem( i_id );
322 /* Delete and Suppr key remove the selection
323 FilterKey function and code function */
324 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
335 void StandardPLPanel::deleteSelection()
337 QItemSelectionModel *selection = view->selectionModel();
338 QModelIndexList list = selection->selectedIndexes();
339 model->doDelete( list );
342 StandardPLPanel::~StandardPLPanel()
344 getSettings()->beginGroup("Playlist");
345 getSettings()->setValue( "headerState", view->header()->saveState() );
346 getSettings()->endGroup();