]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Don't include config.h from the headers - refs #297.
[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( qtr( "X" ), this );
141     BUTTONACT( gotoPlayingButton, gotoPlayingItem() );
142     buttons->addWidget( gotoPlayingButton );
143
144     /* A Spacer and the search possibilities */
145     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
146     buttons->addItem( spacer );
147
148     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
149     buttons->addWidget( filter );
150
151     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
152     searchLine->setMinimumWidth( 80 );
153     CONNECT( searchLine, textChanged(QString), this, search(QString));
154     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
155
156     QPushButton *clear = new QPushButton;
157     clear->setText( qfu( "CL") );
158     clear->setMaximumWidth( 30 );
159     clear->setToolTip( qtr( "Clear" ));
160     BUTTONACT( clear, clearFilter() );
161     buttons->addWidget( clear );
162
163     /* Finish the layout */
164     layout->addWidget( view );
165     layout->addLayout( buttons );
166 //    layout->addWidget( bar );
167     setLayout( layout );
168 }
169
170 /* Function to toggle between the Repeat states */
171 void StandardPLPanel::toggleRepeat()
172 {
173     if( model->hasRepeat() )
174     {
175         model->setRepeat( false ); model->setLoop( true );
176         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
177         repeatButton->setToolTip( qtr( I_PL_LOOP ));
178     }
179     else if( model->hasLoop() )
180     {
181         model->setRepeat( false ) ; model->setLoop( false );
182         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
183         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
184     }
185     else
186     {
187         model->setRepeat( true );
188         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
189         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
190     }
191 }
192
193 /* Function to toggle between the Random states */
194 void StandardPLPanel::toggleRandom()
195 {
196     bool prev = model->hasRandom();
197     model->setRandom( !prev );
198     randomButton->setIcon( prev ?
199                 QIcon( ":/pixmaps/playlist_shuffle_off.png" ) :
200                 QIcon( ":/pixmaps/playlist_shuffle_on.png" ) );
201     randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
202 }
203
204 void StandardPLPanel::gotoPlayingItem()
205 {
206     view->scrollTo( view->currentIndex() );
207 }
208
209 void StandardPLPanel::handleExpansion( const QModelIndex &index )
210 {
211     if( model->isCurrent( index ) )
212         view->scrollTo( index, QAbstractItemView::EnsureVisible );
213 }
214
215 void StandardPLPanel::setCurrentRootId( int _new )
216 {
217     currentRootId = _new;
218     if( currentRootId == THEPL->p_local_category->i_id ||
219         currentRootId == THEPL->p_local_onelevel->i_id  )
220     {
221         addButton->setEnabled( true );
222         addButton->setToolTip( qtr(I_PL_ADDPL) );
223     }
224     else if( ( THEPL->p_ml_category &&
225                         currentRootId == THEPL->p_ml_category->i_id ) ||
226              ( THEPL->p_ml_onelevel &&
227                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
228     {
229         addButton->setEnabled( true );
230         addButton->setToolTip( qtr(I_PL_ADDML) );
231     }
232     else
233         addButton->setEnabled( false );
234 }
235
236 /* PopupAdd Menu for the Add Menu */
237 void StandardPLPanel::popupAdd()
238 {
239     QMenu popup;
240     if( currentRootId == THEPL->p_local_category->i_id ||
241         currentRootId == THEPL->p_local_onelevel->i_id )
242     {
243         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
244         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
245         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
246     }
247     else if( ( THEPL->p_ml_category &&
248                 currentRootId == THEPL->p_ml_category->i_id ) ||
249              ( THEPL->p_ml_onelevel &&
250                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
251     {
252         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
253         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
254         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
255     }
256     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
257                         + QPoint( 0, addButton->height() ) );
258 }
259
260 void StandardPLPanel::popupSelectColumn( QPoint pos )
261 {
262     ContextUpdateMapper = new QSignalMapper(this);
263
264     QMenu selectColMenu;
265
266 #define ADD_META_ACTION( meta ) { \
267     QAction* option = selectColMenu.addAction( qfu(VLC_META_##meta) );      \
268     option->setCheckable( true );                                           \
269     option->setChecked( model->shownFlags() & VLC_META_ENGINE_##meta );     \
270     ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta );      \
271     CONNECT( option, triggered(), ContextUpdateMapper, map() );             \
272 }
273
274     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
275
276     ADD_META_ACTION( TITLE );
277     ADD_META_ACTION( ARTIST );
278     ADD_META_ACTION( DURATION );
279     ADD_META_ACTION( COLLECTION );
280     ADD_META_ACTION( GENRE );
281     ADD_META_ACTION( SEQ_NUM );
282     ADD_META_ACTION( RATING );
283     ADD_META_ACTION( DESCRIPTION );
284
285 #undef ADD_META_ACTION
286
287     selectColMenu.exec( QCursor::pos() );
288 }
289
290 /* ClearFilter LineEdit */
291 void StandardPLPanel::clearFilter()
292 {
293     searchLine->setText( "" );
294 }
295
296 /* Search in the playlist */
297 void StandardPLPanel::search( QString searchText )
298 {
299     model->search( searchText );
300 }
301
302 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
303 {
304     if( !index.isValid() ) return;
305     QItemSelectionModel *selection = view->selectionModel();
306     QModelIndexList list = selection->selectedIndexes();
307     model->popup( index, point, list );
308 }
309
310 /* Set the root of the new Playlist */
311 /* This activated by the selector selection */
312 void StandardPLPanel::setRoot( int i_root_id )
313 {
314     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
315                                                     VLC_TRUE );
316     assert( p_item );
317     p_item = playlist_GetPreferredNode( THEPL, p_item );
318     assert( p_item );
319     model->rebuild( p_item );
320 }
321
322 void StandardPLPanel::removeItem( int i_id )
323 {
324     model->removeItem( i_id );
325 }
326
327 /* Delete and Suppr key remove the selection 
328    FilterKey function and code function */
329 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
330 {
331     switch( e->key() )
332     {
333     case Qt::Key_Back:
334     case Qt::Key_Delete:
335         deleteSelection();
336         break;
337     }
338 }
339
340 void StandardPLPanel::deleteSelection()
341 {
342     QItemSelectionModel *selection = view->selectionModel();
343     QModelIndexList list = selection->selectedIndexes();
344     model->doDelete( list );
345 }
346
347 StandardPLPanel::~StandardPLPanel()
348 {}