]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
qt4: fix typo + cosmetics
[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->setModel( model );
65     view->setIconSize( QSize( 20, 20 ) );
66     view->setAlternatingRowColors( true );
67     view->setAnimated( true );
68     view->setSelectionBehavior( QAbstractItemView::SelectRows );
69     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
70     view->setDragEnabled( true );
71     view->setAcceptDrops( true );
72     view->setDropIndicatorShown( true );
73     view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
74     view->setUniformRowHeights( true );
75     view->setSortingEnabled( 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( view->header(), customContextMenuRequested( const QPoint & ),
101              this, popupSelectColumn( QPoint ) );
102     CONNECT( model, currentChanged( const QModelIndex& ),
103              this, handleExpansion( const QModelIndex& ) );
104     CONNECT( model, columnsChanged( int ),
105             this, checkSortingIndicator( int ) );
106
107     currentRootId = -1;
108     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
109
110     /* Buttons configuration */
111     QHBoxLayout *buttons = new QHBoxLayout;
112
113     /* Add item to the playlist button */
114     addButton = new QPushButton;
115     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
116     addButton->setMaximumWidth( 30 );
117     BUTTONACT( addButton, popupAdd() );
118     buttons->addWidget( addButton );
119
120     /* Random 2-state button */
121     randomButton = new QPushButton( this );
122     randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
123     randomButton->setToolTip( qtr( I_PL_RANDOM ));
124     randomButton->setCheckable( true );
125     randomButton->setChecked( model->hasRandom() );
126     BUTTONACT( randomButton, toggleRandom() );
127     buttons->addWidget( randomButton );
128
129     /* Repeat 3-state button */
130     repeatButton = new QPushButton( this );
131     repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
132     repeatButton->setCheckable( true );
133
134     if( model->hasRepeat() )
135     {
136         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
137         repeatButton->setChecked( true );
138     }
139     else if( model->hasLoop() )
140     {
141         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
142         repeatButton->setChecked( true );
143     }
144     else
145     {
146         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
147         repeatButton->setChecked( false );
148     }
149     BUTTONACT( repeatButton, toggleRepeat() );
150     buttons->addWidget( repeatButton );
151
152     /* Goto */
153     gotoPlayingButton = new QPushButton;
154     BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
155             qtr( "Show the current item" ), gotoPlayingItem() );
156     buttons->addWidget( gotoPlayingButton );
157
158     /* A Spacer and the search possibilities */
159     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
160     buttons->addItem( spacer );
161
162     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
163     buttons->addWidget( filter );
164
165     SearchLineEdit *search = new SearchLineEdit( this );
166     buttons->addWidget( search );
167     filter->setBuddy( search );
168     CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
169
170     /* Finish the layout */
171     layout->addWidget( view );
172     layout->addLayout( buttons );
173 //    layout->addWidget( bar );
174     setLayout( layout );
175 }
176
177 /* Function to toggle between the Repeat states */
178 void StandardPLPanel::toggleRepeat()
179 {
180     if( model->hasRepeat() )
181     {
182         model->setRepeat( false ); model->setLoop( true );
183         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
184         repeatButton->setChecked( true );
185     }
186     else if( model->hasLoop() )
187     {
188         model->setRepeat( false ) ; model->setLoop( false );
189         repeatButton->setChecked( false );
190         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
191     }
192     else
193     {
194         model->setRepeat( true ); model->setLoop( false );
195         repeatButton->setChecked( true );
196         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
197     }
198 }
199
200 /* Function to toggle between the Random states */
201 void StandardPLPanel::toggleRandom()
202 {
203     bool prev = model->hasRandom();
204     model->setRandom( !prev );
205 }
206
207 void StandardPLPanel::gotoPlayingItem()
208 {
209     view->scrollTo( model->currentIndex() );
210 }
211
212 void StandardPLPanel::handleExpansion( const QModelIndex& index )
213 {
214     view->scrollTo( index );
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_ADDDIR), THEDP, SLOT( PLAppendDir()) );
247         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
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_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
256         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
257     }
258
259     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
260                         + QPoint( 0, addButton->height() ) );
261 }
262
263 /* Set sortingindicator to -1 if it's on column thats removed,
264  * else check that it's still showing on correct column
265  */
266 void StandardPLPanel::checkSortingIndicator( int meta )
267 {
268     int index=0;
269
270     if( view->header()->isSortIndicatorShown() == false )
271         return;
272
273     int sortIndex = view->header()->sortIndicatorSection();
274     if( sortIndex < 0 || sortIndex > view->header()->count() || meta == 0 )
275         return;
276
277     int _meta = meta;
278
279     while( _meta )
280     {
281         if( _meta & model->shownFlags() )
282             index++;
283         _meta >>= 1;
284     }
285
286     /* Adding column */
287     if( model->shownFlags() & meta )
288     {
289         /* If column is added before sortIndex, move it one to right*/
290         if( sortIndex >= index )
291         {
292             sortIndex += 1;
293         }
294     } else {
295         /* Column removed */
296         if( sortIndex == index )
297         {
298             sortIndex = -1;
299         } else if( sortIndex > index )
300         {
301             /* Move indicator left one step*/
302             sortIndex -= 1;
303         }
304     }
305     view->header()->setSortIndicator( sortIndex  ,
306                 view->header()->sortIndicatorOrder() );
307 }
308
309 void StandardPLPanel::popupSelectColumn( QPoint pos )
310 {
311     ContextUpdateMapper = new QSignalMapper(this);
312
313     QMenu selectColMenu;
314
315     int i_column = 1;
316     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
317     {
318         QAction* option = selectColMenu.addAction(
319             qfu( psz_column_title( i_column ) ) );
320         option->setCheckable( true );
321         option->setChecked( model->shownFlags() & i_column );
322         ContextUpdateMapper->setMapping( option, i_column );
323         CONNECT( option, triggered(), ContextUpdateMapper, map() );
324     }
325
326     CONNECT( ContextUpdateMapper, mapped( int ),  model, toggleColumnShown( int ) );
327
328     selectColMenu.exec( QCursor::pos() );
329 }
330
331 /* Search in the playlist */
332 void StandardPLPanel::search( const QString& searchText )
333 {
334     model->search( searchText );
335 }
336
337 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
338 {
339     QItemSelectionModel *selection = view->selectionModel();
340     QModelIndexList list = selection->selectedIndexes();
341     model->popup( index, point, list );
342 }
343
344 /* Set the root of the new Playlist */
345 /* This activated by the selector selection */
346 void StandardPLPanel::setRoot( int i_root_id )
347 {
348     QPL_LOCK;
349     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
350     assert( p_item );
351     p_item = playlist_GetPreferredNode( THEPL, p_item );
352     assert( p_item );
353     QPL_UNLOCK;
354
355     model->rebuild( p_item );
356 }
357
358 void StandardPLPanel::removeItem( int i_id )
359 {
360     model->removeItem( i_id );
361 }
362
363 /* Delete and Suppr key remove the selection
364    FilterKey function and code function */
365 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
366 {
367     switch( e->key() )
368     {
369     case Qt::Key_Back:
370     case Qt::Key_Delete:
371         deleteSelection();
372         break;
373     }
374 }
375
376 void StandardPLPanel::deleteSelection()
377 {
378     QItemSelectionModel *selection = view->selectionModel();
379     QModelIndexList list = selection->selectedIndexes();
380     model->doDelete( list );
381 }
382
383 StandardPLPanel::~StandardPLPanel()
384 {
385     getSettings()->beginGroup("Playlist");
386     getSettings()->setValue( "headerState", view->header()->saveState() );
387     getSettings()->endGroup();
388 }
389
390