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