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