]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
Qt: PLSelector: Make effectless root entries not selectable
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
1 /*****************************************************************************
2  * selector.cpp : Playlist source selector
3  ****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf
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 "qt4.hpp"
30 #include "components/playlist/selector.hpp"
31 #include "playlist_model.hpp"                /* plMimeData */
32 #include "input_manager.hpp"                 /* MainInputManager, for podcast */
33
34 #include <QApplication>
35 #include <QInputDialog>
36 #include <QMessageBox>
37 #include <QMimeData>
38 #include <QDragMoveEvent>
39 #include <QTreeWidgetItem>
40 #include <QHBoxLayout>
41 #include <QPainter>
42 #include <QPalette>
43 #include <QScrollBar>
44
45 #include <vlc_playlist.h>
46 #include <vlc_services_discovery.h>
47
48 void SelectorActionButton::paintEvent( QPaintEvent *event )
49 {
50     QPainter p( this );
51     QColor color = palette().color( QPalette::HighlightedText );
52     color.setAlpha( 80 );
53     if( underMouse() )
54         p.fillRect( rect(), color );
55     p.setPen( color );
56     int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
57     p.drawLine( rect().topLeft() + QPoint( 0, frame ),
58                 rect().bottomLeft() - QPoint( 0, frame ) );
59     QFramelessButton::paintEvent( event );
60 }
61
62 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
63     : qitem(i), lblAction( NULL)
64 {
65     layout = new QHBoxLayout( this );
66     layout->setContentsMargins(0,0,0,0);
67     layout->addSpacing( 3 );
68
69     lbl = new QElidingLabel( text );
70     layout->addWidget(lbl, 1);
71
72     int height = qMax( 22, fontMetrics().height() + 8 );
73     setMinimumHeight( height );
74 }
75
76 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
77 {
78     if( lblAction ) return; //might change later
79
80     QIcon icon;
81
82     switch( act )
83     {
84     case ADD_ACTION:
85         icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
86     case RM_ACTION:
87         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
88     default:
89         return;
90     }
91
92     lblAction = new SelectorActionButton();
93     lblAction->setIcon( icon );
94     lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
95
96     if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
97
98     layout->addWidget( lblAction, 0 );
99     lblAction->hide();
100
101     CONNECT( lblAction, clicked(), this, triggerAction() );
102 }
103
104
105 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
106            : QTreeWidget( p ), p_intf(_p_intf)
107 {
108     /* Properties */
109     setFrameStyle( QFrame::NoFrame );
110     setAttribute( Qt::WA_MacShowFocusRect, false );
111     viewport()->setAutoFillBackground( false );
112     setIconSize( QSize( 24,24 ) );
113     setIndentation( 12 );
114     setHeaderHidden( true );
115     setRootIsDecorated( true );
116     setAlternatingRowColors( false );
117
118     /* drops */
119     viewport()->setAcceptDrops(true);
120     setDropIndicatorShown(true);
121     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
122
123 #ifdef Q_WS_MAC
124     setAutoFillBackground( true );
125     QPalette palette;
126     palette.setColor( QPalette::Window, QColor(209,215,226) );
127     setPalette( palette );
128 #endif
129     setMinimumHeight( 120 );
130
131     /* Podcasts */
132     podcastsParent = NULL;
133     podcastsParentId = -1;
134
135     /* Podcast connects */
136     CONNECT( THEMIM, playlistItemAppended( int, int ),
137              this, plItemAdded( int, int ) );
138     CONNECT( THEMIM, playlistItemRemoved( int ),
139              this, plItemRemoved( int ) );
140     DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
141               this, inputItemUpdate( input_item_t * ) );
142
143     createItems();
144
145     /* Expand at least to show level 2 */
146     for ( int i = 0; i < topLevelItemCount(); i++ )
147         expandItem( topLevelItem( i ) );
148
149     /***
150      * We need to react to both clicks and activation (enter-key) here.
151      * We use curItem to avoid rebuilding twice.
152      * See QStyle::SH_ItemView_ActivateItemOnSingleClick
153      ***/
154     curItem = NULL;
155     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
156              this, setSource( QTreeWidgetItem *) );
157     CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
158              this, setSource( QTreeWidgetItem *) );
159 }
160
161 PLSelector::~PLSelector()
162 {
163     if( podcastsParent )
164     {
165         int c = podcastsParent->childCount();
166         for( int i = 0; i < c; i++ )
167         {
168             QTreeWidgetItem *item = podcastsParent->child(i);
169             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
170             vlc_gc_decref( p_input );
171         }
172     }
173 }
174
175 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
176 {
177     item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
178     item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
179     return item;
180 }
181
182 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
183 {
184     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
185 /*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
186     item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
187     return item;
188 }
189
190 void PLSelector::createItems()
191 {
192     /* PL */
193     PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
194                               THEPL->p_playing );
195     pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
196     setCurrentItem( pl->treeItem() );
197
198     /* ML */
199     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
200                               THEPL->p_media_library );
201     ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
202
203 #ifdef MEDIA_LIBRARY
204     /* SQL ML */
205     addItem( SQL_ML_TYPE, "SQL Media Library" )->treeItem();
206 #endif
207
208     /* SD nodes */
209     QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, N_("My Computer") )->treeItem();
210     QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, N_("Devices") )->treeItem();
211     QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, N_("Local Network") )->treeItem();
212     QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, N_("Internet") )->treeItem();
213
214 #define NOT_SELECTABLE(w) w->setFlags( w->flags() ^ Qt::ItemIsSelectable );
215     NOT_SELECTABLE( mycomp );
216     NOT_SELECTABLE( devices );
217     NOT_SELECTABLE( lan );
218     NOT_SELECTABLE( internet );
219 #undef NOT_SELECTABLE
220
221     /* SD subnodes */
222     char **ppsz_longnames;
223     int *p_categories;
224     char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
225     if( !ppsz_names )
226         return;
227
228     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
229     int *p_category = p_categories;
230     for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
231     {
232         //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
233
234         PLSelItem *selItem;
235         switch( *p_category )
236         {
237         case SD_CAT_INTERNET:
238             {
239             selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
240             if( !strncmp( *ppsz_name, "podcast", 7 ) )
241             {
242                 selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
243                 selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
244                 CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
245                 podcastsParent = selItem->treeItem();
246             }
247             }
248             break;
249         case SD_CAT_DEVICES:
250             selItem = addItem( SD_TYPE, *ppsz_longname, false, devices );
251             break;
252         case SD_CAT_LAN:
253             selItem = addItem( SD_TYPE, *ppsz_longname, false, lan );
254             break;
255         case SD_CAT_MYCOMPUTER:
256             selItem = addItem( SD_TYPE, *ppsz_longname, false, mycomp );
257             break;
258         default:
259             selItem = addItem( SD_TYPE, *ppsz_longname );
260         }
261
262         putSDData( selItem, *ppsz_name, *ppsz_longname );
263         free( *ppsz_name );
264         free( *ppsz_longname );
265     }
266     free( ppsz_names );
267     free( ppsz_longnames );
268     free( p_categories );
269
270     if( mycomp->childCount() == 0 ) delete mycomp;
271     if( devices->childCount() == 0 ) delete devices;
272     if( lan->childCount() == 0 ) delete lan;
273     if( internet->childCount() == 0 ) delete internet;
274 }
275
276 void PLSelector::setSource( QTreeWidgetItem *item )
277 {
278     if( !item || item == curItem )
279         return;
280
281     bool b_ok;
282     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
283     if( !b_ok || i_type == CATEGORY_TYPE )
284         return;
285
286     bool sd_loaded;
287     if( i_type == SD_TYPE )
288     {
289         QString qs = item->data( 0, NAME_ROLE ).toString();
290         sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
291         if( !sd_loaded )
292         {
293             if ( playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) ) != VLC_SUCCESS )
294                 return ;
295
296             services_discovery_descriptor_t *p_test = new services_discovery_descriptor_t;
297             int i_ret = playlist_ServicesDiscoveryControl( THEPL, qtu( qs ), SD_CMD_DESCRIPTOR, p_test );
298             if( i_ret == VLC_SUCCESS && p_test->i_capabilities & SD_CAP_SEARCH )
299                 item->setData( 0, CAP_SEARCH_ROLE, true );
300         }
301     }
302 #ifdef MEDIA_LIBRARY
303     else if( i_type == SQL_ML_TYPE )
304     {
305         emit categoryActivated( NULL, true );
306         return;
307     }
308 #endif
309
310     curItem = item;
311
312     /* */
313     playlist_Lock( THEPL );
314     playlist_item_t *pl_item = NULL;
315
316     /* Special case for podcast */
317     // FIXME: simplify
318     if( i_type == SD_TYPE )
319     {
320         /* Find the right item for the SD */
321         pl_item = playlist_ChildSearchName( THEPL->p_root,
322                       qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
323
324         /* Podcasts */
325         if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
326         {
327             if( pl_item && !sd_loaded )
328             {
329                 podcastsParentId = pl_item->i_id;
330                 for( int i=0; i < pl_item->i_children; i++ )
331                     addPodcastItem( pl_item->pp_children[i] );
332             }
333             pl_item = NULL; //to prevent activating it
334         }
335     }
336     else
337         pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
338
339     playlist_Unlock( THEPL );
340
341     /* */
342     if( pl_item )
343         emit categoryActivated( pl_item, false );
344 }
345
346 PLSelItem * PLSelector::addItem (
347     SelectorItemType type, const char* str, bool drop,
348     QTreeWidgetItem* parentItem )
349 {
350   QTreeWidgetItem *item = parentItem ?
351       new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
352
353   PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
354   setItemWidget( item, 0, selItem );
355   item->setData( 0, TYPE_ROLE, (int)type );
356   if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
357
358   return selItem;
359 }
360
361 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
362 {
363     vlc_gc_incref( p_item->p_input );
364
365     char *psz_name = input_item_GetName( p_item->p_input );
366     PLSelItem *item = addItem( PL_ITEM_TYPE,  psz_name, false, podcastsParent );
367     free( psz_name );
368
369     item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
370     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
371     item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
372     item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
373     CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
374     return item;
375 }
376
377 QStringList PLSelector::mimeTypes() const
378 {
379     QStringList types;
380     types << "vlc/qt-input-items";
381     return types;
382 }
383
384 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int,
385     const QMimeData * data, Qt::DropAction )
386 {
387     if( !parent ) return false;
388
389     QVariant type = parent->data( 0, TYPE_ROLE );
390     if( type == QVariant() ) return false;
391
392     int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
393     if( i_truth != IS_PL && i_truth != IS_ML ) return false;
394
395     bool to_pl = ( i_truth == IS_PL );
396
397     const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
398     if( !plMimeData ) return false;
399
400     QList<input_item_t*> inputItems = plMimeData->inputItems();
401
402     playlist_Lock( THEPL );
403
404     foreach( input_item_t *p_input, inputItems )
405     {
406         playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
407         if( !p_item ) continue;
408
409         playlist_NodeAddCopy( THEPL, p_item,
410                               to_pl ? THEPL->p_playing : THEPL->p_media_library,
411                               PLAYLIST_END );
412     }
413
414     playlist_Unlock( THEPL );
415
416     return true;
417 }
418
419 void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
420 {
421     event->setDropAction( Qt::CopyAction );
422     QAbstractItemView::dragMoveEvent( event );
423 }
424
425 void PLSelector::plItemAdded( int item, int parent )
426 {
427     if( parent != podcastsParentId ) return;
428
429     playlist_Lock( THEPL );
430
431     playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
432     if( !p_item ) {
433         playlist_Unlock( THEPL );
434         return;
435     }
436
437     int c = podcastsParent->childCount();
438     for( int i = 0; i < c; i++ )
439     {
440         QTreeWidgetItem *podItem = podcastsParent->child(i);
441         if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
442         {
443           //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
444           playlist_Unlock( THEPL );
445           return;
446         }
447     }
448
449     //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
450     addPodcastItem( p_item );
451
452     playlist_Unlock( THEPL );
453
454     podcastsParent->setExpanded( true );
455 }
456
457 void PLSelector::plItemRemoved( int id )
458 {
459     int c = podcastsParent->childCount();
460     for( int i = 0; i < c; i++ )
461     {
462         QTreeWidgetItem *item = podcastsParent->child(i);
463         if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
464         {
465             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
466             //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
467             vlc_gc_decref( p_input );
468             delete item;
469             return;
470         }
471     }
472 }
473
474 void PLSelector::inputItemUpdate( input_item_t *arg )
475 {
476     int c = podcastsParent->childCount();
477     for( int i = 0; i < c; i++ )
478     {
479         QTreeWidgetItem *item = podcastsParent->child(i);
480         input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
481         if( p_input == arg )
482         {
483             PLSelItem *si = itemWidget( item );
484             char *psz_name = input_item_GetName( p_input );
485             si->setText( qfu( psz_name ) );
486             free( psz_name );
487             return;
488         }
489     }
490 }
491
492 void PLSelector::podcastAdd( PLSelItem * )
493 {
494     bool ok;
495     QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
496                                          qtr( "Enter URL of the podcast to subscribe to:" ),
497                                          QLineEdit::Normal, QString(), &ok );
498     if( !ok || url.isEmpty() ) return;
499
500     setSource( podcastsParent ); //to load the SD in case it's not loaded
501
502     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name( p_intf->p_libvlc, "podcast" );
503     if( !p_obj ) return;
504
505     QString request("ADD:");
506     request += url.trimmed();
507     var_SetString( p_obj, "podcast-request", qtu( request ) );
508     vlc_object_release( p_obj );
509 }
510
511 void PLSelector::podcastRemove( PLSelItem* item )
512 {
513     QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
514     question = question.arg( item->text() );
515     QMessageBox::StandardButton res =
516         QMessageBox::question( this, qtr( "Unsubscribe" ), question,
517                                QMessageBox::Ok | QMessageBox::Cancel,
518                                QMessageBox::Cancel );
519     if( res == QMessageBox::Cancel ) return;
520
521     input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
522     if( !input ) return;
523
524     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
525         p_intf->p_libvlc, "podcast" );
526     if( !p_obj ) return;
527
528     QString request("RM:");
529     char *psz_uri = input_item_GetURI( input );
530     request += qfu( psz_uri );
531     var_SetString( p_obj, "podcast-request", qtu( request ) );
532     vlc_object_release( p_obj );
533     free( psz_uri );
534 }
535
536 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
537 {
538     return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
539 }
540
541 void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
542 {
543     if( !model()->hasChildren( index ) ) return;
544     QStyleOption option;
545     option.initFrom( this );
546     option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
547     style()->drawPrimitive( isExpanded( index ) ?
548                             QStyle::PE_IndicatorArrowDown :
549                             QStyle::PE_IndicatorArrowRight, &option, painter );
550 }
551
552 void PLSelector::getCurrentItemInfos( int* type, bool* can_delay_search, QString *string)
553 {
554     *type = currentItem()->data( 0, TYPE_ROLE ).toInt();
555     *string = currentItem()->data( 0, NAME_ROLE ).toString();
556     *can_delay_search = currentItem()->data( 0, CAP_SEARCH_ROLE ).toBool();
557 }
558
559 int PLSelector::getCurrentItemCategory()
560 {
561     return currentItem()->data( 0, SPECIAL_ROLE ).toInt();
562 }
563
564 void PLSelector::wheelEvent( QWheelEvent *e )
565 {
566     if( verticalScrollBar()->isVisible() && (
567         (verticalScrollBar()->value() != verticalScrollBar()->minimum() && e->delta() >= 0 ) ||
568         (verticalScrollBar()->value() != verticalScrollBar()->maximum() && e->delta() < 0 )
569         ) )
570         QApplication::sendEvent(verticalScrollBar(), e);
571
572     // Accept this event in order to prevent unwanted volume up/down changes
573     e->accept();
574 }