]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Add URI column to Qt4 playlist. Add sorting by URI in playlist core.
[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 #include "components/playlist/playlist_model.hpp"
31 #include "components/playlist/panels.hpp"
32 #include "util/customwidgets.hpp"
33
34 #include <vlc_intf_strings.h>
35
36 #include <QTreeView>
37 #include <QPushButton>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
40 #include <QHeaderView>
41 #include <QKeyEvent>
42 #include <QModelIndexList>
43 #include <QToolBar>
44 #include <QLabel>
45 #include <QSpacerItem>
46 #include <QMenu>
47 #include <QSignalMapper>
48
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( 0 );
66     view->setSortingEnabled( true );
67     view->sortByColumn( 0 , Qt::AscendingOrder );
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 #if HAS_QT43
79     if( getSettings()->contains( "headerState" ) )
80     {
81         view->header()->restoreState( getSettings()->value( "headerState" ).toByteArray() );
82         msg_Dbg( p_intf, "exists" );
83     }
84     else
85 #endif
86     {
87         /* Configure the size of the header */
88         view->header()->resizeSection( 0, 200 );
89         view->header()->resizeSection( 1, 80 );
90         view->header()->setSortIndicatorShown( true );
91         view->header()->setClickable( true );
92         view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
93     }
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( ":/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     if( model->hasRandom() )
121     {
122         randomButton->setIcon( QIcon( ":/shuffle_on" ));
123         randomButton->setToolTip( qtr( I_PL_RANDOM ));
124     }
125     else
126     {
127          randomButton->setIcon( QIcon( ":/shuffle_off" ) );
128          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
129     }
130     BUTTONACT( randomButton, toggleRandom() );
131     buttons->addWidget( randomButton );
132
133     /* Repeat 3-state button */
134     repeatButton = new QPushButton( this );
135     if( model->hasRepeat() )
136     {
137         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
138         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
139     }
140     else if( model->hasLoop() )
141     {
142         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
143         repeatButton->setToolTip( qtr( I_PL_LOOP ));
144     }
145     else
146     {
147         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
148         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
149     }
150     BUTTONACT( repeatButton, toggleRepeat() );
151     buttons->addWidget( repeatButton );
152
153     /* Goto */
154     gotoPlayingButton = new QPushButton;
155     BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
156             qtr( "Show the current item" ), gotoPlayingItem() );
157     buttons->addWidget( gotoPlayingButton );
158
159     /* A Spacer and the search possibilities */
160     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
161     buttons->addItem( spacer );
162
163     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
164     buttons->addWidget( filter );
165
166     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
167     searchLine->setMinimumWidth( 80 );
168     CONNECT( searchLine, textChanged(QString), this, search(QString));
169     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
170
171     QPushButton *clear = new QPushButton;
172     clear->setMaximumWidth( 30 );
173     BUTTON_SET_ACT_I( clear, "", clear, qtr( "Clear" ), clearFilter() );
174     buttons->addWidget( clear );
175
176     /* Finish the layout */
177     layout->addWidget( view );
178     layout->addLayout( buttons );
179 //    layout->addWidget( bar );
180     setLayout( layout );
181 }
182
183 /* Function to toggle between the Repeat states */
184 void StandardPLPanel::toggleRepeat()
185 {
186     if( model->hasRepeat() )
187     {
188         model->setRepeat( false ); model->setLoop( true );
189         repeatButton->setIcon( QIcon( ":/repeat_all" ) );
190         repeatButton->setToolTip( qtr( I_PL_LOOP ));
191     }
192     else if( model->hasLoop() )
193     {
194         model->setRepeat( false ) ; model->setLoop( false );
195         repeatButton->setIcon( QIcon( ":/repeat_off" ) );
196         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
197     }
198     else
199     {
200         model->setRepeat( true );
201         repeatButton->setIcon( QIcon( ":/repeat_one" ) );
202         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
203     }
204 }
205
206 /* Function to toggle between the Random states */
207 void StandardPLPanel::toggleRandom()
208 {
209     bool prev = model->hasRandom();
210     model->setRandom( !prev );
211     randomButton->setIcon( prev ?
212                 QIcon( ":/shuffle_off" ) :
213                 QIcon( ":/shuffle_on" ) );
214     randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
215 }
216
217 void StandardPLPanel::gotoPlayingItem()
218 {
219     view->scrollTo( view->currentIndex() );
220 }
221
222 void StandardPLPanel::handleExpansion( const QModelIndex &index )
223 {
224     if( model->isCurrent( index ) )
225         view->scrollTo( index, QAbstractItemView::EnsureVisible );
226 }
227
228 void StandardPLPanel::setCurrentRootId( int _new )
229 {
230     currentRootId = _new;
231     if( currentRootId == THEPL->p_local_category->i_id ||
232         currentRootId == THEPL->p_local_onelevel->i_id  )
233     {
234         addButton->setEnabled( true );
235         addButton->setToolTip( qtr(I_PL_ADDPL) );
236     }
237     else if( ( THEPL->p_ml_category &&
238                         currentRootId == THEPL->p_ml_category->i_id ) ||
239              ( THEPL->p_ml_onelevel &&
240                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
241     {
242         addButton->setEnabled( true );
243         addButton->setToolTip( qtr(I_PL_ADDML) );
244     }
245     else
246         addButton->setEnabled( false );
247 }
248
249 /* PopupAdd Menu for the Add Menu */
250 void StandardPLPanel::popupAdd()
251 {
252     QMenu popup;
253     if( currentRootId == THEPL->p_local_category->i_id ||
254         currentRootId == THEPL->p_local_onelevel->i_id )
255     {
256         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(PLAppendDialog()) );
257         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
258     }
259     else if( ( THEPL->p_ml_category &&
260                 currentRootId == THEPL->p_ml_category->i_id ) ||
261              ( THEPL->p_ml_onelevel &&
262                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
263     {
264         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
265         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
266     }
267     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
268                         + QPoint( 0, addButton->height() ) );
269 }
270
271 void StandardPLPanel::popupSelectColumn( QPoint pos )
272 {
273     ContextUpdateMapper = new QSignalMapper(this);
274
275     QMenu selectColMenu;
276
277     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
278
279     int i_column = 1;
280     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
281     {
282         QAction* option = selectColMenu.addAction(
283             qfu( psz_column_title( i_column ) ) );
284         option->setCheckable( true );
285         option->setChecked( model->shownFlags() & i_column );
286         ContextUpdateMapper->setMapping( option, i_column );
287         CONNECT( option, triggered(), ContextUpdateMapper, map() );
288     }
289
290     selectColMenu.exec( QCursor::pos() );
291 }
292
293 /* ClearFilter LineEdit */
294 void StandardPLPanel::clearFilter()
295 {
296     searchLine->setText( "" );
297 }
298
299 /* Search in the playlist */
300 void StandardPLPanel::search( QString searchText )
301 {
302     model->search( searchText );
303 }
304
305 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
306 {
307     if( !index.isValid() ) return;
308     QItemSelectionModel *selection = view->selectionModel();
309     QModelIndexList list = selection->selectedIndexes();
310     model->popup( index, point, list );
311 }
312
313 /* Set the root of the new Playlist */
314 /* This activated by the selector selection */
315 void StandardPLPanel::setRoot( int i_root_id )
316 {
317     QPL_LOCK;
318     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
319                                                     pl_Locked );
320     assert( p_item );
321     p_item = playlist_GetPreferredNode( THEPL, p_item );
322     assert( p_item );
323     QPL_UNLOCK;
324
325     model->rebuild( p_item );
326 }
327
328 void StandardPLPanel::removeItem( int i_id )
329 {
330     model->removeItem( i_id );
331 }
332
333 /* Delete and Suppr key remove the selection
334    FilterKey function and code function */
335 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
336 {
337     switch( e->key() )
338     {
339     case Qt::Key_Back:
340     case Qt::Key_Delete:
341         deleteSelection();
342         break;
343     }
344 }
345
346 void StandardPLPanel::deleteSelection()
347 {
348     QItemSelectionModel *selection = view->selectionModel();
349     QModelIndexList list = selection->selectedIndexes();
350     model->doDelete( list );
351 }
352
353 StandardPLPanel::~StandardPLPanel()
354 {
355 #if HAS_QT43
356     getSettings()->beginGroup("playlistdialog");
357     getSettings()->setValue( "headerState", view->header()->saveState() );
358     getSettings()->endGroup();
359 #endif
360 }
361
362