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