]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
QT: move sortingIndicator to correct position when columns are added/removed
[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->setSortingEnabled( true );
75
76
77     getSettings()->beginGroup("Playlist");
78     if( getSettings()->contains( "headerState" ) )
79     {
80         view->header()->restoreState(
81                 getSettings()->value( "headerState" ).toByteArray() );
82     }
83     else
84     {
85         /* Configure the size of the header */
86         view->header()->resizeSection( 0, 200 );
87         view->header()->resizeSection( 1, 80 );
88     }
89     view->header()->setSortIndicatorShown( true );
90     view->header()->setClickable( true );
91     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
92     getSettings()->endGroup();
93
94     /* Connections for the TreeView */
95     CONNECT( view, activated( const QModelIndex& ) ,
96              model,activateItem( const QModelIndex& ) );
97     CONNECT( view, rightClicked( QModelIndex , QPoint ),
98              this, doPopup( QModelIndex, QPoint ) );
99     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
100              this, popupSelectColumn( QPoint ) );
101     CONNECT( model, currentChanged( const QModelIndex& ),
102              this, handleExpansion( const QModelIndex& ) );
103     CONNECT( model, columnsChanged( int ),
104             this, checkSortingIndicator( int ) );
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( ":/buttons/playlist/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( ":/buttons/playlist/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( ":/buttons/playlist/repeat_one" ) );
136         repeatButton->setChecked( true );
137     }
138     else if( model->hasLoop() )
139     {
140         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
141         repeatButton->setChecked( true );
142     }
143     else
144     {
145         repeatButton->setIcon( QIcon( ":/buttons/playlist/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, "", buttons/playlist/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( const QString& ), this, search( const 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( ":/buttons/playlist/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( ":/buttons/playlist/repeat_one" ) );
190     }
191     else
192     {
193         model->setRepeat( true ); model->setLoop( false );
194         repeatButton->setChecked( true );
195         repeatButton->setIcon( QIcon( ":/buttons/playlist/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     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( simplePLAppendDialog()) );
245         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
246         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
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( simpleMLAppendDialog()) );
254         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
255         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
256     }
257
258     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
259                         + QPoint( 0, addButton->height() ) );
260 }
261
262 /* Set sortingindicator to -1 if it's on column thats removed,
263  * else check that it's still showing on correct column
264  */
265 void StandardPLPanel::checkSortingIndicator( int meta )
266 {
267     int index=0;
268
269     if( view->header()->isSortIndicatorShown() == false )
270         return;
271
272     int sortIndex = view->header()->sortIndicatorSection();
273     if( sortIndex < 0 || sortIndex > view->header()->count() || meta == 0 )
274         return;
275
276     int _meta = meta;
277
278     while( _meta )
279     {
280         if( _meta & model->shownFlags() )
281             index++;
282         _meta >>= 1;
283     }
284
285     /* Adding column */
286     if( model->shownFlags() & meta )
287     {
288         /* If column is added before sortIndex, move it one to right*/
289         if( sortIndex >= index )
290         {
291             sortIndex += 1;
292         }
293     } else {
294         /* Column removed */
295         if( sortIndex == index )
296         {
297             sortIndex = -1;
298         } else if( sortIndex > index )
299         {
300             /* Move indicator left one step*/
301             sortIndex -= 1;
302         }
303     }
304     view->header()->setSortIndicator( sortIndex  ,
305                 view->header()->sortIndicatorOrder() );
306 }
307
308 void StandardPLPanel::popupSelectColumn( QPoint pos )
309 {
310     ContextUpdateMapper = new QSignalMapper(this);
311
312     QMenu selectColMenu;
313
314     int i_column = 1;
315     for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
316     {
317         QAction* option = selectColMenu.addAction(
318             qfu( psz_column_title( i_column ) ) );
319         option->setCheckable( true );
320         option->setChecked( model->shownFlags() & i_column );
321         ContextUpdateMapper->setMapping( option, i_column );
322         CONNECT( option, triggered(), ContextUpdateMapper, map() );
323     }
324
325     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
326
327     selectColMenu.exec( QCursor::pos() );
328 }
329
330 /* Search in the playlist */
331 void StandardPLPanel::search( const QString& searchText )
332 {
333     model->search( searchText );
334 }
335
336 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
337 {
338     if( !index.isValid() ) return;
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