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