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