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