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