]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
qt4: selector: switch source on single click
[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 "qt4.hpp"
33
34 #include <QVBoxLayout>
35 #include <QHeaderView>
36 #include <QTreeWidget>
37
38 #include <vlc_playlist.h>
39 #include <vlc_services_discovery.h>
40
41 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
42            : QWidget( p ), p_intf(_p_intf)
43 {
44     view = new QTreeWidget;
45
46     view->setIconSize( QSize( 24,24 ) );
47 //    view->setAlternatingRowColors( true );
48     view->setIndentation( 10 );
49     view->header()->hide();
50     view->setRootIsDecorated( false );
51 //    model = new PLModel( THEPL, p_intf, THEPL->p_root_category, 1, this );
52 //    view->setModel( model );
53 //    view->setAcceptDrops(true);
54 //    view->setDropIndicatorShown(true);
55
56     createItems();
57     /* CONNECT( view, itemActivated( QTreeWidgetItem *, int ),
58              this, setSource( QTreeWidgetItem *) ); */
59     /* I believe this is unnecessary, seeing
60        QStyle::SH_ItemView_ActivateItemOnSingleClick */
61     /* <jleben> No, you can only make custom styles by creating whole new
62        or subclassing an existing QStyle.
63        Connecting itemClicked signal is easier, of course */
64     CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
65           this, setSource( QTreeWidgetItem *) );
66
67     QVBoxLayout *layout = new QVBoxLayout;
68     layout->setSpacing( 0 ); layout->setMargin( 0 );
69     layout->addWidget( view );
70     setLayout( layout );
71
72     /* select the first item */
73 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
74 }
75
76 void PLSelector::setSource( QTreeWidgetItem *item )
77 {
78     if( !item )
79         return;
80
81     QVariant type = item->data( 0, TYPE_ROLE );
82     if( type == QVariant() ) return;
83
84     int i_type = type.toInt();
85
86     assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
87     if( i_type == SD_TYPE )
88     {
89         QString qs = item->data( 0, NAME_ROLE ).toString();
90         if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
91         {
92             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
93 #warning FIXME
94             playlist_item_t *pl_item =
95                     THEPL->p_root_category->pp_children[THEPL->p_root_category->i_children-1];
96             item->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( pl_item ) );
97
98             emit activated( pl_item );
99             return;
100         }
101     }
102
103     if( i_type == SD_TYPE )
104         msg_Dbg( p_intf, "SD already loaded, reloading" );
105
106     playlist_item_t *pl_item =
107             item->data( 0, PPL_ITEM_ROLE ).value<playlist_item_t *>();
108     if( pl_item )
109             emit activated( pl_item );
110 }
111
112 void PLSelector::createItems()
113 {
114     assert( view );
115     QTreeWidgetItem *pl = new QTreeWidgetItem( view );
116     pl->setText( 0, qtr( "Playlist" ) );
117     pl->setData( 0, TYPE_ROLE, PL_TYPE );
118     pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
119
120 /*  QTreeWidgetItem *empty = new QTreeWidgetItem( view );
121     empty->setFlags(Qt::NoItemFlags); */
122
123     QTreeWidgetItem *lib = new QTreeWidgetItem( view );
124     lib->setText( 0, qtr( "Library" ) );
125     lib->setData( 0, TYPE_ROLE, ML_TYPE );
126     lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
127
128 /*  QTreeWidgetItem *empty2 = new QTreeWidgetItem( view );
129     empty2->setFlags(Qt::NoItemFlags);*/
130
131     QTreeWidgetItem *sds = new QTreeWidgetItem( view );
132     sds->setExpanded( true );
133     sds->setText( 0, qtr( "Libraries" ) );
134
135     char **ppsz_longnames;
136     char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
137     if( !ppsz_names )
138         return;
139
140     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
141     QTreeWidgetItem *sd_item;
142     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
143     {
144         sd_item = new QTreeWidgetItem( QStringList( *ppsz_longname ) );
145         sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
146         sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
147         sds->addChild( sd_item );
148     }
149 }
150
151 PLSelector::~PLSelector()
152 {
153 }