1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright (C) 2000-2009 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * JB Kempf <jb@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include "dialogs_provider.hpp"
32 #include "components/playlist/playlist_model.hpp"
33 #include "components/playlist/standardpanel.hpp"
34 #include "components/playlist/icon_view.hpp"
35 #include "util/customwidgets.hpp"
37 #include <vlc_intf_strings.h>
39 #include <QPushButton>
40 #include <QHeaderView>
42 #include <QModelIndexList>
45 #include <QSignalMapper>
46 #include <QWheelEvent>
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53 intf_thread_t *_p_intf,
54 playlist_t *p_playlist,
55 playlist_item_t *p_root ):
56 QWidget( _parent ), p_intf( _p_intf )
58 layout = new QGridLayout( this );
59 layout->setSpacing( 0 ); layout->setMargin( 0 );
60 setMinimumWidth( 300 );
62 model = new PLModel( p_playlist, p_intf, p_root, this );
64 /* Create and configure the QTreeView */
65 treeView = new QTreeView;
66 treeView->setModel( model );
69 treeView->setIconSize( QSize( 20, 20 ) );
70 treeView->setAlternatingRowColors( true );
71 treeView->setAnimated( true );
72 treeView->setUniformRowHeights( true );
73 treeView->setSortingEnabled( true );
74 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
75 treeView->header()->setSortIndicatorShown( true );
76 treeView->header()->setClickable( true );
77 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
79 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
80 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
81 treeView->setDragEnabled( true );
82 treeView->setAcceptDrops( true );
83 treeView->setDropIndicatorShown( true );
84 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
86 //treeView->installEventFilter( this );
87 //<jleben> I guess we don't need that
90 getSettings()->beginGroup("Playlist");
91 if( getSettings()->contains( "headerStateV2" ) )
93 treeView->header()->restoreState(
94 getSettings()->value( "headerStateV2" ).toByteArray() );
98 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
100 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
101 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
102 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
105 getSettings()->endGroup();
107 /* Connections for the TreeView */
108 CONNECT( treeView, activated( const QModelIndex& ),
109 model,activateItem( const QModelIndex& ) );
110 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
111 this, popupSelectColumn( QPoint ) );
112 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
113 this, treeViewPopup( const QPoint & ) );
114 CONNECT( model, currentChanged( const QModelIndex& ),
115 this, handleExpansion( const QModelIndex& ) );
122 titleFont.setPointSize( titleFont.pointSize() + 6 );
123 titleFont.setFamily( "Verdana" );
124 title->setFont( titleFont );
125 layout->addWidget( title, 0, 0 );
127 /* A Spacer and the search possibilities */
128 layout->setColumnStretch( 1, 10 );
130 SearchLineEdit *search = new SearchLineEdit( this );
131 search->setMaximumWidth( 200 );
132 layout->addWidget( search, 0, 4 );
133 CONNECT( search, textChanged( const QString& ),
134 this, search( const QString& ) );
135 layout->setColumnStretch( 4, 1 );
137 /* Add item to the playlist button */
138 addButton = new QPushButton;
139 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
140 addButton->setMaximumWidth( 30 );
141 BUTTONACT( addButton, popupAdd() );
142 layout->addWidget( addButton, 0, 3 );
144 QPushButton *viewButton = new QPushButton( this );
145 viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
146 layout->addWidget( viewButton, 0, 2 );
147 BUTTONACT( viewButton, toggleView() );
149 /* Finish the layout */
150 layout->addWidget( treeView, 1, 0, 1, -1 );
152 selectColumnsSigMapper = new QSignalMapper( this );
153 CONNECT( selectColumnsSigMapper, mapped( int ),
154 this, toggleColumnShown( int ) );
157 StandardPLPanel::~StandardPLPanel()
159 getSettings()->beginGroup("Playlist");
160 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
161 getSettings()->endGroup();
164 /* Unused anymore, but might be useful, like in right-click menu */
165 void StandardPLPanel::gotoPlayingItem()
167 treeView->scrollTo( model->currentIndex() );
170 void StandardPLPanel::handleExpansion( const QModelIndex& index )
172 treeView->scrollTo( index );
175 /* PopupAdd Menu for the Add Menu */
176 void StandardPLPanel::popupAdd()
179 if( currentRootId == THEPL->p_local_category->i_id ||
180 currentRootId == THEPL->p_local_onelevel->i_id )
182 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
183 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
184 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
186 else if( ( THEPL->p_ml_category &&
187 currentRootId == THEPL->p_ml_category->i_id ) ||
188 ( THEPL->p_ml_onelevel &&
189 currentRootId == THEPL->p_ml_onelevel->i_id ) )
191 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
192 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
193 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
196 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
197 + QPoint( 0, addButton->height() ) );
200 void StandardPLPanel::popupSelectColumn( QPoint pos )
204 /* We do not offer the option to hide index 0 column, or
205 * QTreeView will behave weird */
207 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
209 QAction* option = menu.addAction(
210 qfu( psz_column_title( i ) ) );
211 option->setCheckable( true );
212 option->setChecked( !treeView->isColumnHidden( j ) );
213 selectColumnsSigMapper->setMapping( option, j );
214 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
216 menu.exec( QCursor::pos() );
219 void StandardPLPanel::treeViewPopup( const QPoint &point )
221 QModelIndex index = treeView->indexAt( point );
222 QPoint globalPoint = treeView->viewport()->mapToGlobal( point );
223 QItemSelectionModel *selection = treeView->selectionModel();
224 QModelIndexList list = selection->selectedIndexes();
225 model->popup( index, globalPoint, list );
228 void StandardPLPanel::toggleColumnShown( int i )
230 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
233 /* Search in the playlist */
234 void StandardPLPanel::search( const QString& searchText )
236 model->search( searchText );
239 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
241 QItemSelectionModel *selection = treeView->selectionModel();
242 QModelIndexList list = selection->selectedIndexes();
243 model->popup( index, point, list );
246 /* Set the root of the new Playlist */
247 /* This activated by the selector selection */
248 void StandardPLPanel::setRoot( playlist_item_t *p_item )
253 playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
254 if( p_pref_item ) p_item = p_pref_item;
256 /* needed for popupAdd() */
257 currentRootId = p_item->i_id;
259 /* cosmetics, ..still need playlist locking.. */
260 char *psz_title = input_item_GetName( p_item->p_input );
261 title->setText( qfu(psz_title) );
267 model->rebuild( p_item );
269 /* enable/disable adding */
270 if( p_item == THEPL->p_local_category ||
271 p_item == THEPL->p_local_onelevel )
273 addButton->setEnabled( true );
274 addButton->setToolTip( qtr(I_PL_ADDPL) );
276 else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
277 ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
279 addButton->setEnabled( true );
280 addButton->setToolTip( qtr(I_PL_ADDML) );
283 addButton->setEnabled( false );
286 void StandardPLPanel::removeItem( int i_id )
288 model->removeItem( i_id );
291 /* Delete and Suppr key remove the selection
292 FilterKey function and code function */
293 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
304 void StandardPLPanel::deleteSelection()
306 QItemSelectionModel *selection = treeView->selectionModel();
307 QModelIndexList list = selection->selectedIndexes();
308 model->doDelete( list );
311 void StandardPLPanel::toggleView()
313 if( treeView && treeView->isVisible() )
315 if( iconView == NULL )
317 iconView = new PlIconView( model, this );
318 layout->addWidget( iconView, 1, 0, 1, -1 );
319 installEventFilter( iconView );
331 bool StandardPLPanel::eventFilter( QObject *obj, QEvent *event )
333 QAbstractItemView *aView = qobject_cast<QAbstractItemView *>(obj);
334 if( !aView ) return false;
336 switch( event->type() )
338 case QEvent::MouseButtonPress:
340 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
341 if( mouseEvent->button() & Qt::RightButton )
343 QModelIndex index = aView->indexAt(
344 QPoint( mouseEvent->x(), mouseEvent->y() ) );
345 doPopup( index, QCursor::pos() );
348 else if( mouseEvent->button() & Qt::LeftButton )
350 if( !aView->indexAt( QPoint( mouseEvent->x(),
351 mouseEvent->y() ) ).isValid() )
352 aView->clearSelection();
354 // aView->mousePressEvent( mouseEvent );
357 case QEvent::MouseButtonRelease:
359 QMouseEvent *mouseEvent2 = static_cast<QMouseEvent *>(event);
360 if( mouseEvent2->button() & Qt::RightButton )
361 return false; /* Do NOT forward to QTreeView!! */
362 // aView->mouseReleaseEvent( mouseEvent );
371 void StandardPLPanel::wheelEvent( QWheelEvent *e )
373 // Accept this event in order to prevent unwanted volume up/down changes