]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: connect currentChanged signal from model earlier
[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 "dialogs_provider.hpp"
30
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/standardpanel.hpp"
33 #include "components/playlist/icon_view.hpp"
34 #include "util/customwidgets.hpp"
35
36 #include <vlc_intf_strings.h>
37
38 #include <QPushButton>
39 #include <QHeaderView>
40 #include <QKeyEvent>
41 #include <QModelIndexList>
42 #include <QLabel>
43 #include <QMenu>
44 #include <QSignalMapper>
45 #include <QWheelEvent>
46
47 #include <assert.h>
48
49 #include "sorting.h"
50
51 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
52                                   intf_thread_t *_p_intf,
53                                   playlist_t *p_playlist,
54                                   playlist_item_t *p_root ):
55                                   QWidget( _parent ), p_intf( _p_intf )
56 {
57     layout = new QGridLayout( this );
58     layout->setSpacing( 0 ); layout->setMargin( 0 );
59     setMinimumWidth( 300 );
60
61     model = new PLModel( p_playlist, p_intf, p_root, this );
62     CONNECT( model, currentChanged( const QModelIndex& ),
63              this, handleExpansion( const QModelIndex& ) );
64
65     iconView = NULL;
66     treeView = NULL;
67
68     /* Saved Settings */
69     getSettings()->beginGroup("Playlist");
70
71     int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
72     if( i_viewMode == ICON_VIEW )
73     {
74         createIconView();
75         currentView = iconView;
76     }
77     else
78     {
79         createTreeView();
80         currentView = treeView;
81     }
82
83     getSettings()->endGroup();
84
85     currentRootId = -1;
86
87     /* Title label */
88     title = new QLabel;
89     QFont titleFont;
90     titleFont.setPointSize( titleFont.pointSize() + 6 );
91     titleFont.setFamily( "Verdana" );
92     title->setFont( titleFont );
93     layout->addWidget( title, 0, 0 );
94
95     /* A Spacer and the search possibilities */
96     layout->setColumnStretch( 1, 10 );
97
98     SearchLineEdit *search = new SearchLineEdit( this );
99     search->setMaximumWidth( 300 );
100     layout->addWidget( search, 0, 4 );
101     CONNECT( search, textChanged( const QString& ),
102              this, search( const QString& ) );
103     layout->setColumnStretch( 4, 2 );
104
105     /* Add item to the playlist button */
106     addButton = new QPushButton;
107     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
108     addButton->setMaximumWidth( 30 );
109     BUTTONACT( addButton, popupAdd() );
110     layout->addWidget( addButton, 0, 3 );
111
112     QPushButton *viewButton = new QPushButton( this );
113     viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
114     layout->addWidget( viewButton, 0, 2 );
115     BUTTONACT( viewButton, toggleView() );
116 }
117
118 StandardPLPanel::~StandardPLPanel()
119 {
120     getSettings()->beginGroup("Playlist");
121     if( treeView )
122         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
123     getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
124     getSettings()->endGroup();
125 }
126
127 /* Unused anymore, but might be useful, like in right-click menu */
128 void StandardPLPanel::gotoPlayingItem()
129 {
130     currentView->scrollTo( model->currentIndex() );
131 }
132
133 void StandardPLPanel::handleExpansion( const QModelIndex& index )
134 {
135     assert( currentView );
136     currentView->scrollTo( index );
137 }
138
139 /* PopupAdd Menu for the Add Menu */
140 void StandardPLPanel::popupAdd()
141 {
142     QMenu popup;
143     if( currentRootId == THEPL->p_local_category->i_id ||
144         currentRootId == THEPL->p_local_onelevel->i_id )
145     {
146         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
147         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
148         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
149     }
150     else if( ( THEPL->p_ml_category &&
151                 currentRootId == THEPL->p_ml_category->i_id ) ||
152              ( THEPL->p_ml_onelevel &&
153                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
154     {
155         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
156         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
157         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
158     }
159
160     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
161                         + QPoint( 0, addButton->height() ) );
162 }
163
164 void StandardPLPanel::popupPlView( const QPoint &point )
165 {
166     QModelIndex index = currentView->indexAt( point );
167     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
168     QItemSelectionModel *selection = currentView->selectionModel();
169     QModelIndexList list = selection->selectedIndexes();
170     model->popup( index, globalPoint, list );
171 }
172
173 void StandardPLPanel::popupSelectColumn( QPoint pos )
174 {
175     QMenu menu;
176     assert( treeView );
177
178     /* We do not offer the option to hide index 0 column, or
179     * QTreeView will behave weird */
180     int i, j;
181     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
182     {
183         QAction* option = menu.addAction(
184             qfu( psz_column_title( i ) ) );
185         option->setCheckable( true );
186         option->setChecked( !treeView->isColumnHidden( j ) );
187         selectColumnsSigMapper->setMapping( option, j );
188         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
189     }
190     menu.exec( QCursor::pos() );
191 }
192
193 void StandardPLPanel::toggleColumnShown( int i )
194 {
195     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
196 }
197
198 /* Search in the playlist */
199 void StandardPLPanel::search( const QString& searchText )
200 {
201     model->search( searchText );
202 }
203
204 /* Set the root of the new Playlist */
205 /* This activated by the selector selection */
206 void StandardPLPanel::setRoot( playlist_item_t *p_item )
207 {
208     QPL_LOCK;
209     assert( p_item );
210
211     playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
212     if( p_pref_item ) p_item = p_pref_item;
213
214     /* needed for popupAdd() */
215     currentRootId = p_item->i_id;
216
217     /* cosmetics, ..still need playlist locking.. */
218     char *psz_title = input_item_GetName( p_item->p_input );
219     title->setText( qfu(psz_title) );
220     free( psz_title );
221
222     QPL_UNLOCK;
223
224     /* do THE job */
225     model->rebuild( p_item );
226
227     /* enable/disable adding */
228     if( p_item == THEPL->p_local_category ||
229         p_item == THEPL->p_local_onelevel )
230     {
231         addButton->setEnabled( true );
232         addButton->setToolTip( qtr(I_PL_ADDPL) );
233     }
234     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
235               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
236     {
237         addButton->setEnabled( true );
238         addButton->setToolTip( qtr(I_PL_ADDML) );
239     }
240     else
241         addButton->setEnabled( false );
242 }
243
244 void StandardPLPanel::removeItem( int i_id )
245 {
246     model->removeItem( i_id );
247 }
248
249 /* Delete and Suppr key remove the selection
250    FilterKey function and code function */
251 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
252 {
253     switch( e->key() )
254     {
255     case Qt::Key_Back:
256     case Qt::Key_Delete:
257         deleteSelection();
258         break;
259     }
260 }
261
262 void StandardPLPanel::deleteSelection()
263 {
264     QItemSelectionModel *selection = currentView->selectionModel();
265     QModelIndexList list = selection->selectedIndexes();
266     model->doDelete( list );
267 }
268
269 void StandardPLPanel::createIconView()
270 {
271     iconView = new PlIconView( model, this );
272     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
273     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
274              this, popupPlView( const QPoint & ) );
275
276     layout->addWidget( iconView, 1, 0, 1, -1 );
277 }
278
279 void StandardPLPanel::createTreeView()
280 {
281     /* Create and configure the QTreeView */
282     treeView = new QTreeView;
283     treeView->setModel( model );
284
285     treeView->setIconSize( QSize( 20, 20 ) );
286     treeView->setAlternatingRowColors( true );
287     treeView->setAnimated( true );
288     treeView->setUniformRowHeights( true );
289     treeView->setSortingEnabled( true );
290     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
291     treeView->header()->setSortIndicatorShown( true );
292     treeView->header()->setClickable( true );
293     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
294
295     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
296     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
297     treeView->setDragEnabled( true );
298     treeView->setAcceptDrops( true );
299     treeView->setDropIndicatorShown( true );
300     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
301
302     if( getSettings()->contains( "headerStateV2" ) )
303     {
304         treeView->header()->restoreState(
305                 getSettings()->value( "headerStateV2" ).toByteArray() );
306     }
307     else
308     {
309         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
310         {
311             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
312             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
313             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
314         }
315     }
316
317     /* Connections for the TreeView */
318     CONNECT( treeView, activated( const QModelIndex& ),
319              model,activateItem( const QModelIndex& ) );
320     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
321              this, popupSelectColumn( QPoint ) );
322     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
323              this, popupPlView( const QPoint & ) );
324
325     /* SignalMapper for columns */
326     selectColumnsSigMapper = new QSignalMapper( this );
327     CONNECT( selectColumnsSigMapper, mapped( int ),
328              this, toggleColumnShown( int ) );
329
330     /* Finish the layout */
331     layout->addWidget( treeView, 1, 0, 1, -1 );
332 }
333
334 void StandardPLPanel::toggleView()
335 {
336     if( treeView && treeView->isVisible() )
337     {
338         if( iconView == NULL )
339             createIconView();
340
341         treeView->hide();
342         iconView->show();
343         currentView = iconView;
344     }
345     else
346     {
347         if( treeView == NULL )
348             createTreeView();
349
350         iconView->hide();
351         treeView->show();
352         currentView = treeView;
353     }
354 }
355
356 void StandardPLPanel::wheelEvent( QWheelEvent *e )
357 {
358     // Accept this event in order to prevent unwanted volume up/down changes
359     e->accept();
360 }