]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
qt4: implement drag&drop in playlist selector
[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
35 #include <QVBoxLayout>
36 #include <QHeaderView>
37 #include <QMimeData>
38
39 #include <vlc_playlist.h>
40 #include <vlc_services_discovery.h>
41
42 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
43            : QTreeWidget( p ), p_intf(_p_intf)
44 {
45     setIconSize( QSize( 24,24 ) );
46 //    view->setAlternatingRowColors( true );
47     setIndentation( 10 );
48     header()->hide();
49     setRootIsDecorated( false );
50 //    model = new PLModel( THEPL, p_intf, THEPL->p_root_category, 1, this );
51 //    view->setModel( model );
52     viewport()->setAcceptDrops(true);
53     setDropIndicatorShown(true);
54     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
55
56     createItems();
57     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
58              this, setSource( QTreeWidgetItem *) );
59     /* I believe this is unnecessary, seeing
60        QStyle::SH_ItemView_ActivateItemOnSingleClick
61         CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
62              this, setSource( QTreeWidgetItem *) ); */
63
64     /* select the first item */
65 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
66 }
67
68 void PLSelector::setSource( QTreeWidgetItem *item )
69 {
70     if( !item )
71         return;
72
73     int i_type = item->data( 0, TYPE_ROLE ).toInt();
74
75     assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
76     if( i_type == SD_TYPE )
77     {
78         QString qs = item->data( 0, NAME_ROLE ).toString();
79         if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
80         {
81             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
82 #warning FIXME
83             playlist_item_t *pl_item =
84                     THEPL->p_root_category->pp_children[THEPL->p_root_category->i_children-1];
85             item->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( pl_item ) );
86
87             emit activated( pl_item );
88             return;
89         }
90     }
91
92     if( i_type == SD_TYPE )
93         msg_Dbg( p_intf, "SD already loaded, reloading" );
94
95     playlist_item_t *pl_item =
96             item->data( 0, PPL_ITEM_ROLE ).value<playlist_item_t *>();
97     if( pl_item )
98             emit activated( pl_item );
99 }
100
101 void PLSelector::createItems()
102 {
103     QTreeWidgetItem *pl = new QTreeWidgetItem( this );
104     pl->setText( 0, qtr( "Playlist" ) );
105     pl->setData( 0, TYPE_ROLE, PL_TYPE );
106     pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
107 /*  QTreeWidgetItem *empty = new QTreeWidgetItem( view );
108     empty->setFlags(Qt::NoItemFlags); */
109
110     QTreeWidgetItem *lib = new QTreeWidgetItem( this );
111     lib->setText( 0, qtr( "Library" ) );
112     lib->setData( 0, TYPE_ROLE, ML_TYPE );
113     lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
114
115 /*  QTreeWidgetItem *empty2 = new QTreeWidgetItem( view );
116     empty2->setFlags(Qt::NoItemFlags);*/
117
118     QTreeWidgetItem *sds = new QTreeWidgetItem( this );
119     sds->setExpanded( true );
120     sds->setText( 0, qtr( "Libraries" ) );
121     sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
122
123     char **ppsz_longnames;
124     char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
125     if( !ppsz_names )
126         return;
127
128     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
129     QTreeWidgetItem *sd_item;
130     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
131     {
132         sd_item = new QTreeWidgetItem( QStringList( *ppsz_longname ) );
133         sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
134         sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
135         sd_item->setFlags( sd_item->flags() & ~Qt::ItemIsDropEnabled );
136         sds->addChild( sd_item );
137     }
138 }
139
140 #include <iostream>
141 QStringList PLSelector::mimeTypes() const
142 {
143     QStringList types;
144     types << "vlc/qt-playlist-item";
145     return types;
146 }
147
148 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
149   const QMimeData * data, Qt::DropAction action )
150 {
151     if( !parent ) return false;
152
153     QVariant type = parent->data( 0, TYPE_ROLE );
154     if( type == QVariant() ) return false;
155     int i_type = type.toInt();
156     if( i_type != PL_TYPE && i_type != ML_TYPE ) return false;
157     bool to_pl = i_type == PL_TYPE;
158
159     if( data->hasFormat( "vlc/qt-playlist-item" ) )
160     {
161         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
162         QDataStream stream( &encodedData, QIODevice::ReadOnly );
163         playlist_Lock( THEPL );
164         while( !stream.atEnd() )
165         {
166             PLItem *item;
167             stream.readRawData( (char*)&item, sizeof(PLItem*) );
168             input_item_t *pl_input =item->inputItem();
169             playlist_AddExt ( THEPL,
170                 pl_input->psz_uri, pl_input->psz_name,
171                 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
172                 pl_input->i_duration,
173                 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
174                 to_pl, true );
175         }
176         playlist_Unlock( THEPL );
177     }
178     return true;
179 }
180
181 PLSelector::~PLSelector()
182 {
183 }