]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt4 - small cosmetic for the playlist.
[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 #include "qt4.hpp"
25 #include "dialogs_provider.hpp"
26 #include "playlist_model.hpp"
27 #include "components/playlist/panels.hpp"
28 #include "util/customwidgets.hpp"
29
30 #include <vlc_intf_strings.h>
31
32 #include <QTreeView>
33 #include <QPushButton>
34 #include <QHBoxLayout>
35 #include <QVBoxLayout>
36 #include <QHeaderView>
37 #include <QKeyEvent>
38 #include <QModelIndexList>
39 #include <QToolBar>
40 #include <QLabel>
41 #include <QSpacerItem>
42 #include <QMenu>
43
44 #include <assert.h>
45
46 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
47                                   intf_thread_t *_p_intf,
48                                   playlist_t *p_playlist,
49                                   playlist_item_t *p_root ):
50                                   PLPanel( _parent, _p_intf )
51 {
52     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
53
54     QVBoxLayout *layout = new QVBoxLayout();
55     layout->setSpacing( 0 ); layout->setMargin( 0 );
56
57     /* Create and configure the QTreeView */
58     view = new QVLCTreeView( 0 );
59     view->setModel(model);
60     view->setIconSize( QSize( 20, 20 ) );
61     view->setAlternatingRowColors( true );
62     view->setAnimated( true );
63     view->setSortingEnabled( true );
64     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
65     view->setDragEnabled( true );
66     view->setAcceptDrops( true );
67     view->setDropIndicatorShown( true );
68     view->setAutoScroll( true );
69
70     view->header()->resizeSection( 0, 230 );
71     view->header()->resizeSection( 1, 80 );
72     view->header()->setSortIndicatorShown( true );
73     view->header()->setClickable( true );
74     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
75
76     CONNECT( view, activated( const QModelIndex& ) ,
77              model,activateItem( const QModelIndex& ) );
78     CONNECT( view, rightClicked( QModelIndex , QPoint ),
79              this, doPopup( QModelIndex, QPoint ) );
80     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
81              this, handleExpansion( const QModelIndex& ) );
82     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
83              this, popupSelectColumn( QPoint ) );
84
85     currentRootId = -1;
86     CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
87
88     /* Buttons configuration */
89     QHBoxLayout *buttons = new QHBoxLayout;
90
91     addButton = new QPushButton( QIcon( ":/pixmaps/playlist_add.png" ), "", this );
92     addButton->setMaximumWidth( 25 );
93     BUTTONACT( addButton, popupAdd() );
94     buttons->addWidget( addButton );
95
96     randomButton = new QPushButton( this );
97     if( model->hasRandom() )
98     {
99         randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_on.png" ));
100         randomButton->setToolTip( qtr( I_PL_RANDOM ));
101     }
102     else
103     {
104          randomButton->setIcon( QIcon( ":/pixmaps/playlist_shuffle_off.png" ) );
105          randomButton->setToolTip( qtr( I_PL_NORANDOM ));
106     }
107     BUTTONACT( randomButton, toggleRandom() );
108     buttons->addWidget( randomButton );
109
110     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
111     buttons->addItem( spacer );
112
113     repeatButton = new QPushButton( this );
114     if( model->hasRepeat() )
115     {
116         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
117         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
118     }
119     else if( model->hasLoop() )
120     {
121         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
122         repeatButton->setToolTip( qtr( I_PL_LOOP ));
123     }
124     else
125     {
126         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
127         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
128     }
129     BUTTONACT( repeatButton, toggleRepeat() );
130     buttons->addWidget( repeatButton );
131
132     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
133     buttons->addWidget( filter );
134     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
135     CONNECT( searchLine, textChanged(QString), this, search(QString));
136     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
137
138     QPushButton *clear = new QPushButton( qfu( "CL") );
139     clear->setMaximumWidth( 30 );
140     clear->setToolTip( qtr( "Clear" ));
141     BUTTONACT( clear, clearFilter() );
142     buttons->addWidget( clear );
143
144     layout->addWidget( view );
145     layout->addLayout( buttons );
146 //    layout->addWidget( bar );
147     setLayout( layout );
148 }
149
150 void StandardPLPanel::toggleRepeat()
151 {
152     if( model->hasRepeat() )
153     {
154         model->setRepeat( false ); model->setLoop( true );
155         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_all.png" ) );
156         repeatButton->setToolTip( qtr( I_PL_LOOP ));
157     }
158     else if( model->hasLoop() )
159     {
160         model->setRepeat( false ) ; model->setLoop( false );
161         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_off.png" ) );
162         repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
163     }
164     else
165     {
166         model->setRepeat( true );
167         repeatButton->setIcon( QIcon( ":/pixmaps/playlist_repeat_one.png" ) );
168         repeatButton->setToolTip( qtr( I_PL_REPEAT ));
169     }
170 }
171
172 void StandardPLPanel::toggleRandom()
173 {
174     bool prev = model->hasRandom();
175     model->setRandom( !prev );
176     randomButton->setIcon( prev ?
177                 QIcon( ":/pixmaps/playlist_shuffle_off.png" ) :
178                 QIcon( ":/pixmaps/playlist_shuffle_on.png" ) );
179     randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
180 }
181
182 void StandardPLPanel::handleExpansion( const QModelIndex &index )
183 {
184     if( model->isCurrent( index ) )
185         view->scrollTo( index, QAbstractItemView::EnsureVisible );
186 }
187
188 void StandardPLPanel::setCurrentRootId( int _new )
189 {
190     currentRootId = _new;
191     if( currentRootId == THEPL->p_local_category->i_id ||
192         currentRootId == THEPL->p_local_onelevel->i_id  )
193     {
194         addButton->setEnabled( true );
195         addButton->setToolTip( qtr(I_PL_ADDPL) );
196     }
197     else if( ( THEPL->p_ml_category &&
198                         currentRootId == THEPL->p_ml_category->i_id ) ||
199              ( THEPL->p_ml_onelevel &&
200                         currentRootId == THEPL->p_ml_onelevel->i_id ) )
201     {
202         addButton->setEnabled( true );
203         addButton->setToolTip( qtr(I_PL_ADDML) );
204     }
205     else
206         addButton->setEnabled( false );
207 }
208
209 void StandardPLPanel::popupAdd()
210 {
211     QMenu popup;
212     if( currentRootId == THEPL->p_local_category->i_id ||
213         currentRootId == THEPL->p_local_onelevel->i_id )
214     {
215         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
216         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
217         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
218     }
219     else if( ( THEPL->p_ml_category &&
220                 currentRootId == THEPL->p_ml_category->i_id ) ||
221              ( THEPL->p_ml_onelevel &&
222                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
223     {
224         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
225         popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
226         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
227     }
228     popup.exec( QCursor::pos() );
229 }
230
231 void StandardPLPanel::popupSelectColumn( QPoint )
232 {
233     ContextUpdateMapper = new QSignalMapper(this);
234
235     QMenu selectColMenu;
236
237 #define ADD_META_ACTION( meta ) { \
238    QAction* option = selectColMenu.addAction( qfu(VLC_META_##meta) );     \
239    option->setCheckable( true );                                           \
240    option->setChecked( model->shownFlags() & VLC_META_ENGINE_##meta );   \
241    ContextUpdateMapper->setMapping( option, VLC_META_ENGINE_##meta );      \
242    CONNECT( option, triggered(), ContextUpdateMapper, map() );             \
243    }
244     CONNECT( ContextUpdateMapper, mapped( int ),  model, viewchanged( int ) );
245
246     ADD_META_ACTION( TITLE );
247     ADD_META_ACTION( ARTIST );
248     ADD_META_ACTION( DURATION );
249     ADD_META_ACTION( COLLECTION );
250     ADD_META_ACTION( GENRE );
251     ADD_META_ACTION( SEQ_NUM );
252     ADD_META_ACTION( RATING );
253     ADD_META_ACTION( DESCRIPTION );
254
255 #undef ADD_META_ACTION
256
257     selectColMenu.exec( QCursor::pos() );
258  }
259
260 void StandardPLPanel::clearFilter()
261 {
262     searchLine->setText( "" );
263 }
264
265 void StandardPLPanel::search( QString searchText )
266 {
267     model->search( searchText );
268 }
269
270 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
271 {
272     if( !index.isValid() ) return;
273     QItemSelectionModel *selection = view->selectionModel();
274     QModelIndexList list = selection->selectedIndexes();
275     model->popup( index, point, list );
276 }
277
278 void StandardPLPanel::setRoot( int i_root_id )
279 {
280     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
281                                                     VLC_TRUE );
282     assert( p_item );
283     p_item = playlist_GetPreferredNode( THEPL, p_item );
284     assert( p_item );
285     model->rebuild( p_item );
286 }
287
288 void StandardPLPanel::removeItem( int i_id )
289 {
290     model->removeItem( i_id );
291 }
292
293 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
294 {
295     switch( e->key() )
296     {
297     case Qt::Key_Back:
298     case Qt::Key_Delete:
299         deleteSelection();
300         break;
301     }
302 }
303
304 void StandardPLPanel::deleteSelection()
305 {
306     QItemSelectionModel *selection = view->selectionModel();
307     QModelIndexList list = selection->selectedIndexes();
308     model->doDelete( list );
309 }
310
311 StandardPLPanel::~StandardPLPanel()
312 {}