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