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