]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
qt4: The media library can be disabled
[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 #include "qt4.hpp"
25 #include "dialogs_provider.hpp"
26 #include "playlist_model.hpp"
27 #include "components/playlist/panels.hpp"
28 #include "util/customwidgets.hpp"
29
30 #include <vlc_intf_strings.h>
31
32 #include <QTreeView>
33 #include <QPushButton>
34 #include <QHBoxLayout>
35 #include <QVBoxLayout>
36 #include <QHeaderView>
37 #include <QKeyEvent>
38 #include <QModelIndexList>
39 #include <QToolBar>
40 #include <QLabel>
41 #include <QSpacerItem>
42 #include <QMenu>
43
44 #include <assert.h>
45
46 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
47                                   intf_thread_t *_p_intf,
48                                   playlist_t *p_playlist,
49                                   playlist_item_t *p_root ):
50                                   PLPanel( _parent, _p_intf )
51 {
52     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
53  
54     /* Create and configure the QTreeView */
55     view = new QVLCTreeView( 0 );
56     view->setModel(model);
57     view->setIconSize( QSize(20,20) );
58     view->setAlternatingRowColors( true );
59     view->setAnimated( true );
60     view->setSortingEnabled( true );    
61     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
62     view->setDragEnabled( true );
63     view->setAcceptDrops( true );
64     view->setDropIndicatorShown( true );
65     view->setAutoScroll( true );
66  
67     view->header()->resizeSection( 0, 230 );
68     view->header()->resizeSection( 1, 170 );
69     view->header()->setSortIndicatorShown( true );
70     view->header()->setClickable( true );
71     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
72  
73     CONNECT( view, activated( const QModelIndex& ) ,
74              model,activateItem( const QModelIndex& ) );
75     CONNECT( view, rightClicked( QModelIndex , QPoint ),
76              this, doPopup( QModelIndex, QPoint ) );
77     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
78              this, handleExpansion( const QModelIndex& ) );
79     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
80              this, popupSelectColumn( QPoint ) );
81
82     currentRootId = -1;
83     CONNECT( parent, rootChanged(int), this, setCurrentRootId( int ) );
84
85     QVBoxLayout *layout = new QVBoxLayout();
86     layout->setSpacing( 0 ); layout->setMargin( 0 );
87
88     /* Buttons configuration */
89     QHBoxLayout *buttons = new QHBoxLayout();
90  
91     addButton = new QPushButton( QIcon( ":/pixmaps/playlist_add.png" ), "", this );
92     addButton->setMaximumWidth( 25 );
93     BUTTONACT( addButton, popupAdd() );
94     buttons->addWidget( addButton );
95
96     randomButton = new QPushButton( this );
97     if( model->hasRandom() )
98     {
99         randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_on.png" ));
100         randomButton->setToolTip( qtr( I_PL_RANDOM ));
101     }
102     else
103     {
104          randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_off.png" ) );
105          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
106     }
107     BUTTONACT( randomButton, toggleRandom() );
108     buttons->addWidget( randomButton );
109
110     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
111     buttons->addItem( spacer );
112
113     repeatButton = new QPushButton( this );
114     if( model->hasRepeat() ) 
115     {
116         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
117         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
118     }
119     else if( model->hasLoop() )
120     {
121         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
122         repeatButton->setToolTip( qtr( I_PL_LOOP ));
123     }   
124     else
125     {
126         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
127         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
128     }   
129     BUTTONACT( repeatButton, toggleRepeat() );
130     buttons->addWidget( repeatButton );
131
132
133     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
134     buttons->addWidget( filter );
135     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
136     CONNECT( searchLine, textChanged(QString), this, search(QString));
137     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
138
139     QPushButton *clear = new QPushButton( qfu( "CL") );
140     clear->setMaximumWidth( 30 );
141     clear->setToolTip( qtr( "Clear" ));
142     BUTTONACT( clear, clearFilter() );
143     buttons->addWidget( clear );
144
145     layout->addWidget( view );
146     layout->addLayout( buttons );
147 //    layout->addWidget( bar );
148     setLayout( layout );
149 }
150
151 void StandardPLPanel::toggleRepeat()
152 {
153     if( model->hasRepeat() )
154     {
155         model->setRepeat( false ); model->setLoop( true );
156         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
157         repeatButton->setToolTip( qtr( I_PL_LOOP ));
158     }
159     else if( model->hasLoop() )
160     {
161         model->setRepeat( false ) ; model->setLoop( false );
162         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
163         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
164     }
165     else
166     {
167         model->setRepeat( true );
168         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
169         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
170     }
171 }
172
173 void StandardPLPanel::toggleRandom()
174 {
175     bool prev = model->hasRandom();
176     model->setRandom( !prev );
177     randomButton->setIcon( prev ?
178                 QIcon( ":/pixmaps/playlist_shuffle_off.png" ) :
179                 QIcon( ":/pixmaps/playlist_shuffle_on.png" ) );
180     randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
181 }
182
183 void StandardPLPanel::handleExpansion( const QModelIndex &index )
184 {
185     QModelIndex parent;
186     if( model->isCurrent( index ) )
187     {
188         view->scrollTo( index, QAbstractItemView::EnsureVisible );
189         parent = index;
190         while( parent.isValid() )
191         {
192             view->setExpanded( parent, true );
193             parent = model->parent( parent );
194         }
195     }
196 }
197
198 void StandardPLPanel::setCurrentRootId( int _new )
199 {
200     currentRootId = _new;
201     if( currentRootId == THEPL->p_local_category->i_id ||
202         currentRootId == THEPL->p_local_onelevel->i_id  )
203     {
204         addButton->setEnabled( true );
205         addButton->setToolTip( qtr(I_PL_ADDPL) );
206     }
207     else if( ( THEPL->p_ml_category &&
208                         currentRootId == THEPL->p_ml_category->i_id ) ||
209              ( THEPL->p_ml_onelevel &&
210                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
211     {
212         addButton->setEnabled( true );
213         addButton->setToolTip( qtr(I_PL_ADDML) );
214     }
215     else
216         addButton->setEnabled( false );
217 }
218
219 void StandardPLPanel::popupAdd()
220 {
221     QMenu popup;
222     if( currentRootId == THEPL->p_local_category->i_id ||
223         currentRootId == THEPL->p_local_onelevel->i_id )
224     {
225         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
226         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
227         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
228     }
229     else if( ( THEPL->p_ml_category &&
230                 currentRootId == THEPL->p_ml_category->i_id ) ||
231              ( THEPL->p_ml_onelevel &&
232                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
233     {
234         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
235         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
236         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
237     }
238     popup.exec( QCursor::pos() );
239 }
240
241 void StandardPLPanel::popupSelectColumn( QPoint )
242 {
243     ContextUpdateMapper = new QSignalMapper(this);
244
245     QMenu selectColMenu;
246
247 #define ADD_META_ACTION( meta ) { \
248    QAction* option = selectColMenu.addAction( qfu(VLC_META_##meta) );     \
249    option->setCheckable( true );                                           \
250    option->setChecked( model->shownFlags() & VLC_META_ENGINE_##meta );   \
251    ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta );      \
252    CONNECT( option, triggered(), ContextUpdateMapper, map() );             \
253    }
254     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
255
256     ADD_META_ACTION( TITLE );
257     ADD_META_ACTION( ARTIST );
258     ADD_META_ACTION( DURATION );
259     ADD_META_ACTION( COLLECTION );
260     ADD_META_ACTION( GENRE );
261     ADD_META_ACTION( SEQ_NUM );
262     ADD_META_ACTION( RATING );
263     ADD_META_ACTION( DESCRIPTION );
264
265 #undef ADD_META_ACTION
266  
267     selectColMenu.exec( QCursor::pos() );
268  }
269
270 void StandardPLPanel::clearFilter()
271 {
272     searchLine->setText( "" );
273 }
274
275 void StandardPLPanel::search( QString searchText )
276 {
277     model->search( searchText );
278 }
279
280 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
281 {
282     if( !index.isValid() ) return;
283     QItemSelectionModel *selection = view->selectionModel();
284     QModelIndexList list = selection->selectedIndexes();
285     model->popup( index, point, list );
286 }
287
288 void StandardPLPanel::setRoot( int i_root_id )
289 {
290     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
291                                                     VLC_TRUE );
292     assert( p_item );
293     p_item = playlist_GetPreferredNode( THEPL, p_item );
294     assert( p_item );
295     model->rebuild( p_item );
296 }
297
298 void StandardPLPanel::removeItem( int i_id )
299 {
300     model->removeItem( i_id );
301 }
302
303 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
304 {
305     switch( e->key() )
306     {
307     case Qt::Key_Back:
308     case Qt::Key_Delete:
309         deleteSelection();
310         break;
311     }
312 }
313
314 void StandardPLPanel::deleteSelection()
315 {
316     QItemSelectionModel *selection = view->selectionModel();
317     QModelIndexList list = selection->selectedIndexes();
318     model->doDelete( list );
319 }
320
321 StandardPLPanel::~StandardPLPanel()
322 {}