]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.hpp
000034d56cfb1ea98206858ba462ec5bf3d47f65
[vlc] / modules / gui / qt4 / playlist_model.hpp
1 /*****************************************************************************
2  * playlist_model.hpp : Model for a playlist tree
3  ****************************************************************************
4  * Copyright (C) 2006 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 #ifndef _PLAYLIST_MODEL_H_
25 #define _PLAYLIST_MODEL_H_
26
27 #include <QObject>
28 #include <QEvent>
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc_playlist.h>
32
33 class PLModel;
34
35 class PLItem
36 {
37 public:
38     PLItem( int, int, PLItem *parent , PLModel *);
39     PLItem( playlist_item_t *, PLItem *parent, PLModel *);
40     ~PLItem();
41
42     int row() const;
43     void insertChild( PLItem *, int p, bool signal = true );
44
45     void appendChild( PLItem *item, bool signal = true )
46     {
47         insertChild( item, children.count(), signal );
48     };
49     PLItem *child( int row ) { return children.value( row ); };
50     int childCount() const { return children.count(); };
51     QString columnString( int col ) { return strings.value( col ); };
52     PLItem *parent() { return parentItem; };
53
54     void update( playlist_item_t *);
55 protected:
56     QList<PLItem*> children;
57     QList<QString> strings;
58     int i_id;
59     int i_input_id;
60     friend class PLModel;
61 private:
62     void init( int, int, PLItem *, PLModel * );
63     PLItem *parentItem;
64     PLModel *model;
65 };
66
67 static int ItemUpdate_Type = QEvent::User + 2;
68 static int ItemDelete_Type = QEvent::User + 3;
69 static int ItemAppend_Type = QEvent::User + 4;
70
71 class PLEvent : public QEvent
72 {
73 public:
74     PLEvent( int type, int id ) : QEvent( (QEvent::Type)(type) )
75     { i_id = id; p_add = NULL; };
76     PLEvent(  playlist_add_t  *a ) : QEvent( (QEvent::Type)(ItemAppend_Type) )
77     { p_add = a; };
78     virtual ~PLEvent() {};
79
80     int i_id;
81     playlist_add_t *p_add;
82 };
83
84 #include <QAbstractItemModel>
85 #include <QModelIndex>
86 #include <QVariant>
87
88 class PLModel : public QAbstractItemModel
89 {
90     Q_OBJECT
91
92 public:
93     PLModel( playlist_t *, playlist_item_t *, int, QObject *parent = 0);
94     ~PLModel();
95
96     void customEvent( QEvent * );
97
98     /* QModel stuff */
99     QVariant data( const QModelIndex &index, int role) const;
100     Qt::ItemFlags flags( const QModelIndex &index) const;
101     QVariant headerData( int section, Qt::Orientation orientation,
102                          int role = Qt::DisplayRole) const;
103
104     QModelIndex index( int r, int c, const QModelIndex &parent ) const;
105     QModelIndex index( PLItem *, int c ) const;
106     int itemId( const QModelIndex &index ) const;
107
108     QModelIndex parent( const QModelIndex &index) const;
109     int childrenCount( const QModelIndex &parent = QModelIndex() ) const;
110     int rowCount( const QModelIndex &parent = QModelIndex() ) const;
111     int columnCount( const QModelIndex &parent = QModelIndex() ) const;
112
113     bool b_need_update;
114     int i_items_to_append;
115     void Rebuild();
116     void rebuildRoot( playlist_item_t * );
117 private:
118     void addCallbacks();
119     void delCallbacks();
120     PLItem *rootItem;
121
122     playlist_t *p_playlist;
123     int i_depth;
124
125     /* Update processing */
126     void ProcessInputItemUpdate( int i_input_id );
127     void ProcessItemRemoval( int i_id );
128     void ProcessItemAppend( playlist_add_t *p_add );
129
130     void UpdateTreeItem( PLItem *, bool, bool force = false );
131     void UpdateTreeItem( playlist_item_t *, PLItem *, bool, bool forc = false );
132     void UpdateNodeChildren( PLItem * );
133     void UpdateNodeChildren( playlist_item_t *, PLItem * );
134
135     /* Lookups */
136     PLItem *FindById( PLItem *, int );
137     PLItem *FindByInput( PLItem *, int );
138     PLItem *FindInner( PLItem *, int , bool );
139     PLItem *p_cached_item;
140     PLItem *p_cached_item_bi;
141     int i_cached_id;
142     int i_cached_input_id;
143 public slots:
144     void activateItem( const QModelIndex &index );
145 friend class PLItem;
146 };
147
148 #endif