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