]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt4: move the selector from View/Model to QTreeWidget
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "qt4.hpp"
29 #include "dialogs_provider.hpp"
30
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/panels.hpp"
33 #include "util/customwidgets.hpp"
34
35 #include <vlc_intf_strings.h>
36
37 #include <QPushButton>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
40 #include <QHeaderView>
41 #include <QKeyEvent>
42 #include <QModelIndexList>
43 #include <QLabel>
44 #include <QSpacerItem>
45 #include <QMenu>
46 #include <QSignalMapper>
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                                   PLPanel( _parent, _p_intf )
56 {
57     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
58
59     QVBoxLayout *layout = new QVBoxLayout();
60     layout->setSpacing( 0 ); layout->setMargin( 0 );
61
62     /* Create and configure the QTreeView */
63     view = new QVLCTreeView;
64     view->setModel( model );
65     view->setIconSize( QSize( 20, 20 ) );
66     view->setAlternatingRowColors( true );
67     view->setAnimated( true );
68     view->setSelectionBehavior( QAbstractItemView::SelectRows );
69     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
70     view->setDragEnabled( true );
71     view->setAcceptDrops( true );
72     view->setDropIndicatorShown( true );
73     view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
74     view->setUniformRowHeights( true );
75     view->setSortingEnabled( true );
76
77
78     getSettings()->beginGroup("Playlist");
79     if( getSettings()->contains( "headerState" ) )
80     {
81         view->header()->restoreState(
82                 getSettings()->value( "headerState" ).toByteArray() );
83     }
84     else
85     {
86         int m, c;
87         for( m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
88         {
89             view->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
90             if( m == COLUMN_TITLE ) view->header()->resizeSection( c, 200 );
91             else if( m == COLUMN_DURATION ) view->header()->resizeSection( c, 80 );
92         }
93     }
94     view->header()->setSortIndicatorShown( true );
95     view->header()->setClickable( true );
96     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
97     getSettings()->endGroup();
98
99     /* Connections for the TreeView */
100     CONNECT( view, activated( const QModelIndex& ) ,
101              model,activateItem( const QModelIndex& ) );
102     CONNECT( view, rightClicked( QModelIndex , QPoint ),
103              this, doPopup( QModelIndex, QPoint ) );
104     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
105              this, popupSelectColumn( QPoint ) );
106     CONNECT( model, currentChanged( const QModelIndex& ),
107              this, handleExpansion( const QModelIndex& ) );
108
109     currentRootId = -1;
110     CONNECT( parent, rootChanged( playlist_item_t * ),
111              this, setCurrentRootId( playlist_item_t * ) );
112
113     /* Buttons configuration */
114     QHBoxLayout *buttons = new QHBoxLayout;
115
116     /* Add item to the playlist button */
117     addButton = new QPushButton;
118     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
119     addButton->setMaximumWidth( 30 );
120     BUTTONACT( addButton, popupAdd() );
121     buttons->addWidget( addButton );
122
123     /* Random 2-state button */
124     randomButton = new QPushButton( this );
125     randomButton->setIcon( QIcon( ":/buttons/playlist/shuffle_on" ));
126     randomButton->setToolTip( qtr( I_PL_RANDOM ));
127     randomButton->setCheckable( true );
128     randomButton->setChecked( model->hasRandom() );
129     BUTTONACT( randomButton, toggleRandom() );
130     buttons->addWidget( randomButton );
131
132     /* Repeat 3-state button */
133     repeatButton = new QPushButton( this );
134     repeatButton->setToolTip( qtr( "Click to toggle between loop one, loop all" ) );
135     repeatButton->setCheckable( true );
136
137     if( model->hasRepeat() )
138     {
139         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
140         repeatButton->setChecked( true );
141     }
142     else if( model->hasLoop() )
143     {
144         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
145         repeatButton->setChecked( true );
146     }
147     else
148     {
149         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
150         repeatButton->setChecked( false );
151     }
152     BUTTONACT( repeatButton, toggleRepeat() );
153     buttons->addWidget( repeatButton );
154
155     /* Goto */
156     gotoPlayingButton = new QPushButton;
157     BUTTON_SET_ACT_I( gotoPlayingButton, "", buttons/playlist/jump_to,
158             qtr( "Show the current item" ), gotoPlayingItem() );
159     buttons->addWidget( gotoPlayingButton );
160
161     /* A Spacer and the search possibilities */
162     QSpacerItem *spacer = new QSpacerItem( 10, 20 );
163     buttons->addItem( spacer );
164
165     QLabel *filter = new QLabel( qtr(I_PL_SEARCH) + " " );
166     buttons->addWidget( filter );
167
168     SearchLineEdit *search = new SearchLineEdit( this );
169     buttons->addWidget( search );
170     filter->setBuddy( search );
171     CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
172
173     /* Finish the layout */
174     layout->addWidget( view );
175     layout->addLayout( buttons );
176 //    layout->addWidget( bar );
177     setLayout( layout );
178
179     selectColumnsSigMapper = new QSignalMapper( this );
180     CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
181 }
182
183 /* Function to toggle between the Repeat states */
184 void StandardPLPanel::toggleRepeat()
185 {
186     if( model->hasRepeat() )
187     {
188         model->setRepeat( false ); model->setLoop( true );
189         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
190         repeatButton->setChecked( true );
191     }
192     else if( model->hasLoop() )
193     {
194         model->setRepeat( false ) ; model->setLoop( false );
195         repeatButton->setChecked( false );
196         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
197     }
198     else
199     {
200         model->setRepeat( true ); model->setLoop( false );
201         repeatButton->setChecked( true );
202         repeatButton->setIcon( QIcon( ":/buttons/playlist/repeat_one" ) );
203     }
204 }
205
206 /* Function to toggle between the Random states */
207 void StandardPLPanel::toggleRandom()
208 {
209     bool prev = model->hasRandom();
210     model->setRandom( !prev );
211 }
212
213 void StandardPLPanel::gotoPlayingItem()
214 {
215     view->scrollTo( model->currentIndex() );
216 }
217
218 void StandardPLPanel::handleExpansion( const QModelIndex& index )
219 {
220     view->scrollTo( index );
221 }
222
223 void StandardPLPanel::setCurrentRootId( playlist_item_t *p_item )
224 {
225     if( p_item == THEPL->p_local_category ||
226         p_item == THEPL->p_local_onelevel )
227     {
228         addButton->setEnabled( true );
229         addButton->setToolTip( qtr(I_PL_ADDPL) );
230     }
231     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
232              ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
233     {
234         addButton->setEnabled( true );
235         addButton->setToolTip( qtr(I_PL_ADDML) );
236     }
237     else
238         addButton->setEnabled( false );
239 }
240
241 /* PopupAdd Menu for the Add Menu */
242 void StandardPLPanel::popupAdd()
243 {
244     QMenu popup;
245     if( currentRootId == THEPL->p_local_category->i_id ||
246         currentRootId == THEPL->p_local_onelevel->i_id )
247     {
248         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
249         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
250         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
251     }
252     else if( ( THEPL->p_ml_category &&
253                 currentRootId == THEPL->p_ml_category->i_id ) ||
254              ( THEPL->p_ml_onelevel &&
255                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
256     {
257         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
258         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
259         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
260     }
261
262     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
263                         + QPoint( 0, addButton->height() ) );
264 }
265
266 void StandardPLPanel::popupSelectColumn( QPoint pos )
267 {
268     QMenu menu;
269
270     /* We do not offer the option to hide index 0 column, or
271     * QTreeView will behave weird */
272     int i, j;
273     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
274     {
275         QAction* option = menu.addAction(
276             qfu( psz_column_title( i ) ) );
277         option->setCheckable( true );
278         option->setChecked( !view->isColumnHidden( j ) );
279         selectColumnsSigMapper->setMapping( option, j );
280         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
281     }
282     menu.exec( QCursor::pos() );
283 }
284
285 void StandardPLPanel::toggleColumnShown( int i )
286 {
287     view->setColumnHidden( i, !view->isColumnHidden( i ) );
288 }
289
290 /* Search in the playlist */
291 void StandardPLPanel::search( const QString& searchText )
292 {
293     model->search( searchText );
294 }
295
296 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
297 {
298     QItemSelectionModel *selection = view->selectionModel();
299     QModelIndexList list = selection->selectedIndexes();
300     model->popup( index, point, list );
301 }
302
303 /* Set the root of the new Playlist */
304 /* This activated by the selector selection */
305 void StandardPLPanel::setRoot( int i_root_id )
306 {
307     QPL_LOCK;
308     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
309     assert( p_item );
310     p_item = playlist_GetPreferredNode( THEPL, p_item );
311     setRoot( p_item );
312     QPL_UNLOCK;
313 }
314
315 void StandardPLPanel::setRoot( playlist_item_t *p_item )
316 {
317     QPL_LOCK;
318     p_item = playlist_GetPreferredNode( THEPL, p_item );
319     assert( p_item );
320     QPL_UNLOCK;
321
322     model->rebuild( p_item );
323 }
324
325 void StandardPLPanel::removeItem( int i_id )
326 {
327     model->removeItem( i_id );
328 }
329
330 /* Delete and Suppr key remove the selection
331    FilterKey function and code function */
332 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
333 {
334     switch( e->key() )
335     {
336     case Qt::Key_Back:
337     case Qt::Key_Delete:
338         deleteSelection();
339         break;
340     }
341 }
342
343 void StandardPLPanel::deleteSelection()
344 {
345     QItemSelectionModel *selection = view->selectionModel();
346     QModelIndexList list = selection->selectedIndexes();
347     model->doDelete( list );
348 }
349
350 StandardPLPanel::~StandardPLPanel()
351 {
352     getSettings()->beginGroup("Playlist");
353     getSettings()->setValue( "headerState", view->header()->saveState() );
354     getSettings()->endGroup();
355 }
356
357