]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt Imageset update.
[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 #include "sorting.h"
51
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53                                   intf_thread_t *_p_intf,
54                                   playlist_t *p_playlist,
55                                   playlist_item_t *p_root ):
56                                   PLPanel( _parent, _p_intf )
57 {
58     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
59
60     QVBoxLayout *layout = new QVBoxLayout();
61     layout->setSpacing( 0 ); layout->setMargin( 0 );
62
63     /* Create and configure the QTreeView */
64     view = new QVLCTreeView( 0 );
65     view->setSortingEnabled( true );
66     view->sortByColumn( 0 , Qt::AscendingOrder );
67     view->setModel( model );
68     view->setIconSize( QSize( 20, 20 ) );
69     view->setAlternatingRowColors( true );
70     view->setAnimated( true );
71     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
72     view->setDragEnabled( true );
73     view->setAcceptDrops( true );
74     view->setDropIndicatorShown( true );
75     view->setAutoScroll( true );
76
77     /* Configure the size of the header */
78     view->header()->resizeSection( 0, 200 );
79     view->header()->resizeSection( 1, 80 );
80     view->header()->setSortIndicatorShown( true );
81     view->header()->setClickable( true );
82     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
83
84     /* Connections for the TreeView */
85     CONNECT( view, activated( const QModelIndex& ) ,
86              model,activateItem( const QModelIndex& ) );
87     CONNECT( view, rightClicked( QModelIndex , QPoint ),
88              this, doPopup( QModelIndex, QPoint ) );
89     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
90              this, handleExpansion( const QModelIndex& ) );
91     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
92              this, popupSelectColumn( QPoint ) );
93
94     currentRootId = -1;
95     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
96
97     /* Buttons configuration */
98     QHBoxLayout *buttons = new QHBoxLayout;
99
100     /* Add item to the playlist button */
101     addButton = new QPushButton;
102     addButton->setIcon( QIcon( ":/playlist_add" ) );
103     addButton->setMaximumWidth( 30 );
104     BUTTONACT( addButton, popupAdd() );
105     buttons->addWidget( addButton );
106
107     /* Random 2-state button */
108     randomButton = new QPushButton( this );
109     if( model->hasRandom() )
110     {
111         randomButton->setIcon( QIcon( ":/shuffle_on" ));
112         randomButton->setToolTip( qtr( I_PL_RANDOM ));
113     }
114     else
115     {
116          randomButton->setIcon( QIcon( ":/shuffle_off" ) );
117          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
118     }
119     BUTTONACT( randomButton, toggleRandom() );
120     buttons->addWidget( randomButton );
121
122     /* Repeat 3-state button */
123     repeatButton = new QPushButton( this );
124     if( model->hasRepeat() )
125     {
126         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
127         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
128     }
129     else if( model->hasLoop() )
130     {
131         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
132         repeatButton->setToolTip( qtr( I_PL_LOOP ));
133     }
134     else
135     {
136         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
137         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
138     }
139     BUTTONACT( repeatButton, toggleRepeat() );
140     buttons->addWidget( repeatButton );
141
142     /* Goto */
143     gotoPlayingButton = new QPushButton( "X" , this );
144     gotoPlayingButton->setToolTip( qtr( "Show the current item" ));
145     BUTTONACT( gotoPlayingButton, gotoPlayingItem() );
146     buttons->addWidget( gotoPlayingButton );
147
148     /* A Spacer and the search possibilities */
149     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
150     buttons->addItem( spacer );
151
152     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
153     buttons->addWidget( filter );
154
155     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
156     searchLine->setMinimumWidth( 80 );
157     CONNECT( searchLine, textChanged(QString), this, search(QString));
158     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
159
160     QPushButton *clear = new QPushButton;
161     clear->setMaximumWidth( 30 );
162     BUTTON_SET_ACT_I( clear, "", clear, qtr( "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( ":/repeat_all" ) );
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( ":/repeat_off" ) );
185         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
186     }
187     else
188     {
189         model->setRepeat( true );
190         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
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( ":/shuffle_off" ) :
202                 QIcon( ":/shuffle_on" ) );
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( psz_column_title( meta ) ) );     \
270     option->setCheckable( true );                                              \
271     option->setChecked( model->shownFlags() & meta );                          \
272     ContextUpdateMapper->setMapping( option, meta );                           \
273     CONNECT( option, triggered(), ContextUpdateMapper, map() );                \
274 }
275
276     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
277
278     ADD_META_ACTION( COLUMN_NUMBER );
279     ADD_META_ACTION( COLUMN_TITLE );
280     ADD_META_ACTION( COLUMN_DURATION );
281     ADD_META_ACTION( COLUMN_ARTIST );
282     ADD_META_ACTION( COLUMN_GENRE );
283     ADD_META_ACTION( COLUMN_ALBUM );
284     ADD_META_ACTION( COLUMN_TRACK_NUMBER );
285     ADD_META_ACTION( COLUMN_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     QPL_LOCK;
317     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
318                                                     pl_Locked );
319     assert( p_item );
320     p_item = playlist_GetPreferredNode( THEPL, p_item );
321     assert( p_item );
322     QPL_UNLOCK;
323
324     model->rebuild( p_item );
325 }
326
327 void StandardPLPanel::removeItem( int i_id )
328 {
329     model->removeItem( i_id );
330 }
331
332 /* Delete and Suppr key remove the selection
333    FilterKey function and code function */
334 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
335 {
336     switch( e->key() )
337     {
338     case Qt::Key_Back:
339     case Qt::Key_Delete:
340         deleteSelection();
341         break;
342     }
343 }
344
345 void StandardPLPanel::deleteSelection()
346 {
347     QItemSelectionModel *selection = view->selectionModel();
348     QModelIndexList list = selection->selectedIndexes();
349     model->doDelete( list );
350 }
351
352 StandardPLPanel::~StandardPLPanel()
353 {}