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 *****************************************************************************/
29 #include "dialogs_provider.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/panels.hpp"
33 #include "util/customwidgets.hpp"
35 #include <vlc_intf_strings.h>
37 #include <QPushButton>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
40 #include <QHeaderView>
42 #include <QModelIndexList>
44 #include <QSpacerItem>
46 #include <QSignalMapper>
51 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
52 intf_thread_t *_p_intf,
53 playlist_t *p_playlist,
54 playlist_item_t *p_root ):
55 PLPanel( _parent, _p_intf )
57 model = new PLModel( p_playlist, p_intf, p_root, -1, this );
59 QVBoxLayout *layout = new QVBoxLayout();
60 layout->setSpacing( 0 ); layout->setMargin( 0 );
62 /* Create and configure the QTreeView */
63 view = new QVLCTreeView;
64 view->setModel( model );
65 view->setIconSize( QSize( 20, 20 ) );
66 view->setAlternatingRowColors( true );
67 view->setAnimated( true );
68 view->setSelectionBehavior( QAbstractItemView::SelectRows );
69 view->setSelectionMode( QAbstractItemView::ExtendedSelection );
70 view->setDragEnabled( true );
71 view->setAcceptDrops( true );
72 view->setDropIndicatorShown( true );
73 view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
74 view->setUniformRowHeights( true );
75 view->setSortingEnabled( true );
78 getSettings()->beginGroup("Playlist");
79 if( getSettings()->contains( "headerState" ) )
81 view->header()->restoreState(
82 getSettings()->value( "headerState" ).toByteArray() );
87 for( m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
89 view->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
90 if( m == COLUMN_TITLE ) view->header()->resizeSection( c, 200 );
91 else if( m == COLUMN_DURATION ) view->header()->resizeSection( c, 80 );
94 view->header()->setSortIndicatorShown( true );
95 view->header()->setClickable( true );
96 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
97 getSettings()->endGroup();
99 /* Connections for the TreeView */
100 CONNECT( view, activated( const QModelIndex& ) ,
101 model,activateItem( const QModelIndex& ) );
102 CONNECT( view, rightClicked( QModelIndex , QPoint ),
103 this, doPopup( QModelIndex, QPoint ) );
104 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
105 this, popupSelectColumn( QPoint ) );
106 CONNECT( model, currentChanged( const QModelIndex& ),
107 this, handleExpansion( const QModelIndex& ) );
110 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
112 /* Buttons configuration */
113 QHBoxLayout *buttons = new QHBoxLayout;
115 /* Add item to the playlist button */
116 addButton = new QPushButton;
117 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
118 addButton->setMaximumWidth( 30 );
119 BUTTONACT( addButton, popupAdd() );
120 buttons->addWidget( addButton );
122 /* Random 2-state button */
123 randomButton = new QPushButton( this );
124 randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
125 randomButton->setToolTip( qtr( I_PL_RANDOM ));
126 randomButton->setCheckable( true );
127 randomButton->setChecked( model->hasRandom() );
128 BUTTONACT( randomButton, toggleRandom() );
129 buttons->addWidget( randomButton );
131 /* Repeat 3-state button */
132 repeatButton = new QPushButton( this );
133 repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
134 repeatButton->setCheckable( true );
136 if( model->hasRepeat() )
138 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
139 repeatButton->setChecked( true );
141 else if( model->hasLoop() )
143 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
144 repeatButton->setChecked( true );
148 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
149 repeatButton->setChecked( false );
151 BUTTONACT( repeatButton, toggleRepeat() );
152 buttons->addWidget( repeatButton );
155 gotoPlayingButton = new QPushButton;
156 BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
157 qtr( "Show the current item" ), gotoPlayingItem() );
158 buttons->addWidget( gotoPlayingButton );
160 /* A Spacer and the search possibilities */
161 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
162 buttons->addItem( spacer );
164 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
165 buttons->addWidget( filter );
167 SearchLineEdit *search = new SearchLineEdit( this );
168 buttons->addWidget( search );
169 filter->setBuddy( search );
170 CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
172 /* Finish the layout */
173 layout->addWidget( view );
174 layout->addLayout( buttons );
175 // layout->addWidget( bar );
178 selectColumnsSigMapper = new QSignalMapper( this );
179 CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
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( ":/buttons/playlist/repeat_all" ) );
189 repeatButton->setChecked( true );
191 else if( model->hasLoop() )
193 model->setRepeat( false ) ; model->setLoop( false );
194 repeatButton->setChecked( false );
195 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
199 model->setRepeat( true ); model->setLoop( false );
200 repeatButton->setChecked( true );
201 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
205 /* Function to toggle between the Random states */
206 void StandardPLPanel::toggleRandom()
208 bool prev = model->hasRandom();
209 model->setRandom( !prev );
212 void StandardPLPanel::gotoPlayingItem()
214 view->scrollTo( model->currentIndex() );
217 void StandardPLPanel::handleExpansion( const QModelIndex& index )
219 view->scrollTo( index );
222 void StandardPLPanel::setCurrentRootId( int _new )
224 currentRootId = _new;
225 if( currentRootId == THEPL->p_local_category->i_id ||
226 currentRootId == THEPL->p_local_onelevel->i_id )
228 addButton->setEnabled( true );
229 addButton->setToolTip( qtr(I_PL_ADDPL) );
231 else if( ( THEPL->p_ml_category &&
232 currentRootId == THEPL->p_ml_category->i_id ) ||
233 ( THEPL->p_ml_onelevel &&
234 currentRootId == THEPL->p_ml_onelevel->i_id ) )
236 addButton->setEnabled( true );
237 addButton->setToolTip( qtr(I_PL_ADDML) );
240 addButton->setEnabled( false );
243 /* PopupAdd Menu for the Add Menu */
244 void StandardPLPanel::popupAdd()
247 if( currentRootId == THEPL->p_local_category->i_id ||
248 currentRootId == THEPL->p_local_onelevel->i_id )
250 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
251 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
252 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
254 else if( ( THEPL->p_ml_category &&
255 currentRootId == THEPL->p_ml_category->i_id ) ||
256 ( THEPL->p_ml_onelevel &&
257 currentRootId == THEPL->p_ml_onelevel->i_id ) )
259 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
260 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
261 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
264 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
265 + QPoint( 0, addButton->height() ) );
268 void StandardPLPanel::popupSelectColumn( QPoint pos )
273 for( i = 1, j = 0; i < COLUMN_END; i <<= 1, j++ )
275 QAction* option = menu.addAction(
276 qfu( psz_column_title( i ) ) );
277 option->setCheckable( true );
278 option->setChecked( !view->isColumnHidden( j ) );
279 selectColumnsSigMapper->setMapping( option, j );
280 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
282 menu.exec( QCursor::pos() );
285 void StandardPLPanel::toggleColumnShown( int i )
287 if( view->isColumnHidden( i ) )
289 view->setColumnHidden( i, false );
295 for( m = 1, c = 0; m != COLUMN_END && visible < 2; m <<= 1, c++ )
296 if( !view->isColumnHidden( c ) ) visible++;
297 if( visible < 2 ) return;
298 view->setColumnHidden( i, true );
302 /* Search in the playlist */
303 void StandardPLPanel::search( const QString& searchText )
305 model->search( searchText );
308 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
310 QItemSelectionModel *selection = view->selectionModel();
311 QModelIndexList list = selection->selectedIndexes();
312 model->popup( index, point, list );
315 /* Set the root of the new Playlist */
316 /* This activated by the selector selection */
317 void StandardPLPanel::setRoot( int i_root_id )
320 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
322 p_item = playlist_GetPreferredNode( THEPL, p_item );
326 model->rebuild( p_item );
329 void StandardPLPanel::removeItem( int i_id )
331 model->removeItem( i_id );
334 /* Delete and Suppr key remove the selection
335 FilterKey function and code function */
336 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
347 void StandardPLPanel::deleteSelection()
349 QItemSelectionModel *selection = view->selectionModel();
350 QModelIndexList list = selection->selectedIndexes();
351 model->doDelete( list );
354 StandardPLPanel::~StandardPLPanel()
356 getSettings()->beginGroup("Playlist");
357 getSettings()->setValue( "headerState", view->header()->saveState() );
358 getSettings()->endGroup();