]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.hpp
Qt4:
[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     void remove( PLItem *removed );
50     PLItem *child( int row ) { return children.value( row ); };
51     int childCount() const { return children.count(); };
52     QString columnString( int col ) { return strings.value( col ); };
53     PLItem *parent() { return parentItem; };
54
55     void update( playlist_item_t *, bool);
56 protected:
57     QList<PLItem*> children;
58     QList<QString> strings;
59     bool current;
60     int type;
61     int i_id;
62     int i_input_id;
63     friend class PLModel;
64 private:
65     void init( int, int, PLItem *, PLModel * );
66     PLItem *parentItem;
67     PLModel *model;
68 };
69
70 static int ItemUpdate_Type = QEvent::User + 2;
71 static int ItemDelete_Type = QEvent::User + 3;
72 static int ItemAppend_Type = QEvent::User + 4;
73
74 class PLEvent : public QEvent
75 {
76 public:
77     PLEvent( int type, int id ) : QEvent( (QEvent::Type)(type) )
78     { i_id = id; p_add = NULL; };
79     PLEvent(  playlist_add_t  *a ) : QEvent( (QEvent::Type)(ItemAppend_Type) )
80     { p_add = a; };
81     virtual ~PLEvent() {};
82
83     int i_id;
84     playlist_add_t *p_add;
85 };
86
87 #include <QAbstractItemModel>
88 #include <QModelIndex>
89 #include <QVariant>
90
91 class PLModel : public QAbstractItemModel
92 {
93     Q_OBJECT
94
95 public:
96     PLModel( playlist_t *, playlist_item_t *, int, QObject *parent = 0);
97     ~PLModel();
98
99     void customEvent( QEvent * );
100
101     /* QModel stuff */
102     QVariant data( const QModelIndex &index, int role) const;
103     Qt::ItemFlags flags( const QModelIndex &index) const;
104     QVariant headerData( int section, Qt::Orientation orientation,
105                          int role = Qt::DisplayRole) const;
106
107     QModelIndex index( int r, int c, const QModelIndex &parent ) const;
108     QModelIndex index( PLItem *, int c ) const;
109     int itemId( const QModelIndex &index ) const;
110     bool isCurrent( const QModelIndex &index );
111
112     QModelIndex parent( const QModelIndex &index) const;
113     int childrenCount( const QModelIndex &parent = QModelIndex() ) const;
114     int rowCount( const QModelIndex &parent = QModelIndex() ) const;
115     int columnCount( const QModelIndex &parent = QModelIndex() ) const;
116
117     bool b_need_update;
118     int i_items_to_append;
119     void Rebuild();
120     void rebuildRoot( playlist_item_t * );
121 private:
122     void addCallbacks();
123     void delCallbacks();
124     PLItem *rootItem;
125
126     playlist_t *p_playlist;
127     int i_depth;
128
129     static QIcon icons[ITEM_TYPE_NUMBER];
130
131     /* Update processing */
132     void ProcessInputItemUpdate( int i_input_id );
133     void ProcessItemRemoval( int i_id );
134     void ProcessItemAppend( playlist_add_t *p_add );
135
136     void UpdateTreeItem( PLItem *, bool, bool force = false );
137     void UpdateTreeItem( playlist_item_t *, PLItem *, bool, bool forc = false );
138     void UpdateNodeChildren( PLItem * );
139     void UpdateNodeChildren( playlist_item_t *, PLItem * );
140
141     /* Lookups */
142     PLItem *FindById( PLItem *, int );
143     PLItem *FindByInput( PLItem *, int );
144     PLItem *FindInner( PLItem *, int , bool );
145     PLItem *p_cached_item;
146     PLItem *p_cached_item_bi;
147     int i_cached_id;
148     int i_cached_input_id;
149 public slots:
150     void activateItem( const QModelIndex &index );
151 friend class PLItem;
152 };
153
154 #endif