]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_item.cpp
Qt4 - Split PLItem and PLModel in two different files.
[vlc] / modules / gui / qt4 / components / playlist / playlist_item.cpp
1 /*****************************************************************************
2  * playlist_item.cpp : Manage playlist item
3  ****************************************************************************
4  * Copyright (C) 2006-2007 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include "qt4.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "dialogs/mediainfo.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 main one */
61     if( parentItem == NULL )
62     {
63         i_showflags = config_GetInt( model->p_intf, "qt-pl-showflags" );
64         updateColumnHeaders();
65     }
66     else
67     {
68         i_showflags = parentItem->i_showflags;
69         //Add empty string and update() handles data appending
70         item_col_strings.append( "" );
71     }
72     msg_Dbg( model->p_intf, "PLItem created of type: %i", model->i_depth );
73 }
74
75 /*
76    Constructors
77    Call the above function init
78    So far the first constructor isn't used...
79    */
80 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
81 {
82     init( _i_id, _i_input_id, parent, m );
83 }
84
85 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
86 {
87     init( p_item->i_id, p_item->p_input->i_id, parent, m );
88 }
89
90 PLItem::~PLItem()
91 {
92     qDeleteAll( children );
93     children.clear();
94 }
95
96 /* Column manager */
97 void PLItem::updateColumnHeaders()
98 {
99     item_col_strings.clear();
100
101     if( model->i_depth == 1 )  /* Selector Panel */
102     {
103         item_col_strings.append( "" );
104         return;
105     }
106
107     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
108     {
109         if( i_showflags & i_index )
110         {
111             switch( i_index )
112             {
113             case VLC_META_ENGINE_ARTIST:
114                 item_col_strings.append( qtr( VLC_META_ARTIST ) );
115                 break;
116             case VLC_META_ENGINE_TITLE:
117                 item_col_strings.append( qtr( VLC_META_TITLE ) );
118                 break;
119             case VLC_META_ENGINE_DESCRIPTION:
120                 item_col_strings.append( qtr( VLC_META_DESCRIPTION ) );
121                 break;
122             case VLC_META_ENGINE_DURATION:
123                 item_col_strings.append( qtr( "Duration" ) );
124                 break;
125             case VLC_META_ENGINE_GENRE:
126                 item_col_strings.append( qtr( VLC_META_GENRE ) );
127                 break;
128             case VLC_META_ENGINE_COLLECTION:
129                 item_col_strings.append( qtr( VLC_META_COLLECTION ) );
130                 break;
131             case VLC_META_ENGINE_SEQ_NUM:
132                 item_col_strings.append( qtr( VLC_META_SEQ_NUM ) );
133                 break;
134             case VLC_META_ENGINE_RATING:
135                 item_col_strings.append( qtr( VLC_META_RATING ) );
136                 break;
137             default:
138                 break;
139             }
140         }
141     }
142 }
143
144 /* So far signal is always true.
145    Using signal false would not call PLModel... Why ?
146  */
147 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
148 {
149     if( signal )
150         model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
151     children.insert( i_pos, item );
152     if( signal )
153         model->endInsertRows();
154 }
155
156 void PLItem::remove( PLItem *removed )
157 {
158     if( model->i_depth == DEPTH_SEL || parentItem )
159     {
160         int i_index = parentItem->children.indexOf( removed );
161         model->beginRemoveRows( model->index( parentItem, 0 ),
162                                 i_index, i_index );
163         parentItem->children.removeAt( i_index );
164         model->endRemoveRows();
165     }
166 }
167
168 /* This function is used to get one's parent's row number in the model */
169 int PLItem::row() const
170 {
171     if( parentItem )
172         return parentItem->children.indexOf( const_cast<PLItem*>(this) );
173        // We don't ever inherit PLItem, yet, but it might come :D
174     return 0;
175 }
176
177 /* update the PL Item, get the good names and so on */
178 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
179 {
180     char psz_duration[MSTRTIME_MAX_SIZE];
181     char *psz_meta;
182
183     assert( p_item->p_input->i_id == i_input_id );
184
185     i_type = p_item->p_input->i_type;
186     b_current = iscurrent;
187
188     item_col_strings.clear();
189
190     if( model->i_depth == 1 )  /* Selector Panel */
191     {
192         item_col_strings.append( qfu( p_item->p_input->psz_name ) );
193         return;
194     }
195
196 #define ADD_META( item, meta ) \
197     psz_meta = input_item_Get ## meta ( item->p_input ); \
198     item_col_strings.append( qfu( psz_meta ) ); \
199     free( psz_meta );
200
201     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
202     {
203         if( parentItem->i_showflags & i_index )
204         {
205             switch( i_index )
206             {
207             case VLC_META_ENGINE_ARTIST:
208                 ADD_META( p_item, Artist );
209                 break;
210             case VLC_META_ENGINE_TITLE:
211                 char *psz_title;
212                 psz_title = input_item_GetTitle( p_item->p_input );
213                 if( psz_title )
214                 {
215                     ADD_META( p_item, Title );
216                     free( psz_title );
217                 }
218                 else
219                 {
220                     psz_title = input_item_GetName( p_item->p_input );
221                     if( psz_title )
222                     {
223                         item_col_strings.append( qfu( psz_title ) );
224                     }
225                     free( psz_title );
226                 }
227                 break;
228             case VLC_META_ENGINE_DESCRIPTION:
229                 ADD_META( p_item, Description );
230                 break;
231             case VLC_META_ENGINE_DURATION:
232                 secstotimestr( psz_duration,
233                     input_item_GetDuration( p_item->p_input ) / 1000000 );
234                 item_col_strings.append( QString( psz_duration ) );
235                 break;
236             case VLC_META_ENGINE_GENRE:
237                 ADD_META( p_item, Genre );
238                 break;
239             case VLC_META_ENGINE_COLLECTION:
240                 ADD_META( p_item, Album );
241                 break;
242             case VLC_META_ENGINE_SEQ_NUM:
243                 ADD_META( p_item, TrackNum );
244                 break;
245             case VLC_META_ENGINE_RATING:
246                 ADD_META( p_item, Rating );
247             default:
248                 break;
249             }
250         }
251
252     }
253 #undef ADD_META
254 }
255