]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_item.cpp
Merge branch 'master' into lpcm_encoder
[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 "playlist_item.hpp"
33 #include <vlc_intf_strings.h>
34
35 #include "sorting.h"
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( playlist_item_t *_playlist_item, PLItem *parent )
50 {
51     parentItem = parent;          /* Can be NULL, but only for the rootItem */
52     i_id       = _playlist_item->i_id;           /* Playlist item specific id */
53     p_input    = _playlist_item->p_input;
54     vlc_gc_incref( p_input );
55
56 }
57
58 /*
59    Constructors
60    Call the above function init
61    */
62 PLItem::PLItem( playlist_item_t *p_item, PLItem *parent )
63 {
64     init( p_item, parent );
65 }
66
67 PLItem::PLItem( playlist_item_t * p_item )
68 {
69     init( p_item, NULL );
70 }
71
72 PLItem::~PLItem()
73 {
74     vlc_gc_decref( p_input );
75     qDeleteAll( children );
76     children.clear();
77 }
78
79 /* So far signal is always true.
80    Using signal false would not call PLModel... Why ?
81  */
82 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
83 {
84     children.insert( i_pos, item );
85 }
86
87 void PLItem::removeChild( PLItem *item )
88 {
89     children.removeOne( item );
90     delete item;
91 }
92
93 void PLItem::removeChildren()
94 {
95     qDeleteAll( children );
96     children.clear();
97 }
98
99 void PLItem::takeChildAt( int index )
100 {
101     PLItem *child = children[index];
102     child->parentItem = NULL;
103     children.removeAt( index );
104 }
105
106 /* This function is used to get one's parent's row number in the model */
107 int PLItem::row() const
108 {
109     if( parentItem )
110         return parentItem->children.indexOf( const_cast<PLItem*>(this) );
111        // We don't ever inherit PLItem, yet, but it might come :D
112     return 0;
113 }
114
115 bool PLItem::operator< ( PLItem& other )
116 {
117     PLItem *item1 = this;
118     while( item1->parentItem )
119     {
120         PLItem *item2 = &other;
121         while( item2->parentItem )
122         {
123             if( item1 == item2->parentItem ) return true;
124             if( item2 == item1->parentItem ) return false;
125             if( item1->parentItem == item2->parentItem )
126                 return item1->parentItem->children.indexOf( item1 ) <
127                        item1->parentItem->children.indexOf( item2 );
128             item2 = item2->parentItem;
129         }
130         item1 = item1->parentItem;
131     }
132     return false;
133 }