First steps towards making the playlist behave nicely with respect to grouped tracks...
[amarok.git] / src / playlist / PlaylistModel.h
bloba3b4dd52f402f651e939ab6d8b9e1159c853cd90
1 /***************************************************************************
2 * copyright : (C) 2007 Ian Monroe <ian@monroe.nu> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License version 2 *
6 * as published by the Free Software Foundation. *
7 ***************************************************************************/
9 #ifndef AMAROK_PLAYLISTMODEL_H
10 #define AMAROK_PLAYLISTMODEL_H
12 #include "meta/meta.h"
13 #include "PlaylistHandler.h"
15 #include "UndoCommands.h"
17 #include <QAbstractListModel>
18 #include <QHash>
19 #include <QVector>
21 #include <KLocale>
22 #include <kdemacros.h>
23 #include "PlaylistItem.h"
25 class QMimeData;
26 class QModelIndex;
27 class QueryMaker;
28 class QUndoStack;
30 namespace Playlist {
32 class TrackNavigator;
34 enum PlaybackMode
36 Standard = 1,
37 Repeat
39 enum Column
41 Album = 1,
42 AlbumArtist,
43 Artist,
44 Bitrate,
45 Bpm,
46 Comment,
47 Composer,
48 CoverImage,
49 Directory,
50 DiscNumber,
51 Filename,
52 Filesize,
53 Genre,
54 LastPlayed,
55 Length,
56 Mood,
57 PlayCount,
58 Rating,
59 SampleRate,
60 Score,
61 Title,
62 TrackNumber,
63 Type,
64 Year,
65 NUM_COLUMNS
67 enum DataRoles
69 TrackRole = 0xc0ffee,
70 ItemRole,
71 ActiveTrackRole,
72 GroupRole
74 ///Options for insertTracks
75 enum AddOptions
77 Append = 1, /// inserts media after the last item in the playlist
78 Queue = 2, /// inserts media after the currentTrack
79 Replace = 4, /// clears the playlist first
80 DirectPlay = 8, /// start playback of the first item in the list
81 Unique = 16, /// don't insert anything already in the playlist
82 StartPlay = 32, /// start playback of the first item in the list if nothing else playing
83 Colorize = 64, /// colorize newly added items
84 AppendAndPlay = Append | Unique | StartPlay
87 enum GroupMode
89 None = 1,
90 Head,
91 Body,
92 End
95 typedef QMap<Meta::AlbumPtr, QList< int > > AlbumGroupMap;
97 class Model : public QAbstractListModel, Meta::Observer
99 friend class AddTracksCmd;
100 friend class RemoveTracksCmd;
101 Q_OBJECT
103 public:
104 Model( QObject* parent = 0 );
105 ~Model();
107 //required by QAbstractListModel
108 int rowCount(const QModelIndex &parent = QModelIndex() ) const;
109 int columnCount(const QModelIndex &parent = QModelIndex() ) const { Q_UNUSED(parent); return 3; }
110 QVariant data(const QModelIndex &index, int role) const;
112 //overriding QAbstractItemModel
113 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() );
114 QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
115 Qt::DropActions supportedDropActions() const;
117 //Drag and Drop methods
118 virtual bool insertRows( int /*row*/, int /*count*/, const QModelIndex &parent = QModelIndex() ) { Q_UNUSED(parent); return true; }
119 Qt::ItemFlags flags(const QModelIndex &index) const;
120 QStringList mimeTypes() const;
121 QMimeData* mimeData(const QModelIndexList &indexes) const;
122 bool dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent );
124 //other methods
125 void init();
126 ///Restore playlist from previous session of Amarok
127 void restoreSession() { }
128 ///Save M3U of current playlist to a given location
129 bool saveM3U( const QString &path ) const;
131 ///Return list of items in playlist
132 QList<Item*> itemList() const { return m_items; }
134 inline const QString defaultPlaylistPath() const { return Amarok::saveLocation() + "current.xspf"; }
138 * Insert tracks into the playlist with some handy options.
139 * @param list tracks to add
140 * @param options valid values are Unique || (Append xor Queue xor Replace) || ( DirectPlay xor StartPlay )
142 void insertOptioned( Meta::TrackList list, int options );
143 void insertOptioned( Meta::TrackPtr track, int options ); //convenience method
144 void insertOptioned( QueryMaker *qm, int options );
145 void insertTrack( int row, Meta::TrackPtr track ); //convenience method
146 void insertTracks( int row, Meta::TrackList list );
147 void insertTracks( int row, QueryMaker *qm );
149 int activeRow() const { return m_activeRow; }
150 void setActiveRow( int row );
151 Meta::TrackPtr activeTrack() const { return m_items.at( m_activeRow )->track(); }
152 void setActiveItem( Playlist::Item* active) { setActiveRow( m_items.lastIndexOf(active) ); }
154 void moveRow( int row, int to );
156 ///deprecated function to ease porting to Meta::Track from URLs
157 KDE_DEPRECATED void insertMedia( KUrl::List list, int options = Append );
158 virtual void metadataChanged( Meta::Track *track );
159 virtual void metadataChanged( Meta::Album *album );
161 void play( int row );
163 //various methods for playlist name
164 //I believe this is used for when you open a playlist file, it can keep the same name when it is
165 //later saved
166 void setPlaylistName( const QString &name, bool proposeOverwriting = false ) { m_playlistName = name; m_proposeOverwriting = proposeOverwriting; }
167 void proposePlaylistName( const QString &name, bool proposeOverwriting = false ) { if( ( rowCount() == 0 ) || m_playlistName==i18n("Untitled") ) m_playlistName = name; m_proposeOverwriting = proposeOverwriting; }
168 const QString &playlistName() const { return m_playlistName; }
169 bool proposeOverwriteOnSave() const { return m_proposeOverwriting; }
171 static Model* s_instance; //! instance variable
173 public slots:
174 void play( const QModelIndex& index );
175 void playlistRepeatMode( int item );
176 void next();
177 void back();
178 void clear(); ///clear the playlist of all items
180 signals:
181 void playlistCountChanged( int newCount );
183 private slots:
184 void trackFinished(); //! what to do when a track finishes
185 void queryDone();
186 void newResultReady( const QString &collectionId, const Meta::TrackList &tracks );
187 void playCurrentTrack(); ///connected to EngineController::orderCurrent
189 private:
191 * This performs the actual work involved with inserting tracks. It is to be *only* called by an UndoCommand.
192 * @arg row Row number in the playlist to insert the list after.
193 * @arg list The list to be inserted.
195 void insertTracksCommand( int row, Meta::TrackList list );
198 * This performs the actual work involved with removing tracks. It is to be *only* called by an UndoCommand.
199 * @arg row Row number in the playlist to insert the list after.
200 * @arg list The list to be inserted.
202 Meta::TrackList removeRowsCommand( int position, int rows );
206 * This Method naively deletes all grouping information adn regroups all items from scratch. This is an O(n) opperation,
207 * but it is very simple and handles pretty much all nasty corner cases without involving the black arts or the sacrifice of
208 * of chickens under a full moon.
210 void regroupAlbums();
212 static QString prettyColumnName( Column index ); //!takes a Column enum and returns its string name
214 void playModeChanged( int mode ); //! Changes the trackadvancer to the given mode
216 QString m_playlistName;
217 bool m_proposeOverwriting;
219 QList<Item*> m_items; //! list of tracks in order currently in the playlist
220 int m_activeRow; //! the row being played
221 TrackNavigator* m_advancer; //! the strategy of what to do when a track finishes playing
222 QUndoStack* m_undoStack; //! for pushing on undo commands
223 QHash<QueryMaker*, int> m_queryMap; //! maps queries to the row where the results should be inserted
224 QHash<QueryMaker*, int> m_optionedQueryMap; //! maps queries to the options to be used when inserting the result
226 PlaylistHandler * m_playlistHandler;
228 AlbumGroupMap m_albumGroups;
229 Meta::AlbumPtr m_lastAddedTrackAlbum;
236 #endif