]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt4 - PlaylistWidget use a much nicer QSplitter. Removes also too much class inherita...
[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( PlaylistWidget *_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     view->scrollTo( index, QAbstractItemView::EnsureVisible );
148     if( model->isCurrent( index ) )
149     {
150         parent = index;
151         while( parent.isValid() )
152         {
153             view->setExpanded( parent, true );
154             parent = model->parent( parent );
155         }
156     }
157 }
158
159 void StandardPLPanel::setCurrentRootId( int _new )
160 {
161     currentRootId = _new;
162     if( currentRootId == THEPL->p_local_category->i_id ||
163         currentRootId == THEPL->p_local_onelevel->i_id  )
164     {
165         addButton->setEnabled( true );
166         addButton->setToolTip( qtr(I_PL_ADDPL) );
167     }
168     else if( currentRootId == THEPL->p_ml_category->i_id ||
169              currentRootId == THEPL->p_ml_onelevel->i_id )
170     {
171         addButton->setEnabled( true );
172         addButton->setToolTip( qtr(I_PL_ADDML) );
173     }
174     else
175         addButton->setEnabled( false );
176 }
177
178 void StandardPLPanel::add()
179 {
180     QMenu *popup = new QMenu();
181     if( currentRootId == THEPL->p_local_category->i_id ||
182         currentRootId == THEPL->p_local_onelevel->i_id )
183     {
184         popup->addAction( qtr(I_PL_ADDF), THEDP, SLOT(simplePLAppendDialog()));
185         popup->addAction( qtr(I_PL_ADVADD), THEDP, SLOT(PLAppendDialog()) );
186         popup->addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
187     }
188     else if( currentRootId == THEPL->p_ml_category->i_id ||
189              currentRootId == THEPL->p_ml_onelevel->i_id )
190     {
191         popup->addAction( qtr(I_PL_ADDF), THEDP, SLOT(simpleMLAppendDialog()));
192         popup->addAction( qtr(I_PL_ADVADD), THEDP, SLOT( MLAppendDialog() ) );
193         popup->addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
194     }
195     popup->popup( QCursor::pos() );
196 }
197
198 void StandardPLPanel::clearFilter()
199 {
200     searchLine->setText( "" );
201 }
202
203 void StandardPLPanel::search( QString searchText )
204 {
205     model->search( searchText );
206 }
207
208 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
209 {
210     if( !index.isValid() ) return;
211     QItemSelectionModel *selection = view->selectionModel();
212     QModelIndexList list = selection->selectedIndexes();
213     model->popup( index, point, list );
214 }
215
216 void StandardPLPanel::setRoot( int i_root_id )
217 {
218     playlist_item_t *p_item = playlist_ItemGetById( THEPL, i_root_id,
219                                                     VLC_TRUE );
220     assert( p_item );
221     p_item = playlist_GetPreferredNode( THEPL, p_item );
222     assert( p_item );
223     model->rebuild( p_item );
224 }
225
226 void StandardPLPanel::removeItem( int i_id )
227 {
228     model->removeItem( i_id );
229 }
230
231 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
232 {
233     switch( e->key() )
234     {
235     case Qt::Key_Back:
236     case Qt::Key_Delete:
237         deleteSelection();
238         break;
239     }
240 }
241
242 void StandardPLPanel::deleteSelection()
243 {
244     QItemSelectionModel *selection = view->selectionModel();
245     QModelIndexList list = selection->selectedIndexes();
246     model->doDelete( list );
247 }
248
249 StandardPLPanel::~StandardPLPanel()
250 {}