]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
qt4: make PLModel::getItem inline
[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  *
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "qt4.hpp"
32
33 #include <vlc_input.h>
34 #include <vlc_playlist.h>
35
36 #include "playlist_item.hpp"
37
38 #include <QModelIndex>
39 #include <QObject>
40 #include <QEvent>
41 #include <QMimeData>
42 #include <QSignalMapper>
43 #include <QAbstractItemModel>
44 #include <QVariant>
45
46 class QSignalMapper;
47
48 class PLItem;
49
50 #define DEPTH_PL -1
51 #define DEPTH_SEL 1
52
53 enum {
54     ItemUpdate_Type = QEvent::User + PLEventType + 2,
55     ItemDelete_Type = QEvent::User + PLEventType + 3,
56     ItemAppend_Type = QEvent::User + PLEventType + 4,
57     PLUpdate_Type   = QEvent::User + PLEventType + 5,
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 *, int, QObject *parent = 0 );
91     ~PLModel();
92
93     /* All types of lookups / QModel stuff */
94     QVariant data( const QModelIndex &index, int role ) const;
95     Qt::ItemFlags flags( const QModelIndex &index ) const;
96     QVariant headerData( int section, Qt::Orientation orientation,
97                          int role = Qt::DisplayRole ) const;
98     QModelIndex index( int r, int c, const QModelIndex &parent ) const;
99     QModelIndex index( PLItem *, int c ) const;
100     QModelIndex currentIndex( ) { return index( currentItem, 0 ); };
101     int itemId( const QModelIndex &index ) const;
102     bool isCurrent( const QModelIndex &index ) const;
103     QModelIndex parent( const QModelIndex &index ) const;
104     int childrenCount( const QModelIndex &parent = QModelIndex() ) const;
105     int rowCount( const QModelIndex &parent = QModelIndex() ) const;
106     int columnCount( const QModelIndex &parent = QModelIndex() ) const;
107
108     /* Get current selection */
109     QStringList selectedURIs();
110
111     void rebuild(); void rebuild( playlist_item_t * );
112     bool hasRandom(); bool hasLoop(); bool hasRepeat();
113
114     /* Actions made by the views */
115     void popup( QModelIndex & index, QPoint &point, QModelIndexList list );
116     void doDelete( QModelIndexList selected );
117     void search( const QString& search_text );
118     void sort( int column, Qt::SortOrder order );
119     void sort( int i_root_id, int column, Qt::SortOrder order );
120     void removeItem( int );
121
122     /* DnD handling */
123     Qt::DropActions supportedDropActions() const;
124     QMimeData* mimeData( const QModelIndexList &indexes ) const;
125     bool dropMimeData( const QMimeData *data, Qt::DropAction action,
126                       int row, int column, const QModelIndex &target );
127     QStringList mimeTypes() const;
128
129     int shownFlags() { return i_showflags;  }
130
131 private:
132     void addCallbacks();
133     void delCallbacks();
134     void customEvent( QEvent * );
135
136     PLItem *rootItem;
137     PLItem *currentItem;
138
139     playlist_t *p_playlist;
140     intf_thread_t *p_intf;
141     int i_depth;
142     int i_showflags;
143
144     static QIcon icons[ITEM_TYPE_NUMBER];
145
146     /* Update processing */
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 *, bool, bool force = false );
154     /* The following actions will not signal the view! */
155     void RemoveItem ( PLItem * );
156     void RemoveChildren( PLItem * );
157     void UpdateChildren( PLItem * );
158     void UpdateChildren( playlist_item_t *, PLItem * );
159
160     /* Popup */
161     int i_popup_item, i_popup_parent, i_popup_column;
162     QModelIndexList current_selection;
163     QSignalMapper *ContextUpdateMapper;
164
165     /* Lookups */
166     PLItem *FindById( PLItem *, int );
167     PLItem *FindByInput( PLItem *, int );
168     PLItem *FindInner( PLItem *, int , bool );
169     static inline PLItem *getItem( QModelIndex index );
170     int metaColumn ( int column ) const;
171     PLItem *p_cached_item;
172     PLItem *p_cached_item_bi;
173     int i_cached_id;
174     int i_cached_input_id;
175 signals:
176     void shouldRemove( int );
177     void currentChanged( const QModelIndex& );
178     void columnsChanged( int );
179
180
181 public slots:
182     void activateItem( const QModelIndex &index );
183     void activateItem( playlist_item_t *p_item );
184     void setRandom( bool );
185     void setLoop( bool );
186     void setRepeat( bool );
187
188 private slots:
189     void popupPlay();
190     void popupDel();
191     void popupInfo();
192     void popupStream();
193     void popupSave();
194     void popupExplore();
195     void popupAddNode();
196     void popupSortAsc();
197     void popupSortDesc();
198     void viewchanged( int );
199     void ProcessInputItemUpdate( input_item_t *);
200     void ProcessInputItemUpdate( input_thread_t* p_input );
201 };
202
203 #endif