]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_model.hpp
Qt, playlist: Move getMeta helper functions to PLModel
[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 <QMimeData>
40 #include <QAbstractItemModel>
41 #include <QVariant>
42 #include <QModelIndex>
43
44 class PLItem;
45 class PLSelector;
46 class PlMimeData;
47 class QSignalMapper;
48
49 class PLModel : public QAbstractItemModel
50 {
51     Q_OBJECT
52
53 public:
54     enum {
55       IsCurrentRole = Qt::UserRole,
56       IsLeafNodeRole,
57       IsCurrentsParentNodeRole
58     };
59
60     PLModel( playlist_t *, intf_thread_t *,
61              playlist_item_t *, QObject *parent = 0 );
62     ~PLModel();
63
64     /*** QModel subclassing ***/
65
66     /* Data structure */
67     QVariant data( const QModelIndex &index, const int role ) const;
68     QVariant headerData( int section, Qt::Orientation orientation,
69                          int role = Qt::DisplayRole ) const;
70     int rowCount( const QModelIndex &parent = QModelIndex() ) const;
71     int columnCount( const QModelIndex &parent = QModelIndex() ) const;
72     Qt::ItemFlags flags( const QModelIndex &index ) const;
73     QModelIndex index( const int r, const int c, const QModelIndex &parent ) const;
74     QModelIndex parent( const QModelIndex &index ) const;
75
76     /* Drag and Drop */
77     Qt::DropActions supportedDropActions() const;
78     QMimeData* mimeData( const QModelIndexList &indexes ) const;
79     bool dropMimeData( const QMimeData *data, Qt::DropAction action,
80                       int row, int column, const QModelIndex &target );
81     QStringList mimeTypes() const;
82
83     /**** Custom ****/
84
85     /* Lookups */
86     QStringList selectedURIs();
87     QModelIndex index( PLItem *, const int c ) const;
88     QModelIndex index( const int i_id, const int c );
89     QModelIndex currentIndex() const;
90     bool isParent( const QModelIndex &index, const QModelIndex &current) const;
91     bool isCurrent( const QModelIndex &index ) const;
92     int itemId( const QModelIndex &index ) const;
93     static int columnFromMeta( int meta_column );
94     static int columnToMeta( int column );
95
96     static QString getMeta( const QModelIndex & index, int meta );
97     static QPixmap getArtPixmap( const QModelIndex & index, const QSize & size );
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( const int column, Qt::SortOrder order );
104     void sort( const int i_root_id, const 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     void dropAppendCopy( const PlMimeData * data, PLItem *target, int pos );
145     void dropMove( const PlMimeData * data, PLItem *target, int new_pos );
146
147     /* Popup */
148     int i_popup_item, i_popup_parent, i_popup_column;
149     QModelIndexList current_selection;
150     QMenu *sortingMenu;
151     QSignalMapper *sortingMapper;
152
153     /* Lookups */
154     PLItem *findById( PLItem *, int ) const;
155     PLItem *findByInput( PLItem *, int ) const;
156     PLItem *findInner(PLItem *, int , bool ) const;
157     bool canEdit() const;
158
159     PLItem *p_cached_item;
160     PLItem *p_cached_item_bi;
161     int i_cached_id;
162     int i_cached_input_id;
163
164 private slots:
165     void popupPlay();
166     void popupDel();
167     void popupInfo();
168     void popupStream();
169     void popupSave();
170     void popupExplore();
171     void popupAddNode();
172     void popupSort( int column );
173     void processInputItemUpdate( input_item_t *);
174     void processInputItemUpdate( input_thread_t* p_input );
175     void processItemRemoval( int i_id );
176     void processItemAppend( int item, int parent );
177 };
178
179 class PlMimeData : public QMimeData
180 {
181     Q_OBJECT
182
183 public:
184     PlMimeData();
185     ~PlMimeData();
186     void appendItem( input_item_t *p_item );
187     QList<input_item_t*> inputItems() const;
188     QStringList formats () const;
189
190 private:
191     QList<input_item_t*> _inputItems;
192     QMimeData *_mimeData;
193 };
194
195 #endif