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