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