]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Multiple selection
[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 #include "playlist_model.hpp"
25 #include "components/playlist/panels.hpp"
26 #include <QTreeView>
27 #include <QPushButton>
28 #include <QHBoxLayout>
29 #include <QVBoxLayout>
30 #include <QHeaderView>
31 #include <QKeyEvent>
32 #include "qt4.hpp"
33 #include <assert.h>
34 #include <QModelIndexList>
35
36 StandardPLPanel::StandardPLPanel( QWidget *_parent, intf_thread_t *_p_intf,
37                                   playlist_t *p_playlist,
38                                   playlist_item_t *p_root ):
39                                   PLPanel( _parent, _p_intf )
40 {
41     model = new PLModel( p_playlist, p_root, -1, this );
42     view = new QTreeView( 0 );
43     view->setModel(model);
44     view->header()->resizeSection( 0, 300 );
45     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
46
47     connect( view, SIGNAL( activated( const QModelIndex& ) ), model,
48              SLOT( activateItem( const QModelIndex& ) ) );
49
50     connect( model,
51              SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
52              this, SLOT( handleExpansion( const QModelIndex& ) ) );
53
54     model->Rebuild();
55
56     QVBoxLayout *layout = new QVBoxLayout();
57     layout->setSpacing( 0 ); layout->setMargin( 0 );
58
59     QHBoxLayout *buttons = new QHBoxLayout();
60     repeatButton = new QPushButton( this );
61     buttons->addWidget( repeatButton );
62     randomButton = new QPushButton( this );
63     buttons->addWidget( randomButton );
64
65     if( model->hasRandom() ) randomButton->setText( qtr( "Random" ) );
66     else randomButton->setText( qtr( "No random" ) );
67
68     if( model->hasRepeat() ) repeatButton->setText( qtr( "Repeat One" ) );
69     else if( model->hasLoop() ) repeatButton->setText( qtr( "Repeat All" ) );
70     else repeatButton->setText( qtr( "No Repeat" ) );
71
72     connect( repeatButton, SIGNAL( clicked() ), this, SLOT( toggleRepeat() ));
73     connect( randomButton, SIGNAL( clicked() ), this, SLOT( toggleRandom() ));
74
75     layout->addWidget( view );
76     layout->addLayout( buttons );
77     setLayout( layout );
78 }
79
80 void StandardPLPanel::toggleRepeat()
81 {
82     if( model->hasRepeat() )
83     {
84         model->setRepeat( false ); model->setLoop( true );
85         repeatButton->setText( qtr( "Repeat All" ) );
86     }
87     else if( model->hasLoop() )
88     {
89         model->setRepeat( false ) ; model->setLoop( false );
90         repeatButton->setText( qtr( "No Repeat" ) );
91     }
92     else
93     {
94         model->setRepeat( true );
95         repeatButton->setText( qtr( "Repeat One" ) );
96     }
97 }
98
99 void StandardPLPanel::toggleRandom()
100 {
101     bool prev = model->hasRandom();
102     model->setRandom( !prev );
103     randomButton->setText( prev ? qtr( "No Random" ) : qtr( "Random" ) );
104 }
105
106 void StandardPLPanel::handleExpansion( const QModelIndex &index )
107 {
108     QModelIndex parent;
109     if( model->isCurrent( index ) )
110     {
111         parent = index;
112         while( parent.isValid() )
113         {
114             view->setExpanded( parent, true );
115             parent = model->parent( parent );
116         }
117     }
118 }
119
120 void StandardPLPanel::setRoot( int i_root_id )
121 {
122     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id );
123     assert( p_item );
124     model->rebuildRoot( p_item );
125     model->Rebuild();
126 }
127
128 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
129 {
130     switch( e->key() )
131     {
132     case Qt::Key_Back:
133     case Qt::Key_Delete:
134         deleteSelection();
135         break;
136     }
137 }
138
139 void StandardPLPanel::deleteSelection()
140 {
141     QItemSelectionModel *selection = view->selectionModel();
142     QModelIndexList list = selection->selectedIndexes();
143     model->doDelete( list );
144 }
145
146 StandardPLPanel::~StandardPLPanel()
147 {}