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( playlist_item_t * ),
111 this, setCurrentRootId( playlist_item_t * ) );
113 /* Buttons configuration */
114 QHBoxLayout *buttons = new QHBoxLayout;
116 /* Add item to the playlist button */
117 addButton = new QPushButton;
118 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
119 addButton->setMaximumWidth( 30 );
120 BUTTONACT( addButton, popupAdd() );
121 buttons->addWidget( addButton );
123 /* Random 2-state button */
124 randomButton = new QPushButton( this );
125 randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
126 randomButton->setToolTip( qtr( I_PL_RANDOM ));
127 randomButton->setCheckable( true );
128 randomButton->setChecked( model->hasRandom() );
129 BUTTONACT( randomButton, toggleRandom() );
130 buttons->addWidget( randomButton );
132 /* Repeat 3-state button */
133 repeatButton = new QPushButton( this );
134 repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
135 repeatButton->setCheckable( true );
137 if( model->hasRepeat() )
139 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
140 repeatButton->setChecked( true );
142 else if( model->hasLoop() )
144 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
145 repeatButton->setChecked( true );
149 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
150 repeatButton->setChecked( false );
152 BUTTONACT( repeatButton, toggleRepeat() );
153 buttons->addWidget( repeatButton );
156 gotoPlayingButton = new QPushButton;
157 BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
158 qtr( "Show the current item" ), gotoPlayingItem() );
159 buttons->addWidget( gotoPlayingButton );
161 /* A Spacer and the search possibilities */
162 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
163 buttons->addItem( spacer );
165 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
166 buttons->addWidget( filter );
168 SearchLineEdit *search = new SearchLineEdit( this );
169 buttons->addWidget( search );
170 filter->setBuddy( search );
171 CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
173 /* Finish the layout */
174 layout->addWidget( view );
175 layout->addLayout( buttons );
176 // layout->addWidget( bar );
179 selectColumnsSigMapper = new QSignalMapper( this );
180 CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
183 /* Function to toggle between the Repeat states */
184 void StandardPLPanel::toggleRepeat()
186 if( model->hasRepeat() )
188 model->setRepeat( false ); model->setLoop( true );
189 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
190 repeatButton->setChecked( true );
192 else if( model->hasLoop() )
194 model->setRepeat( false ) ; model->setLoop( false );
195 repeatButton->setChecked( false );
196 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
200 model->setRepeat( true ); model->setLoop( false );
201 repeatButton->setChecked( true );
202 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
206 /* Function to toggle between the Random states */
207 void StandardPLPanel::toggleRandom()
209 bool prev = model->hasRandom();
210 model->setRandom( !prev );
213 void StandardPLPanel::gotoPlayingItem()
215 view->scrollTo( model->currentIndex() );
218 void StandardPLPanel::handleExpansion( const QModelIndex& index )
220 view->scrollTo( index );
223 void StandardPLPanel::setCurrentRootId( playlist_item_t *p_item )
225 if( p_item == THEPL->p_local_category ||
226 p_item == THEPL->p_local_onelevel )
228 addButton->setEnabled( true );
229 addButton->setToolTip( qtr(I_PL_ADDPL) );
231 else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
232 ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
234 addButton->setEnabled( true );
235 addButton->setToolTip( qtr(I_PL_ADDML) );
238 addButton->setEnabled( false );
241 /* PopupAdd Menu for the Add Menu */
242 void StandardPLPanel::popupAdd()
245 if( currentRootId == THEPL->p_local_category->i_id ||
246 currentRootId == THEPL->p_local_onelevel->i_id )
248 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
249 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
250 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
252 else if( ( THEPL->p_ml_category &&
253 currentRootId == THEPL->p_ml_category->i_id ) ||
254 ( THEPL->p_ml_onelevel &&
255 currentRootId == THEPL->p_ml_onelevel->i_id ) )
257 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
258 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
259 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
262 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
263 + QPoint( 0, addButton->height() ) );
266 void StandardPLPanel::popupSelectColumn( QPoint pos )
270 /* We do not offer the option to hide index 0 column, or
271 * QTreeView will behave weird */
273 for( i = 1 << 1, j = 1; 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 view->setColumnHidden( i, !view->isColumnHidden( i ) );
290 /* Search in the playlist */
291 void StandardPLPanel::search( const QString& searchText )
293 model->search( searchText );
296 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
298 QItemSelectionModel *selection = view->selectionModel();
299 QModelIndexList list = selection->selectedIndexes();
300 model->popup( index, point, list );
303 /* Set the root of the new Playlist */
304 /* This activated by the selector selection */
305 void StandardPLPanel::setRoot( int i_root_id )
308 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
310 p_item = playlist_GetPreferredNode( THEPL, p_item );
315 void StandardPLPanel::setRoot( playlist_item_t *p_item )
318 p_item = playlist_GetPreferredNode( THEPL, p_item );
322 model->rebuild( p_item );
325 void StandardPLPanel::removeItem( int i_id )
327 model->removeItem( i_id );
330 /* Delete and Suppr key remove the selection
331 FilterKey function and code function */
332 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
343 void StandardPLPanel::deleteSelection()
345 QItemSelectionModel *selection = view->selectionModel();
346 QModelIndexList list = selection->selectedIndexes();
347 model->doDelete( list );
350 StandardPLPanel::~StandardPLPanel()
352 getSettings()->beginGroup("Playlist");
353 getSettings()->setValue( "headerState", view->header()->saveState() );
354 getSettings()->endGroup();