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 *****************************************************************************/
28 #include "dialogs_provider.hpp"
29 #include "components/playlist/playlist_model.hpp"
30 #include "components/playlist/panels.hpp"
31 #include "util/customwidgets.hpp"
33 #include <vlc_intf_strings.h>
36 #include <QPushButton>
37 #include <QHBoxLayout>
38 #include <QVBoxLayout>
39 #include <QHeaderView>
41 #include <QModelIndexList>
44 #include <QSpacerItem>
46 #include <QSignalMapper>
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53 intf_thread_t *_p_intf,
54 playlist_t *p_playlist,
55 playlist_item_t *p_root ):
56 PLPanel( _parent, _p_intf )
58 model = new PLModel( p_playlist, p_intf, p_root, -1, this );
60 QVBoxLayout *layout = new QVBoxLayout();
61 layout->setSpacing( 0 ); layout->setMargin( 0 );
63 /* Create and configure the QTreeView */
64 view = new QVLCTreeView( 0 );
65 view->setSortingEnabled( true );
66 view->sortByColumn( 0 , Qt::AscendingOrder );
67 view->setModel( model );
68 view->setIconSize( QSize( 20, 20 ) );
69 view->setAlternatingRowColors( true );
70 view->setAnimated( true );
71 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
72 view->setDragEnabled( true );
73 view->setAcceptDrops( true );
74 view->setDropIndicatorShown( true );
75 view->setAutoScroll( true );
77 /* Configure the size of the header */
78 view->header()->resizeSection( 0, 200 );
79 view->header()->resizeSection( 1, 80 );
80 view->header()->setSortIndicatorShown( true );
81 view->header()->setClickable( true );
82 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
84 /* Connections for the TreeView */
85 CONNECT( view, activated( const QModelIndex& ) ,
86 model,activateItem( const QModelIndex& ) );
87 CONNECT( view, rightClicked( QModelIndex , QPoint ),
88 this, doPopup( QModelIndex, QPoint ) );
89 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
90 this, handleExpansion( const QModelIndex& ) );
91 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
92 this, popupSelectColumn( QPoint ) );
95 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
97 /* Buttons configuration */
98 QHBoxLayout *buttons = new QHBoxLayout;
100 /* Add item to the playlist button */
101 addButton = new QPushButton;
102 addButton->setIcon( QIcon( ":/pixmaps/playlist_add.png" ) );
103 addButton->setMaximumWidth( 30 );
104 BUTTONACT( addButton, popupAdd() );
105 buttons->addWidget( addButton );
107 /* Random 2-state button */
108 randomButton = new QPushButton( this );
109 if( model->hasRandom() )
111 randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_on.png" ));
112 randomButton->setToolTip( qtr( I_PL_RANDOM ));
116 randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_off.png" ) );
117 randomButton->setToolTip( qtr( I_PL_NORANDOM ));
119 BUTTONACT( randomButton, toggleRandom() );
120 buttons->addWidget( randomButton );
122 /* Repeat 3-state button */
123 repeatButton = new QPushButton( this );
124 if( model->hasRepeat() )
126 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
127 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
129 else if( model->hasLoop() )
131 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
132 repeatButton->setToolTip( qtr( I_PL_LOOP ));
136 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
137 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
139 BUTTONACT( repeatButton, toggleRepeat() );
140 buttons->addWidget( repeatButton );
143 gotoPlayingButton = new QPushButton( "X" , this );
144 gotoPlayingButton->setToolTip( qtr( "Show the current item" ));
145 BUTTONACT( gotoPlayingButton, gotoPlayingItem() );
146 buttons->addWidget( gotoPlayingButton );
148 /* A Spacer and the search possibilities */
149 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
150 buttons->addItem( spacer );
152 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
153 buttons->addWidget( filter );
155 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
156 searchLine->setMinimumWidth( 80 );
157 CONNECT( searchLine, textChanged(QString), this, search(QString));
158 buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
160 QPushButton *clear = new QPushButton;
161 clear->setText( qfu( "CL") );
162 clear->setMaximumWidth( 30 );
163 clear->setToolTip( qtr( "Clear" ));
164 BUTTONACT( clear, clearFilter() );
165 buttons->addWidget( clear );
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( ":/pixmaps/playlist_repeat_all.png" ) );
181 repeatButton->setToolTip( qtr( I_PL_LOOP ));
183 else if( model->hasLoop() )
185 model->setRepeat( false ) ; model->setLoop( false );
186 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
187 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
191 model->setRepeat( true );
192 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
193 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
197 /* Function to toggle between the Random states */
198 void StandardPLPanel::toggleRandom()
200 bool prev = model->hasRandom();
201 model->setRandom( !prev );
202 randomButton->setIcon( prev ?
203 QIcon( ":/pixmaps/playlist_shuffle_off.png" ) :
204 QIcon( ":/pixmaps/playlist_shuffle_on.png" ) );
205 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
208 void StandardPLPanel::gotoPlayingItem()
210 view->scrollTo( view->currentIndex() );
213 void StandardPLPanel::handleExpansion( const QModelIndex &index )
215 if( model->isCurrent( index ) )
216 view->scrollTo( index, QAbstractItemView::EnsureVisible );
219 void StandardPLPanel::setCurrentRootId( int _new )
221 currentRootId = _new;
222 if( currentRootId == THEPL->p_local_category->i_id ||
223 currentRootId == THEPL->p_local_onelevel->i_id )
225 addButton->setEnabled( true );
226 addButton->setToolTip( qtr(I_PL_ADDPL) );
228 else if( ( THEPL->p_ml_category &&
229 currentRootId == THEPL->p_ml_category->i_id ) ||
230 ( THEPL->p_ml_onelevel &&
231 currentRootId == THEPL->p_ml_onelevel->i_id ) )
233 addButton->setEnabled( true );
234 addButton->setToolTip( qtr(I_PL_ADDML) );
237 addButton->setEnabled( false );
240 /* PopupAdd Menu for the Add Menu */
241 void StandardPLPanel::popupAdd()
244 if( currentRootId == THEPL->p_local_category->i_id ||
245 currentRootId == THEPL->p_local_onelevel->i_id )
247 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
248 popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
249 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
251 else if( ( THEPL->p_ml_category &&
252 currentRootId == THEPL->p_ml_category->i_id ) ||
253 ( THEPL->p_ml_onelevel &&
254 currentRootId == THEPL->p_ml_onelevel->i_id ) )
256 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
257 popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
258 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
260 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
261 + QPoint( 0, addButton->height() ) );
264 void StandardPLPanel::popupSelectColumn( QPoint pos )
266 ContextUpdateMapper = new QSignalMapper(this);
270 #define ADD_META_ACTION( meta ) { \
271 QAction* option = selectColMenu.addAction( qfu( psz_column_title( meta ) ) ); \
272 option->setCheckable( true ); \
273 option->setChecked( model->shownFlags() & meta ); \
274 ContextUpdateMapper->setMapping( option, meta ); \
275 CONNECT( option, triggered(), ContextUpdateMapper, map() ); \
278 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
280 ADD_META_ACTION( COLUMN_NUMBER );
281 ADD_META_ACTION( COLUMN_TITLE );
282 ADD_META_ACTION( COLUMN_DURATION );
283 ADD_META_ACTION( COLUMN_ARTIST );
284 ADD_META_ACTION( COLUMN_GENRE );
285 ADD_META_ACTION( COLUMN_ALBUM );
286 ADD_META_ACTION( COLUMN_TRACK_NUMBER );
287 ADD_META_ACTION( COLUMN_DESCRIPTION );
289 #undef ADD_META_ACTION
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 )
318 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
321 p_item = playlist_GetPreferredNode( THEPL, p_item );
323 model->rebuild( p_item );
326 void StandardPLPanel::removeItem( int i_id )
328 model->removeItem( i_id );
331 /* Delete and Suppr key remove the selection
332 FilterKey function and code function */
333 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
344 void StandardPLPanel::deleteSelection()
346 QItemSelectionModel *selection = view->selectionModel();
347 QModelIndexList list = selection->selectedIndexes();
348 model->doDelete( list );
351 StandardPLPanel::~StandardPLPanel()