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