]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.hpp
68a7340e3a4eae68c86f968f717e396033ba97bc
[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 <QModelIndex>
28 #include <QObject>
29 #include <QEvent>
30 #include <QMimeData>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc_playlist.h>
35
36 class PLModel;
37
38 class PLItem
39 {
40 public:
41     PLItem( int, int, PLItem *parent , PLModel *);
42     PLItem( playlist_item_t *, PLItem *parent, PLModel *);
43     ~PLItem();
44
45     int row() const;
46     void insertChild( PLItem *, int p, bool signal = true );
47
48     void appendChild( PLItem *item, bool signal = true )
49     {
50         insertChild( item, children.count(), signal );
51     };
52     void remove( PLItem *removed );
53     PLItem *child( int row ) { return children.value( row ); };
54     int childCount() const { return children.count(); };
55     QString columnString( int col ) { return strings.value( col ); };
56     PLItem *parent() { return parentItem; };
57
58     void update( playlist_item_t *, bool);
59 protected:
60     QList<PLItem*> children;
61     QList<QString> strings;
62     bool current;
63     int type;
64     int i_id;
65     int i_input_id;
66     friend class PLModel;
67 private:
68     void init( int, int, PLItem *, PLModel * );
69     PLItem *parentItem;
70     PLModel *model;
71 };
72
73 static int ItemUpdate_Type = QEvent::User + 2;
74 static int ItemDelete_Type = QEvent::User + 3;
75 static int ItemAppend_Type = QEvent::User + 4;
76 static int PLUpdate_Type = QEvent::User + 5;
77
78 class PLEvent : public QEvent
79 {
80 public:
81     PLEvent( int type, int id ) : QEvent( (QEvent::Type)(type) )
82     { i_id = id; p_add = NULL; };
83     PLEvent(  playlist_add_t  *a ) : QEvent( (QEvent::Type)(ItemAppend_Type) )
84     { p_add = a; };
85     virtual ~PLEvent() {};
86
87     int i_id;
88     playlist_add_t *p_add;
89 };
90
91 #include <QAbstractItemModel>
92 #include <QVariant>
93
94 class PLModel : public QAbstractItemModel
95 {
96     Q_OBJECT
97
98 public:
99     PLModel( playlist_t *, playlist_item_t *, int, QObject *parent = 0);
100     ~PLModel();
101
102     /* All types of lookups / QModel stuff */
103     QVariant data( const QModelIndex &index, int role) const;
104     Qt::ItemFlags flags( const QModelIndex &index) const;
105     QVariant headerData( int section, Qt::Orientation orientation,
106                          int role = Qt::DisplayRole) const;
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     QModelIndex parent( const QModelIndex &index) const;
112     int childrenCount( const QModelIndex &parent = QModelIndex() ) const;
113     int rowCount( const QModelIndex &parent = QModelIndex() ) const;
114     int columnCount( const QModelIndex &parent = QModelIndex() ) const;
115
116     bool b_need_update;
117     int i_items_to_append;
118     void rebuild(); void rebuild( playlist_item_t *);
119     bool hasRandom(); bool hasLoop(); bool hasRepeat();
120
121     /* Actions made by the views */
122     void popup( QModelIndex & index, QPoint &point, QModelIndexList list );
123     void doDelete( QModelIndexList selected );
124     void search( QString search );
125     void sort( int column, Qt::SortOrder order );
126
127     /* DnD handling */
128     Qt::DropActions supportedDropActions() const;
129     QMimeData* mimeData(const QModelIndexList &indexes) const;
130     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
131                       int row, int column, const QModelIndex &target);
132     QStringList mimeTypes() const;
133
134     void sendArt( QString url );
135 private:
136     void addCallbacks();
137     void delCallbacks();
138     void customEvent( QEvent * );
139
140     PLItem *rootItem;
141
142     playlist_t *p_playlist;
143     int i_depth;
144
145     static QIcon icons[ITEM_TYPE_NUMBER];
146
147     /* Update processing */
148     void ProcessInputItemUpdate( int i_input_id );
149     void ProcessItemRemoval( int i_id );
150     void ProcessItemAppend( playlist_add_t *p_add );
151
152     void UpdateTreeItem( PLItem *, bool, bool force = false );
153     void UpdateTreeItem( playlist_item_t *, PLItem *, bool, bool forc = false );
154     void UpdateNodeChildren( PLItem * );
155     void UpdateNodeChildren( playlist_item_t *, PLItem * );
156
157     /* Actions */
158     void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList);
159     void doDeleteItem( PLItem *item, QModelIndexList *fullList );
160
161     /* Popup */
162     int i_popup_item, i_popup_parent;
163     QModelIndexList current_selection;
164
165     /* Lookups */
166     PLItem *FindById( PLItem *, int );
167     PLItem *FindByInput( PLItem *, int );
168     PLItem *FindInner( PLItem *, int , bool );
169     PLItem *p_cached_item;
170     PLItem *p_cached_item_bi;
171     int i_cached_id;
172     int i_cached_input_id;
173 signals:
174     void artSet( QString );
175 public slots:
176     void activateItem( const QModelIndex &index );
177     void activateItem( playlist_item_t *p_item );
178     void setRandom( bool );
179     void setLoop( bool );
180     void setRepeat( bool );
181 private slots:
182     void popupPlay();
183     void popupDel();
184     void popupInfo();
185     void popupStream();
186     void popupSave();
187 friend class PLItem;
188 };
189
190 #endif