]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist.cpp
Qt4: more media browser (PL Selector) beautification
[vlc] / modules / gui / qt4 / components / playlist / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : Custom widgets for the playlist
3  ****************************************************************************
4  * Copyright © 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
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 "components/playlist/panels.hpp"
30 #include "components/playlist/selector.hpp"
31 #include "components/playlist/playlist.hpp"
32
33 #include "input_manager.hpp" /* art signal */
34 #include "main_interface.hpp" /* DropEvent TODO remove this*/
35
36 #include <QGroupBox>
37
38 #include <iostream>
39 /**********************************************************************
40  * Playlist Widget. The embedded playlist
41  **********************************************************************/
42
43 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i ) : p_intf ( _p_i )
44 {
45     setContentsMargins( 3, 3, 3, 3 );
46
47     /* Left Part and design */
48     QSplitter *leftW = new QSplitter( Qt::Vertical, this );
49
50     /* Source Selector */
51     selector = new PLSelector( this, p_intf );
52     QVBoxLayout *selBox = new QVBoxLayout();
53     selBox->setContentsMargins(0,5,0,0);
54     selBox->addWidget( selector );
55     QGroupBox *selGroup = new QGroupBox( qtr( "Media Browser") );
56     selGroup->setLayout( selBox );
57     leftW->addWidget( selGroup );
58
59     /* Create a Container for the Art Label
60        in order to have a beautiful resizing for the selector above it */
61     QWidget *artContainer = new QWidget;
62     QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
63     artContLay->setMargin( 0 );
64     artContLay->setSpacing( 0 );
65     artContainer->setMaximumHeight( 128 );
66
67     /* Art label */
68     art = new ArtLabel( artContainer, p_intf );
69     art->setToolTip( qtr( "Double click to get media information" ) );
70
71     CONNECT( THEMIM->getIM(), artChanged( QString ),
72              art, showArtUpdate( const QString& ) );
73
74     artContLay->addWidget( art, 1 );
75
76     leftW->addWidget( artContainer );
77
78     /* Initialisation of the playlist */
79     playlist_t * p_playlist = THEPL;
80     PL_LOCK;
81     playlist_item_t *p_root =
82                   playlist_GetPreferredNode( THEPL, THEPL->p_local_category );
83     PL_UNLOCK;
84
85     rightPanel = new StandardPLPanel( this, p_intf, THEPL, p_root );
86
87     /* Connect the activation of the selector to a redefining of the PL */
88     CONNECT( selector, activated( playlist_item_t * ),
89              rightPanel, setRoot( playlist_item_t * ) );
90
91     rightPanel->setRoot( p_root );
92
93     /* Add the two sides of the QSplitter */
94     addWidget( leftW );
95     addWidget( rightPanel );
96
97     QList<int> sizeList;
98     sizeList << 180 << 420 ;
99     setSizes( sizeList );
100     //setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
101     setStretchFactor( 0, 0 );
102     setStretchFactor( 1, 3 );
103     leftW->setMaximumWidth( 250 );
104     setCollapsible( 1, false );
105
106     /* In case we want to keep the splitter informations */
107     // components shall never write there setting to a fixed location, may infer
108     // with other uses of the same component...
109     // getSettings()->beginGroup( "playlist" );
110     getSettings()->beginGroup("Playlist");
111     restoreState( getSettings()->value("splitterSizes").toByteArray());
112     getSettings()->endGroup();
113
114     setAcceptDrops( true );
115     setWindowTitle( qtr( "Playlist" ) );
116     setWindowRole( "vlc-playlist" );
117     setWindowIcon( QApplication::windowIcon() );
118 }
119
120 PlaylistWidget::~PlaylistWidget()
121 {
122     getSettings()->beginGroup("Playlist");
123     getSettings()->setValue( "splitterSizes", saveState() );
124     getSettings()->endGroup();
125     msg_Dbg( p_intf, "Playlist Destroyed" );
126 }
127
128 void PlaylistWidget::dropEvent( QDropEvent *event )
129 {
130     if( p_intf->p_sys->p_mi )
131         p_intf->p_sys->p_mi->dropEventPlay( event, false );
132 }
133 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
134 {
135     event->acceptProposedAction();
136 }
137
138 void PlaylistWidget::closeEvent( QCloseEvent *event )
139 {
140     if( THEDP->isDying() )
141     {
142         /* FIXME is it needed ? */
143         event->accept();
144     }
145     else
146     {
147         hide();
148         event->ignore();
149     }
150 }
151
152 PlaylistEventManager::PlaylistEventManager( playlist_t *_pl )
153     : pl( _pl )
154 {
155   var_AddCallback( pl, "playlist-item-append", itemAddedCb, this );
156   var_AddCallback( pl, "playlist-item-deleted", itemRemovedCb, this );
157 }
158
159 PlaylistEventManager::~PlaylistEventManager()
160 {
161   var_DelCallback( pl, "playlist-item-append", itemAddedCb, this );
162   var_DelCallback( pl, "playlist-item-deleted", itemRemovedCb, this );
163 }
164
165 int PlaylistEventManager::itemAddedCb
166 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
167 {
168     PlaylistEventManager *p_this = static_cast<PlaylistEventManager*>(data);
169     p_this->trigger( cur, ItemAddedEv );
170     return VLC_SUCCESS;
171 }
172
173 int PlaylistEventManager::itemRemovedCb
174 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
175 {
176     PlaylistEventManager *p_this = static_cast<PlaylistEventManager*>(data);
177     p_this->trigger( cur, ItemRemovedEv );
178     return VLC_SUCCESS;
179 }
180
181 void PlaylistEventManager::trigger( vlc_value_t val, int type )
182 {
183     if( type == ItemAddedEv )
184     {
185         playlist_add_t *p_add = static_cast<playlist_add_t*>( val.p_address );
186         QApplication::postEvent( this, new PLEMEvent( type, p_add->i_item, p_add->i_node ) );
187     }
188     else
189     {
190         QApplication::postEvent( this, new PLEMEvent( type, val.i_int, 0 ) );
191     }
192 }
193
194 void PlaylistEventManager::customEvent( QEvent *e )
195 {
196     PLEMEvent *ev = static_cast<PLEMEvent*>(e);
197     if( (int) ev->type() == ItemAddedEv )
198         emit itemAdded( ev->item, ev->parent );
199     else
200         emit itemRemoved( ev->item );
201 }