]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
Qt: playlist model cleanup
[vlc] / modules / gui / qt4 / components / playlist / playlist_model.hpp
1 /*****************************************************************************
2  * playlist_model.hpp : Model for a playlist tree
3  ****************************************************************************
4  * Copyright (C) 2006-2011 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 <vlc_input.h>
33 #include <vlc_playlist.h>
34 #include "vlc_model.hpp"
35 #include "playlist_item.hpp"
36
37 #include <QObject>
38 #include <QEvent>
39 #include <QSignalMapper>
40 #include <QMimeData>
41 #include <QAbstractItemModel>
42 #include <QVariant>
43 #include <QModelIndex>
44
45 class PLItem;
46 class PLSelector;
47 class PlMimeData;
48 class QSignalMapper;
49
50 class PLModel : public VLCModel
51 {
52     Q_OBJECT
53
54 public:
55     PLModel( playlist_t *, intf_thread_t *,
56              playlist_item_t *, QObject *parent = 0 );
57     virtual ~PLModel();
58
59     /* Qt4 main PLModel */
60     static PLModel* getPLModel( intf_thread_t *p_intf )
61     {
62         if(!p_intf->p_sys->pl_model )
63         {
64             playlist_Lock( THEPL );
65             playlist_item_t *p_root = THEPL->p_playing;
66             playlist_Unlock( THEPL );
67             p_intf->p_sys->pl_model = new PLModel( THEPL, p_intf, p_root, NULL );
68         }
69
70         return p_intf->p_sys->pl_model;
71     }
72
73     /*** QAbstractItemModel subclassing ***/
74
75     /* Data structure */
76     virtual QVariant data( const QModelIndex &index, const int role ) const;
77     virtual QVariant headerData( int section, Qt::Orientation orientation,
78                          int role = Qt::DisplayRole ) const;
79     virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
80     virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const;
81     virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
82     virtual QModelIndex index( const int r, const int c, const QModelIndex &parent ) const;
83     virtual QModelIndex parent( const QModelIndex &index ) const;
84
85     /* Drag and Drop */
86     virtual Qt::DropActions supportedDropActions() const;
87     virtual QMimeData* mimeData( const QModelIndexList &indexes ) const;
88     virtual bool dropMimeData( const QMimeData *data, Qt::DropAction action,
89                       int row, int column, const QModelIndex &target );
90     virtual QStringList mimeTypes() const;
91
92     /* Sort */
93     virtual void sort( const int column, Qt::SortOrder order = Qt::AscendingOrder );
94
95     /**** Custom ****/
96
97     /* Lookups */
98     QModelIndex index( const int i_id, const int c );
99     virtual QModelIndex currentIndex() const;
100     int itemId( const QModelIndex &index ) const;
101
102     /* */
103     void search( const QString& search_text, const QModelIndex & root, bool b_recursive );
104     void rebuild( playlist_item_t * p = NULL );
105
106     /* Popup Actions */
107     virtual bool popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &list );
108     virtual void doDelete( QModelIndexList selected );
109
110     PLItem *getItem( const QModelIndex & index ) const
111     {
112         if( index.isValid() )
113             return static_cast<PLItem*>( index.internalPointer() );
114         else return rootItem;
115     }
116     int getZoom() const
117     {
118         return i_zoom;
119     }
120
121 signals:
122     void currentChanged( const QModelIndex& );
123     void rootChanged();
124
125 public slots:
126     virtual void activateItem( const QModelIndex &index );
127     void changeZoom( const int zoom )
128     {
129         i_zoom = zoom;
130         emit layoutChanged();
131     }
132
133 private:
134     /* General */
135     PLItem *rootItem;
136
137     playlist_t *p_playlist;
138
139     static QIcon icons[ITEM_TYPE_NUMBER];
140
141     /* Custom model private methods */
142     /* Lookups */
143     QStringList selectedURIs();
144     QModelIndex index( PLItem *, const int c ) const;
145     bool isCurrent( const QModelIndex &index ) const;
146     bool isParent( const QModelIndex &index, const QModelIndex &current) const;
147
148     /* Shallow actions (do not affect core playlist) */
149     void updateTreeItem( PLItem * );
150     void removeItem ( PLItem * );
151     void removeItem( int );
152     void recurseDelete( QList<PLItem*> children, QModelIndexList *fullList );
153     void takeItem( PLItem * ); //will not delete item
154     void insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos );
155     /* ...of which  the following will not update the views */
156     void updateChildren( PLItem * );
157     void updateChildren( playlist_item_t *, PLItem * );
158
159     /* Deep actions (affect core playlist) */
160     void dropAppendCopy( const PlMimeData * data, PLItem *target, int pos );
161     void dropMove( const PlMimeData * data, PLItem *target, int new_pos );
162
163     /* */
164     void sort( const int i_root_id, const int column, Qt::SortOrder order );
165
166     /* Popup */
167     int i_popup_item, i_popup_parent;
168     QModelIndexList current_selection;
169     QMenu *sortingMenu;
170     QSignalMapper *sortingMapper;
171
172     /* Lookups */
173     PLItem *findById( PLItem *, int ) const;
174     PLItem *findByInput( PLItem *, int ) const;
175     PLItem *findInner(PLItem *, int , bool ) const;
176     bool canEdit() const;
177
178     PLItem *p_cached_item;
179     PLItem *p_cached_item_bi;
180     int i_cached_id;
181     int i_cached_input_id;
182
183     /* Zoom factor for font-size */
184     int i_zoom;
185
186 private slots:
187     void popupPlay();
188     void popupDel();
189     void popupInfo();
190     void popupStream();
191     void popupSave();
192     void popupExplore();
193     void popupAddNode();
194     void popupAddToPlaylist();
195     void popupSort( int column );
196     void processInputItemUpdate( input_item_t *);
197     void processInputItemUpdate( input_thread_t* p_input );
198     void processItemRemoval( int i_id );
199     void processItemAppend( int item, int parent );
200     void activateItem( playlist_item_t *p_item );
201 };
202
203 class PlMimeData : public QMimeData
204 {
205     Q_OBJECT
206
207 public:
208     PlMimeData() {}
209     ~PlMimeData();
210     void appendItem( input_item_t *p_item );
211     QList<input_item_t*> inputItems() const;
212     QStringList formats () const;
213
214 private:
215     QList<input_item_t*> _inputItems;
216     QMimeData *_mimeData;
217 };
218
219 #endif