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->setSelectionBehavior( QAbstractItemView::SelectRows );
71 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
72 view->setDragEnabled( true );
73 view->setAcceptDrops( true );
74 view->setDropIndicatorShown( true );
75 view->setAutoScroll( true );
78 getSettings()->beginGroup("Playlist");
79 if( getSettings()->contains( "headerState" ) )
81 view->header()->restoreState(
82 getSettings()->value( "headerState" ).toByteArray() );
86 /* Configure the size of the header */
87 view->header()->resizeSection( 0, 200 );
88 view->header()->resizeSection( 1, 80 );
90 view->header()->setSortIndicatorShown( true );
91 view->header()->setClickable( true );
92 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
93 getSettings()->endGroup();
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( ":/buttons/playlist/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 randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
121 randomButton->setToolTip( qtr( I_PL_RANDOM ));
122 randomButton->setCheckable( true );
123 randomButton->setChecked( model->hasRandom() );
124 BUTTONACT( randomButton, toggleRandom() );
125 buttons->addWidget( randomButton );
127 /* Repeat 3-state button */
128 repeatButton = new QPushButton( this );
129 repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
130 repeatButton->setCheckable( true );
132 if( model->hasRepeat() )
134 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
135 repeatButton->setChecked( true );
137 else if( model->hasLoop() )
139 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
140 repeatButton->setChecked( true );
144 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
145 repeatButton->setChecked( false );
147 BUTTONACT( repeatButton, toggleRepeat() );
148 buttons->addWidget( repeatButton );
151 gotoPlayingButton = new QPushButton;
152 BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
153 qtr( "Show the current item" ), gotoPlayingItem() );
154 buttons->addWidget( gotoPlayingButton );
156 /* A Spacer and the search possibilities */
157 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
158 buttons->addItem( spacer );
160 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
161 buttons->addWidget( filter );
163 SearchLineEdit *search = new SearchLineEdit( this );
164 buttons->addWidget( search );
165 filter->setBuddy( search );
166 CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
168 /* Finish the layout */
169 layout->addWidget( view );
170 layout->addLayout( buttons );
171 // layout->addWidget( bar );
175 /* Function to toggle between the Repeat states */
176 void StandardPLPanel::toggleRepeat()
178 if( model->hasRepeat() )
180 model->setRepeat( false ); model->setLoop( true );
181 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
182 repeatButton->setChecked( true );
184 else if( model->hasLoop() )
186 model->setRepeat( false ) ; model->setLoop( false );
187 repeatButton->setChecked( false );
188 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
192 model->setRepeat( true ); model->setLoop( false );
193 repeatButton->setChecked( true );
194 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
198 /* Function to toggle between the Random states */
199 void StandardPLPanel::toggleRandom()
201 bool prev = model->hasRandom();
202 model->setRandom( !prev );
205 void StandardPLPanel::gotoPlayingItem()
207 view->scrollTo( view->currentIndex() );
210 void StandardPLPanel::handleExpansion( const QModelIndex &index )
212 if( model->isCurrent( index ) )
213 view->scrollTo( index, QAbstractItemView::EnsureVisible );
216 void StandardPLPanel::setCurrentRootId( int _new )
218 currentRootId = _new;
219 if( currentRootId == THEPL->p_local_category->i_id ||
220 currentRootId == THEPL->p_local_onelevel->i_id )
222 addButton->setEnabled( true );
223 addButton->setToolTip( qtr(I_PL_ADDPL) );
225 else if( ( THEPL->p_ml_category &&
226 currentRootId == THEPL->p_ml_category->i_id ) ||
227 ( THEPL->p_ml_onelevel &&
228 currentRootId == THEPL->p_ml_onelevel->i_id ) )
230 addButton->setEnabled( true );
231 addButton->setToolTip( qtr(I_PL_ADDML) );
234 addButton->setEnabled( false );
237 /* PopupAdd Menu for the Add Menu */
238 void StandardPLPanel::popupAdd()
241 if( currentRootId == THEPL->p_local_category->i_id ||
242 currentRootId == THEPL->p_local_onelevel->i_id )
244 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog( OPEN_FILE_TAB )) );
245 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog( OPEN_DISC_TAB )) );
246 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog( OPEN_NETWORK_TAB )) );
247 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog( OPEN_FILE_TAB )) );
248 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
250 else if( ( THEPL->p_ml_category &&
251 currentRootId == THEPL->p_ml_category->i_id ) ||
252 ( THEPL->p_ml_onelevel &&
253 currentRootId == THEPL->p_ml_onelevel->i_id ) )
255 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
256 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
259 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
260 + QPoint( 0, addButton->height() ) );
263 void StandardPLPanel::popupSelectColumn( QPoint pos )
265 ContextUpdateMapper = new QSignalMapper(this);
269 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
272 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
274 QAction* option = selectColMenu.addAction(
275 qfu( psz_column_title( i_column ) ) );
276 option->setCheckable( true );
277 option->setChecked( model->shownFlags() & i_column );
278 ContextUpdateMapper->setMapping( option, i_column );
279 CONNECT( option, triggered(), ContextUpdateMapper, map() );
282 selectColMenu.exec( QCursor::pos() );
285 /* Search in the playlist */
286 void StandardPLPanel::search( const QString& searchText )
288 model->search( searchText );
291 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
293 if( !index.isValid() ) return;
294 QItemSelectionModel *selection = view->selectionModel();
295 QModelIndexList list = selection->selectedIndexes();
296 model->popup( index, point, list );
299 /* Set the root of the new Playlist */
300 /* This activated by the selector selection */
301 void StandardPLPanel::setRoot( int i_root_id )
304 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
306 p_item = playlist_GetPreferredNode( THEPL, p_item );
310 model->rebuild( p_item );
313 void StandardPLPanel::removeItem( int i_id )
315 model->removeItem( i_id );
318 /* Delete and Suppr key remove the selection
319 FilterKey function and code function */
320 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
331 void StandardPLPanel::deleteSelection()
333 QItemSelectionModel *selection = view->selectionModel();
334 QModelIndexList list = selection->selectedIndexes();
335 model->doDelete( list );
338 StandardPLPanel::~StandardPLPanel()
340 getSettings()->beginGroup("Playlist");
341 getSettings()->setValue( "headerState", view->header()->saveState() );
342 getSettings()->endGroup();