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->setSortingEnabled( true );
77 getSettings()->beginGroup("Playlist");
78 if( getSettings()->contains( "headerState" ) )
80 view->header()->restoreState(
81 getSettings()->value( "headerState" ).toByteArray() );
85 /* Configure the size of the header */
86 view->header()->resizeSection( 0, 200 );
87 view->header()->resizeSection( 1, 80 );
89 view->header()->setSortIndicatorShown( true );
90 view->header()->setClickable( true );
91 view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
92 getSettings()->endGroup();
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( view->header(), customContextMenuRequested( const QPoint & ),
100 this, popupSelectColumn( QPoint ) );
101 CONNECT( model, currentChanged( const QModelIndex& ),
102 this, handleExpansion( const QModelIndex& ) );
103 CONNECT( model, columnsChanged( int ),
104 this, checkSortingIndicator( int ) );
107 CONNECT( parent, rootChanged( int ), this, setCurrentRootId( int ) );
109 /* Buttons configuration */
110 QHBoxLayout *buttons = new QHBoxLayout;
112 /* Add item to the playlist button */
113 addButton = new QPushButton;
114 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
115 addButton->setMaximumWidth( 30 );
116 BUTTONACT( addButton, popupAdd() );
117 buttons->addWidget( addButton );
119 /* Random 2-state button */
120 randomButton = new QPushButton( this );
121 randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
122 randomButton->setToolTip( qtr( I_PL_RANDOM ));
123 randomButton->setCheckable( true );
124 randomButton->setChecked( model->hasRandom() );
125 BUTTONACT( randomButton, toggleRandom() );
126 buttons->addWidget( randomButton );
128 /* Repeat 3-state button */
129 repeatButton = new QPushButton( this );
130 repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
131 repeatButton->setCheckable( true );
133 if( model->hasRepeat() )
135 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
136 repeatButton->setChecked( true );
138 else if( model->hasLoop() )
140 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
141 repeatButton->setChecked( true );
145 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
146 repeatButton->setChecked( false );
148 BUTTONACT( repeatButton, toggleRepeat() );
149 buttons->addWidget( repeatButton );
152 gotoPlayingButton = new QPushButton;
153 BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
154 qtr( "Show the current item" ), gotoPlayingItem() );
155 buttons->addWidget( gotoPlayingButton );
157 /* A Spacer and the search possibilities */
158 QSpacerItem *spacer = new QSpacerItem( 10, 20 );
159 buttons->addItem( spacer );
161 QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
162 buttons->addWidget( filter );
164 SearchLineEdit *search = new SearchLineEdit( this );
165 buttons->addWidget( search );
166 filter->setBuddy( search );
167 CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
169 /* Finish the layout */
170 layout->addWidget( view );
171 layout->addLayout( buttons );
172 // layout->addWidget( bar );
176 /* Function to toggle between the Repeat states */
177 void StandardPLPanel::toggleRepeat()
179 if( model->hasRepeat() )
181 model->setRepeat( false ); model->setLoop( true );
182 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
183 repeatButton->setChecked( true );
185 else if( model->hasLoop() )
187 model->setRepeat( false ) ; model->setLoop( false );
188 repeatButton->setChecked( false );
189 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
193 model->setRepeat( true ); model->setLoop( false );
194 repeatButton->setChecked( true );
195 repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
199 /* Function to toggle between the Random states */
200 void StandardPLPanel::toggleRandom()
202 bool prev = model->hasRandom();
203 model->setRandom( !prev );
206 void StandardPLPanel::gotoPlayingItem()
208 view->scrollTo( view->currentIndex() );
211 void StandardPLPanel::handleExpansion( const QModelIndex& index )
213 view->scrollTo( index, QAbstractItemView::EnsureVisible );
216 void StandardPLPanel::setCurrentRootId( int _new )
218 currentRootId = _new;
219 if( currentRootId == THEPL->p_local_category->i_id ||
220 currentRootId == THEPL->p_local_onelevel->i_id )
222 addButton->setEnabled( true );
223 addButton->setToolTip( qtr(I_PL_ADDPL) );
225 else if( ( THEPL->p_ml_category &&
226 currentRootId == THEPL->p_ml_category->i_id ) ||
227 ( THEPL->p_ml_onelevel &&
228 currentRootId == THEPL->p_ml_onelevel->i_id ) )
230 addButton->setEnabled( true );
231 addButton->setToolTip( qtr(I_PL_ADDML) );
234 addButton->setEnabled( false );
237 /* PopupAdd Menu for the Add Menu */
238 void StandardPLPanel::popupAdd()
241 if( currentRootId == THEPL->p_local_category->i_id ||
242 currentRootId == THEPL->p_local_onelevel->i_id )
244 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
245 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
246 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
248 else if( ( THEPL->p_ml_category &&
249 currentRootId == THEPL->p_ml_category->i_id ) ||
250 ( THEPL->p_ml_onelevel &&
251 currentRootId == THEPL->p_ml_onelevel->i_id ) )
253 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
254 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
255 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
258 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
259 + QPoint( 0, addButton->height() ) );
262 /* Set sortingindicator to -1 if it's on column thats removed,
263 * else check that it's still showing on correct column
265 void StandardPLPanel::checkSortingIndicator( int meta )
269 if( view->header()->isSortIndicatorShown() == false )
272 int sortIndex = view->header()->sortIndicatorSection();
273 if( sortIndex < 0 || sortIndex > view->header()->count() || meta == 0 )
280 if( _meta & model->shownFlags() )
286 if( model->shownFlags() & meta )
288 /* If column is added before sortIndex, move it one to right*/
289 if( sortIndex >= index )
295 if( sortIndex == index )
298 } else if( sortIndex > index )
300 /* Move indicator left one step*/
304 view->header()->setSortIndicator( sortIndex ,
305 view->header()->sortIndicatorOrder() );
308 void StandardPLPanel::popupSelectColumn( QPoint pos )
310 ContextUpdateMapper = new QSignalMapper(this);
315 for( i_column = 1; i_column != COLUMN_END; i_column<<=1 )
317 QAction* option = selectColMenu.addAction(
318 qfu( psz_column_title( i_column ) ) );
319 option->setCheckable( true );
320 option->setChecked( model->shownFlags() & i_column );
321 ContextUpdateMapper->setMapping( option, i_column );
322 CONNECT( option, triggered(), ContextUpdateMapper, map() );
325 CONNECT( ContextUpdateMapper, mapped( int ), model, viewchanged( int ) );
327 selectColMenu.exec( QCursor::pos() );
330 /* Search in the playlist */
331 void StandardPLPanel::search( const QString& searchText )
333 model->search( searchText );
336 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
338 if( !index.isValid() ) return;
339 QItemSelectionModel *selection = view->selectionModel();
340 QModelIndexList list = selection->selectedIndexes();
341 model->popup( index, point, list );
344 /* Set the root of the new Playlist */
345 /* This activated by the selector selection */
346 void StandardPLPanel::setRoot( int i_root_id )
349 playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
351 p_item = playlist_GetPreferredNode( THEPL, p_item );
355 model->rebuild( p_item );
358 void StandardPLPanel::removeItem( int i_id )
360 model->removeItem( i_id );
363 /* Delete and Suppr key remove the selection
364 FilterKey function and code function */
365 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
376 void StandardPLPanel::deleteSelection()
378 QItemSelectionModel *selection = view->selectionModel();
379 QModelIndexList list = selection->selectedIndexes();
380 model->doDelete( list );
383 StandardPLPanel::~StandardPLPanel()
385 getSettings()->beginGroup("Playlist");
386 getSettings()->setValue( "headerState", view->header()->saveState() );
387 getSettings()->endGroup();