]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Lock the playlist so we can add SAP item to the playlist without asserting.
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "qt4.hpp"
28 #include "dialogs_provider.hpp"
29 #include "components/playlist/playlist_model.hpp"
30 #include "components/playlist/panels.hpp"
31 #include "util/customwidgets.hpp"
32
33 #include <vlc_intf_strings.h>
34
35 #include <QTreeView>
36 #include <QPushButton>
37 #include <QHBoxLayout>
38 #include <QVBoxLayout>
39 #include <QHeaderView>
40 #include <QKeyEvent>
41 #include <QModelIndexList>
42 #include <QToolBar>
43 #include <QLabel>
44 #include <QSpacerItem>
45 #include <QMenu>
46 #include <QSignalMapper>
47
48 #include <assert.h>
49
50 #include "sorting.h"
51
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 )
57 {
58     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
59
60     QVBoxLayout *layout = new QVBoxLayout();
61     layout->setSpacing( 0 ); layout->setMargin( 0 );
62
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 );
76
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 );
83
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 ) );
93
94     currentRootId = -1;
95     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
96
97     /* Buttons configuration */
98     QHBoxLayout *buttons = new QHBoxLayout;
99
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 );
106
107     /* Random 2-state button */
108     randomButton = new QPushButton( this );
109     if( model->hasRandom() )
110     {
111         randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_on.png" ));
112         randomButton->setToolTip( qtr( I_PL_RANDOM ));
113     }
114     else
115     {
116          randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_off.png" ) );
117          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
118     }
119     BUTTONACT( randomButton, toggleRandom() );
120     buttons->addWidget( randomButton );
121
122     /* Repeat 3-state button */
123     repeatButton = new QPushButton( this );
124     if( model->hasRepeat() )
125     {
126         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
127         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
128     }
129     else if( model->hasLoop() )
130     {
131         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
132         repeatButton->setToolTip( qtr( I_PL_LOOP ));
133     }
134     else
135     {
136         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
137         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
138     }
139     BUTTONACT( repeatButton, toggleRepeat() );
140     buttons->addWidget( repeatButton );
141
142     /* Goto */
143     gotoPlayingButton = new QPushButton( "X" , this );
144     gotoPlayingButton->setToolTip( qtr( "Show the current item" ));
145     BUTTONACT( gotoPlayingButton, gotoPlayingItem() );
146     buttons->addWidget( gotoPlayingButton );
147
148     /* A Spacer and the search possibilities */
149     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
150     buttons->addItem( spacer );
151
152     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
153     buttons->addWidget( filter );
154
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 );
159
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 );
166
167     /* Finish the layout */
168     layout->addWidget( view );
169     layout->addLayout( buttons );
170 //    layout->addWidget( bar );
171     setLayout( layout );
172 }
173
174 /* Function to toggle between the Repeat states */
175 void StandardPLPanel::toggleRepeat()
176 {
177     if( model->hasRepeat() )
178     {
179         model->setRepeat( false ); model->setLoop( true );
180         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
181         repeatButton->setToolTip( qtr( I_PL_LOOP ));
182     }
183     else if( model->hasLoop() )
184     {
185         model->setRepeat( false ) ; model->setLoop( false );
186         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
187         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
188     }
189     else
190     {
191         model->setRepeat( true );
192         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
193         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
194     }
195 }
196
197 /* Function to toggle between the Random states */
198 void StandardPLPanel::toggleRandom()
199 {
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 ) );
206 }
207
208 void StandardPLPanel::gotoPlayingItem()
209 {
210     view->scrollTo( view->currentIndex() );
211 }
212
213 void StandardPLPanel::handleExpansion( const QModelIndex &index )
214 {
215     if( model->isCurrent( index ) )
216         view->scrollTo( index, QAbstractItemView::EnsureVisible );
217 }
218
219 void StandardPLPanel::setCurrentRootId( int _new )
220 {
221     currentRootId = _new;
222     if( currentRootId == THEPL->p_local_category->i_id ||
223         currentRootId == THEPL->p_local_onelevel->i_id  )
224     {
225         addButton->setEnabled( true );
226         addButton->setToolTip( qtr(I_PL_ADDPL) );
227     }
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 ) )
232     {
233         addButton->setEnabled( true );
234         addButton->setToolTip( qtr(I_PL_ADDML) );
235     }
236     else
237         addButton->setEnabled( false );
238 }
239
240 /* PopupAdd Menu for the Add Menu */
241 void StandardPLPanel::popupAdd()
242 {
243     QMenu popup;
244     if( currentRootId == THEPL->p_local_category->i_id ||
245         currentRootId == THEPL->p_local_onelevel->i_id )
246     {
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()) );
250     }
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 ) )
255     {
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() ) );
259     }
260     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
261                         + QPoint( 0, addButton->height() ) );
262 }
263
264 void StandardPLPanel::popupSelectColumn( QPoint pos )
265 {
266     ContextUpdateMapper = new QSignalMapper(this);
267
268     QMenu selectColMenu;
269
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() );                \
276 }
277
278     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
279
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 );
288
289 #undef ADD_META_ACTION
290
291     selectColMenu.exec( QCursor::pos() );
292 }
293
294 /* ClearFilter LineEdit */
295 void StandardPLPanel::clearFilter()
296 {
297     searchLine->setText( "" );
298 }
299
300 /* Search in the playlist */
301 void StandardPLPanel::search( QString searchText )
302 {
303     model->search( searchText );
304 }
305
306 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
307 {
308     if( !index.isValid() ) return;
309     QItemSelectionModel *selection = view->selectionModel();
310     QModelIndexList list = selection->selectedIndexes();
311     model->popup( index, point, list );
312 }
313
314 /* Set the root of the new Playlist */
315 /* This activated by the selector selection */
316 void StandardPLPanel::setRoot( int i_root_id )
317 {
318     QPL_LOCK;
319     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
320                                                     true );
321     assert( p_item );
322     p_item = playlist_GetPreferredNode( THEPL, p_item );
323     assert( p_item );
324     QPL_UNLOCK;
325
326     model->rebuild( p_item );
327 }
328
329 void StandardPLPanel::removeItem( int i_id )
330 {
331     model->removeItem( i_id );
332 }
333
334 /* Delete and Suppr key remove the selection
335    FilterKey function and code function */
336 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
337 {
338     switch( e->key() )
339     {
340     case Qt::Key_Back:
341     case Qt::Key_Delete:
342         deleteSelection();
343         break;
344     }
345 }
346
347 void StandardPLPanel::deleteSelection()
348 {
349     QItemSelectionModel *selection = view->selectionModel();
350     QModelIndexList list = selection->selectedIndexes();
351     model->doDelete( list );
352 }
353
354 StandardPLPanel::~StandardPLPanel()
355 {}