]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
Qt4: media selector beautification
[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
36 #include <QVBoxLayout>
37 #include <QHeaderView>
38 #include <QMimeData>
39 #include <QPushButton>
40 #include <QLabel>
41
42 #include <vlc_playlist.h>
43 #include <vlc_services_discovery.h>
44
45 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
46            : QTreeWidget( p ), p_intf(_p_intf)
47 {
48     setItemDelegate( new PLSelectorDelegate() );
49     setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
50     setIconSize( QSize( 24,24 ) );
51     setIndentation( 15 );
52     header()->hide();
53     setRootIsDecorated( false );
54     setAlternatingRowColors( true );
55
56     viewport()->setAcceptDrops(true);
57     setDropIndicatorShown(true);
58     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
59
60     createItems();
61     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
62              this, setSource( QTreeWidgetItem *) );
63     /* I believe this is unnecessary, seeing
64        QStyle::SH_ItemView_ActivateItemOnSingleClick
65         CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
66              this, setSource( QTreeWidgetItem *) ); */
67
68     /* select the first item */
69 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
70 }
71
72 void PLSelector::setSource( QTreeWidgetItem *item )
73 {
74     if( !item )
75         return;
76
77     bool b_ok;
78     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
79     if( !b_ok )
80         return;
81
82     assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
83     if( i_type == SD_TYPE )
84     {
85         QString qs = item->data( 0, NAME_ROLE ).toString();
86         if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
87         {
88             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
89         }
90     }
91
92     if( i_type == SD_TYPE )
93         msg_Dbg( p_intf, "SD already loaded, reloading" );
94
95     playlist_Lock( THEPL );
96     playlist_item_t *pl_item = NULL;
97     if( i_type == SD_TYPE )
98        pl_item = playlist_ChildSearchName( THEPL->p_root_category, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
99     else if ( i_type == PL_TYPE )
100        pl_item = THEPL->p_local_category;
101     else if ( i_type == ML_TYPE )
102        pl_item = THEPL->p_ml_category;
103     playlist_Unlock( THEPL );
104
105     if( pl_item )
106        emit activated( pl_item );
107 }
108
109 void PLSelector::makeStandardItem( QTreeWidgetItem* item, const QString& str )
110 {
111   item->setText( 0,str );
112 }
113
114 void PLSelector::createItems()
115 {
116     QTreeWidgetItem *pl = new QTreeWidgetItem( this );
117     makeStandardItem( pl, qtr( "Playlist" ) );
118     pl->setData( 0, TYPE_ROLE, PL_TYPE );
119     pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
120
121     QTreeWidgetItem *lib = new QTreeWidgetItem( this );
122     makeStandardItem( lib, qtr( "Media Library" ) );
123     lib->setData( 0, TYPE_ROLE, ML_TYPE );
124     lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
125
126     QTreeWidgetItem *sds = new QTreeWidgetItem( this );
127     makeStandardItem( sds, qtr( "Services" ) );
128     sds->setExpanded( true );
129     sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
130
131     QTreeWidgetItem *mfldrs = new QTreeWidgetItem( sds );
132     makeStandardItem( mfldrs, qtr( "Media Folders" ) );
133     mfldrs->setFlags( mfldrs->flags() & ~Qt::ItemIsDropEnabled );
134
135     QTreeWidgetItem *shouts = new QTreeWidgetItem( sds );
136     makeStandardItem( shouts, qtr( "Shoutcast" ) );
137     shouts->setFlags( shouts->flags() & ~Qt::ItemIsDropEnabled );
138
139     char **ppsz_longnames;
140     char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
141     if( !ppsz_names )
142         return;
143
144     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
145     QTreeWidgetItem *sd_item;
146     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
147     {
148         sd_item = new QTreeWidgetItem(  );
149
150 #define SD_IS( name ) ( !strcmp( *ppsz_name, name ) )
151
152         if( SD_IS("shoutcast") || SD_IS("shoutcasttv") ||
153             SD_IS("frenchtv") || SD_IS("freebox") )
154         {
155             shouts->addChild( sd_item );
156             makeStandardItem( sd_item, qfu(*ppsz_longname) );
157         }
158         else if( SD_IS("video_dir") || SD_IS("audio_dir") || SD_IS("picture_dir") )
159         {
160             mfldrs->addChild( sd_item );
161             makeStandardItem( sd_item, qfu(*ppsz_longname) );
162         }
163         else if( SD_IS("podcast") )
164         {
165             sds->addChild( sd_item );
166
167             QLabel *lbl = new QLabel("Podcasts");
168             lbl->setMargin(3);
169             QPushButton *btn = new QPushButton();
170             btn->setMaximumWidth(30);
171             btn->setIcon( QIcon( ":/menu/preferences" ) );
172             QHBoxLayout *hbox = new QHBoxLayout();
173             hbox->setContentsMargins(0,0,0,0);
174             hbox->addWidget(lbl);
175             hbox->addWidget(btn);
176             QWidget *w = new QWidget();
177             w->setLayout(hbox);
178
179             CONNECT( btn, clicked(), THEDP, podcastConfigureDialog() );
180             setItemWidget( sd_item, 0, w );
181         }
182         else
183         {
184             sds->addChild( sd_item );
185             makeStandardItem( sd_item, qfu(*ppsz_longname) );
186         }
187
188 #undef SD_IS
189
190         sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
191         sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
192         sd_item->setData( 0, LONGNAME_ROLE, qfu( *ppsz_longname ) );
193         sd_item->setFlags( sd_item->flags() & ~Qt::ItemIsDropEnabled );
194         free( *ppsz_name );
195         free( *ppsz_longname );
196     }
197     free( ppsz_names );
198     free( ppsz_longnames );
199 }
200
201 QStringList PLSelector::mimeTypes() const
202 {
203     QStringList types;
204     types << "vlc/qt-playlist-item";
205     return types;
206 }
207
208 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
209   const QMimeData * data, Qt::DropAction action )
210 {
211     if( !parent ) return false;
212
213     QVariant type = parent->data( 0, TYPE_ROLE );
214     if( type == QVariant() ) return false;
215     int i_type = type.toInt();
216     if( i_type != PL_TYPE && i_type != ML_TYPE ) return false;
217     bool to_pl = i_type == PL_TYPE;
218
219     if( data->hasFormat( "vlc/qt-playlist-item" ) )
220     {
221         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
222         QDataStream stream( &encodedData, QIODevice::ReadOnly );
223         playlist_Lock( THEPL );
224         while( !stream.atEnd() )
225         {
226             PLItem *item;
227             stream.readRawData( (char*)&item, sizeof(PLItem*) );
228             input_item_t *pl_input =item->inputItem();
229             playlist_AddExt ( THEPL,
230                 pl_input->psz_uri, pl_input->psz_name,
231                 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
232                 pl_input->i_duration,
233                 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
234                 to_pl, true );
235         }
236         playlist_Unlock( THEPL );
237     }
238     return true;
239 }
240
241 PLSelector::~PLSelector()
242 {
243 }