]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
qt4: let QTreeView manage visible playlist columns selection
[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         int m, c;
87         for( m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
88         {
89             view->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
90             if( m == COLUMN_TITLE ) view->header()->resizeSection( c, 200 );
91             else if( m == COLUMN_DURATION ) view->header()->resizeSection( c, 80 );
92         }
93     }
94     view->header()->setSortIndicatorShown( true );
95     view->header()->setClickable( true );
96     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
97     getSettings()->endGroup();
98
99     /* Connections for the TreeView */
100     CONNECT( view, activated( const QModelIndex& ) ,
101              model,activateItem( const QModelIndex& ) );
102     CONNECT( view, rightClicked( QModelIndex , QPoint ),
103              this, doPopup( QModelIndex, QPoint ) );
104     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
105              this, popupSelectColumn( QPoint ) );
106     CONNECT( model, currentChanged( const QModelIndex& ),
107              this, handleExpansion( const QModelIndex& ) );
108
109     currentRootId = -1;
110     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
111
112     /* Buttons configuration */
113     QHBoxLayout *buttons = new QHBoxLayout;
114
115     /* Add item to the playlist button */
116     addButton = new QPushButton;
117     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
118     addButton->setMaximumWidth( 30 );
119     BUTTONACT( addButton, popupAdd() );
120     buttons->addWidget( addButton );
121
122     /* Random 2-state button */
123     randomButton = new QPushButton( this );
124     randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
125     randomButton->setToolTip( qtr( I_PL_RANDOM ));
126     randomButton->setCheckable( true );
127     randomButton->setChecked( model->hasRandom() );
128     BUTTONACT( randomButton, toggleRandom() );
129     buttons->addWidget( randomButton );
130
131     /* Repeat 3-state button */
132     repeatButton = new QPushButton( this );
133     repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
134     repeatButton->setCheckable( true );
135
136     if( model->hasRepeat() )
137     {
138         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
139         repeatButton->setChecked( true );
140     }
141     else if( model->hasLoop() )
142     {
143         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
144         repeatButton->setChecked( true );
145     }
146     else
147     {
148         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
149         repeatButton->setChecked( false );
150     }
151     BUTTONACT( repeatButton, toggleRepeat() );
152     buttons->addWidget( repeatButton );
153
154     /* Goto */
155     gotoPlayingButton = new QPushButton;
156     BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/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     SearchLineEdit *search = new SearchLineEdit( this );
168     buttons->addWidget( search );
169     filter->setBuddy( search );
170     CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
171
172     /* Finish the layout */
173     layout->addWidget( view );
174     layout->addLayout( buttons );
175 //    layout->addWidget( bar );
176     setLayout( layout );
177
178     selectColumnsSigMapper = new QSignalMapper( this );
179     CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
180 }
181
182 /* Function to toggle between the Repeat states */
183 void StandardPLPanel::toggleRepeat()
184 {
185     if( model->hasRepeat() )
186     {
187         model->setRepeat( false ); model->setLoop( true );
188         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
189         repeatButton->setChecked( true );
190     }
191     else if( model->hasLoop() )
192     {
193         model->setRepeat( false ) ; model->setLoop( false );
194         repeatButton->setChecked( false );
195         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
196     }
197     else
198     {
199         model->setRepeat( true ); model->setLoop( false );
200         repeatButton->setChecked( true );
201         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
202     }
203 }
204
205 /* Function to toggle between the Random states */
206 void StandardPLPanel::toggleRandom()
207 {
208     bool prev = model->hasRandom();
209     model->setRandom( !prev );
210 }
211
212 void StandardPLPanel::gotoPlayingItem()
213 {
214     view->scrollTo( model->currentIndex() );
215 }
216
217 void StandardPLPanel::handleExpansion( const QModelIndex& index )
218 {
219     view->scrollTo( index );
220 }
221
222 void StandardPLPanel::setCurrentRootId( int _new )
223 {
224     currentRootId = _new;
225     if( currentRootId == THEPL->p_local_category->i_id ||
226         currentRootId == THEPL->p_local_onelevel->i_id  )
227     {
228         addButton->setEnabled( true );
229         addButton->setToolTip( qtr(I_PL_ADDPL) );
230     }
231     else if( ( THEPL->p_ml_category &&
232                         currentRootId == THEPL->p_ml_category->i_id ) ||
233              ( THEPL->p_ml_onelevel &&
234                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
235     {
236         addButton->setEnabled( true );
237         addButton->setToolTip( qtr(I_PL_ADDML) );
238     }
239     else
240         addButton->setEnabled( false );
241 }
242
243 /* PopupAdd Menu for the Add Menu */
244 void StandardPLPanel::popupAdd()
245 {
246     QMenu popup;
247     if( currentRootId == THEPL->p_local_category->i_id ||
248         currentRootId == THEPL->p_local_onelevel->i_id )
249     {
250         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
251         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
252         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
253     }
254     else if( ( THEPL->p_ml_category &&
255                 currentRootId == THEPL->p_ml_category->i_id ) ||
256              ( THEPL->p_ml_onelevel &&
257                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
258     {
259         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
260         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
261         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
262     }
263
264     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
265                         + QPoint( 0, addButton->height() ) );
266 }
267
268 void StandardPLPanel::popupSelectColumn( QPoint pos )
269 {
270     QMenu menu;
271
272     int i, j;
273     for( i = 1, j = 0; i < COLUMN_END; i <<= 1, j++ )
274     {
275         QAction* option = menu.addAction(
276             qfu( psz_column_title( i ) ) );
277         option->setCheckable( true );
278         option->setChecked( !view->isColumnHidden( j ) );
279         selectColumnsSigMapper->setMapping( option, j );
280         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
281     }
282     menu.exec( QCursor::pos() );
283 }
284
285 void StandardPLPanel::toggleColumnShown( int i )
286 {
287     if( view->isColumnHidden( i ) )
288     {
289         view->setColumnHidden( i, false );
290     }
291     else
292     {
293         int visible = 0;
294         int m, c;
295         for( m = 1, c = 0; m != COLUMN_END && visible < 2; m <<= 1, c++ )
296             if( !view->isColumnHidden( c ) ) visible++;
297         if( visible < 2 ) return;
298         view->setColumnHidden( i, true );
299     }
300 }
301
302 /* Search in the playlist */
303 void StandardPLPanel::search( const QString& searchText )
304 {
305     model->search( searchText );
306 }
307
308 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
309 {
310     QItemSelectionModel *selection = view->selectionModel();
311     QModelIndexList list = selection->selectedIndexes();
312     model->popup( index, point, list );
313 }
314
315 /* Set the root of the new Playlist */
316 /* This activated by the selector selection */
317 void StandardPLPanel::setRoot( int i_root_id )
318 {
319     QPL_LOCK;
320     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
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