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