]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
65e4e51637166ac3e4f039c21a9f1164768a4271
[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 #if HAS_QT43
78     if( getSettings()->contains( "headerState" ) )
79     {
80         view->header()->restoreState( getSettings()->value( "headerState" ).toByteArray() );
81         msg_Dbg( p_intf, "exists" );
82     }
83     else
84 #endif
85     {
86         /* Configure the size of the header */
87         view->header()->resizeSection( 0, 200 );
88         view->header()->resizeSection( 1, 80 );
89         view->header()->setSortIndicatorShown( true );
90         view->header()->setClickable( true );
91         view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
92     }
93
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 ) );
103
104     currentRootId = -1;
105     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
106
107     /* Buttons configuration */
108     QHBoxLayout *buttons = new QHBoxLayout;
109
110     /* Add item to the playlist button */
111     addButton = new QPushButton;
112     addButton->setIcon( QIcon( ":/playlist_add" ) );
113     addButton->setMaximumWidth( 30 );
114     BUTTONACT( addButton, popupAdd() );
115     buttons->addWidget( addButton );
116
117     /* Random 2-state button */
118     randomButton = new QPushButton( this );
119     if( model->hasRandom() )
120     {
121         randomButton->setIcon( QIcon( ":/shuffle_on" ));
122         randomButton->setToolTip( qtr( I_PL_RANDOM ));
123     }
124     else
125     {
126          randomButton->setIcon( QIcon( ":/shuffle_off" ) );
127          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
128     }
129     BUTTONACT( randomButton, toggleRandom() );
130     buttons->addWidget( randomButton );
131
132     /* Repeat 3-state button */
133     repeatButton = new QPushButton( this );
134     if( model->hasRepeat() )
135     {
136         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
137         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
138     }
139     else if( model->hasLoop() )
140     {
141         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
142         repeatButton->setToolTip( qtr( I_PL_LOOP ));
143     }
144     else
145     {
146         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
147         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
148     }
149     BUTTONACT( repeatButton, toggleRepeat() );
150     buttons->addWidget( repeatButton );
151
152     /* Goto */
153     gotoPlayingButton = new QPushButton;
154     BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
155             qtr( "Show the current item" ), gotoPlayingItem() );
156     buttons->addWidget( gotoPlayingButton );
157
158     /* A Spacer and the search possibilities */
159     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
160     buttons->addItem( spacer );
161
162     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
163     buttons->addWidget( filter );
164
165     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
166     searchLine->setMinimumWidth( 80 );
167     CONNECT( searchLine, textChanged(QString), this, search(QString));
168     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
169
170     QPushButton *clear = new QPushButton;
171     clear->setMaximumWidth( 30 );
172     BUTTON_SET_ACT_I( clear, "", clear, qtr( "Clear" ), clearFilter() );
173     buttons->addWidget( clear );
174
175     /* Finish the layout */
176     layout->addWidget( view );
177     layout->addLayout( buttons );
178 //    layout->addWidget( bar );
179     setLayout( layout );
180 }
181
182 /* Function to toggle between the Repeat states */
183 void StandardPLPanel::toggleRepeat()
184 {
185     if( model->hasRepeat() )
186     {
187         model->setRepeat( false ); model->setLoop( true );
188         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
189         repeatButton->setToolTip( qtr( I_PL_LOOP ));
190     }
191     else if( model->hasLoop() )
192     {
193         model->setRepeat( false ) ; model->setLoop( false );
194         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
195         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
196     }
197     else
198     {
199         model->setRepeat( true );
200         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
201         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
202     }
203 }
204
205 /* Function to toggle between the Random states */
206 void StandardPLPanel::toggleRandom()
207 {
208     bool prev = model->hasRandom();
209     model->setRandom( !prev );
210     randomButton->setIcon( prev ?
211                 QIcon( ":/shuffle_off" ) :
212                 QIcon( ":/shuffle_on" ) );
213     randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
214 }
215
216 void StandardPLPanel::gotoPlayingItem()
217 {
218     view->scrollTo( view->currentIndex() );
219 }
220
221 void StandardPLPanel::handleExpansion( const QModelIndex &index )
222 {
223     if( model->isCurrent( index ) )
224         view->scrollTo( index, QAbstractItemView::EnsureVisible );
225 }
226
227 void StandardPLPanel::setCurrentRootId( int _new )
228 {
229     currentRootId = _new;
230     if( currentRootId == THEPL->p_local_category->i_id ||
231         currentRootId == THEPL->p_local_onelevel->i_id  )
232     {
233         addButton->setEnabled( true );
234         addButton->setToolTip( qtr(I_PL_ADDPL) );
235     }
236     else if( ( THEPL->p_ml_category &&
237                         currentRootId == THEPL->p_ml_category->i_id ) ||
238              ( THEPL->p_ml_onelevel &&
239                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
240     {
241         addButton->setEnabled( true );
242         addButton->setToolTip( qtr(I_PL_ADDML) );
243     }
244     else
245         addButton->setEnabled( false );
246 }
247
248 /* PopupAdd Menu for the Add Menu */
249 void StandardPLPanel::popupAdd()
250 {
251     QMenu popup;
252     if( currentRootId == THEPL->p_local_category->i_id ||
253         currentRootId == THEPL->p_local_onelevel->i_id )
254     {
255         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
256         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
257         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
258     }
259     else if( ( THEPL->p_ml_category &&
260                 currentRootId == THEPL->p_ml_category->i_id ) ||
261              ( THEPL->p_ml_onelevel &&
262                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
263     {
264         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
265         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
266         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
267     }
268     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
269                         + QPoint( 0, addButton->height() ) );
270 }
271
272 void StandardPLPanel::popupSelectColumn( QPoint pos )
273 {
274     ContextUpdateMapper = new QSignalMapper(this);
275
276     QMenu selectColMenu;
277
278 #define ADD_META_ACTION( meta ) {                                              \
279     QAction* option = selectColMenu.addAction( qfu( psz_column_title( meta ) ) );     \
280     option->setCheckable( true );                                              \
281     option->setChecked( model->shownFlags() & meta );                          \
282     ContextUpdateMapper->setMapping( option, meta );                           \
283     CONNECT( option, triggered(), ContextUpdateMapper, map() );                \
284 }
285
286     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
287
288     ADD_META_ACTION( COLUMN_NUMBER );
289     ADD_META_ACTION( COLUMN_TITLE );
290     ADD_META_ACTION( COLUMN_DURATION );
291     ADD_META_ACTION( COLUMN_ARTIST );
292     ADD_META_ACTION( COLUMN_GENRE );
293     ADD_META_ACTION( COLUMN_ALBUM );
294     ADD_META_ACTION( COLUMN_TRACK_NUMBER );
295     ADD_META_ACTION( COLUMN_DESCRIPTION );
296
297 #undef ADD_META_ACTION
298
299     selectColMenu.exec( QCursor::pos() );
300 }
301
302 /* ClearFilter LineEdit */
303 void StandardPLPanel::clearFilter()
304 {
305     searchLine->setText( "" );
306 }
307
308 /* Search in the playlist */
309 void StandardPLPanel::search( QString searchText )
310 {
311     model->search( searchText );
312 }
313
314 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
315 {
316     if( !index.isValid() ) return;
317     QItemSelectionModel *selection = view->selectionModel();
318     QModelIndexList list = selection->selectedIndexes();
319     model->popup( index, point, list );
320 }
321
322 /* Set the root of the new Playlist */
323 /* This activated by the selector selection */
324 void StandardPLPanel::setRoot( int i_root_id )
325 {
326     QPL_LOCK;
327     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
328                                                     pl_Locked );
329     assert( p_item );
330     p_item = playlist_GetPreferredNode( THEPL, p_item );
331     assert( p_item );
332     QPL_UNLOCK;
333
334     model->rebuild( p_item );
335 }
336
337 void StandardPLPanel::removeItem( int i_id )
338 {
339     model->removeItem( i_id );
340 }
341
342 /* Delete and Suppr key remove the selection
343    FilterKey function and code function */
344 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
345 {
346     switch( e->key() )
347     {
348     case Qt::Key_Back:
349     case Qt::Key_Delete:
350         deleteSelection();
351         break;
352     }
353 }
354
355 void StandardPLPanel::deleteSelection()
356 {
357     QItemSelectionModel *selection = view->selectionModel();
358     QModelIndexList list = selection->selectedIndexes();
359     model->doDelete( list );
360 }
361
362 StandardPLPanel::~StandardPLPanel()
363 {
364 #if HAS_QT43
365     getSettings()->beginGroup("playlistdialog");
366     getSettings()->setValue( "headerState", view->header()->saveState() );
367     getSettings()->endGroup();
368 #endif
369 }
370
371