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