]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
e5ba2e6d134f746c7524ec6cc079857c54cc0f7d
[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
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "qt4.hpp"
29 #include "dialogs_provider.hpp"
30
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/panels.hpp"
33 #include "util/customwidgets.hpp"
34
35 #include <vlc_intf_strings.h>
36
37 #include <QTreeView>
38 #include <QPushButton>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
41 #include <QHeaderView>
42 #include <QKeyEvent>
43 #include <QModelIndexList>
44 #include <QToolBar>
45 #include <QLabel>
46 #include <QSpacerItem>
47 #include <QMenu>
48 #include <QSignalMapper>
49 #include <assert.h>
50
51 #include "sorting.h"
52
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54                                   intf_thread_t *_p_intf,
55                                   playlist_t *p_playlist,
56                                   playlist_item_t *p_root ):
57                                   PLPanel( _parent, _p_intf )
58 {
59     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
60
61     QVBoxLayout *layout = new QVBoxLayout();
62     layout->setSpacing( 0 ); layout->setMargin( 0 );
63
64     /* Create and configure the QTreeView */
65     view = new QVLCTreeView;
66     view->header()->setSortIndicator( 0 , Qt::AscendingOrder );
67     view->setSortingEnabled( true );
68     view->setModel( model );
69     view->setIconSize( QSize( 20, 20 ) );
70     view->setAlternatingRowColors( true );
71     view->setAnimated( true );
72     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
73     view->setDragEnabled( true );
74     view->setAcceptDrops( true );
75     view->setDropIndicatorShown( true );
76     view->setAutoScroll( true );
77
78
79     getSettings()->beginGroup("Playlist");
80     if( getSettings()->contains( "headerState" ) )
81     {
82         view->header()->restoreState(
83                 getSettings()->value( "headerState" ).toByteArray() );
84     }
85     else
86     {
87         /* Configure the size of the header */
88         view->header()->resizeSection( 0, 200 );
89         view->header()->resizeSection( 1, 80 );
90     }
91     view->header()->setSortIndicatorShown( true );
92     view->header()->setClickable( true );
93     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
94     getSettings()->endGroup();
95
96     /* Connections for the TreeView */
97     CONNECT( view, activated( const QModelIndex& ) ,
98              model,activateItem( const QModelIndex& ) );
99     CONNECT( view, rightClicked( QModelIndex , QPoint ),
100              this, doPopup( QModelIndex, QPoint ) );
101     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
102              this, handleExpansion( const QModelIndex& ) );
103     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
104              this, popupSelectColumn( QPoint ) );
105
106     currentRootId = -1;
107     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
108
109     /* Buttons configuration */
110     QHBoxLayout *buttons = new QHBoxLayout;
111
112     /* Add item to the playlist button */
113     addButton = new QPushButton;
114     addButton->setIcon( QIcon( ":/playlist_add" ) );
115     addButton->setMaximumWidth( 30 );
116     BUTTONACT( addButton, popupAdd() );
117     buttons->addWidget( addButton );
118
119     /* Random 2-state button */
120     randomButton = new QPushButton( this );
121     if( model->hasRandom() )
122     {
123         randomButton->setIcon( QIcon( ":/shuffle_on" ));
124         randomButton->setToolTip( qtr( I_PL_RANDOM ));
125     }
126     else
127     {
128          randomButton->setIcon( QIcon( ":/shuffle_off" ) );
129          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
130     }
131     BUTTONACT( randomButton, toggleRandom() );
132     buttons->addWidget( randomButton );
133
134     /* Repeat 3-state button */
135     repeatButton = new QPushButton( this );
136     if( model->hasRepeat() )
137     {
138         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
139         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
140     }
141     else if( model->hasLoop() )
142     {
143         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
144         repeatButton->setToolTip( qtr( I_PL_LOOP ));
145     }
146     else
147     {
148         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
149         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
150     }
151     BUTTONACT( repeatButton, toggleRepeat() );
152     buttons->addWidget( repeatButton );
153
154     /* Goto */
155     gotoPlayingButton = new QPushButton;
156     BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
157             qtr( "Show the current item" ), gotoPlayingItem() );
158     buttons->addWidget( gotoPlayingButton );
159
160     /* A Spacer and the search possibilities */
161     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
162     buttons->addItem( spacer );
163
164     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
165     buttons->addWidget( filter );
166
167     SearchLineEdit *search = new SearchLineEdit( this );
168     buttons->addWidget( search );
169     filter->setBuddy( search );
170     CONNECT( search, textChanged( QString ), this, search( QString ) );
171
172     /* Finish the layout */
173     layout->addWidget( view );
174     layout->addLayout( buttons );
175 //    layout->addWidget( bar );
176     setLayout( layout );
177 }
178
179 /* Function to toggle between the Repeat states */
180 void StandardPLPanel::toggleRepeat()
181 {
182     if( model->hasRepeat() )
183     {
184         model->setRepeat( false ); model->setLoop( true );
185         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
186         repeatButton->setToolTip( qtr( I_PL_LOOP ));
187     }
188     else if( model->hasLoop() )
189     {
190         model->setRepeat( false ) ; model->setLoop( false );
191         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
192         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
193     }
194     else
195     {
196         model->setRepeat( true );
197         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
198         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
199     }
200 }
201
202 /* Function to toggle between the Random states */
203 void StandardPLPanel::toggleRandom()
204 {
205     bool prev = model->hasRandom();
206     model->setRandom( !prev );
207     randomButton->setIcon( prev ?
208                 QIcon( ":/shuffle_off" ) :
209                 QIcon( ":/shuffle_on" ) );
210     randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
211 }
212
213 void StandardPLPanel::gotoPlayingItem()
214 {
215     view->scrollTo( view->currentIndex() );
216 }
217
218 void StandardPLPanel::handleExpansion( const QModelIndex &index )
219 {
220     if( model->isCurrent( index ) )
221         view->scrollTo( index, QAbstractItemView::EnsureVisible );
222 }
223
224 void StandardPLPanel::setCurrentRootId( int _new )
225 {
226     currentRootId = _new;
227     if( currentRootId == THEPL->p_local_category->i_id ||
228         currentRootId == THEPL->p_local_onelevel->i_id  )
229     {
230         addButton->setEnabled( true );
231         addButton->setToolTip( qtr(I_PL_ADDPL) );
232     }
233     else if( ( THEPL->p_ml_category &&
234                         currentRootId == THEPL->p_ml_category->i_id ) ||
235              ( THEPL->p_ml_onelevel &&
236                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
237     {
238         addButton->setEnabled( true );
239         addButton->setToolTip( qtr(I_PL_ADDML) );
240     }
241     else
242         addButton->setEnabled( false );
243 }
244
245 /* PopupAdd Menu for the Add Menu */
246 void StandardPLPanel::popupAdd()
247 {
248     QMenu popup;
249     if( currentRootId == THEPL->p_local_category->i_id ||
250         currentRootId == THEPL->p_local_onelevel->i_id )
251     {
252         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
253         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
254     }
255     else if( ( THEPL->p_ml_category &&
256                 currentRootId == THEPL->p_ml_category->i_id ) ||
257              ( THEPL->p_ml_onelevel &&
258                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
259     {
260         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
261         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
262     }
263     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
264                         + QPoint( 0, addButton->height() ) );
265 }
266
267 void StandardPLPanel::popupSelectColumn( QPoint pos )
268 {
269     ContextUpdateMapper = new QSignalMapper(this);
270
271     QMenu selectColMenu;
272
273     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
274
275     int i_column = 1;
276     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
277     {
278         QAction* option = selectColMenu.addAction(
279             qfu( psz_column_title( i_column ) ) );
280         option->setCheckable( true );
281         option->setChecked( model->shownFlags() & i_column );
282         ContextUpdateMapper->setMapping( option, i_column );
283         CONNECT( option, triggered(), ContextUpdateMapper, map() );
284     }
285
286     selectColMenu.exec( QCursor::pos() );
287 }
288
289 /* Search in the playlist */
290 void StandardPLPanel::search( QString searchText )
291 {
292     model->search( searchText );
293 }
294
295 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
296 {
297     if( !index.isValid() ) return;
298     QItemSelectionModel *selection = view->selectionModel();
299     QModelIndexList list = selection->selectedIndexes();
300     model->popup( index, point, list );
301 }
302
303 /* Set the root of the new Playlist */
304 /* This activated by the selector selection */
305 void StandardPLPanel::setRoot( int i_root_id )
306 {
307     QPL_LOCK;
308     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
309                                                     pl_Locked );
310     assert( p_item );
311     p_item = playlist_GetPreferredNode( THEPL, p_item );
312     assert( p_item );
313     QPL_UNLOCK;
314
315     model->rebuild( p_item );
316 }
317
318 void StandardPLPanel::removeItem( int i_id )
319 {
320     model->removeItem( i_id );
321 }
322
323 /* Delete and Suppr key remove the selection
324    FilterKey function and code function */
325 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
326 {
327     switch( e->key() )
328     {
329     case Qt::Key_Back:
330     case Qt::Key_Delete:
331         deleteSelection();
332         break;
333     }
334 }
335
336 void StandardPLPanel::deleteSelection()
337 {
338     QItemSelectionModel *selection = view->selectionModel();
339     QModelIndexList list = selection->selectedIndexes();
340     model->doDelete( list );
341 }
342
343 StandardPLPanel::~StandardPLPanel()
344 {
345     getSettings()->beginGroup("Playlist");
346     getSettings()->setValue( "headerState", view->header()->saveState() );
347     getSettings()->endGroup();
348 }
349
350