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