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