]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
4ecc873918e405b70300ebf7d1775c97f68ab378
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "qt4.hpp"
29 #include "dialogs_provider.hpp"
30
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/panels.hpp"
33 #include "util/customwidgets.hpp"
34
35 #include <vlc_intf_strings.h>
36
37 #include <QTreeView>
38 #include <QPushButton>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
41 #include <QHeaderView>
42 #include <QKeyEvent>
43 #include <QModelIndexList>
44 #include <QToolBar>
45 #include <QLabel>
46 #include <QSpacerItem>
47 #include <QMenu>
48 #include <QSignalMapper>
49 #include <assert.h>
50
51 #include "sorting.h"
52
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54                                   intf_thread_t *_p_intf,
55                                   playlist_t *p_playlist,
56                                   playlist_item_t *p_root ):
57                                   PLPanel( _parent, _p_intf )
58 {
59     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
60
61     QVBoxLayout *layout = new QVBoxLayout();
62     layout->setSpacing( 0 ); layout->setMargin( 0 );
63
64     /* Create and configure the QTreeView */
65     view = new QVLCTreeView;
66     view->header()->setSortIndicator( 0 , Qt::AscendingOrder );
67     view->setSortingEnabled( true );
68     view->setModel( model );
69     view->setIconSize( QSize( 20, 20 ) );
70     view->setAlternatingRowColors( true );
71     view->setAnimated( true );
72     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
73     view->setDragEnabled( true );
74     view->setAcceptDrops( true );
75     view->setDropIndicatorShown( true );
76     view->setAutoScroll( true );
77
78
79     getSettings()->beginGroup("Playlist");
80     if( getSettings()->contains( "headerState" ) )
81     {
82         view->header()->restoreState(
83                 getSettings()->value( "headerState" ).toByteArray() );
84     }
85     else
86     {
87         /* Configure the size of the header */
88         view->header()->resizeSection( 0, 200 );
89         view->header()->resizeSection( 1, 80 );
90     }
91     view->header()->setSortIndicatorShown( true );
92     view->header()->setClickable( true );
93     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
94     getSettings()->endGroup();
95
96     /* Connections for the TreeView */
97     CONNECT( view, activated( const QModelIndex& ) ,
98              model,activateItem( const QModelIndex& ) );
99     CONNECT( view, rightClicked( QModelIndex , QPoint ),
100              this, doPopup( QModelIndex, QPoint ) );
101     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
102              this, handleExpansion( const QModelIndex& ) );
103     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
104              this, popupSelectColumn( QPoint ) );
105
106     currentRootId = -1;
107     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
108
109     /* Buttons configuration */
110     QHBoxLayout *buttons = new QHBoxLayout;
111
112     /* Add item to the playlist button */
113     addButton = new QPushButton;
114     addButton->setIcon( QIcon( ":/playlist_add" ) );
115     addButton->setMaximumWidth( 30 );
116     BUTTONACT( addButton, popupAdd() );
117     buttons->addWidget( addButton );
118
119     /* Random 2-state button */
120     randomButton = new QPushButton( this );
121     randomButton->setIcon( QIcon( ":/shuffle_on" ));
122     randomButton->setToolTip( qtr( I_PL_RANDOM ));
123     randomButton->setCheckable( true );
124     randomButton->setChecked( model->hasRandom() );
125     BUTTONACT( randomButton, toggleRandom() );
126     buttons->addWidget( randomButton );
127
128     /* Repeat 3-state button */
129     repeatButton = new QPushButton( this );
130     repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
131     repeatButton->setCheckable( true );
132
133     if( model->hasRepeat() )
134     {
135         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
136         repeatButton->setChecked( true );
137     }
138     else if( model->hasLoop() )
139     {
140         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
141         repeatButton->setChecked( true );
142     }
143     else
144     {
145         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
146         repeatButton->setChecked( false );
147     }
148     BUTTONACT( repeatButton, toggleRepeat() );
149     buttons->addWidget( repeatButton );
150
151     /* Goto */
152     gotoPlayingButton = new QPushButton;
153     BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
154             qtr( "Show the current item" ), gotoPlayingItem() );
155     buttons->addWidget( gotoPlayingButton );
156
157     /* A Spacer and the search possibilities */
158     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
159     buttons->addItem( spacer );
160
161     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
162     buttons->addWidget( filter );
163
164     SearchLineEdit *search = new SearchLineEdit( this );
165     buttons->addWidget( search );
166     filter->setBuddy( search );
167     CONNECT( search, textChanged( QString ), this, search( QString ) );
168
169     /* Finish the layout */
170     layout->addWidget( view );
171     layout->addLayout( buttons );
172 //    layout->addWidget( bar );
173     setLayout( layout );
174 }
175
176 /* Function to toggle between the Repeat states */
177 void StandardPLPanel::toggleRepeat()
178 {
179     if( model->hasRepeat() )
180     {
181         model->setRepeat( false ); model->setLoop( true );
182         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
183         repeatButton->setChecked( true );
184     }
185     else if( model->hasLoop() )
186     {
187         model->setRepeat( false ) ; model->setLoop( false );
188         repeatButton->setChecked( false );
189         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
190     }
191     else
192     {
193         model->setRepeat( true ); model->setLoop( false );
194         repeatButton->setChecked( true );
195         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
196     }
197 }
198
199 /* Function to toggle between the Random states */
200 void StandardPLPanel::toggleRandom()
201 {
202     bool prev = model->hasRandom();
203     model->setRandom( !prev );
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(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( 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     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
267
268     int i_column = 1;
269     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
270     {
271         QAction* option = selectColMenu.addAction(
272             qfu( psz_column_title( i_column ) ) );
273         option->setCheckable( true );
274         option->setChecked( model->shownFlags() & i_column );
275         ContextUpdateMapper->setMapping( option, i_column );
276         CONNECT( option, triggered(), ContextUpdateMapper, map() );
277     }
278
279     selectColMenu.exec( QCursor::pos() );
280 }
281
282 /* Search in the playlist */
283 void StandardPLPanel::search( QString searchText )
284 {
285     model->search( searchText );
286 }
287
288 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
289 {
290     if( !index.isValid() ) return;
291     QItemSelectionModel *selection = view->selectionModel();
292     QModelIndexList list = selection->selectedIndexes();
293     model->popup( index, point, list );
294 }
295
296 /* Set the root of the new Playlist */
297 /* This activated by the selector selection */
298 void StandardPLPanel::setRoot( int i_root_id )
299 {
300     QPL_LOCK;
301     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
302     assert( p_item );
303     p_item = playlist_GetPreferredNode( THEPL, p_item );
304     assert( p_item );
305     QPL_UNLOCK;
306
307     model->rebuild( p_item );
308 }
309
310 void StandardPLPanel::removeItem( int i_id )
311 {
312     model->removeItem( i_id );
313 }
314
315 /* Delete and Suppr key remove the selection
316    FilterKey function and code function */
317 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
318 {
319     switch( e->key() )
320     {
321     case Qt::Key_Back:
322     case Qt::Key_Delete:
323         deleteSelection();
324         break;
325     }
326 }
327
328 void StandardPLPanel::deleteSelection()
329 {
330     QItemSelectionModel *selection = view->selectionModel();
331     QModelIndexList list = selection->selectedIndexes();
332     model->doDelete( list );
333 }
334
335 StandardPLPanel::~StandardPLPanel()
336 {
337     getSettings()->beginGroup("Playlist");
338     getSettings()->setValue( "headerState", view->header()->saveState() );
339     getSettings()->endGroup();
340 }
341
342