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>
50 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
51 intf_thread_t *_p_intf,
52 playlist_t *p_playlist,
53 playlist_item_t *p_root ):
54 PLPanel( _parent, _p_intf )
56 model = new PLModel( p_playlist, p_intf, p_root, -1, this );
58 QVBoxLayout *layout = new QVBoxLayout();
59 layout->setSpacing( 0 ); layout->setMargin( 0 );
61 /* Create and configure the QTreeView */
62 view = new QVLCTreeView( 0 );
63 view->setModel(model);
64 view->setIconSize( QSize( 20, 20 ) );
65 view->setAlternatingRowColors( true );
66 view->setAnimated( true );
67 view->setSortingEnabled( true );
68 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
69 view->setDragEnabled( true );
70 view->setAcceptDrops( true );
71 view->setDropIndicatorShown( true );
72 view->setAutoScroll( true );
74 /* Configure the size of the header */
75 view->header()->resizeSection( 0, 200 );
76 view->header()->resizeSection( 1, 80 );
77 view->header()->setSortIndicatorShown( true );
78 view->header()->setClickable( true );
79 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
81 /* Connections for the TreeView */
82 CONNECT( view, activated( const QModelIndex& ) ,
83 model,activateItem( const QModelIndex& ) );
84 CONNECT( view, rightClicked( QModelIndex , QPoint ),
85 this, doPopup( QModelIndex, QPoint ) );
86 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
87 this, handleExpansion( const QModelIndex& ) );
88 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
89 this, popupSelectColumn( QPoint ) );
92 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
94 /* Buttons configuration */
95 QHBoxLayout *buttons = new QHBoxLayout;
97 /* Add item to the playlist button */
98 addButton = new QPushButton;
99 addButton->setIcon( QIcon( ":/pixmaps/playlist_add.png" ) );
100 addButton->setMaximumWidth( 30 );
101 BUTTONACT( addButton, popupAdd() );
102 buttons->addWidget( addButton );
104 /* Random 2-state button */
105 randomButton = new QPushButton( this );
106 if( model->hasRandom() )
108 randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_on.png" ));
109 randomButton->setToolTip( qtr( I_PL_RANDOM ));
113 randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_off.png" ) );
114 randomButton->setToolTip( qtr( I_PL_NORANDOM ));
116 BUTTONACT( randomButton, toggleRandom() );
117 buttons->addWidget( randomButton );
119 /* Repeat 3-state button */
120 repeatButton = new QPushButton( this );
121 if( model->hasRepeat() )
123 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
124 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
126 else if( model->hasLoop() )
128 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
129 repeatButton->setToolTip( qtr( I_PL_LOOP ));
133 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
134 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
136 BUTTONACT( repeatButton, toggleRepeat() );
137 buttons->addWidget( repeatButton );
140 gotoPlayingButton = new QPushButton( qtr( "X" ), this );
141 BUTTONACT( gotoPlayingButton, gotoPlayingItem() );
142 buttons->addWidget( gotoPlayingButton );
144 /* A Spacer and the search possibilities */
145 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
146 buttons->addItem( spacer );
148 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
149 buttons->addWidget( filter );
151 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
152 searchLine->setMinimumWidth( 80 );
153 CONNECT( searchLine, textChanged(QString), this, search(QString));
154 buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
156 QPushButton *clear = new QPushButton;
157 clear->setText( qfu( "CL") );
158 clear->setMaximumWidth( 30 );
159 clear->setToolTip( qtr( "Clear" ));
160 BUTTONACT( clear, clearFilter() );
161 buttons->addWidget( clear );
163 /* Finish the layout */
164 layout->addWidget( view );
165 layout->addLayout( buttons );
166 // layout->addWidget( bar );
170 /* Function to toggle between the Repeat states */
171 void StandardPLPanel::toggleRepeat()
173 if( model->hasRepeat() )
175 model->setRepeat( false ); model->setLoop( true );
176 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
177 repeatButton->setToolTip( qtr( I_PL_LOOP ));
179 else if( model->hasLoop() )
181 model->setRepeat( false ) ; model->setLoop( false );
182 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
183 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
187 model->setRepeat( true );
188 repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
189 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
193 /* Function to toggle between the Random states */
194 void StandardPLPanel::toggleRandom()
196 bool prev = model->hasRandom();
197 model->setRandom( !prev );
198 randomButton->setIcon( prev ?
199 QIcon( ":/pixmaps/playlist_shuffle_off.png" ) :
200 QIcon( ":/pixmaps/playlist_shuffle_on.png" ) );
201 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
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(simplePLAppendDialog()));
244 popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
245 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
247 else if( ( THEPL->p_ml_category &&
248 currentRootId == THEPL->p_ml_category->i_id ) ||
249 ( THEPL->p_ml_onelevel &&
250 currentRootId == THEPL->p_ml_onelevel->i_id ) )
252 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
253 popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
254 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
256 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
257 + QPoint( 0, addButton->height() ) );
260 void StandardPLPanel::popupSelectColumn( QPoint pos )
262 ContextUpdateMapper = new QSignalMapper(this);
266 #define ADD_META_ACTION( meta ) { \
267 QAction* option = selectColMenu.addAction( qfu(VLC_META_##meta) ); \
268 option->setCheckable( true ); \
269 option->setChecked( model->shownFlags() & VLC_META_ENGINE_##meta ); \
270 ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta ); \
271 CONNECT( option, triggered(), ContextUpdateMapper, map() ); \
274 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
276 ADD_META_ACTION( TITLE );
277 ADD_META_ACTION( ARTIST );
278 ADD_META_ACTION( DURATION );
279 ADD_META_ACTION( COLLECTION );
280 ADD_META_ACTION( GENRE );
281 ADD_META_ACTION( SEQ_NUM );
282 ADD_META_ACTION( RATING );
283 ADD_META_ACTION( DESCRIPTION );
285 #undef ADD_META_ACTION
287 selectColMenu.exec( QCursor::pos() );
290 /* ClearFilter LineEdit */
291 void StandardPLPanel::clearFilter()
293 searchLine->setText( "" );
296 /* Search in the playlist */
297 void StandardPLPanel::search( QString searchText )
299 model->search( searchText );
302 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
304 if( !index.isValid() ) return;
305 QItemSelectionModel *selection = view->selectionModel();
306 QModelIndexList list = selection->selectedIndexes();
307 model->popup( index, point, list );
310 /* Set the root of the new Playlist */
311 /* This activated by the selector selection */
312 void StandardPLPanel::setRoot( int i_root_id )
314 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
317 p_item = playlist_GetPreferredNode( THEPL, p_item );
319 model->rebuild( p_item );
322 void StandardPLPanel::removeItem( int i_id )
324 model->removeItem( i_id );
327 /* Delete and Suppr key remove the selection
328 FilterKey function and code function */
329 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
340 void StandardPLPanel::deleteSelection()
342 QItemSelectionModel *selection = view->selectionModel();
343 QModelIndexList list = selection->selectedIndexes();
344 model->doDelete( list );
347 StandardPLPanel::~StandardPLPanel()