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