]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: make the search box take less space.
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2009 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          JB Kempf
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "qt4.hpp"
30 #include "dialogs_provider.hpp"
31
32 #include "components/playlist/playlist_model.hpp"
33 #include "components/playlist/standardpanel.hpp"
34 #include "util/customwidgets.hpp"
35
36 #include <vlc_intf_strings.h>
37
38 #include <QPushButton>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
41 #include <QHeaderView>
42 #include <QKeyEvent>
43 #include <QModelIndexList>
44 #include <QLabel>
45 #include <QSpacerItem>
46 #include <QMenu>
47 #include <QSignalMapper>
48 #include <assert.h>
49
50 #include "sorting.h"
51
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53                                   intf_thread_t *_p_intf,
54                                   playlist_t *p_playlist,
55                                   playlist_item_t *p_root ):
56                                   QWidget( _parent ), p_intf( _p_intf )
57 {
58     QGridLayout *layout = new QGridLayout( this );
59     layout->setSpacing( 0 ); layout->setMargin( 0 );
60
61     model = new PLModel( p_playlist, p_intf, p_root, this );
62
63     /* Create and configure the QTreeView */
64     view = new QVLCTreeView;
65     view->setModel( model );
66
67     view->setIconSize( QSize( 20, 20 ) );
68     view->setAlternatingRowColors( true );
69     view->setAnimated( true );
70     view->setUniformRowHeights( true );
71     view->setSortingEnabled( true );
72     view->header()->setSortIndicator( -1 , Qt::AscendingOrder );
73     view->header()->setSortIndicatorShown( true );
74     view->header()->setClickable( true );
75     view->header()->setContextMenuPolicy( Qt::CustomContextMenu );
76
77     view->setSelectionBehavior( QAbstractItemView::SelectRows );
78     view->setSelectionMode( QAbstractItemView::ExtendedSelection );
79     view->setDragEnabled( true );
80     view->setAcceptDrops( true );
81     view->setDropIndicatorShown( true );
82
83     /* Saved Settings */
84     getSettings()->beginGroup("Playlist");
85     if( getSettings()->contains( "headerStateV2" ) )
86     {
87         view->header()->restoreState(
88                 getSettings()->value( "headerStateV2" ).toByteArray() );
89     }
90     else
91     {
92         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
93         {
94             view->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
95             if( m == COLUMN_TITLE ) view->header()->resizeSection( c, 200 );
96             else if( m == COLUMN_DURATION ) view->header()->resizeSection( c, 80 );
97         }
98     }
99     getSettings()->endGroup();
100
101     /* Connections for the TreeView */
102     CONNECT( view, activated( const QModelIndex& ) ,
103              model,activateItem( const QModelIndex& ) );
104     CONNECT( view, rightClicked( QModelIndex , QPoint ),
105              this, doPopup( QModelIndex, QPoint ) );
106     CONNECT( view->header(), customContextMenuRequested( const QPoint & ),
107              this, popupSelectColumn( QPoint ) );
108     CONNECT( model, currentChanged( const QModelIndex& ),
109              this, handleExpansion( const QModelIndex& ) );
110
111     currentRootId = -1;
112
113     /* Title label */
114     title = new QLabel;
115     QFont titleFont;
116     titleFont.setPointSize( titleFont.pointSize() + 6 );
117     titleFont.setFamily( "Verdana" );
118     title->setFont( titleFont );
119     layout->addWidget( title, 0, 0 );
120
121     /* A Spacer and the search possibilities */
122     layout->setColumnStretch( 1, 10 );
123
124     SearchLineEdit *search = new SearchLineEdit( this );
125     search->setMaximumWidth( 160 );
126     layout->addWidget( search, 0, 4 );
127     CONNECT( search, textChanged( const QString& ), this, search( const QString& ) );
128
129     /* Add item to the playlist button */
130     addButton = new QPushButton;
131     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
132     addButton->setMaximumWidth( 30 );
133     BUTTONACT( addButton, popupAdd() );
134     layout->addWidget( addButton, 0, 3 );
135
136     /* Finish the layout */
137     layout->addWidget( view, 1, 0, 1, -1 );
138
139     selectColumnsSigMapper = new QSignalMapper( this );
140     CONNECT( selectColumnsSigMapper, mapped( int ), this, toggleColumnShown( int ) );
141 }
142
143 StandardPLPanel::~StandardPLPanel()
144 {
145     getSettings()->beginGroup("Playlist");
146     getSettings()->setValue( "headerStateV2", view->header()->saveState() );
147     getSettings()->endGroup();
148 }
149
150 /* Unused anymore, but might be useful, like in right-click menu */
151 void StandardPLPanel::gotoPlayingItem()
152 {
153     view->scrollTo( model->currentIndex() );
154 }
155
156 void StandardPLPanel::handleExpansion( const QModelIndex& index )
157 {
158     view->scrollTo( index );
159 }
160
161 /* PopupAdd Menu for the Add Menu */
162 void StandardPLPanel::popupAdd()
163 {
164     QMenu popup;
165     if( currentRootId == THEPL->p_local_category->i_id ||
166         currentRootId == THEPL->p_local_onelevel->i_id )
167     {
168         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
169         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
170         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
171     }
172     else if( ( THEPL->p_ml_category &&
173                 currentRootId == THEPL->p_ml_category->i_id ) ||
174              ( THEPL->p_ml_onelevel &&
175                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
176     {
177         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
178         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
179         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
180     }
181
182     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
183                         + QPoint( 0, addButton->height() ) );
184 }
185
186 void StandardPLPanel::popupSelectColumn( QPoint pos )
187 {
188     QMenu menu;
189
190     /* We do not offer the option to hide index 0 column, or
191     * QTreeView will behave weird */
192     int i, j;
193     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
194     {
195         QAction* option = menu.addAction(
196             qfu( psz_column_title( i ) ) );
197         option->setCheckable( true );
198         option->setChecked( !view->isColumnHidden( j ) );
199         selectColumnsSigMapper->setMapping( option, j );
200         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
201     }
202     menu.exec( QCursor::pos() );
203 }
204
205 void StandardPLPanel::toggleColumnShown( int i )
206 {
207     view->setColumnHidden( i, !view->isColumnHidden( i ) );
208 }
209
210 /* Search in the playlist */
211 void StandardPLPanel::search( const QString& searchText )
212 {
213     model->search( searchText );
214 }
215
216 void StandardPLPanel::doPopup( QModelIndex index, QPoint point )
217 {
218     QItemSelectionModel *selection = view->selectionModel();
219     QModelIndexList list = selection->selectedIndexes();
220     model->popup( index, point, list );
221 }
222
223 /* Set the root of the new Playlist */
224 /* This activated by the selector selection */
225 void StandardPLPanel::setRoot( playlist_item_t *p_item )
226 {
227     QPL_LOCK;
228     assert( p_item );
229
230     playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
231     if( p_pref_item ) p_item = p_pref_item;
232
233     /* needed for popupAdd() */
234     currentRootId = p_item->i_id;
235
236     /* cosmetics, ..still need playlist locking.. */
237     char *psz_title = input_item_GetName( p_item->p_input );
238     title->setText( qfu(psz_title) );
239     free( psz_title );
240
241     QPL_UNLOCK;
242
243     /* do THE job */
244     model->rebuild( p_item );
245
246     /* enable/disable adding */
247     if( p_item == THEPL->p_local_category ||
248         p_item == THEPL->p_local_onelevel )
249     {
250         addButton->setEnabled( true );
251         addButton->setToolTip( qtr(I_PL_ADDPL) );
252     }
253     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
254               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
255     {
256         addButton->setEnabled( true );
257         addButton->setToolTip( qtr(I_PL_ADDML) );
258     }
259     else
260         addButton->setEnabled( false );
261 }
262
263 void StandardPLPanel::removeItem( int i_id )
264 {
265     model->removeItem( i_id );
266 }
267
268 /* Delete and Suppr key remove the selection
269    FilterKey function and code function */
270 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
271 {
272     switch( e->key() )
273     {
274     case Qt::Key_Back:
275     case Qt::Key_Delete:
276         deleteSelection();
277         break;
278     }
279 }
280
281 void StandardPLPanel::deleteSelection()
282 {
283     QItemSelectionModel *selection = view->selectionModel();
284     QModelIndexList list = selection->selectedIndexes();
285     model->doDelete( list );
286 }
287