]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
035681f8b065010a1c0e18278117389b20a50115
[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 <QAction>
45
46 class PLItem;
47 class PlMimeData;
48
49 class PLModel : public VLCModel
50 {
51     Q_OBJECT
52
53 public:
54     PLModel( playlist_t *, intf_thread_t *,
55              playlist_item_t *, QObject *parent = 0 );
56     virtual ~PLModel();
57
58     /* Qt main PLModel */
59     static PLModel* getPLModel( intf_thread_t *p_intf )
60     {
61         if(!p_intf->p_sys->pl_model )
62         {
63             playlist_Lock( THEPL );
64             playlist_item_t *p_root = THEPL->p_playing;
65             playlist_Unlock( THEPL );
66             p_intf->p_sys->pl_model = new PLModel( THEPL, p_intf, p_root, NULL );
67         }
68
69         return p_intf->p_sys->pl_model;
70     }
71
72     /*** QAbstractItemModel subclassing ***/
73
74     /* Data structure */
75     QVariant data( const QModelIndex &index, const int role ) const Q_DECL_OVERRIDE;
76     int rowCount( const QModelIndex &parent = QModelIndex() ) const Q_DECL_OVERRIDE;
77     Qt::ItemFlags flags( const QModelIndex &index ) const Q_DECL_OVERRIDE;
78     QModelIndex index( const int r, const int c, const QModelIndex &parent ) const Q_DECL_OVERRIDE;
79     QModelIndex parent( const QModelIndex &index ) const Q_DECL_OVERRIDE;
80
81     /* Drag and Drop */
82     Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
83     QMimeData* mimeData( const QModelIndexList &indexes ) const Q_DECL_OVERRIDE;
84     bool dropMimeData( const QMimeData *data, Qt::DropAction action,
85               int row, int column, const QModelIndex &target );
86     QStringList mimeTypes() const Q_DECL_OVERRIDE;
87
88     /* Sort */
89     void sort( const int column, Qt::SortOrder order = Qt::AscendingOrder ) Q_DECL_OVERRIDE;
90
91     /*** VLCModelSubInterface subclassing ***/
92     void rebuild( playlist_item_t * p = NULL ) Q_DECL_OVERRIDE;
93     void doDelete( QModelIndexList selected ) Q_DECL_OVERRIDE;
94     void createNode( QModelIndex index, QString name ) Q_DECL_OVERRIDE;
95     void renameNode( QModelIndex index, QString name ) Q_DECL_OVERRIDE;
96     void removeAll();
97
98     /* Lookups */
99     QModelIndex rootIndex() const Q_DECL_OVERRIDE;
100     void filter( const QString& search_text, const QModelIndex & root, bool b_recursive ) Q_DECL_OVERRIDE;
101     QModelIndex currentIndex() const Q_DECL_OVERRIDE;
102     QModelIndex indexByPLID( const int i_plid, const int c ) const Q_DECL_OVERRIDE;
103     QModelIndex indexByInputItemID( const int i_inputitem_id, const int c ) const Q_DECL_OVERRIDE;
104     bool isTree() const Q_DECL_OVERRIDE;
105     bool canEdit() const Q_DECL_OVERRIDE;
106     bool action( QAction *action, const QModelIndexList &indexes ) Q_DECL_OVERRIDE;
107     bool isSupportedAction( actions action, const QModelIndex & ) const Q_DECL_OVERRIDE;
108
109 protected:
110     /* VLCModel subclassing */
111     bool isParent( const QModelIndex &index, const QModelIndex &current) const;
112     bool isLeaf( const QModelIndex &index ) const;
113     PLItem *getItem( const QModelIndex & index ) const;
114
115 private:
116     /* General */
117     PLItem *rootItem;
118
119     playlist_t *p_playlist;
120
121     /* Custom model private methods */
122     /* Lookups */
123     QModelIndex index( PLItem *, const int c ) const;
124
125     /* Shallow actions (do not affect core playlist) */
126     void updateTreeItem( PLItem * );
127     void removeItem ( PLItem * );
128     void recurseDelete( QList<AbstractPLItem*> children, QModelIndexList *fullList );
129     void takeItem( PLItem * ); //will not delete item
130     void insertChildren( PLItem *node, QList<PLItem*>& items, int i_pos );
131     /* ...of which  the following will not update the views */
132     void updateChildren( PLItem * );
133     void updateChildren( playlist_item_t *, PLItem * );
134
135     /* Deep actions (affect core playlist) */
136     void dropAppendCopy( const PlMimeData * data, PLItem *target, int pos );
137     void dropMove( const PlMimeData * data, PLItem *target, int new_pos );
138
139     /* */
140     void sort( QModelIndex caller, QModelIndex rootIndex, const int column, Qt::SortOrder order );
141
142     /* Lookups */
143     PLItem *findByPLId( PLItem *, int i_plitemid ) const;
144     PLItem *findByInputId( PLItem *, int i_input_itemid ) const;
145     PLItem *findInner(PLItem *, int i_id, bool b_isinputid ) const;
146     enum pl_nodetype
147     {
148         ROOTTYPE_CURRENT_PLAYING,
149         ROOTTYPE_MEDIA_LIBRARY,
150         ROOTTYPE_OTHER
151     };
152     pl_nodetype getPLRootType() const;
153
154     /* */
155     QString latestSearch;
156
157 private slots:
158     void processInputItemUpdate( input_item_t *);
159     void processInputItemUpdate();
160     void processItemRemoval( int i_pl_itemid );
161     void processItemAppend( int i_pl_itemid, int i_pl_itemidparent );
162     void activateItem( playlist_item_t *p_item );
163     void activateItem( const QModelIndex &index );
164 };
165
166 class PlMimeData : public QMimeData
167 {
168     Q_OBJECT
169
170 public:
171     PlMimeData() {}
172     virtual ~PlMimeData();
173     void appendItem( input_item_t *p_item );
174     QList<input_item_t*> inputItems() const;
175     QStringList formats () const;
176
177 private:
178     QList<input_item_t*> _inputItems;
179     QMimeData *_mimeData;
180 };
181
182 #endif