]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: make playlist tree-view popup menu work again
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2009 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          JB Kempf <jb@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "qt4.hpp"
30 #include "dialogs_provider.hpp"
31
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"
36
37 #include <vlc_intf_strings.h>
38
39 #include <QPushButton>
40 #include <QHeaderView>
41 #include <QKeyEvent>
42 #include <QModelIndexList>
43 #include <QLabel>
44 #include <QMenu>
45 #include <QSignalMapper>
46 #include <QWheelEvent>
47
48 #include <assert.h>
49
50 #include "sorting.h"
51
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 )
57 {
58     layout = new QGridLayout( this );
59     layout->setSpacing( 0 ); layout->setMargin( 0 );
60     setMinimumWidth( 300 );
61
62     model = new PLModel( p_playlist, p_intf, p_root, this );
63
64     /* Create and configure the QTreeView */
65     treeView = new QTreeView;
66     treeView->setModel( model );
67     iconView = NULL;
68
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 );
78
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 );
85
86     //treeView->installEventFilter( this );
87     //<jleben> I guess we don't need that
88
89     /* Saved Settings */
90     getSettings()->beginGroup("Playlist");
91     if( getSettings()->contains( "headerStateV2" ) )
92     {
93         treeView->header()->restoreState(
94                 getSettings()->value( "headerStateV2" ).toByteArray() );
95     }
96     else
97     {
98         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
99         {
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 );
103         }
104     }
105     getSettings()->endGroup();
106
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& ) );
116
117     currentRootId = -1;
118
119     /* Title label */
120     title = new QLabel;
121     QFont titleFont;
122     titleFont.setPointSize( titleFont.pointSize() + 6 );
123     titleFont.setFamily( "Verdana" );
124     title->setFont( titleFont );
125     layout->addWidget( title, 0, 0 );
126
127     /* A Spacer and the search possibilities */
128     layout->setColumnStretch( 1, 10 );
129
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 );
136
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 );
143
144     QPushButton *viewButton = new QPushButton( this );
145     viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
146     layout->addWidget( viewButton, 0, 2 );
147     BUTTONACT( viewButton, toggleView() );
148
149     /* Finish the layout */
150     layout->addWidget( treeView, 1, 0, 1, -1 );
151
152     selectColumnsSigMapper = new QSignalMapper( this );
153     CONNECT( selectColumnsSigMapper, mapped( int ),
154              this, toggleColumnShown( int ) );
155 }
156
157 StandardPLPanel::~StandardPLPanel()
158 {
159     getSettings()->beginGroup("Playlist");
160     getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
161     getSettings()->endGroup();
162 }
163
164 /* Unused anymore, but might be useful, like in right-click menu */
165 void StandardPLPanel::gotoPlayingItem()
166 {
167     treeView->scrollTo( model->currentIndex() );
168 }
169
170 void StandardPLPanel::handleExpansion( const QModelIndex& index )
171 {
172     treeView->scrollTo( index );
173 }
174
175 /* PopupAdd Menu for the Add Menu */
176 void StandardPLPanel::popupAdd()
177 {
178     QMenu popup;
179     if( currentRootId == THEPL->p_local_category->i_id ||
180         currentRootId == THEPL->p_local_onelevel->i_id )
181     {
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()) );
185     }
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 ) )
190     {
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() ) );
194     }
195
196     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
197                         + QPoint( 0, addButton->height() ) );
198 }
199
200 void StandardPLPanel::popupSelectColumn( QPoint pos )
201 {
202     QMenu menu;
203
204     /* We do not offer the option to hide index 0 column, or
205     * QTreeView will behave weird */
206     int i, j;
207     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
208     {
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() );
215     }
216     menu.exec( QCursor::pos() );
217 }
218
219 void StandardPLPanel::treeViewPopup( const QPoint &point )
220 {
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 );
226 }
227
228 void StandardPLPanel::toggleColumnShown( int i )
229 {
230     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
231 }
232
233 /* Search in the playlist */
234 void StandardPLPanel::search( const QString& searchText )
235 {
236     model->search( searchText );
237 }
238
239 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
240 {
241     QItemSelectionModel *selection = treeView->selectionModel();
242     QModelIndexList list = selection->selectedIndexes();
243     model->popup( index, point, list );
244 }
245
246 /* Set the root of the new Playlist */
247 /* This activated by the selector selection */
248 void StandardPLPanel::setRoot( playlist_item_t *p_item )
249 {
250     QPL_LOCK;
251     assert( p_item );
252
253     playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
254     if( p_pref_item ) p_item = p_pref_item;
255
256     /* needed for popupAdd() */
257     currentRootId = p_item->i_id;
258
259     /* cosmetics, ..still need playlist locking.. */
260     char *psz_title = input_item_GetName( p_item->p_input );
261     title->setText( qfu(psz_title) );
262     free( psz_title );
263
264     QPL_UNLOCK;
265
266     /* do THE job */
267     model->rebuild( p_item );
268
269     /* enable/disable adding */
270     if( p_item == THEPL->p_local_category ||
271         p_item == THEPL->p_local_onelevel )
272     {
273         addButton->setEnabled( true );
274         addButton->setToolTip( qtr(I_PL_ADDPL) );
275     }
276     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
277               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
278     {
279         addButton->setEnabled( true );
280         addButton->setToolTip( qtr(I_PL_ADDML) );
281     }
282     else
283         addButton->setEnabled( false );
284 }
285
286 void StandardPLPanel::removeItem( int i_id )
287 {
288     model->removeItem( i_id );
289 }
290
291 /* Delete and Suppr key remove the selection
292    FilterKey function and code function */
293 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
294 {
295     switch( e->key() )
296     {
297     case Qt::Key_Back:
298     case Qt::Key_Delete:
299         deleteSelection();
300         break;
301     }
302 }
303
304 void StandardPLPanel::deleteSelection()
305 {
306     QItemSelectionModel *selection = treeView->selectionModel();
307     QModelIndexList list = selection->selectedIndexes();
308     model->doDelete( list );
309 }
310
311 void StandardPLPanel::toggleView()
312 {
313     if( treeView && treeView->isVisible() )
314     {
315         if( iconView == NULL )
316         {
317             iconView = new PlIconView( model, this );
318             layout->addWidget( iconView, 1, 0, 1, -1 );
319             installEventFilter( iconView );
320         }
321         treeView->hide();
322         iconView->show();
323     }
324     else
325     {
326         iconView->hide();
327         treeView->show();
328     }
329 }
330
331 bool StandardPLPanel::eventFilter( QObject *obj, QEvent *event )
332 {
333     QAbstractItemView *aView = qobject_cast<QAbstractItemView *>(obj);
334     if( !aView ) return false;
335
336     switch( event->type() )
337     {
338         case QEvent::MouseButtonPress:
339             {
340                 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
341                 if( mouseEvent->button() & Qt::RightButton )
342                 {
343                     QModelIndex index = aView->indexAt(
344                             QPoint( mouseEvent->x(), mouseEvent->y() ) );
345                     doPopup( index, QCursor::pos() );
346                     return true;
347                 }
348                 else if( mouseEvent->button() & Qt::LeftButton )
349                 {
350                     if( !aView->indexAt( QPoint( mouseEvent->x(),
351                                                 mouseEvent->y() ) ).isValid() )
352                         aView->clearSelection();
353                 }
354                 // aView->mousePressEvent( mouseEvent );
355             }
356             return true;
357         case QEvent::MouseButtonRelease:
358             {
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 );
363                 return true;
364             }
365         default:
366             return false;
367     }
368     return true;
369 }
370
371 void StandardPLPanel::wheelEvent( QWheelEvent *e )
372 {
373     // Accept this event in order to prevent unwanted volume up/down changes
374     e->accept();
375 }