]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.hpp
Improve drag&drop handling
[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     void removeItem( int );
127
128     /* DnD handling */
129     Qt::DropActions supportedDropActions() const;
130     QMimeData* mimeData(const QModelIndexList &indexes) const;
131     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
132                       int row, int column, const QModelIndex &target);
133     QStringList mimeTypes() const;
134
135     void sendArt( QString url );
136 private:
137     void addCallbacks();
138     void delCallbacks();
139     void customEvent( QEvent * );
140
141     PLItem *rootItem;
142
143     playlist_t *p_playlist;
144     int i_depth;
145
146     static QIcon icons[ITEM_TYPE_NUMBER];
147
148     /* Update processing */
149     void ProcessInputItemUpdate( int i_input_id );
150     void ProcessItemRemoval( int i_id );
151     void ProcessItemAppend( playlist_add_t *p_add );
152
153     void UpdateTreeItem( PLItem *, bool, bool force = false );
154     void UpdateTreeItem( playlist_item_t *, PLItem *, bool, bool forc = false );
155     void UpdateNodeChildren( PLItem * );
156     void UpdateNodeChildren( playlist_item_t *, PLItem * );
157
158     /* Actions */
159     void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList);
160     void doDeleteItem( PLItem *item, QModelIndexList *fullList );
161
162     /* Popup */
163     int i_popup_item, i_popup_parent;
164     QModelIndexList current_selection;
165
166     /* Lookups */
167     PLItem *FindById( PLItem *, int );
168     PLItem *FindByInput( PLItem *, int );
169     PLItem *FindInner( PLItem *, int , bool );
170     PLItem *p_cached_item;
171     PLItem *p_cached_item_bi;
172     int i_cached_id;
173     int i_cached_input_id;
174 signals:
175     void artSet( QString );
176     void shouldRemove( int );
177 public slots:
178     void activateItem( const QModelIndex &index );
179     void activateItem( playlist_item_t *p_item );
180     void setRandom( bool );
181     void setLoop( bool );
182     void setRepeat( bool );
183 private slots:
184     void popupPlay();
185     void popupDel();
186     void popupInfo();
187     void popupStream();
188     void popupSave();
189 friend class PLItem;
190 };
191
192 #endif