]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
620ea81db8bc0f0d5e15234e33f6bc4abf14097d
[vlc] / modules / gui / qt4 / components / playlist / 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  *          Jakob Leben <jleben@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 #ifndef _PLAYLIST_MODEL_H_
26 #define _PLAYLIST_MODEL_H_
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include "qt4.hpp"
33
34 #include <vlc_input.h>
35 #include <vlc_playlist.h>
36
37 #include "playlist_item.hpp"
38
39 #include <QModelIndex>
40 #include <QObject>
41 #include <QEvent>
42 #include <QMimeData>
43 #include <QSignalMapper>
44 #include <QAbstractItemModel>
45 #include <QVariant>
46
47 class QSignalMapper;
48
49 class PLItem;
50
51 #define DEPTH_PL -1
52 #define DEPTH_SEL 1
53
54 enum {
55     ItemUpdate_Type = QEvent::User + PLEventType + 2,
56     ItemDelete_Type = QEvent::User + PLEventType + 3,
57     ItemAppend_Type = QEvent::User + PLEventType + 4,
58 };
59
60 class PLEvent : public QEvent
61 {
62 public:
63     PLEvent( int type, int id ) : QEvent( (QEvent::Type)(type) )
64     {
65         i_id = id;
66         add.i_node = -1;
67         add.i_item = -1;
68     };
69
70     PLEvent( const playlist_add_t  *a ) : QEvent( (QEvent::Type)(ItemAppend_Type) )
71     {
72         add = *a;
73     };
74
75     virtual ~PLEvent() { };
76
77     int i_id;
78     playlist_add_t add;
79 };
80
81
82 class PLModel : public QAbstractItemModel
83 {
84     Q_OBJECT
85
86 friend class PLItem;
87
88 public:
89     PLModel( playlist_t *, intf_thread_t *,
90              playlist_item_t *, QObject *parent = 0 );
91     ~PLModel();
92
93     /*** QModel subclassing ***/
94
95     /* Data structure */
96     QVariant data( const QModelIndex &index, int role ) const;
97     QVariant headerData( int section, Qt::Orientation orientation,
98                          int role = Qt::DisplayRole ) const;
99     int rowCount( const QModelIndex &parent = QModelIndex() ) const;
100     int columnCount( const QModelIndex &parent = QModelIndex() ) const;
101     Qt::ItemFlags flags( const QModelIndex &index ) const;
102     QModelIndex index( int r, int c, const QModelIndex &parent ) const;
103     QModelIndex parent( const QModelIndex &index ) const;
104
105     /* Drag and Drop */
106     Qt::DropActions supportedDropActions() const;
107     QMimeData* mimeData( const QModelIndexList &indexes ) const;
108     bool dropMimeData( const QMimeData *data, Qt::DropAction action,
109                       int row, int column, const QModelIndex &target );
110     QStringList mimeTypes() const;
111
112     /**** Custom ****/
113
114     /* Lookups */
115     QStringList selectedURIs();
116     bool hasRandom(); bool hasLoop(); bool hasRepeat();
117     QModelIndex index( PLItem *, int c ) const;
118     QModelIndex currentIndex( ) { return index( currentItem, 0 ); };
119     bool isCurrent( const QModelIndex &index ) const;
120     int itemId( const QModelIndex &index ) const;
121
122     /* Actions */
123     void popup( QModelIndex & index, QPoint &point, QModelIndexList list );
124     void doDelete( QModelIndexList selected );
125     void search( const QString& search_text );
126     void sort( int column, Qt::SortOrder order );
127     void sort( int i_root_id, int column, Qt::SortOrder order );
128     void removeItem( int );
129     void rebuild(); void rebuild( playlist_item_t *, bool b_first = false );
130
131 private:
132
133     /* General */
134     PLItem *rootItem;
135     PLItem *currentItem;
136
137     playlist_t *p_playlist;
138     intf_thread_t *p_intf;
139     int i_depth;
140
141     static QIcon icons[ITEM_TYPE_NUMBER];
142
143     /* Callbacks related */
144     void addCallbacks();
145     void delCallbacks();
146     void customEvent( QEvent * );
147     void processItemRemoval( int i_id );
148     void processItemAppend( const playlist_add_t *p_add );
149
150     /* Actions */
151     void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList );
152     void doDeleteItem( PLItem *item, QModelIndexList *fullList );
153     void updateTreeItem( PLItem * );
154     void removeItem ( PLItem * );
155     void takeItem( PLItem * ); //will not delete item
156     void insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos );
157     void dropAppendCopy( QByteArray& data, PLItem *target );
158     void dropMove( QByteArray& data, PLItem *target, int new_pos );
159     /* The following actions will not signal the view! */
160     void updateChildren( PLItem * );
161     void updateChildren( playlist_item_t *, PLItem * );
162
163     /* Popup */
164     int i_popup_item, i_popup_parent, i_popup_column;
165     QModelIndexList current_selection;
166
167     /* Lookups */
168     PLItem *findById( PLItem *, int );
169     PLItem *findByInput( PLItem *, int );
170     PLItem *findInner( PLItem *, int , bool );
171     static inline PLItem *getItem( QModelIndex index );
172     int columnFromMeta( int meta_column ) const;
173     int columnToMeta( int column ) const;
174     bool canEdit() const;
175     PLItem *p_cached_item;
176     PLItem *p_cached_item_bi;
177     int i_cached_id;
178     int i_cached_input_id;
179
180 signals:
181     void currentChanged( const QModelIndex& );
182
183 public slots:
184     void activateItem( const QModelIndex &index );
185     void activateItem( playlist_item_t *p_item );
186     void setRandom( bool );
187     void setLoop( bool );
188     void setRepeat( bool );
189
190 private slots:
191     void popupPlay();
192     void popupDel();
193     void popupInfo();
194     void popupStream();
195     void popupSave();
196     void popupExplore();
197     void popupAddNode();
198     void popupSortAsc();
199     void popupSortDesc();
200     void processInputItemUpdate( input_item_t *);
201     void processInputItemUpdate( input_thread_t* p_input );
202 };
203
204 #endif