]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: StandardPLPanel: show dropzone on views
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright © 2000-2010 VideoLAN
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "components/playlist/standardpanel.hpp"
30
31 #include "components/playlist/vlc_model.hpp"      /* VLCModel */
32 #include "components/playlist/playlist_model.hpp" /* PLModel */
33 #include "components/playlist/ml_model.hpp"       /* MLModel */
34 #include "components/playlist/views.hpp"          /* 3 views */
35 #include "components/playlist/selector.hpp"       /* PLSelector */
36 #include "menus.hpp"                              /* Popup */
37 #include "input_manager.hpp"                      /* THEMIM */
38
39 #include <vlc_services_discovery.h>               /* SD_CMD_SEARCH */
40
41 #include <QHeaderView>
42 #include <QModelIndexList>
43 #include <QMenu>
44 #include <QKeyEvent>
45 #include <QWheelEvent>
46 #include <QStackedLayout>
47 #include <QSignalMapper>
48 #include <QSettings>
49 #include <QStylePainter>
50
51 #include <assert.h>
52
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54                                   intf_thread_t *_p_intf,
55                                   playlist_item_t *p_root,
56                                   PLSelector *_p_selector,
57                                   PLModel *_p_model,
58                                   MLModel *_p_plmodel)
59                 : QWidget( _parent ),
60                   model( _p_model ),
61                   mlmodel( _p_plmodel),
62                   p_intf( _p_intf ),
63                   p_selector( _p_selector )
64 {
65     viewStack = new QStackedLayout( this );
66     viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
67     setMinimumWidth( 300 );
68
69     iconView    = NULL;
70     treeView    = NULL;
71     listView    = NULL;
72     picFlowView = NULL;
73
74     currentRootIndexId  = -1;
75     lastActivatedId     = -1;
76
77     /* Saved Settings */
78     int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
79     showView( i_savedViewMode );
80
81     DCONNECT( THEMIM, leafBecameParent( int ),
82               this, browseInto( int ) );
83
84     CONNECT( model, currentIndexChanged( const QModelIndex& ),
85              this, handleExpansion( const QModelIndex& ) );
86     CONNECT( model, rootIndexChanged(), this, browseInto() );
87
88     setRootItem( p_root, false );
89 }
90
91 StandardPLPanel::~StandardPLPanel()
92 {
93     getSettings()->beginGroup("Playlist");
94     if( treeView )
95         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
96     getSettings()->setValue( "view-mode", currentViewIndex() );
97     getSettings()->endGroup();
98 }
99
100 /* Unused anymore, but might be useful, like in right-click menu */
101 void StandardPLPanel::gotoPlayingItem()
102 {
103     currentView->scrollTo( model->currentIndex() );
104 }
105
106 void StandardPLPanel::handleExpansion( const QModelIndex& index )
107 {
108     assert( currentView );
109     if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
110         browseInto( index.parent() );
111     currentView->scrollTo( index );
112 }
113
114 void StandardPLPanel::popupPlView( const QPoint &point )
115 {
116     QModelIndex index = currentView->indexAt( point );
117     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
118     QItemSelectionModel *selection = currentView->selectionModel();
119     QModelIndexList list = selection->selectedIndexes();
120
121     if( !model->popup( index, globalPoint, list ) )
122         VLCMenuBar::PopupMenu( p_intf, true );
123 }
124
125 void StandardPLPanel::popupSelectColumn( QPoint )
126 {
127     QMenu menu;
128     assert( treeView );
129
130     /* We do not offer the option to hide index 0 column, or
131      * QTreeView will behave weird */
132     for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
133     {
134         QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
135         option->setCheckable( true );
136         option->setChecked( !treeView->isColumnHidden( j ) );
137         selectColumnsSigMapper->setMapping( option, j );
138         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
139     }
140     menu.exec( QCursor::pos() );
141 }
142
143 void StandardPLPanel::toggleColumnShown( int i )
144 {
145     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
146 }
147
148 /* Search in the playlist */
149 void StandardPLPanel::search( const QString& searchText )
150 {
151     int type;
152     QString name;
153     bool can_search;
154     p_selector->getCurrentItemInfos( &type, &can_search, &name );
155
156     if( type != SD_TYPE || !can_search )
157     {
158         bool flat = ( currentView == iconView ||
159                       currentView == listView ||
160                       currentView == picFlowView );
161         model->search( searchText,
162                        flat ? currentView->rootIndex() : QModelIndex(),
163                        !flat );
164     }
165 }
166
167 void StandardPLPanel::searchDelayed( const QString& searchText )
168 {
169     int type;
170     QString name;
171     bool can_search;
172     p_selector->getCurrentItemInfos( &type, &can_search, &name );
173
174     if( type == SD_TYPE && can_search )
175     {
176         if( !name.isEmpty() && !searchText.isEmpty() )
177             playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
178                                               qtu( searchText ) );
179     }
180 }
181
182 /* Set the root of the new Playlist */
183 /* This activated by the selector selection */
184 void StandardPLPanel::setRootItem( playlist_item_t *p_item, bool b )
185 {
186 #ifdef MEDIA_LIBRARY
187     if( b )
188     {
189         msg_Dbg( p_intf, "Setting the SQL ML" );
190         currentView->setModel( mlmodel );
191     }
192     else
193 #else
194     Q_UNUSED( b );
195 #endif
196     {
197         if( currentView->model() != model )
198             currentView->setModel( model );
199         model->rebuild( p_item );
200     }
201 }
202
203 void StandardPLPanel::browseInto( const QModelIndex &index )
204 {
205     if( currentView == iconView || currentView == listView || currentView == picFlowView )
206     {
207
208         currentView->setRootIndex( index );
209
210         /* When going toward root in LocationBar, scroll to the item
211            that was previously as root */
212         QModelIndex newIndex = model->index(currentRootIndexId,0);
213         while( newIndex.isValid() && (newIndex.parent() != index) )
214             newIndex = newIndex.parent();
215         if( newIndex.isValid() )
216             currentView->scrollTo( newIndex );
217
218         /* Store new rootindexid*/
219         currentRootIndexId = model->itemId( index );
220         model->ensureArtRequested( index );
221     }
222
223     emit viewChanged( index );
224 }
225
226 void StandardPLPanel::browseInto()
227 {
228     browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
229                  model->index( currentRootIndexId, 0 ) :
230                  QModelIndex() );
231 }
232
233 void StandardPLPanel::wheelEvent( QWheelEvent *e )
234 {
235     // Accept this event in order to prevent unwanted volume up/down changes
236     e->accept();
237 }
238
239 bool StandardPLPanel::eventFilter ( QObject *obj, QEvent * event )
240 {
241     if (event->type() == QEvent::KeyPress)
242     {
243         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
244         if( keyEvent->key() == Qt::Key_Delete ||
245             keyEvent->key() == Qt::Key_Backspace )
246         {
247             deleteSelection();
248             return true;
249         }
250     }
251     else if ( event->type() == QEvent::Paint )
252     {/* Warn! Don't filter events from anything else than views ! */
253         if ( model->rowCount() == 0 && p_selector->getCurrentItemCategory() == PL_ITEM_TYPE )
254         {
255             QWidget *viewport = qobject_cast<QWidget *>( obj );
256             QStylePainter painter( viewport );
257             QPixmap dropzone(":/dropzone");
258             QRect rect = viewport->geometry();
259             QSize size = rect.size() / 2 - dropzone.size() / 2;
260             rect.adjust( 0, size.height(), 0 , 0 );
261             painter.drawItemPixmap( rect, Qt::AlignHCenter, dropzone );
262             /* now select the zone just below the drop zone and let Qt center
263                the text by itself */
264             rect.adjust( 0, dropzone.size().height() + 10, 0, 0 );
265             rect.setRight( viewport->geometry().width() );
266             rect.setLeft( 0 );
267             painter.drawItemText( rect,
268                                   Qt::AlignHCenter,
269                                   palette(),
270                                   true,
271                                   qtr("Playlist is currently Empty\n"
272                                       "Drop a file here or select a "
273                                       "media source from the left"),
274                                   QPalette::Text );
275         }
276     }
277     return false;
278 }
279
280 void StandardPLPanel::deleteSelection()
281 {
282     QItemSelectionModel *selection = currentView->selectionModel();
283     QModelIndexList list = selection->selectedIndexes();
284     model->doDelete( list );
285 }
286
287 void StandardPLPanel::createIconView()
288 {
289     iconView = new PlIconView( model, this );
290     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
291     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
292              this, popupPlView( const QPoint & ) );
293     CONNECT( iconView, activated( const QModelIndex & ),
294              this, activate( const QModelIndex & ) );
295     iconView->installEventFilter( this );
296     iconView->viewport()->installEventFilter( this );
297     viewStack->addWidget( iconView );
298 }
299
300 void StandardPLPanel::createListView()
301 {
302     listView = new PlListView( model, this );
303     listView->setContextMenuPolicy( Qt::CustomContextMenu );
304     CONNECT( listView, customContextMenuRequested( const QPoint & ),
305              this, popupPlView( const QPoint & ) );
306     CONNECT( listView, activated( const QModelIndex & ),
307              this, activate( const QModelIndex & ) );
308     listView->installEventFilter( this );
309     listView->viewport()->installEventFilter( this );
310     viewStack->addWidget( listView );
311 }
312
313 void StandardPLPanel::createCoverView()
314 {
315     picFlowView = new PicFlowView( model, this );
316     picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
317     CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
318              this, popupPlView( const QPoint & ) );
319     CONNECT( picFlowView, activated( const QModelIndex & ),
320              this, activate( const QModelIndex & ) );
321     viewStack->addWidget( picFlowView );
322     picFlowView->installEventFilter( this );
323 }
324
325 void StandardPLPanel::createTreeView()
326 {
327     /* Create and configure the QTreeView */
328     treeView = new PlTreeView;
329
330     treeView->setIconSize( QSize( 20, 20 ) );
331     treeView->setAlternatingRowColors( true );
332     treeView->setAnimated( true );
333     treeView->setUniformRowHeights( true );
334     treeView->setSortingEnabled( true );
335     treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
336     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
337     treeView->header()->setSortIndicatorShown( true );
338     treeView->header()->setClickable( true );
339     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
340
341     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
342     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
343     treeView->setDragEnabled( true );
344     treeView->setAcceptDrops( true );
345     treeView->setDropIndicatorShown( true );
346     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
347
348     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
349
350     /* Connections for the TreeView */
351     CONNECT( treeView, activated( const QModelIndex& ),
352              this, activate( const QModelIndex& ) );
353     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
354              this, popupSelectColumn( QPoint ) );
355     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
356              this, popupPlView( const QPoint & ) );
357     treeView->installEventFilter( this );
358     treeView->viewport()->installEventFilter( this );
359
360     /* SignalMapper for columns */
361     selectColumnsSigMapper = new QSignalMapper( this );
362     CONNECT( selectColumnsSigMapper, mapped( int ),
363              this, toggleColumnShown( int ) );
364
365     viewStack->addWidget( treeView );
366 }
367
368 void StandardPLPanel::changeModel( bool b_ml )
369 {
370 #ifdef MEDIA_LIBRARY
371     VLCModel *mod;
372     if( b_ml )
373         mod = mlmodel;
374     else
375         mod = model;
376     if( currentView->model() != mod )
377         currentView->setModel( mod );
378 #else
379     Q_UNUSED( b_ml );
380     if( currentView->model() != model )
381         currentView->setModel( model );
382 #endif
383 }
384
385 void StandardPLPanel::showView( int i_view )
386 {
387     bool b_treeViewCreated = false;
388
389     switch( i_view )
390     {
391     case ICON_VIEW:
392     {
393         if( iconView == NULL )
394             createIconView();
395         currentView = iconView;
396         break;
397     }
398     case LIST_VIEW:
399     {
400         if( listView == NULL )
401             createListView();
402         currentView = listView;
403         break;
404     }
405     case PICTUREFLOW_VIEW:
406     {
407         if( picFlowView == NULL )
408             createCoverView();
409         currentView = picFlowView;
410         break;
411     }
412     default:
413     case TREE_VIEW:
414     {
415         if( treeView == NULL )
416         {
417             createTreeView();
418             b_treeViewCreated = true;
419         }
420         currentView = treeView;
421         break;
422     }
423     }
424
425     changeModel( false );
426
427     /* Restoring the header Columns must come after changeModel */
428     if( b_treeViewCreated )
429     {
430         assert( treeView );
431         if( getSettings()->contains( "Playlist/headerStateV2" ) )
432         {
433             treeView->header()->restoreState(getSettings()
434                     ->value( "Playlist/headerStateV2" ).toByteArray() );
435             /* if there is allready stuff in playlist, we don't sort it and we reset
436                sorting */
437             if( model->rowCount() )
438             {
439                 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
440             }
441         }
442         else
443         {
444             for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
445             {
446                 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
447                 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
448                 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
449             }
450         }
451     }
452
453     viewStack->setCurrentWidget( currentView );
454     browseInto();
455     gotoPlayingItem();
456 }
457
458 int StandardPLPanel::currentViewIndex() const
459 {
460     if( currentView == treeView )
461         return TREE_VIEW;
462     else if( currentView == iconView )
463         return ICON_VIEW;
464     else if( currentView == listView )
465         return LIST_VIEW;
466     else
467         return PICTUREFLOW_VIEW;
468 }
469
470 void StandardPLPanel::cycleViews()
471 {
472     if( currentView == iconView )
473         showView( TREE_VIEW );
474     else if( currentView == treeView )
475         showView( LIST_VIEW );
476     else if( currentView == listView )
477 #ifndef NDEBUG
478         showView( PICTUREFLOW_VIEW  );
479     else if( currentView == picFlowView )
480 #endif
481         showView( ICON_VIEW );
482     else
483         assert( 0 );
484 }
485
486 void StandardPLPanel::activate( const QModelIndex &index )
487 {
488     if( currentView->model() == model )
489     {
490         /* If we are not a leaf node */
491         if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
492         {
493             if( currentView != treeView )
494                 browseInto( index );
495         }
496         else
497         {
498             playlist_Lock( THEPL );
499             playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
500             p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
501             lastActivatedId = p_item->p_input->i_id;
502             playlist_Unlock( THEPL );
503             model->activateItem( index );
504         }
505     }
506 }
507
508 void StandardPLPanel::browseInto( int i_id )
509 {
510     if( i_id != lastActivatedId ) return;
511
512     QModelIndex index = model->index( i_id, 0 );
513     playlist_Unlock( THEPL );
514
515     if( currentView == treeView )
516         treeView->setExpanded( index, true );
517     else
518         browseInto( index );
519
520     lastActivatedId = -1;
521 }