]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
* Fix preparse of directories
[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 "qt4.hpp"
25 #include "dialogs_provider.hpp"
26 #include "playlist_model.hpp"
27 #include "components/playlist/panels.hpp"
28 #include "util/customwidgets.hpp"
29
30 #include <QTreeView>
31 #include <QPushButton>
32 #include <QHBoxLayout>
33 #include <QVBoxLayout>
34 #include <QHeaderView>
35 #include <QKeyEvent>
36 #include <QModelIndexList>
37 #include <QToolBar>
38 #include <QLabel>
39 #include <QSpacerItem>
40 #include <QMenu>
41
42 #include <assert.h>
43
44 StandardPLPanel::StandardPLPanel( BasePlaylistWidget *_parent,
45                                   intf_thread_t *_p_intf,
46                                   playlist_t *p_playlist,
47                                   playlist_item_t *p_root ):
48                                   PLPanel( _parent, _p_intf )
49 {
50     model = new PLModel( p_playlist, p_intf, p_root, -1, this );
51     view = new QVLCTreeView( 0 );
52     view->setModel(model);
53     view->setIconSize( QSize(20,20) );
54     view->setAlternatingRowColors( true );
55     view->header()->resizeSection( 0, 230 );
56     view->header()->resizeSection( 2, 60 );
57     view->header()->setSortIndicatorShown( true );
58     view->header()->setClickable( true );
59
60     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
61     view->setDragEnabled(true);
62     view->setAcceptDrops(true);
63     view->setDropIndicatorShown(true);
64
65     CONNECT( view, activated( const QModelIndex& ) ,
66              model,activateItem( const QModelIndex& ) );
67     CONNECT( view, rightClicked( QModelIndex , QPoint ),
68              this, doPopup( QModelIndex, QPoint ) );
69     CONNECT( model, dataChanged( const QModelIndex&, const QModelIndex& ),
70              this, handleExpansion( const QModelIndex& ) );
71
72     currentRootId = -1;
73     CONNECT( parent, rootChanged(int), this, setCurrentRootId( int ) );
74
75     QVBoxLayout *layout = new QVBoxLayout();
76     layout->setSpacing( 0 ); layout->setMargin( 0 );
77
78     QHBoxLayout *buttons = new QHBoxLayout();
79
80     addButton = new QPushButton( "+", this );
81     addButton->setMaximumWidth( 25 );
82     BUTTONACT( addButton, add() );
83     buttons->addWidget( addButton );
84
85     repeatButton = new QPushButton( 0 ); buttons->addWidget( repeatButton );
86     if( model->hasRepeat() ) repeatButton->setText( qtr( "R1" ) );
87     else if( model->hasLoop() ) repeatButton->setText( qtr( "RA" ) );
88     else repeatButton->setText( qtr( "NR" ) );
89     BUTTONACT( repeatButton, toggleRepeat() );
90
91     randomButton = new QPushButton( 0 ); buttons->addWidget( randomButton );
92     if( model->hasRandom() ) randomButton->setText( qtr( " RND" ) );
93     else randomButton->setText( qtr( "NRND" ) );
94     BUTTONACT( randomButton, toggleRandom() );
95
96     QSpacerItem *spacer = new QSpacerItem( 10, 20 );buttons->addItem( spacer );
97
98     QLabel *filter = new QLabel( qfu( "&Search:" ) + " " );
99     buttons->addWidget( filter );
100     searchLine = new  ClickLineEdit( qfu( "Playlist filter" ), 0 );
101     CONNECT( searchLine, textChanged(QString), this, search(QString));
102     buttons->addWidget( searchLine ); filter->setBuddy( searchLine );
103
104     QPushButton *clear = new QPushButton( qfu( "CL") );
105     clear->setMaximumWidth( 30 );
106     buttons->addWidget( clear );
107     BUTTONACT( clear, clearFilter() );
108
109     layout->addWidget( view );
110     layout->addLayout( buttons );
111 //    layout->addWidget( bar );
112     setLayout( layout );
113 }
114
115 void StandardPLPanel::toggleRepeat()
116 {
117     if( model->hasRepeat() )
118     {
119         model->setRepeat( false ); model->setLoop( true );
120         repeatButton->setText( qtr( "Repeat All" ) );
121     }
122     else if( model->hasLoop() )
123     {
124         model->setRepeat( false ) ; model->setLoop( false );
125         repeatButton->setText( qtr( "No Repeat" ) );
126     }
127     else
128     {
129         model->setRepeat( true );
130         repeatButton->setText( qtr( "Repeat One" ) );
131     }
132 }
133
134 void StandardPLPanel::toggleRandom()
135 {
136     bool prev = model->hasRandom();
137     model->setRandom( !prev );
138     randomButton->setText( prev ? qtr( "No Random" ) : qtr( "Random" ) );
139 }
140
141 void StandardPLPanel::handleExpansion( const QModelIndex &index )
142 {
143     QModelIndex parent;
144     if( model->isCurrent( index ) )
145     {
146         parent = index;
147         while( parent.isValid() )
148         {
149             view->setExpanded( parent, true );
150             parent = model->parent( parent );
151         }
152     }
153 }
154
155 void StandardPLPanel::setCurrentRootId( int _new )
156 {
157     currentRootId = _new;
158     if( currentRootId == THEPL->p_local_category->i_id ||
159         currentRootId == THEPL->p_local_onelevel->i_id  )
160     {
161         addButton->setEnabled( true );
162         addButton->setToolTip( qtr("Add to playlist" ) );
163     }
164     else if( currentRootId == THEPL->p_ml_category->i_id ||
165              currentRootId == THEPL->p_ml_onelevel->i_id )
166     {
167         addButton->setEnabled( true );
168         addButton->setToolTip( qtr("Add to media library" ) );
169     }
170     else
171         addButton->setEnabled( false );
172 }
173
174 void StandardPLPanel::add()
175 {
176     QMenu *popup = new QMenu();
177     if( currentRootId == THEPL->p_local_category->i_id ||
178         currentRootId == THEPL->p_local_onelevel->i_id )
179     {
180         popup->addAction( qtr("Add file"), THEDP, SLOT( simplePLAppendDialog() ) );
181         popup->addAction( qtr("Advanced add"), THEDP, SLOT( PLAppendDialog() ) );
182     }
183     else if( currentRootId == THEPL->p_ml_category->i_id ||
184              currentRootId == THEPL->p_ml_onelevel->i_id )
185     {
186         popup->addAction( qtr("Add file"), THEDP, SLOT( simpleMLAppendDialog() ) );
187         popup->addAction( qtr("Advanced add"), THEDP, SLOT( MLAppendDialog() ) );
188         popup->addAction( qtr("Directory"), THEDP, SLOT( openMLDirectory() ) );
189     }
190     popup->popup( QCursor::pos() );
191 }
192
193 void StandardPLPanel::clearFilter()
194 {
195     searchLine->setText( "" );
196 }
197
198 void StandardPLPanel::search( QString searchText )
199 {
200     model->search( searchText );
201 }
202
203 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
204 {
205     if( !index.isValid() ) return;
206     QItemSelectionModel *selection = view->selectionModel();
207     QModelIndexList list = selection->selectedIndexes();
208     model->popup( index, point, list );
209 }
210
211 void StandardPLPanel::setRoot( int i_root_id )
212 {
213     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
214                                                     VLC_TRUE );
215     assert( p_item );
216     p_item = playlist_GetPreferredNode( THEPL, p_item );
217     assert( p_item );
218     model->rebuild( p_item );
219 }
220
221 void StandardPLPanel::removeItem( int i_id )
222 {
223     model->removeItem( i_id );
224 }
225
226 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
227 {
228     switch( e->key() )
229     {
230     case Qt::Key_Back:
231     case Qt::Key_Delete:
232         deleteSelection();
233         break;
234     }
235 }
236
237 void StandardPLPanel::deleteSelection()
238 {
239     QItemSelectionModel *selection = view->selectionModel();
240     QModelIndexList list = selection->selectedIndexes();
241     model->doDelete( list );
242 }
243
244 StandardPLPanel::~StandardPLPanel()
245 {}