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() );
86 /* Configure the size of the header */
87 view->header()->resizeSection( 0, 200 );
88 view->header()->resizeSection( 1, 80 );
90 view->header()->setSortIndicatorShown( true );
91 view->header()->setClickable( true );
92 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
93 getSettings()->endGroup();
95 /* Connections for the TreeView */
96 CONNECT( view, activated( const QModelIndex& ) ,
97 model,activateItem( const QModelIndex& ) );
98 CONNECT( view, rightClicked( QModelIndex , QPoint ),
99 this, doPopup( QModelIndex, QPoint ) );
100 CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
101 this, popupSelectColumn( QPoint ) );
102 CONNECT( model, currentChanged( const QModelIndex& ),
103 this, handleExpansion( const QModelIndex& ) );
104 CONNECT( model, columnsChanged( int ),
105 this, checkSortingIndicator( int ) );
108 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
110 /* Buttons configuration */
111 QHBoxLayout *buttons = new QHBoxLayout;
113 /* Add item to the playlist button */
114 addButton = new QPushButton;
115 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
116 addButton->setMaximumWidth( 30 );
117 BUTTONACT( addButton, popupAdd() );
118 buttons->addWidget( addButton );
120 /* Random 2-state button */
121 randomButton = new QPushButton( this );
122 randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
123 randomButton->setToolTip( qtr( I_PL_RANDOM ));
124 randomButton->setCheckable( true );
125 randomButton->setChecked( model->hasRandom() );
126 BUTTONACT( randomButton, toggleRandom() );
127 buttons->addWidget( randomButton );
129 /* Repeat 3-state button */
130 repeatButton = new QPushButton( this );
131 repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
132 repeatButton->setCheckable( true );
134 if( model->hasRepeat() )
136 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
137 repeatButton->setChecked( true );
139 else if( model->hasLoop() )
141 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
142 repeatButton->setChecked( true );
146 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
147 repeatButton->setChecked( false );
149 BUTTONACT( repeatButton, toggleRepeat() );
150 buttons->addWidget( repeatButton );
153 gotoPlayingButton = new QPushButton;
154 BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/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 SearchLineEdit *search = new SearchLineEdit( this );
166 buttons->addWidget( search );
167 filter->setBuddy( search );
168 CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
170 /* Finish the layout */
171 layout->addWidget( view );
172 layout->addLayout( buttons );
173 // layout->addWidget( bar );
177 /* Function to toggle between the Repeat states */
178 void StandardPLPanel::toggleRepeat()
180 if( model->hasRepeat() )
182 model->setRepeat( false ); model->setLoop( true );
183 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
184 repeatButton->setChecked( true );
186 else if( model->hasLoop() )
188 model->setRepeat( false ) ; model->setLoop( false );
189 repeatButton->setChecked( false );
190 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
194 model->setRepeat( true ); model->setLoop( false );
195 repeatButton->setChecked( true );
196 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
200 /* Function to toggle between the Random states */
201 void StandardPLPanel::toggleRandom()
203 bool prev = model->hasRandom();
204 model->setRandom( !prev );
207 void StandardPLPanel::gotoPlayingItem()
209 view->scrollTo( view->currentIndex() );
212 void StandardPLPanel::handleExpansion( const QModelIndex& index )
214 view->scrollTo( index );
217 void StandardPLPanel::setCurrentRootId( int _new )
219 currentRootId = _new;
220 if( currentRootId == THEPL->p_local_category->i_id ||
221 currentRootId == THEPL->p_local_onelevel->i_id )
223 addButton->setEnabled( true );
224 addButton->setToolTip( qtr(I_PL_ADDPL) );
226 else if( ( THEPL->p_ml_category &&
227 currentRootId == THEPL->p_ml_category->i_id ) ||
228 ( THEPL->p_ml_onelevel &&
229 currentRootId == THEPL->p_ml_onelevel->i_id ) )
231 addButton->setEnabled( true );
232 addButton->setToolTip( qtr(I_PL_ADDML) );
235 addButton->setEnabled( false );
238 /* PopupAdd Menu for the Add Menu */
239 void StandardPLPanel::popupAdd()
242 if( currentRootId == THEPL->p_local_category->i_id ||
243 currentRootId == THEPL->p_local_onelevel->i_id )
245 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
246 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
247 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
249 else if( ( THEPL->p_ml_category &&
250 currentRootId == THEPL->p_ml_category->i_id ) ||
251 ( THEPL->p_ml_onelevel &&
252 currentRootId == THEPL->p_ml_onelevel->i_id ) )
254 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
255 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
256 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
259 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
260 + QPoint( 0, addButton->height() ) );
263 /* Set sortingindicator to -1 if it's on column thats removed,
264 * else check that it's still showing on correct column
266 void StandardPLPanel::checkSortingIndicator( int meta )
270 if( view->header()->isSortIndicatorShown() == false )
273 int sortIndex = view->header()->sortIndicatorSection();
274 if( sortIndex < 0 || sortIndex > view->header()->count() || meta == 0 )
281 if( _meta & model->shownFlags() )
287 if( model->shownFlags() & meta )
289 /* If column is added before sortIndex, move it one to right*/
290 if( sortIndex >= index )
296 if( sortIndex == index )
299 } else if( sortIndex > index )
301 /* Move indicator left one step*/
305 view->header()->setSortIndicator( sortIndex ,
306 view->header()->sortIndicatorOrder() );
309 void StandardPLPanel::popupSelectColumn( QPoint pos )
311 ContextUpdateMapper = new QSignalMapper(this);
316 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
318 QAction* option = selectColMenu.addAction(
319 qfu( psz_column_title( i_column ) ) );
320 option->setCheckable( true );
321 option->setChecked( model->shownFlags() & i_column );
322 ContextUpdateMapper->setMapping( option, i_column );
323 CONNECT( option, triggered(), ContextUpdateMapper, map() );
326 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
328 selectColMenu.exec( QCursor::pos() );
331 /* Search in the playlist */
332 void StandardPLPanel::search( const QString& searchText )
334 model->search( searchText );
337 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
339 if( !index.isValid() ) return;
340 QItemSelectionModel *selection = view->selectionModel();
341 QModelIndexList list = selection->selectedIndexes();
342 model->popup( index, point, list );
345 /* Set the root of the new Playlist */
346 /* This activated by the selector selection */
347 void StandardPLPanel::setRoot( int i_root_id )
350 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
352 p_item = playlist_GetPreferredNode( THEPL, p_item );
356 model->rebuild( p_item );
359 void StandardPLPanel::removeItem( int i_id )
361 model->removeItem( i_id );
364 /* Delete and Suppr key remove the selection
365 FilterKey function and code function */
366 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
377 void StandardPLPanel::deleteSelection()
379 QItemSelectionModel *selection = view->selectionModel();
380 QModelIndexList list = selection->selectedIndexes();
381 model->doDelete( list );
384 StandardPLPanel::~StandardPLPanel()
386 getSettings()->beginGroup("Playlist");
387 getSettings()->setValue( "headerState", view->header()->saveState() );
388 getSettings()->endGroup();