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>
37 #include <QPushButton>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
40 #include <QHeaderView>
42 #include <QModelIndexList>
44 #include <QSpacerItem>
46 #include <QSignalMapper>
51 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
52 intf_thread_t *_p_intf,
53 playlist_t *p_playlist,
54 playlist_item_t *p_root ):
55 PLPanel( _parent, _p_intf )
57 model = new PLModel( p_playlist, p_intf, p_root, -1, this );
59 QVBoxLayout *layout = new QVBoxLayout();
60 layout->setSpacing( 0 ); layout->setMargin( 0 );
62 /* Create and configure the QTreeView */
63 view = new QVLCTreeView;
64 view->header()->setSortIndicator( 0 , Qt::AscendingOrder );
65 view->setSortingEnabled( true );
66 view->setModel( model );
67 view->setIconSize( QSize( 20, 20 ) );
68 view->setAlternatingRowColors( true );
69 view->setAnimated( true );
70 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
71 view->setDragEnabled( true );
72 view->setAcceptDrops( true );
73 view->setDropIndicatorShown( true );
74 view->setAutoScroll( true );
77 getSettings()->beginGroup("Playlist");
78 if( getSettings()->contains( "headerState" ) )
80 view->header()->restoreState(
81 getSettings()->value( "headerState" ).toByteArray() );
85 /* Configure the size of the header */
86 view->header()->resizeSection( 0, 200 );
87 view->header()->resizeSection( 1, 80 );
89 view->header()->setSortIndicatorShown( true );
90 view->header()->setClickable( true );
91 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
92 getSettings()->endGroup();
94 /* Connections for the TreeView */
95 CONNECT( view, activated( const QModelIndex& ) ,
96 model,activateItem( const QModelIndex& ) );
97 CONNECT( view, rightClicked( QModelIndex , QPoint ),
98 this, doPopup( QModelIndex, QPoint ) );
99 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
100 this, handleExpansion( const QModelIndex& ) );
101 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
102 this, popupSelectColumn( QPoint ) );
105 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
107 /* Buttons configuration */
108 QHBoxLayout *buttons = new QHBoxLayout;
110 /* Add item to the playlist button */
111 addButton = new QPushButton;
112 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
113 addButton->setMaximumWidth( 30 );
114 BUTTONACT( addButton, popupAdd() );
115 buttons->addWidget( addButton );
117 /* Random 2-state button */
118 randomButton = new QPushButton( this );
119 randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
120 randomButton->setToolTip( qtr( I_PL_RANDOM ));
121 randomButton->setCheckable( true );
122 randomButton->setChecked( model->hasRandom() );
123 BUTTONACT( randomButton, toggleRandom() );
124 buttons->addWidget( randomButton );
126 /* Repeat 3-state button */
127 repeatButton = new QPushButton( this );
128 repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
129 repeatButton->setCheckable( true );
131 if( model->hasRepeat() )
133 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
134 repeatButton->setChecked( true );
136 else if( model->hasLoop() )
138 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
139 repeatButton->setChecked( true );
143 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
144 repeatButton->setChecked( false );
146 BUTTONACT( repeatButton, toggleRepeat() );
147 buttons->addWidget( repeatButton );
150 gotoPlayingButton = new QPushButton;
151 BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
152 qtr( "Show the current item" ), gotoPlayingItem() );
153 buttons->addWidget( gotoPlayingButton );
155 /* A Spacer and the search possibilities */
156 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
157 buttons->addItem( spacer );
159 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
160 buttons->addWidget( filter );
162 SearchLineEdit *search = new SearchLineEdit( this );
163 buttons->addWidget( search );
164 filter->setBuddy( search );
165 CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
167 /* Finish the layout */
168 layout->addWidget( view );
169 layout->addLayout( buttons );
170 // layout->addWidget( bar );
174 /* Function to toggle between the Repeat states */
175 void StandardPLPanel::toggleRepeat()
177 if( model->hasRepeat() )
179 model->setRepeat( false ); model->setLoop( true );
180 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
181 repeatButton->setChecked( true );
183 else if( model->hasLoop() )
185 model->setRepeat( false ) ; model->setLoop( false );
186 repeatButton->setChecked( false );
187 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
191 model->setRepeat( true ); model->setLoop( false );
192 repeatButton->setChecked( true );
193 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
197 /* Function to toggle between the Random states */
198 void StandardPLPanel::toggleRandom()
200 bool prev = model->hasRandom();
201 model->setRandom( !prev );
204 void StandardPLPanel::gotoPlayingItem()
206 view->scrollTo( view->currentIndex() );
209 void StandardPLPanel::handleExpansion( const QModelIndex &index )
211 if( model->isCurrent( index ) )
212 view->scrollTo( index, QAbstractItemView::EnsureVisible );
215 void StandardPLPanel::setCurrentRootId( int _new )
217 currentRootId = _new;
218 if( currentRootId == THEPL->p_local_category->i_id ||
219 currentRootId == THEPL->p_local_onelevel->i_id )
221 addButton->setEnabled( true );
222 addButton->setToolTip( qtr(I_PL_ADDPL) );
224 else if( ( THEPL->p_ml_category &&
225 currentRootId == THEPL->p_ml_category->i_id ) ||
226 ( THEPL->p_ml_onelevel &&
227 currentRootId == THEPL->p_ml_onelevel->i_id ) )
229 addButton->setEnabled( true );
230 addButton->setToolTip( qtr(I_PL_ADDML) );
233 addButton->setEnabled( false );
236 /* PopupAdd Menu for the Add Menu */
237 void StandardPLPanel::popupAdd()
240 if( currentRootId == THEPL->p_local_category->i_id ||
241 currentRootId == THEPL->p_local_onelevel->i_id )
243 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
244 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
246 else if( ( THEPL->p_ml_category &&
247 currentRootId == THEPL->p_ml_category->i_id ) ||
248 ( THEPL->p_ml_onelevel &&
249 currentRootId == THEPL->p_ml_onelevel->i_id ) )
251 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
252 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
254 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
255 + QPoint( 0, addButton->height() ) );
258 void StandardPLPanel::popupSelectColumn( QPoint pos )
260 ContextUpdateMapper = new QSignalMapper(this);
264 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
267 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
269 QAction* option = selectColMenu.addAction(
270 qfu( psz_column_title( i_column ) ) );
271 option->setCheckable( true );
272 option->setChecked( model->shownFlags() & i_column );
273 ContextUpdateMapper->setMapping( option, i_column );
274 CONNECT( option, triggered(), ContextUpdateMapper, map() );
277 selectColMenu.exec( QCursor::pos() );
280 /* Search in the playlist */
281 void StandardPLPanel::search( const QString& searchText )
283 model->search( searchText );
286 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
288 if( !index.isValid() ) return;
289 QItemSelectionModel *selection = view->selectionModel();
290 QModelIndexList list = selection->selectedIndexes();
291 model->popup( index, point, list );
294 /* Set the root of the new Playlist */
295 /* This activated by the selector selection */
296 void StandardPLPanel::setRoot( int i_root_id )
299 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
301 p_item = playlist_GetPreferredNode( THEPL, p_item );
305 model->rebuild( p_item );
308 void StandardPLPanel::removeItem( int i_id )
310 model->removeItem( i_id );
313 /* Delete and Suppr key remove the selection
314 FilterKey function and code function */
315 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
326 void StandardPLPanel::deleteSelection()
328 QItemSelectionModel *selection = view->selectionModel();
329 QModelIndexList list = selection->selectedIndexes();
330 model->doDelete( list );
333 StandardPLPanel::~StandardPLPanel()
335 getSettings()->beginGroup("Playlist");
336 getSettings()->setValue( "headerState", view->header()->saveState() );
337 getSettings()->endGroup();