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