1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include "dialogs_provider.hpp"
29 #include "components/playlist/playlist_model.hpp"
30 #include "components/playlist/panels.hpp"
31 #include "util/customwidgets.hpp"
33 #include <vlc_intf_strings.h>
36 #include <QPushButton>
37 #include <QHBoxLayout>
38 #include <QVBoxLayout>
39 #include <QHeaderView>
41 #include <QModelIndexList>
44 #include <QSpacerItem>
46 #include <QSignalMapper>
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53 intf_thread_t *_p_intf,
54 playlist_t *p_playlist,
55 playlist_item_t *p_root ):
56 PLPanel( _parent, _p_intf )
58 model = new PLModel( p_playlist, p_intf, p_root, -1, this );
60 QVBoxLayout *layout = new QVBoxLayout();
61 layout->setSpacing( 0 ); layout->setMargin( 0 );
63 /* Create and configure the QTreeView */
64 view = new QVLCTreeView( 0 );
65 view->setSortingEnabled( true );
66 view->sortByColumn( 0 , Qt::AscendingOrder );
67 view->setModel( model );
68 view->setIconSize( QSize( 20, 20 ) );
69 view->setAlternatingRowColors( true );
70 view->setAnimated( true );
71 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
72 view->setDragEnabled( true );
73 view->setAcceptDrops( true );
74 view->setDropIndicatorShown( true );
75 view->setAutoScroll( true );
78 if( getSettings()->contains( "headerState" ) )
80 view->header()->restoreState( getSettings()->value( "headerState" ).toByteArray() );
81 msg_Dbg( p_intf, "exists" );
86 /* Configure the size of the header */
87 view->header()->resizeSection( 0, 200 );
88 view->header()->resizeSection( 1, 80 );
89 view->header()->setSortIndicatorShown( true );
90 view->header()->setClickable( true );
91 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
94 /* Connections for the TreeView */
95 CONNECT( view, activated( const QModelIndex& ) ,
96 model,activateItem( const QModelIndex& ) );
97 CONNECT( view, rightClicked( QModelIndex , QPoint ),
98 this, doPopup( QModelIndex, QPoint ) );
99 CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
100 this, handleExpansion( const QModelIndex& ) );
101 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
102 this, popupSelectColumn( QPoint ) );
105 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
107 /* Buttons configuration */
108 QHBoxLayout *buttons = new QHBoxLayout;
110 /* Add item to the playlist button */
111 addButton = new QPushButton;
112 addButton->setIcon( QIcon( ":/playlist_add" ) );
113 addButton->setMaximumWidth( 30 );
114 BUTTONACT( addButton, popupAdd() );
115 buttons->addWidget( addButton );
117 /* Random 2-state button */
118 randomButton = new QPushButton( this );
119 if( model->hasRandom() )
121 randomButton->setIcon( QIcon( ":/shuffle_on" ));
122 randomButton->setToolTip( qtr( I_PL_RANDOM ));
126 randomButton->setIcon( QIcon( ":/shuffle_off" ) );
127 randomButton->setToolTip( qtr( I_PL_NORANDOM ));
129 BUTTONACT( randomButton, toggleRandom() );
130 buttons->addWidget( randomButton );
132 /* Repeat 3-state button */
133 repeatButton = new QPushButton( this );
134 if( model->hasRepeat() )
136 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
137 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
139 else if( model->hasLoop() )
141 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
142 repeatButton->setToolTip( qtr( I_PL_LOOP ));
146 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
147 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
149 BUTTONACT( repeatButton, toggleRepeat() );
150 buttons->addWidget( repeatButton );
153 gotoPlayingButton = new QPushButton;
154 BUTTON_SET_ACT_I( gotoPlayingButton, "", jump_to,
155 qtr( "Show the current item" ), gotoPlayingItem() );
156 buttons->addWidget( gotoPlayingButton );
158 /* A Spacer and the search possibilities */
159 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
160 buttons->addItem( spacer );
162 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
163 buttons->addWidget( filter );
165 searchLine = new ClickLineEdit( qtr(I_PL_FILTER), 0 );
166 searchLine->setMinimumWidth( 80 );
167 CONNECT( searchLine, textChanged(QString), this, search(QString));
168 buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
170 QPushButton *clear = new QPushButton;
171 clear->setMaximumWidth( 30 );
172 BUTTON_SET_ACT_I( clear, "", clear, qtr( "Clear" ), clearFilter() );
173 buttons->addWidget( clear );
175 /* Finish the layout */
176 layout->addWidget( view );
177 layout->addLayout( buttons );
178 // layout->addWidget( bar );
182 /* Function to toggle between the Repeat states */
183 void StandardPLPanel::toggleRepeat()
185 if( model->hasRepeat() )
187 model->setRepeat( false ); model->setLoop( true );
188 repeatButton->setIcon( QIcon( ":/repeat_all" ) );
189 repeatButton->setToolTip( qtr( I_PL_LOOP ));
191 else if( model->hasLoop() )
193 model->setRepeat( false ) ; model->setLoop( false );
194 repeatButton->setIcon( QIcon( ":/repeat_off" ) );
195 repeatButton->setToolTip( qtr( I_PL_NOREPEAT ));
199 model->setRepeat( true );
200 repeatButton->setIcon( QIcon( ":/repeat_one" ) );
201 repeatButton->setToolTip( qtr( I_PL_REPEAT ));
205 /* Function to toggle between the Random states */
206 void StandardPLPanel::toggleRandom()
208 bool prev = model->hasRandom();
209 model->setRandom( !prev );
210 randomButton->setIcon( prev ?
211 QIcon( ":/shuffle_off" ) :
212 QIcon( ":/shuffle_on" ) );
213 randomButton->setToolTip( prev ? qtr( I_PL_NORANDOM ) : qtr(I_PL_RANDOM ) );
216 void StandardPLPanel::gotoPlayingItem()
218 view->scrollTo( view->currentIndex() );
221 void StandardPLPanel::handleExpansion( const QModelIndex &index )
223 if( model->isCurrent( index ) )
224 view->scrollTo( index, QAbstractItemView::EnsureVisible );
227 void StandardPLPanel::setCurrentRootId( int _new )
229 currentRootId = _new;
230 if( currentRootId == THEPL->p_local_category->i_id ||
231 currentRootId == THEPL->p_local_onelevel->i_id )
233 addButton->setEnabled( true );
234 addButton->setToolTip( qtr(I_PL_ADDPL) );
236 else if( ( THEPL->p_ml_category &&
237 currentRootId == THEPL->p_ml_category->i_id ) ||
238 ( THEPL->p_ml_onelevel &&
239 currentRootId == THEPL->p_ml_onelevel->i_id ) )
241 addButton->setEnabled( true );
242 addButton->setToolTip( qtr(I_PL_ADDML) );
245 addButton->setEnabled( false );
248 /* PopupAdd Menu for the Add Menu */
249 void StandardPLPanel::popupAdd()
252 if( currentRootId == THEPL->p_local_category->i_id ||
253 currentRootId == THEPL->p_local_onelevel->i_id )
255 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
256 popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
257 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
259 else if( ( THEPL->p_ml_category &&
260 currentRootId == THEPL->p_ml_category->i_id ) ||
261 ( THEPL->p_ml_onelevel &&
262 currentRootId == THEPL->p_ml_onelevel->i_id ) )
264 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
265 popup.addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
266 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
268 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
269 + QPoint( 0, addButton->height() ) );
272 void StandardPLPanel::popupSelectColumn( QPoint pos )
274 ContextUpdateMapper = new QSignalMapper(this);
278 #define ADD_META_ACTION( meta ) { \
279 QAction* option = selectColMenu.addAction( qfu( psz_column_title( meta ) ) ); \
280 option->setCheckable( true ); \
281 option->setChecked( model->shownFlags() & meta ); \
282 ContextUpdateMapper->setMapping( option, meta ); \
283 CONNECT( option, triggered(), ContextUpdateMapper, map() ); \
286 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
288 ADD_META_ACTION( COLUMN_NUMBER );
289 ADD_META_ACTION( COLUMN_TITLE );
290 ADD_META_ACTION( COLUMN_DURATION );
291 ADD_META_ACTION( COLUMN_ARTIST );
292 ADD_META_ACTION( COLUMN_GENRE );
293 ADD_META_ACTION( COLUMN_ALBUM );
294 ADD_META_ACTION( COLUMN_TRACK_NUMBER );
295 ADD_META_ACTION( COLUMN_DESCRIPTION );
297 #undef ADD_META_ACTION
299 selectColMenu.exec( QCursor::pos() );
302 /* ClearFilter LineEdit */
303 void StandardPLPanel::clearFilter()
305 searchLine->setText( "" );
308 /* Search in the playlist */
309 void StandardPLPanel::search( QString searchText )
311 model->search( searchText );
314 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
316 if( !index.isValid() ) return;
317 QItemSelectionModel *selection = view->selectionModel();
318 QModelIndexList list = selection->selectedIndexes();
319 model->popup( index, point, list );
322 /* Set the root of the new Playlist */
323 /* This activated by the selector selection */
324 void StandardPLPanel::setRoot( int i_root_id )
327 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
330 p_item = playlist_GetPreferredNode( THEPL, p_item );
334 model->rebuild( p_item );
337 void StandardPLPanel::removeItem( int i_id )
339 model->removeItem( i_id );
342 /* Delete and Suppr key remove the selection
343 FilterKey function and code function */
344 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
355 void StandardPLPanel::deleteSelection()
357 QItemSelectionModel *selection = view->selectionModel();
358 QModelIndexList list = selection->selectedIndexes();
359 model->doDelete( list );
362 StandardPLPanel::~StandardPLPanel()
365 getSettings()->beginGroup("playlistdialog");
366 getSettings()->setValue( "headerState", view->header()->saveState() );
367 getSettings()->endGroup();