]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_item.cpp
Qt4 - Remove the qt-pl-showflags from preferences and put it in QSettings,because...
[vlc] / modules / gui / qt4 / components / playlist / playlist_item.cpp
1 /*****************************************************************************
2  * playlist_item.cpp : Manage playlist item
3  ****************************************************************************
4  * Copyright © 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
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 <assert.h>
30
31 #include "qt4.hpp"
32 #include "components/playlist/playlist_model.hpp"
33 #include <vlc_intf_strings.h>
34
35 #include "pixmaps/type_unknown.xpm"
36
37 /*************************************************************************
38  * Playlist item implementation
39  *************************************************************************/
40
41 /*
42    Playlist item is just a wrapper, an abstraction of the playlist_item
43    in order to be managed by PLModel
44
45    PLItem have a parent, and id and a input Id
46 */
47
48
49 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
50 {
51     parentItem = parent;          /* Can be NULL, but only for the rootItem */
52     i_id       = _i_id;           /* Playlist item specific id */
53     i_input_id = _i_input_id;     /* Identifier of the input */
54     model      = m;               /* PLModel (QAbsmodel) */
55     i_type     = -1;              /* Item type - Avoid segfault */
56     b_current  = false;           /* Is the item the current Item or not */
57
58     assert( model );              /* We need a model */
59
60     /* No parent, should be the 2 main ones */
61     if( parentItem == NULL )
62     {
63         if( model->i_depth == DEPTH_SEL )  /* Selector Panel */
64         {
65             item_col_strings.append( "" );
66         }
67         else
68         {
69             QSettings settings( "vlc", "vlc-qt-interface" );
70             i_showflags = settings.value( "qt-pl-showflags" ).toInt();
71             updateColumnHeaders();
72         }
73     }
74     else
75     {
76         i_showflags = parentItem->i_showflags;
77         //Add empty string and update() handles data appending
78         item_col_strings.append( "" );
79     }
80 }
81
82 /*
83    Constructors
84    Call the above function init
85    So far the first constructor isn't used...
86    */
87 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
88 {
89     init( _i_id, _i_input_id, parent, m );
90 }
91
92 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
93 {
94     init( p_item->i_id, p_item->p_input->i_id, parent, m );
95 }
96
97 PLItem::~PLItem()
98 {
99     qDeleteAll( children );
100     children.clear();
101 }
102
103 /* Column manager */
104 void PLItem::updateColumnHeaders()
105 {
106     item_col_strings.clear();
107
108     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
109     {
110         if( i_showflags & i_index )
111         {
112             switch( i_index )
113             {
114             case VLC_META_ENGINE_ARTIST:
115                 item_col_strings.append( qtr( VLC_META_ARTIST ) );
116                 break;
117             case VLC_META_ENGINE_TITLE:
118                 item_col_strings.append( qtr( VLC_META_TITLE ) );
119                 break;
120             case VLC_META_ENGINE_DESCRIPTION:
121                 item_col_strings.append( qtr( VLC_META_DESCRIPTION ) );
122                 break;
123             case VLC_META_ENGINE_DURATION:
124                 item_col_strings.append( qtr( "Duration" ) );
125                 break;
126             case VLC_META_ENGINE_GENRE:
127                 item_col_strings.append( qtr( VLC_META_GENRE ) );
128                 break;
129             case VLC_META_ENGINE_COLLECTION:
130                 item_col_strings.append( qtr( VLC_META_COLLECTION ) );
131                 break;
132             case VLC_META_ENGINE_SEQ_NUM:
133                 item_col_strings.append( qtr( VLC_META_SEQ_NUM ) );
134                 break;
135             case VLC_META_ENGINE_RATING:
136                 item_col_strings.append( qtr( VLC_META_RATING ) );
137                 break;
138             default:
139                 break;
140             }
141         }
142     }
143 }
144
145 /* So far signal is always true.
146    Using signal false would not call PLModel... Why ?
147  */
148 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
149 {
150     if( signal )
151         model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
152     children.insert( i_pos, item );
153     if( signal )
154         model->endInsertRows();
155 }
156
157 void PLItem::remove( PLItem *removed )
158 {
159     if( model->i_depth == DEPTH_SEL || parentItem )
160     {
161         int i_index = parentItem->children.indexOf( removed );
162         model->beginRemoveRows( model->index( parentItem, 0 ),
163                                 i_index, i_index );
164         parentItem->children.removeAt( i_index );
165         model->endRemoveRows();
166     }
167 }
168
169 /* This function is used to get one's parent's row number in the model */
170 int PLItem::row() const
171 {
172     if( parentItem )
173         return parentItem->children.indexOf( const_cast<PLItem*>(this) );
174        // We don't ever inherit PLItem, yet, but it might come :D
175     return 0;
176 }
177
178 /* update the PL Item, get the good names and so on */
179 /* This function may not be the best way to do it
180    It destroys everything and gets everything again instead of just
181    building the necessary columns.
182    This does extra work if you re-display the same column. Slower...
183    On the other hand, this way saves memory.
184    There must be a more clever way.
185    */
186 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
187 {
188     char psz_duration[MSTRTIME_MAX_SIZE];
189     char *psz_meta;
190
191     assert( p_item->p_input->i_id == i_input_id );
192
193     /* Useful for the model */
194     i_type = p_item->p_input->i_type;
195     b_current = iscurrent;
196
197     item_col_strings.clear();
198
199     if( model->i_depth == 1 )  /* Selector Panel */
200     {
201         item_col_strings.append( qfu( p_item->p_input->psz_name ) );
202         return;
203     }
204
205 #define ADD_META( item, meta ) \
206     psz_meta = input_item_Get ## meta ( item->p_input ); \
207     item_col_strings.append( qfu( psz_meta ) ); \
208     free( psz_meta );
209
210     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
211     {
212         if( parentItem->i_showflags & i_index )
213         {
214             switch( i_index )
215             {
216             case VLC_META_ENGINE_ARTIST:
217                 ADD_META( p_item, Artist );
218                 break;
219             case VLC_META_ENGINE_TITLE:
220                 char *psz_title;
221                 psz_title = input_item_GetTitle( p_item->p_input );
222                 if( psz_title )
223                 {
224                     ADD_META( p_item, Title );
225                     free( psz_title );
226                 }
227                 else
228                 {
229                     psz_title = input_item_GetName( p_item->p_input );
230                     if( psz_title )
231                     {
232                         item_col_strings.append( qfu( psz_title ) );
233                     }
234                     free( psz_title );
235                 }
236                 break;
237             case VLC_META_ENGINE_DESCRIPTION:
238                 ADD_META( p_item, Description );
239                 break;
240             case VLC_META_ENGINE_DURATION:
241                 secstotimestr( psz_duration,
242                     input_item_GetDuration( p_item->p_input ) / 1000000 );
243                 item_col_strings.append( QString( psz_duration ) );
244                 break;
245             case VLC_META_ENGINE_GENRE:
246                 ADD_META( p_item, Genre );
247                 break;
248             case VLC_META_ENGINE_COLLECTION:
249                 ADD_META( p_item, Album );
250                 break;
251             case VLC_META_ENGINE_SEQ_NUM:
252                 ADD_META( p_item, TrackNum );
253                 break;
254             case VLC_META_ENGINE_RATING:
255                 ADD_META( p_item, Rating );
256             default:
257                 break;
258             }
259         }
260     }
261 #undef ADD_META
262 }
263