]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
Qt4: implement PLModel::popupSort[Asc/Desc]()
[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     void UpdateTreeItem( PLItem *, bool, bool force = false );
151     void UpdateNodeChildren( PLItem * );
152     void UpdateNodeChildren( playlist_item_t *, PLItem * );
153
154     /* Actions */
155     void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList );
156     void doDeleteItem( PLItem *item, QModelIndexList *fullList );
157
158     /* Popup */
159     int i_popup_item, i_popup_parent, i_popup_column;
160     QModelIndexList current_selection;
161     QSignalMapper *ContextUpdateMapper;
162
163     /* Lookups */
164     PLItem *FindById( PLItem *, int );
165     PLItem *FindByInput( PLItem *, int );
166     PLItem *FindInner( PLItem *, int , bool );
167     PLItem *p_cached_item;
168     PLItem *p_cached_item_bi;
169     int i_cached_id;
170     int i_cached_input_id;
171 signals:
172     void shouldRemove( int );
173     void currentChanged( const QModelIndex& );
174     void columnsChanged( int );
175
176
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
184 private slots:
185     void popupPlay();
186     void popupDel();
187     void popupInfo();
188     void popupStream();
189     void popupSave();
190     void popupExplore();
191     void popupAddNode();
192     void popupSortAsc();
193     void popupSortDesc();
194     void viewchanged( int );
195     void ProcessInputItemUpdate( input_item_t *);
196     void ProcessInputItemUpdate( input_thread_t* p_input );
197 };
198
199 #endif