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