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