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