]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
qt4: access visible playlist column selector menu from general playlist popup menu
[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     int shownFlags() { return i_showflags;  }
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 * );
130
131     /* Helpers */
132     void makeColumnSelectMenu( QMenu *menu );
133 private:
134
135     /* General */
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     /* Callbacks related */
147     void addCallbacks();
148     void delCallbacks();
149     void customEvent( QEvent * );
150     void processItemRemoval( int i_id );
151     void processItemAppend( const playlist_add_t *p_add );
152
153     /* Actions */
154     void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList );
155     void doDeleteItem( PLItem *item, QModelIndexList *fullList );
156     void updateTreeItem( PLItem *, bool, bool force = false );
157     void takeItem( PLItem * ); //will not delete item
158     void insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos );
159     void dropAppendCopy( QByteArray& data, PLItem *target );
160     void dropMove( QByteArray& data, PLItem *target, int new_pos );
161     /* The following actions will not signal the view! */
162     void removeItem ( PLItem * );
163     void updateChildren( PLItem * );
164     void updateChildren( playlist_item_t *, PLItem * );
165
166     /* Popup */
167     int i_popup_item, i_popup_parent, i_popup_column;
168     QModelIndexList current_selection;
169     QSignalMapper *ContextUpdateMapper;
170
171     /* Lookups */
172     PLItem *findById( PLItem *, int );
173     PLItem *findByInput( PLItem *, int );
174     PLItem *findInner( PLItem *, int , bool );
175     static inline PLItem *getItem( QModelIndex index );
176     int columnFromMeta( int meta_column, int shown_flags ) const;
177     int columnToMeta( int column, int shown_flags ) const;
178     PLItem *p_cached_item;
179     PLItem *p_cached_item_bi;
180     int i_cached_id;
181     int i_cached_input_id;
182
183 signals:
184     void shouldRemove( int );
185     void currentChanged( const QModelIndex& );
186     void columnsChanged( int );
187
188
189 public slots:
190     void activateItem( const QModelIndex &index );
191     void activateItem( playlist_item_t *p_item );
192     void setRandom( bool );
193     void setLoop( bool );
194     void setRepeat( bool );
195
196 private slots:
197     void popupPlay();
198     void popupDel();
199     void popupInfo();
200     void popupStream();
201     void popupSave();
202     void popupExplore();
203     void popupAddNode();
204     void popupSortAsc();
205     void popupSortDesc();
206     void toggleColumnShown( int meta_column );
207     void processInputItemUpdate( input_item_t *);
208     void processInputItemUpdate( input_thread_t* p_input );
209 };
210
211 #endif